1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mero\Bundle\BaseBundle\Tests\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Mero\Bundle\BaseBundle\Validator\Constraints\CPF; |
6
|
|
|
use Mero\Bundle\BaseBundle\Validator\Constraints\CPFValidator; |
7
|
|
|
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest; |
8
|
|
|
use Symfony\Component\Validator\Validation; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
*/ |
12
|
|
View Code Duplication |
class CPFValidatorTest extends AbstractConstraintValidatorTest |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
protected function getApiVersion() |
15
|
|
|
{ |
16
|
|
|
return Validation::API_VERSION_2_5; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function createValidator() |
20
|
|
|
{ |
21
|
|
|
return new CPFValidator(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testNullIsValid() |
25
|
|
|
{ |
26
|
|
|
$this->validator->validate(null, new CPF()); |
27
|
|
|
$this->assertNoViolation(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testEmptyStringIsValid() |
31
|
|
|
{ |
32
|
|
|
$this->validator->validate('', new CPF()); |
33
|
|
|
$this->assertNoViolation(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getInvalidCPFs() |
37
|
|
|
{ |
38
|
|
|
return array( |
39
|
|
|
array('111.111.111-11'), |
40
|
|
|
array('222.222.222-22'), |
41
|
|
|
array('398.682.528-23'), |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @dataProvider getInvalidCPFs |
47
|
|
|
*/ |
48
|
|
|
public function testInvalidCPFs($cpf) |
49
|
|
|
{ |
50
|
|
|
$constraint = new CPF(array( |
51
|
|
|
'message' => 'testMessage', |
52
|
|
|
)); |
53
|
|
|
$this->validator->validate($cpf, $constraint); |
54
|
|
|
$this->buildViolation('testMessage') |
55
|
|
|
->setParameter('{{ value }}', '"'.$cpf.'"') |
56
|
|
|
->assertRaised(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getValidCPFs() |
60
|
|
|
{ |
61
|
|
|
return array( |
62
|
|
|
array('398.682.528-22'), |
63
|
|
|
array('534.005.933-20'), |
64
|
|
|
array('235.515.623-93'), |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @dataProvider getValidCPFs |
70
|
|
|
*/ |
71
|
|
|
public function testValidCPFs($cpf) |
72
|
|
|
{ |
73
|
|
|
$this->validator->validate($cpf, new CPF()); |
74
|
|
|
$this->assertNoViolation(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.