Skip to content

Commit

Permalink
Merge pull request #2 from melhorenvio/hotfix/cpf-validation-rule
Browse files Browse the repository at this point in the history
fix: cpf validation rule
  • Loading branch information
bachilli authored Nov 3, 2023
2 parents 1c44b48 + d57b7d5 commit 481cb21
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ docs
vendor
coverage
.idea
.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^10.4"
},
"autoload": {
"psr-4": {
Expand Down
17 changes: 0 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
4 changes: 2 additions & 2 deletions src/Rules/Cpf.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public function __construct($allowMask = true)
public function passes($attribute, $value): bool
{
if (empty($value)) {
return true;
return false;
}

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

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

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

namespace Melhorenvio\ValidationRules\Tests;

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

class CpfTest extends TestCase
{
/**
* @test
* @dataProvider provideInvalidCpfs
*/
public function test_with_invalid_cpfs(string $cpf)
{
$rule = new Cpf();

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

/**
* @test
* @dataProvider provideValidCpfs
*/
public function test_with_valid_cpfs(string $cpf)
{
$rule = new Cpf();

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

public function test_with_empty_cpf()
{
$rule = new Cpf();

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

public function test_with_zeroed_cpf()
{
$rule = new Cpf();

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

public static function provideInvalidCpfs(): array
{
return [
['00000000000'],
['11111111111'],
['22222222222'],
['33333333333'],
['44444444444'],
['55555555555'],
['66666666666'],
['77777777777'],
['88888888888'],
['99999999999'],
['000.000.000-00'],
['111.111.111-11'],
['222.222.222-22'],
['333.333.333-33'],
['444.444.444-44'],
['555.555.555-55'],
['666.666.666-66'],
['777.777.777-77'],
['888.888.888-88'],
['999.999.999-99']
];
}

public static function provideValidCpfs(): array
{
return [
['322.282.090-23'],
['024.278.590-52'],
['014.533.750-23'],
['335.203.900-35'],
['108.810.610-26'],
['97005733037'],
['86939244000'],
['62747195040'],
['75726024010'],
['45061931050'],
];
}
}

0 comments on commit 481cb21

Please sign in to comment.