mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 16:52:16 +02:00
Apply contribution guidelines to "NfeAccessKey" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
21aa8eec62
commit
9f9b33a36c
|
@ -15,9 +15,10 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
/**
|
||||
* @author Andrey Knupp Vital <andreykvital@gmail.com>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class NfeAccessKeyException extends ValidationException
|
||||
final class NfeAccessKeyException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
|
|
@ -14,20 +14,23 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use function array_map;
|
||||
use function floor;
|
||||
use function mb_strlen;
|
||||
use function str_split;
|
||||
|
||||
/**
|
||||
* Rule restrict to Brasil.
|
||||
* Validates the access key of the Brazilian electronic invoice (NFe).
|
||||
*
|
||||
* Valida chave de acesso de NFe.
|
||||
* Mais especificamente, relacionada ao DANFE.
|
||||
*
|
||||
* @see Manual de Integração do Contribuinte v4.0.1 (http://www.nfe.fazenda.gov.br)
|
||||
* (pt-br) Valida chave de acesso de NFe, mais especificamente, relacionada ao DANFE.
|
||||
*
|
||||
* @see (pt-br) Manual de Integração do Contribuinte v4.0.1 em http://www.nfe.fazenda.gov.br
|
||||
*
|
||||
* @author Andrey Knupp Vital <andreykvital@gmail.com>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class NfeAccessKey extends AbstractRule
|
||||
final class NfeAccessKey extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
|
|
@ -111,6 +111,7 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|||
* @method static Validator minAge(int $age, string $format = null)
|
||||
* @method static Validator multiple(int $multipleOf)
|
||||
* @method static Validator negative()
|
||||
* @method static Validator nfeAccessKey()
|
||||
* @method static Validator nif()
|
||||
* @method static Validator no($useLocale = false)
|
||||
* @method static Validator noneOf(Validatable ...$rule)
|
||||
|
|
43
tests/integration/rules/nfeAccessKey.phpt
Normal file
43
tests/integration/rules/nfeAccessKey.phpt
Normal file
|
@ -0,0 +1,43 @@
|
|||
--CREDITS--
|
||||
Danilo Correa <danilosilva87@gmail.com>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\NfeAccessKeyException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::nfeAccessKey()->check('31841136830118868211870485416765268625116906');
|
||||
} catch (NfeAccessKeyException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::nfeAccessKey())->check('52060433009911002506550120000007800267301615');
|
||||
} catch (NfeAccessKeyException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::nfeAccessKey()->assert('31841136830118868211870485416765268625116906');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::nfeAccessKey())->assert('52060433009911002506550120000007800267301615');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
"31841136830118868211870485416765268625116906" must be a valid NFe access key
|
||||
"52060433009911002506550120000007800267301615" must not be a valid NFe access key
|
||||
- "31841136830118868211870485416765268625116906" must be a valid NFe access key
|
||||
- "52060433009911002506550120000007800267301615" must not be a valid NFe access key
|
|
@ -13,107 +13,59 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Test\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\NfeAccessKeyException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\NfeAccessKey
|
||||
*
|
||||
* @author Andrey Knupp Vital <andreykvital@gmail.com>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class NfeAccessKeyTest extends TestCase
|
||||
final class NfeAccessKeyTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @var NfeAccessKey
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $nfeValidator;
|
||||
|
||||
protected function setUp(): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$this->nfeValidator = new NfeAccessKey();
|
||||
}
|
||||
$nfe = new NfeAccessKey();
|
||||
|
||||
/**
|
||||
* @dataProvider validAccessKeyProvider
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function validAccessKey(string $input): void
|
||||
{
|
||||
$this->nfeValidator->assert($input);
|
||||
self::assertTrue($this->nfeValidator->__invoke($input));
|
||||
$this->nfeValidator->check($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidAccessKeyProvider
|
||||
* @expectedException \Respect\Validation\Exceptions\NfeAccessKeyException
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function invalidAccessKey(string $input): void
|
||||
{
|
||||
$this->nfeValidator->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidAccessKeyLengthProvider
|
||||
* @expectedException \Respect\Validation\Exceptions\NfeAccessKeyException
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function invalidLengthCnh(string $input): void
|
||||
{
|
||||
$this->nfeValidator->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[][]
|
||||
*/
|
||||
public function validAccessKeyProvider(): array
|
||||
{
|
||||
return [
|
||||
['52060433009911002506550120000007800267301615'],
|
||||
[$nfe, '52060433009911002506550120000007800267301615'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[][]
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function invalidAccessKeyProvider(): array
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
['31841136830118868211870485416765268625116906'],
|
||||
['21470801245862435081451225624565260861852679'],
|
||||
['45644318091447671194616059650873352394885852'],
|
||||
['17214281716057582143671174314277906696193888'],
|
||||
['56017280182977836779696364362142515138726654'],
|
||||
['90157126614010548506235171976891004177042525'],
|
||||
['78457064241662300187501877048374851128754067'],
|
||||
['39950148079977322431982386613620895568235903'],
|
||||
['90820939577654114875253907311677136672761216'],
|
||||
];
|
||||
}
|
||||
$nfe = new NfeAccessKey();
|
||||
|
||||
/**
|
||||
* @return mixed[][]
|
||||
*/
|
||||
public function invalidAccessKeyLengthProvider(): array
|
||||
{
|
||||
return [
|
||||
['11145573386990252067204852181837301'],
|
||||
['6209433147444876'],
|
||||
['00745996227609395385255721262102'],
|
||||
['58215798856653'],
|
||||
['24149625439084262707824706699374326'],
|
||||
['163907274335'],
|
||||
['67229454773008929675906894698'],
|
||||
['5858836670181917762140106857095788313119136'],
|
||||
['6098412281885524361833754087461339281130'],
|
||||
['9025299113310221'],
|
||||
[$nfe, '31841136830118868211870485416765268625116906'],
|
||||
[$nfe, '21470801245862435081451225624565260861852679'],
|
||||
[$nfe, '45644318091447671194616059650873352394885852'],
|
||||
[$nfe, '17214281716057582143671174314277906696193888'],
|
||||
[$nfe, '56017280182977836779696364362142515138726654'],
|
||||
[$nfe, '90157126614010548506235171976891004177042525'],
|
||||
[$nfe, '78457064241662300187501877048374851128754067'],
|
||||
[$nfe, '39950148079977322431982386613620895568235903'],
|
||||
[$nfe, '90820939577654114875253907311677136672761216'],
|
||||
[$nfe, '11145573386990252067204852181837301'],
|
||||
[$nfe, '6209433147444876'],
|
||||
[$nfe, '00745996227609395385255721262102'],
|
||||
[$nfe, '58215798856653'],
|
||||
[$nfe, '24149625439084262707824706699374326'],
|
||||
[$nfe, '163907274335'],
|
||||
[$nfe, '67229454773008929675906894698'],
|
||||
[$nfe, '5858836670181917762140106857095788313119136'],
|
||||
[$nfe, '6098412281885524361833754087461339281130'],
|
||||
[$nfe, '9025299113310221'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue