Apply contribution guidelines to "Cnpj" rule

This commit is contained in:
William Espindola 2018-06-12 08:37:08 -03:00 committed by Henrique Moody
parent 9f15c6b6d8
commit 527553ce99
No known key found for this signature in database
GPG key ID: 221E9281655813A6
11 changed files with 117 additions and 194 deletions

View file

@ -2,8 +2,8 @@
- `Cnpj()`
Validates the Brazillian CNPJ number. Ignores non-digit chars, so
use `->digit()` if needed.
Validates if the input is a Brazilian National Registry of Legal Entities (CNPJ) number.
Ignores non-digit chars, so use `->digit()` if needed.
## Changelog

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class CnpjException extends ValidationException
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Leonn Leite <leonnleite@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class CnpjException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid CNPJ number',

View file

@ -13,8 +13,25 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class Cnpj extends AbstractRule
use function is_scalar;
use function mb_strlen;
use function preg_replace;
/**
* Validates if the input is a Brazilian National Registry of Legal Entities (CNPJ) number.
*
* @author Alexandre Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jayson Reis <jayson.reis@sabbre.com.br>
* @author Renato Moura <renato@naturalweb.com.br>
* @author Nick Lombard <github@jigsoft.co.za>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class Cnpj extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if (!is_scalar($input)) {

View file

@ -0,0 +1,38 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Exceptions\CnpjException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::cnpj()->check('não cnpj');
} catch (CnpjException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::cnpj())->check('65.150.175/0001-20');
} catch (CnpjException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::cnpj()->assert('test');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::cnpj())->assert('65.150.175/0001-20');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"não cnpj" must be a valid CNPJ number
"65.150.175/0001-20" must not be a valid CNPJ number
- "test" must be a valid CNPJ number
- "65.150.175/0001-20" must not be a valid CNPJ number

View file

@ -1,11 +0,0 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Validator as v;
v::cnpj()->check('53969447000130');
v::cnpj()->assert('53969447000130');
?>
--EXPECTF--;

View file

@ -1,16 +0,0 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Exceptions\CnpjException;
use Respect\Validation\Validator as v;
try {
v::cnpj()->check('não cnpj');
} catch (CnpjException $e) {
echo $e->getMessage();
}
?>
--EXPECTF--;
"não cnpj" must be a valid CNPJ number

View file

@ -1,16 +0,0 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::cnpj()->assert('test');
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
?>
--EXPECTF--
- "test" must be a valid CNPJ number

View file

@ -1,16 +0,0 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Exceptions\CnpjException;
use Respect\Validation\Validator as v;
try {
v::not(v::cnpj())->check('65.150.175/0001-20');
} catch (CnpjException $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
"65.150.175/0001-20" must not be a valid CNPJ number

View file

@ -1,16 +0,0 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::not(v::cnpj())->assert('65150175000120');
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
?>
--EXPECTF--
- "65150175000120" must not be a valid CNPJ number

View file

@ -1,16 +0,0 @@
--FILE--
<?php
require_once 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
try {
v::cnpj()->digit()->assert('65.150.175/0001-20');
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
?>
--EXPECTF--
- "65.150.175/0001-20" must contain only digits (0-9)

View file

@ -13,121 +13,72 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
* @group rule
*
* @covers \Respect\Validation\Rules\Cnpj
* @covers \Respect\Validation\Exceptions\CnpjException
*
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jayson Reis <jayson.reis@sabbre.com.br>
* @author Renato Moura <renato@naturalweb.com.br>
* @author William Espindola <oi@williamespindola.com.br>
*/
class CnpjTest extends TestCase
final class CnpjTest extends RuleTestCase
{
protected $cnpjValidator;
protected function setUp(): void
{
$this->cnpjValidator = new Cnpj();
}
/**
* @dataProvider providerValidFormattedCnpj
* {@inheritdoc}
*/
public function testFormattedCnpjsShouldValidate($input): void
public function providerForValidInput(): array
{
self::assertTrue($this->cnpjValidator->validate($input));
}
$rule = new Cnpj();
/**
* @dataProvider providerValidUnformattedCnpj
*/
public function testUnformattedCnpjsShouldValidate($input): void
{
self::assertTrue($this->cnpjValidator->validate($input));
}
/**
* @dataProvider providerInvalidFormattedCnpj
*/
public function testFormattedCnpjsShouldNotValidate($input): void
{
self::assertFalse($this->cnpjValidator->validate($input));
}
/**
* @dataProvider providerInvalidUnformattedCnpj
*/
public function testUnformattedCnpjsShouldNotValidate($input): void
{
self::assertFalse($this->cnpjValidator->validate($input));
}
/**
* @dataProvider providerInvalidFormattedAndUnformattedCnpjLength
*/
public function testFormattedAndUnformattedCnpjsShouldNotValidate($input): void
{
self::assertFalse($this->cnpjValidator->validate($input));
}
public function providerValidFormattedCnpj()
{
return [
['32.063.364/0001-07'],
['24.663.454/0001-00'],
['57.535.083/0001-30'],
['24.760.428/0001-09'],
['27.355.204/0001-00'],
['36.310.327/0001-07'],
[$rule, '32.063.364/0001-07'],
[$rule, '24.663.454/0001-00'],
[$rule, '57.535.083/0001-30'],
[$rule, '24.760.428/0001-09'],
[$rule, '27.355.204/0001-00'],
[$rule, '36.310.327/0001-07'],
[$rule, '38175021000110'],
[$rule, '37550610000179'],
[$rule, '12774546000189'],
[$rule, '77456211000168'],
[$rule, '02023077000102'],
];
}
public function providerValidUnformattedCnpj()
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
return [
['38175021000110'],
['37550610000179'],
['12774546000189'],
['77456211000168'],
['02023077000102'],
];
}
$rule = new Cnpj();
public function providerInvalidFormattedCnpj()
{
return [
['12.345.678/9012-34'],
['11.111.111/1111-11'],
];
}
public function providerInvalidUnformattedCnpj()
{
return [
['00000000000000'],
['11111111111111'],
['22222222222222'],
['33333333333333'],
['44444444444444'],
['55555555555555'],
['66666666666666'],
['77777777777777'],
['88888888888888'],
['99999999999999'],
['12345678900123'],
['99299929384987'],
['84434895894444'],
['44242340000000'],
];
}
public function providerInvalidFormattedAndUnformattedCnpjLength()
{
return [
['1'],
['22'],
['123'],
['992999999999929384'],
['99-010-0.'],
[$rule, '12.345.678/9012-34'],
[$rule, '11.111.111/1111-11'],
[$rule, '00000000000000'],
[$rule, '11111111111111'],
[$rule, '22222222222222'],
[$rule, '33333333333333'],
[$rule, '44444444444444'],
[$rule, '55555555555555'],
[$rule, '66666666666666'],
[$rule, '77777777777777'],
[$rule, '88888888888888'],
[$rule, '99999999999999'],
[$rule, '12345678900123'],
[$rule, '99299929384987'],
[$rule, '84434895894444'],
[$rule, '44242340000000'],
[$rule, '1'],
[$rule, '22'],
[$rule, '123'],
[$rule, '992999999999929384'],
[$rule, '99-010-0.'],
];
}
}