Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Validator/Constraints/VatNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
class VatNumber extends Constraint
{
public const INVALID_ERROR_CODE = '59421d43-d474-489c-b18c-7701329d51a0';
public const VIES_EXCEPTION_ERROR_CODE = 'a1be6ee0-f27e-4d51-a132-f0a753c0b01e';

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

public bool $checkExistence = true;
public bool $violateOnException = false;
}
8 changes: 8 additions & 0 deletions src/Validator/Constraints/VatNumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public function validate(mixed $value, Constraint $constraint) : void
try {
$valid = $this->validator->validateVatNumber($value);
} catch (ViesException $e) {
if ($constraint->violateOnException) {
$this->context->buildViolation($constraint->exceptionMessage)
->setCode(VatNumber::VIES_EXCEPTION_ERROR_CODE)
->addViolation();

return;
}

// ignore VIES VAT exceptions (when the service is down)
// this could mean that an unexisting VAT number passes validation,
// but it's (probably) better than a hard-error
Expand Down
43 changes: 43 additions & 0 deletions tests/Validator/Constraints/VatNumberExceptionValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Ibericode\Vat\Bundle\Tests\Validator\Constraints;

use Ibericode\Vat\Bundle\Validator\Constraints\VatNumber;
use Ibericode\Vat\Bundle\Validator\Constraints\VatNumberValidator;
use Ibericode\Vat\Validator;
use Ibericode\Vat\Vies\ViesException;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

class VatNumberExceptionValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): ConstraintValidatorInterface
{
$mockValidator = $this->createMock(Validator::class);
$mockValidator
->method('validateVatNumber')
->willThrowException(new ViesException());

return new VatNumberValidator($mockValidator);
}

public function testViesExceptionValid()
{
$constraint = new VatNumber([
'violateOnException' => false,
]);
$this->validator->validate('IE6388047V', $constraint);
$this->assertNoViolation();
}

public function testViesExceptionError()
{
$constraint = new VatNumber([
'violateOnException' => true,
]);
$this->validator->validate('IE6388047V', $constraint);
$this->buildViolation('An error occurred while checking VAT number, please try again later')
->setCode('a1be6ee0-f27e-4d51-a132-f0a753c0b01e')
->assertRaised();
}
}