Updated CNH validator with Exceptions

Created new Exception for CNH Validator
This commit is contained in:
Kinn Coelho Julião 2012-11-25 22:55:16 -02:00
parent fa298ffda6
commit eb519fdc55
3 changed files with 26 additions and 7 deletions

View file

@ -0,0 +1,17 @@
<?php
namespace Respect\Validation\Exceptions;
class CnhException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a valid CNH number',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a valid CNH number',
)
);
}

View file

@ -22,10 +22,9 @@ class Cnh extends AbstractRule
for ( $i = 0 , $j = 1 , $v = 0 ; $i < 9 ; ++$i , ++$j ) {
$v += (int) $input[$i] * $j;
$vl2 = ( $x = ( $v % 11 ) ) >= 10 ? 0 : $x - $dsc;
$ret = sprintf( '%d%d' , $vl1 , $vl2 ) == substr( $input , -2 );
}
$vl2 = ( $x = ( $v % 11 ) ) >= 10 ? 0 : $x - $dsc;
$ret = sprintf( '%d%d' , $vl1 , $vl2 ) == substr( $input , -2 );
}
return $ret;
}

View file

@ -15,34 +15,37 @@ class CnhTest extends \PHPUnit_Framework_TestCase {
*/
public function test_valid_cnh($cnh)
{
$expectedValid = $this->cnhValidator->validate($cnh);
$expectedValid = $this->cnhValidator->assert($cnh);
$this->assertTrue($expectedValid);
}
/**
* @dataProvider invalidCnhProvider
* @expectedException Respect\Validation\Exceptions\CnhException
*/
public function test_invalid_cnh($cnh)
{
$expectedInvalid = $this->cnhValidator->validate($cnh);
$expectedInvalid = $this->cnhValidator->assert($cnh);
$this->assertFalse($expectedInvalid);
}
/**
* @dataProvider notIntegerCnhProvider
* @expectedException Respect\Validation\Exceptions\CnhException
*/
public function test_not_integer_cnh($cnh)
{
$expectedInvalid = $this->cnhValidator->validate($cnh);
$expectedInvalid = $this->cnhValidator->assert($cnh);
$this->assertFalse($expectedInvalid);
}
/**
* @dataProvider invalidCnhLenghtProvider
* @expectedException Respect\Validation\Exceptions\CnhException
*/
public function test_invalid_length_cnh($cnh)
{
$expectedInvalid = $this->cnhValidator->validate($cnh);
$expectedInvalid = $this->cnhValidator->assert($cnh);
$this->assertFalse($expectedInvalid);
}