Skip to content

Commit

Permalink
ADD option in isEuVatNumber to set the return Value If Vies Thrown an…
Browse files Browse the repository at this point in the history
… Exception.
  • Loading branch information
lopadova committed Jul 5, 2024
1 parent 6f09c16 commit dd8c64a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
7 changes: 4 additions & 3 deletions src/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,10 @@ function urlW3c($check, bool $strict = false): bool
* @param bool $validateOnVIES default false. if se to true, first check formal EU country algorithm,
* then if it valid and country code isn't 'IT' try to check by API VIES service.
* If VIES return false or soap exception was thrown, return false.
* @param bool $returnValueIfViesThrownEx if VIES service thrown an exception, return this value.
* @return bool
*/
function isEuVatNumber(string $pi, bool $validateOnVIES = false): bool
function isEuVatNumber(string $pi, bool $validateOnVIES = false, bool $returnValueIfViesThrownEx = false): bool
{
$countryCode = getCoutryCodeByVatNumber($pi, 'IT');

Expand All @@ -748,8 +749,8 @@ function isEuVatNumber(string $pi, bool $validateOnVIES = false): bool
//check vies
try {
return isVATRegisteredInVies($pi);
} catch (SoapFault $e) {
return false;
} catch (\Throwable $e) {
return $returnValueIfViesThrownEx;
}
}

Expand Down
48 changes: 24 additions & 24 deletions tests/ValidationDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1606,30 +1606,30 @@ public function isHostnameProvider()
public function isEuVatNumberProvider()
{
return [
'"", false' => ['', false, false],
'"", true' => ['', true, false],
'" ", false ' => [' ', false, false],
'" ", true' => [' ', true, false],
'null, false' => [null, false, 'TypeError'],
'null, true' => [null, true, 'TypeError'],
'00000000000, false' => ['00000000000', false, true],
'00000000000, true' => ['00000000000', true, true],
'IT00000000000, false' => ['IT00000000000', false, true],
'IT00000000000, true' => ['IT00000000000', true, true],
'02361141209, false' => ['02361141209', false, true],
'02361141209, true' => ['02361141209', true, true],
'IT02361141209, false' => ['IT02361141209', false, true],
'IT02361141209, true' => ['IT02361141209', true, true],
'00000000001, false' => ['00000000001', false, false],
'00000000001, true' => ['00000000001', true, false],
'IT00000000001, false' => ['IT00000000001', false, false],
'IT00000000001, true' => ['IT00000000001', true, false],
'DE00000000001, false' => ['DE00000000001', false, true],//finchè non c'è un validatore per ogni stato se passo un vat estero e non valido via vies torna true
'DE00000000001, true' => ['DE00000000001', true, false],
'0, false' => ['0', false, false],
'0, true' => ['0', true, false],
'123456789012345678990, false' => ['123456789012345678990', false, false],
'123456789012345678990, true' => ['123456789012345678990', true, false],
'"", false, false' => ['', false, false, false],
'"", true, false' => ['', true, false, false],
'" ", false, false' => [' ', false, false, false],
'" ", true, false' => [' ', true, false, false],
'null, false, false' => [null, false, 'TypeError', false],
'null, true, false' => [null, true, 'TypeError', false],
'00000000000, false, false' => ['00000000000', false, true, false],
'00000000000, true, false' => ['00000000000', true, true, false],
'IT00000000000, false, false' => ['IT00000000000', false, true, false],
'IT00000000000, true, false' => ['IT00000000000', true, true, false],
'02361141209, false, false' => ['02361141209', false, true, false],
'02361141209, true, false' => ['02361141209', true, true, false],
'IT02361141209, false, false' => ['IT02361141209', false, true, false],
'IT02361141209, true, false' => ['IT02361141209', true, true, false],
'00000000001, false, false' => ['00000000001', false, false, false],
'00000000001, true, false' => ['00000000001', true, false, false],
'IT00000000001, false, false' => ['IT00000000001', false, false, false],
'IT00000000001, true, false' => ['IT00000000001', true, false, false],
'DE00000000001, false, false' => ['DE00000000001', false, true, false],//finchè non c'è un validatore per ogni stato se passo un vat estero e non valido via vies torna true
'DE00000000001, true, false' => ['DE00000000001', true, false, false],
'0, false, false' => ['0', false, false, false],
'0, true, false' => ['0', true, false, false],
'123456789012345678990, false, false' => ['123456789012345678990', false, false, false],
'123456789012345678990, true, false' => ['123456789012345678990', true, false, false],
];
}

Expand Down
23 changes: 12 additions & 11 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,36 +570,37 @@ public function isCfTest($val, $expected)
/**
* @test
* @param $val
* @param $validateOnVies
* @param $expected
* @dataProvider isEuVatNumberProvider
* @dataProvider isITVatProvider
*/
public function isEuVatNumberTest($val, $validateOnVies, $expected)
public function isITVatTest($val, $expected)
{
if ($this->expectedIsAnException($expected)) {
$this->expectException($expected);
isEuVatNumber($val, $validateOnVies);
isITVat($val);
} else {
$this->assertEquals($expected, isEuVatNumber($val, $validateOnVies));
$result = isITVat($val);
$this->assertEquals($expected, $result);
}
}


/**
* @test
* @param $val
* @param $validateOnVies
* @param $returnValueIfViesThrownEx
* @param $expected
* @dataProvider isITVatProvider
* @dataProvider isEuVatNumberProvider
*/
public function isITVatTest($val, $expected)
public function isEuVatNumberTest($val, $validateOnVies, $expected, $returnValueIfViesThrownEx)
{
if ($this->expectedIsAnException($expected)) {
$this->expectException($expected);
isITVat($val);
} else {
$result = isITVat($val);
$this->assertEquals($expected, $result);
isEuVatNumber($val, $validateOnVies, $returnValueIfViesThrownEx);
return;
}
$this->assertEquals($expected, isEuVatNumber($val, $validateOnVies, $returnValueIfViesThrownEx));
}

/**
Expand Down

0 comments on commit dd8c64a

Please sign in to comment.