diff --git a/.github/workflows/proposing-changes.yml b/.github/workflows/proposing-changes.yml index c78ad33..ec0ebb6 100644 --- a/.github/workflows/proposing-changes.yml +++ b/.github/workflows/proposing-changes.yml @@ -11,6 +11,12 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Setup PHP with Xdebug + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + coverage: xdebug + - name: Validate composer.json and composer.lock run: composer validate diff --git a/.gitignore b/.gitignore index 6a6451a..72fdbe9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ composer.lock .idea/ /tests/log/ *.cache +docker-compose.yml +.phpcs-cache +.phpunit.result.cache diff --git a/src/validator-docs/Contracts/ValidatorFormats.php b/src/validator-docs/Contracts/ValidatorFormats.php new file mode 100644 index 0000000..7d35b80 --- /dev/null +++ b/src/validator-docs/Contracts/ValidatorFormats.php @@ -0,0 +1,8 @@ + 0; + } +} diff --git a/src/validator-docs/Formats/Cnpj.php b/src/validator-docs/Formats/Cnpj.php new file mode 100644 index 0000000..b2a8ae9 --- /dev/null +++ b/src/validator-docs/Formats/Cnpj.php @@ -0,0 +1,11 @@ + 0; + } +} diff --git a/src/validator-docs/Formats/Cpf.php b/src/validator-docs/Formats/Cpf.php new file mode 100644 index 0000000..6f91f32 --- /dev/null +++ b/src/validator-docs/Formats/Cpf.php @@ -0,0 +1,13 @@ + 0; + } +} diff --git a/src/validator-docs/Formats/CpfCnpj.php b/src/validator-docs/Formats/CpfCnpj.php new file mode 100644 index 0000000..06a79ea --- /dev/null +++ b/src/validator-docs/Formats/CpfCnpj.php @@ -0,0 +1,16 @@ +validateFormat($value) || $cnpj->validateFormat($value); + } +} diff --git a/src/validator-docs/Formats/Nis.php b/src/validator-docs/Formats/Nis.php new file mode 100644 index 0000000..c0eaf69 --- /dev/null +++ b/src/validator-docs/Formats/Nis.php @@ -0,0 +1,14 @@ + 0; + } +} diff --git a/src/validator-docs/Validator.php b/src/validator-docs/Validator.php index 7646d4f..631b6bd 100644 --- a/src/validator-docs/Validator.php +++ b/src/validator-docs/Validator.php @@ -1,60 +1,47 @@ 0; + public function __construct( + Translator $translator, + ValidatorFormats $formatValidator, + array $data, + array $rules, + array $messages = [], + array $customAttributes = [] + ) { + parent::__construct($translator, $data, $rules, $messages, $customAttributes); } - protected function validateFormatoCnpj($attribute, $value): bool + protected function validateFormat($value, $document, $attribute = null) { - return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/', $value) > 0; - } - - protected function validateFormatoCpfCnpj($attribute, $value): bool - { - return $this->validateFormatoCpf($attribute, $value) || $this->validateFormatoCnpj($attribute, $value); - } - - protected function validateFormatoNis($attribute, $value): bool - { - return preg_match('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/', $value) > 0; - } - - protected function validateFormatoCertidao($attribute, $value): bool - { - return preg_match('/^\d{6}[. ]\d{2}[. ]\d{2}[. ]\d{4}[. ]\d{1}[. ]\d{5}[. ]\d{3}[. ]\d{7}[- ]\d{2}$/', $value) > 0; + if (!empty($value)) { + return (new ValidatorFormats())->execute($value, $document); + } } protected function validateCpf($attribute, $value): bool { $cpf = new Cpf(); + $this->validateFormat($value, 'cpf'); + return $cpf->validateCpf($attribute, $value); } diff --git a/src/validator-docs/ValidatorFormats.php b/src/validator-docs/ValidatorFormats.php new file mode 100644 index 0000000..a6a36ff --- /dev/null +++ b/src/validator-docs/ValidatorFormats.php @@ -0,0 +1,28 @@ +app['validator']->resolver(function ($translator, $data, $rules, $messages, $attributes) use ($me) { - $messages += $me->getMessages(); + $validatorFormats = new ValidatorFormats(); - return new Validator($translator, $data, $rules, $messages, $attributes); - }); + $this->app['validator'] + ->resolver( + function ($translator, $data, $rules, $messages, $attributes) use ($me, $validatorFormats) { + $messages += $me->getMessages(); + + return new Validator($translator, $validatorFormats, $data, $rules, $messages, $attributes); + } + ); } protected function getMessages() diff --git a/tests/Formats/CertidaoTest.php b/tests/Formats/CertidaoTest.php new file mode 100644 index 0000000..956d2ca --- /dev/null +++ b/tests/Formats/CertidaoTest.php @@ -0,0 +1,17 @@ +validateFormat('434546.02.55.2019.1.71037.134.6484858-10')); + self::assertFalse($certidao->validateFormat('434546.02.55.2019.1.71037.134.6484858')); + } +} diff --git a/tests/Formats/CnpjTest.php b/tests/Formats/CnpjTest.php new file mode 100644 index 0000000..2f783e3 --- /dev/null +++ b/tests/Formats/CnpjTest.php @@ -0,0 +1,17 @@ +validateFormat('63.657.343/0001-43')); + self::assertFalse($cnpjFormat->validateFormat('63.657.343/0001')); + } +} diff --git a/tests/Formats/CpfCnpjTest.php b/tests/Formats/CpfCnpjTest.php new file mode 100644 index 0000000..1d73b09 --- /dev/null +++ b/tests/Formats/CpfCnpjTest.php @@ -0,0 +1,19 @@ +validateFormat('111.111.111-11')); + self::assertTrue($instance->validateFormat('63.657.343/0001-43')); + self::assertFalse($instance->validateFormat('11111111111')); + self::assertFalse($instance->validateFormat('63.657.343/0001')); + } +} diff --git a/tests/Formats/CpfTest.php b/tests/Formats/CpfTest.php new file mode 100644 index 0000000..f3f51a3 --- /dev/null +++ b/tests/Formats/CpfTest.php @@ -0,0 +1,22 @@ +cpf = new Cpf(); + } + + public function testIsFormatValidationWorksWell(): void + { + self::assertTrue($this->cpf->validateFormat('111.111.111-11')); + self::assertFalse($this->cpf->validateFormat('11111111111')); + } +} diff --git a/tests/Formats/NisTest.php b/tests/Formats/NisTest.php new file mode 100644 index 0000000..679e28e --- /dev/null +++ b/tests/Formats/NisTest.php @@ -0,0 +1,16 @@ +validateFormat('201.73374.34-9')); + } +} diff --git a/tests/TestValidator.php b/tests/TestValidator.php index 553861e..01e4ec6 100644 --- a/tests/TestValidator.php +++ b/tests/TestValidator.php @@ -24,24 +24,6 @@ public function cpf() $this->assertTrue($incorrect->fails()); } - /** @test **/ - public function formatoDoCpf() - { - $correct = Validator::make( - ['certo' => '094.050.986-59'], - ['certo' => 'formato-cpf'] - ); - - $incorrect = Validator::make( - ['errado' => '094.050.986-591'], - ['errado' => 'formato-cpf'] - ); - - $this->assertTrue($correct->passes()); - - $this->assertTrue($incorrect->fails()); - } - /** @test **/ public function cnpj() { @@ -60,24 +42,6 @@ public function cnpj() $this->assertTrue($incorrect->fails()); } - /** @test **/ - public function formatoDoCnpj() - { - $correct = Validator::make( - ['certo' => '53.084.587/0001-20'], - ['certo' => 'formato-cnpj'] - ); - - $incorrect = Validator::make( - ['errado' => '51.084.587/000120'], - ['errado' => 'formato-cnpj'] - ); - - $this->assertTrue($correct->passes()); - - $this->assertTrue($incorrect->fails()); - } - /** @test **/ public function cpfECnpjNoMesmoAtributo() { @@ -96,24 +60,6 @@ public function cpfECnpjNoMesmoAtributo() $this->assertTrue($incorrect->fails()); } - /** @test **/ - public function formatoDoCpfECnpjNoMesmoAtributo() - { - $correct = Validator::make( - ['certo' => '094.050.986-59'], - ['certo' => 'formato-cpf-cnpj'] - ); - - $incorrect = \Validator::make( - ['errado' => '51.084.587/000120'], - ['errado' => 'formato-cpf-cnpj'] - ); - - $this->assertTrue($correct->passes()); - - $this->assertTrue($incorrect->fails()); - } - /** @test **/ public function cnh() { @@ -175,24 +121,6 @@ public function nis() $this->assertTrue($incorrect->fails()); } - /** @test **/ - public function formatoDoNis() - { - $correct = Validator::make( - ['certo' => '201.73374.34-9'], - ['certo' => 'formato-nis'] - ); - - $incorrect = Validator::make( - ['errado' => '201.733.7434-9'], - ['errado' => 'formato-nis'] - ); - - $this->assertTrue($correct->passes()); - - $this->assertTrue($incorrect->fails()); - } - /** @test **/ public function cns() { @@ -242,30 +170,6 @@ public function certidao() $this->assertTrue($incorrect->fails()); } - /** @test **/ - public function formatoDacertidao() - { - $correct = Validator::make( - ['certo' => '434546.02.55.2019.1.71037.134.6484858-10'], - ['certo' => 'formato-certidao'] - ); - - $incorrect = Validator::make( - ['errado' => '201.733.7434-9'], - ['errado' => 'formato-certidao'] - ); - - $this->assertTrue($correct->passes()); - $this->assertTrue($incorrect->fails()); - - // com ' ' no lugar de '.' - $correct = Validator::make( - ['certo' => '434546 02 55 2019 1 71037 134 6484858 10'], - ['certo' => 'formato-certidao'] - ); - $this->assertTrue($correct->passes()); - } - public function inscricoesEstaduais() { $inscricoesEstaduaisValidas = [ diff --git a/tests/ValidatorFormatsTest.php b/tests/ValidatorFormatsTest.php new file mode 100644 index 0000000..96515ac --- /dev/null +++ b/tests/ValidatorFormatsTest.php @@ -0,0 +1,34 @@ +expectException(Exception::class); + $this->expectExceptionMessage('Value not informed.'); + + $validatorFormats = new ValidatorFormats(); + $validatorFormats->execute('', 'cpf'); + } + + public function testIsValidatorReturningExceptionWhenDocumentIsNotSupported(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Don\'t exists validator for this document.'); + + $validatorFormats = new ValidatorFormats(); + $validatorFormats->execute('053.222.333-74', 'foo'); + } + + public function testIsValidatorExecutingValidatorFormats(): void + { + $validatorFormats = new ValidatorFormats(); + + self::assertTrue($validatorFormats->execute('053.222.333-74', 'cpf')); + } +}