Skip to content

Commit

Permalink
Merge pull request #4 from melhorenvio/2-x-x-fix-cnpj-validation
Browse files Browse the repository at this point in the history
fix: cnpj validation and phpunit tests
  • Loading branch information
bachilli authored Nov 3, 2023
2 parents a3be92a + e701e06 commit 7052fa1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 10 deletions.
10 changes: 1 addition & 9 deletions src/Rules/Cnpj.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ public function __construct($allowMask = true)

public function passes($attribute, $value): bool
{
if (empty($value)) {
return true;
}

if ($this->allowMask) {
$value = preg_replace('/\D/', '', $value);
}

if (!($value && mb_strlen($value) === 14)) {
return false;
}

if (in_array($value, $this->invalidCnpjs)) {
if (empty($value) || mb_strlen($value) !== 14 || in_array($value, $this->invalidCnpjs)) {
return false;
}

Expand Down
85 changes: 85 additions & 0 deletions tests/CnpjTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Melhorenvio\ValidationRules\Tests;

use Melhorenvio\ValidationRules\Rules\Cnpj;
use PHPUnit\Framework\TestCase;

class CnpjTest extends TestCase
{
/**
* @test
* @dataProvider provideInvalidCnpjs
*/
public function test_with_invalid_cnpjs(string $cnpj)
{
$rule = new Cnpj();

$this->assertFalse($rule->passes('cnpj', $cnpj));
}

/**
* @test
* @dataProvider provideValidCnpjs
*/
public function test_with_valid_cnpjs(string $cnpj)
{
$rule = new Cnpj();

$this->assertTrue($rule->passes('cnpj', $cnpj));
}

public function test_with_empty_cnpj()
{
$rule = new Cnpj();

$this->assertFalse($rule->passes('cnpj', ''));
$this->assertFalse($rule->passes('cnpj', null));
}

public function test_with_zeroed_cnpj()
{
$rule = new Cnpj();

$this->assertFalse($rule->passes('cnpj', '0'));
$this->assertFalse($rule->passes('cnpj', 0));
}

public static function provideInvalidCnpjs(): array
{
return [
['00000000000000'],
['11111111111111'],
['22222222222222'],
['33333333333333'],
['44444444444444'],
['55555555555555'],
['66666666666666'],
['77777777777777'],
['88888888888888'],
['99999999999999'],
['00.000.000/0000-00'],
['11.111.111/1111-11'],
['22.222.222/2222-22'],
['33.333.333/3333-33'],
['44.444.444/4444-44'],
['55.555.555/5555-55'],
['66.666.666/6666-66'],
['77.777.777/7777-77'],
['88.888.888/8888-88'],
['99.999.999/9999-99'],
];
}

public static function provideValidCnpjs(): array
{
return [
['05.495.723/0001-22'],
['04.307.064/0001-90'],
['25.394.311/0001-03'],
['05495723000122'],
['04307064000190'],
['25394311000103'],
];
}
}
2 changes: 1 addition & 1 deletion tests/CpfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function provideInvalidCpfs(): array
['666.666.666-66'],
['777.777.777-77'],
['888.888.888-88'],
['999.999.999-99']
['999.999.999-99'],
];
}

Expand Down

0 comments on commit 7052fa1

Please sign in to comment.