Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cnpj validation and phpunit tests #4

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading