Skip to content

Commit 09b068c

Browse files
Merge pull request #17 from thomas-hiron/configure-vies-exception
Add option to violate on Vies exception
2 parents e59b463 + 6a7b334 commit 09b068c

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/Validator/Constraints/VatNumber.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
class VatNumber extends Constraint
99
{
1010
public const INVALID_ERROR_CODE = '59421d43-d474-489c-b18c-7701329d51a0';
11+
public const VIES_EXCEPTION_ERROR_CODE = 'a1be6ee0-f27e-4d51-a132-f0a753c0b01e';
1112

1213
public string $message = '"{{ string }}" does not look like a valid VAT number.';
14+
public string $exceptionMessage = 'An error occurred while checking VAT number, please try again later';
1315

1416
public bool $checkExistence = true;
17+
public bool $violateOnException = false;
1518
}

src/Validator/Constraints/VatNumberValidator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public function validate(mixed $value, Constraint $constraint) : void
3636
try {
3737
$valid = $this->validator->validateVatNumber($value);
3838
} catch (ViesException $e) {
39+
if ($constraint->violateOnException) {
40+
$this->context->buildViolation($constraint->exceptionMessage)
41+
->setCode(VatNumber::VIES_EXCEPTION_ERROR_CODE)
42+
->addViolation();
43+
44+
return;
45+
}
46+
3947
// ignore VIES VAT exceptions (when the service is down)
4048
// this could mean that an unexisting VAT number passes validation,
4149
// but it's (probably) better than a hard-error
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Ibericode\Vat\Bundle\Tests\Validator\Constraints;
4+
5+
use Ibericode\Vat\Bundle\Validator\Constraints\VatNumber;
6+
use Ibericode\Vat\Bundle\Validator\Constraints\VatNumberValidator;
7+
use Ibericode\Vat\Validator;
8+
use Ibericode\Vat\Vies\ViesException;
9+
use Symfony\Component\Validator\ConstraintValidatorInterface;
10+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
11+
12+
class VatNumberExceptionValidatorTest extends ConstraintValidatorTestCase
13+
{
14+
protected function createValidator(): ConstraintValidatorInterface
15+
{
16+
$mockValidator = $this->createMock(Validator::class);
17+
$mockValidator
18+
->method('validateVatNumber')
19+
->willThrowException(new ViesException());
20+
21+
return new VatNumberValidator($mockValidator);
22+
}
23+
24+
public function testViesExceptionValid()
25+
{
26+
$constraint = new VatNumber([
27+
'violateOnException' => false,
28+
]);
29+
$this->validator->validate('IE6388047V', $constraint);
30+
$this->assertNoViolation();
31+
}
32+
33+
public function testViesExceptionError()
34+
{
35+
$constraint = new VatNumber([
36+
'violateOnException' => true,
37+
]);
38+
$this->validator->validate('IE6388047V', $constraint);
39+
$this->buildViolation('An error occurred while checking VAT number, please try again later')
40+
->setCode('a1be6ee0-f27e-4d51-a132-f0a753c0b01e')
41+
->assertRaised();
42+
}
43+
}

0 commit comments

Comments
 (0)