Skip to content

Commit

Permalink
Merge pull request #115 from vicentimartins/test-improvement
Browse files Browse the repository at this point in the history
refactor: melhora coverage para Renavam
  • Loading branch information
vicentimartins authored Apr 23, 2021
2 parents ef0ba8f + a6d33e5 commit 92eb11c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/validator-docs/Rules/Renavam.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,25 @@ class Renavam extends Sanitization
{
public function validateRenavam($attribute, $renavam): bool
{
$renavam = $this->sanitize($renavam);
$sum = 0;
$renavam = $this->sanitize((string) $renavam);
$renavamArray = str_split($renavam);
$digitCount = 0;
$digit = $this->determinarDigito($renavamArray);

for ($i = 5; $i >= 2; $i--) {
$sum += $renavamArray[$digitCount] * $i;
$digitCount++;
}

$valor = $sum % 11;
return $digit === (int) $renavamArray[4];
}

$digit = $valor;
public function determinarDigito($renavam): int
{
$resultante = 0;
$contador = 0;

if ($valor == 11 || $valor == 0 || $valor >= 10) {
$digit = 0;
for ($indice = 5; $indice >= 2; $indice--) {
$resultante += $renavam[$contador] * $indice;
$contador++;
}

if ($digit == $renavamArray[4]) {
return true;
}
$verificador = $resultante % 11;

return false;
return $verificador >= 10 ? 0 : $verificador;
}
}
37 changes: 37 additions & 0 deletions tests/Rules/RenavamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace geekcom\ValidatorDocs\Tests\Rules;

use geekcom\ValidatorDocs\Rules\Renavam;
use geekcom\ValidatorDocs\Tests\ValidatorTestCase;

final class RenavamTest extends ValidatorTestCase
{
/**
* @test
*
* @dataProvider renavamProvider
*/
public function renavamValido($renavam, $esperado): void
{
$instance = new Renavam();

$atual = $instance->validateRenavam('', $renavam);

$this->assertSame($esperado, $atual);
}

public function renavamProvider(): array
{
return [
'Input correto' => [
'renavam' => 639884962,
'esperado' => true,
],
'Input incorreto' => [
'renavam' => 11111111111,
'esperado' => false
],
];
}
}

0 comments on commit 92eb11c

Please sign in to comment.