-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Gabrielcefetzada/user-test
User test
- Loading branch information
Showing
5 changed files
with
118 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace App\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class CpfRule implements ValidationRule | ||
{ | ||
/** | ||
* Run the validation rule. | ||
* | ||
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$cpf = $this->extractNumbers($value); | ||
|
||
if (!$this->hasValidLength($cpf)) { | ||
$fail('CPF inválido'); | ||
return; | ||
} | ||
|
||
if ($this->hasRepeatedDigits($cpf)) { | ||
$fail('CPF inválido'); | ||
return; | ||
} | ||
|
||
if (!$this->hasValidCheckDigits($cpf)) { | ||
$fail('CPF inválido'); | ||
} | ||
} | ||
|
||
private function extractNumbers(string $value): string | ||
{ | ||
return preg_replace('/[^0-9]/is', '', $value); | ||
} | ||
|
||
private function hasValidLength(string $cpf): bool | ||
{ | ||
return strlen($cpf) == 11; | ||
} | ||
|
||
private function hasRepeatedDigits(string $cpf): bool | ||
{ | ||
return preg_match('/(\d)\1{10}/', $cpf); | ||
} | ||
|
||
private function hasValidCheckDigits(string $cpf): bool | ||
{ | ||
for ($t = 9; $t < 11; $t++) { | ||
$d = 0; | ||
for ($c = 0; $c < $t; $c++) { | ||
$d += $cpf[$c] * (($t + 1) - $c); | ||
} | ||
$d = ((10 * $d) % 11) % 10; | ||
if ($cpf[$c] != $d) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
namespace Tests\Unit; | ||
|
||
use Illuminate\Foundation\Testing\TestCase; | ||
|
||
class UserTest extends TestCase | ||
{ | ||
public function test_register_with_invalid_cpf() | ||
{ | ||
$response = $this->postJson('/register-common-user', [ | ||
'name' => 'John Doe', | ||
'cpf' => '12345678900', | ||
'email' => '[email protected]', | ||
'password' => 'Mpassword123!', | ||
]); | ||
|
||
$response->assertStatus(422); | ||
} | ||
} |