|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\ValidationBundle\Tests\Validator\Constraints; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\ValidationBundle\Validator\Constraints\CEP; |
|
14
|
|
|
use LoginCidadao\ValidationBundle\Validator\Constraints\CEPValidator; |
|
15
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
|
16
|
|
|
|
|
17
|
|
|
class CEPValidatorTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testValid() |
|
20
|
|
|
{ |
|
21
|
|
|
$validValues = [ |
|
22
|
|
|
'00000-000', |
|
23
|
|
|
'00000000', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
foreach ($validValues as $value) { |
|
27
|
|
|
$this->assertTrue(CEPValidator::isCEPValid($value), "Invalid value: {$value}"); |
|
28
|
|
|
$this->assertTrue(CEPValidator::checkLength($value), "Invalid value: {$value}"); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$context = $this->getContext($this->never()); |
|
32
|
|
|
$constraint = new CEP(); |
|
33
|
|
|
|
|
34
|
|
|
$validator = new CEPValidator(); |
|
35
|
|
|
$validator->initialize($context); |
|
36
|
|
|
$validator->validate(null, $constraint); |
|
37
|
|
|
$validator->validate('00000000', $constraint); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testInvalid() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->assertFalse(CEPValidator::isCEPValid('not_postal_code')); |
|
43
|
|
|
$this->assertFalse(CEPValidator::checkLength('123456789')); |
|
44
|
|
|
|
|
45
|
|
|
$context = $this->getContext($this->once()); |
|
46
|
|
|
$constraint = new CEP(); |
|
47
|
|
|
|
|
48
|
|
|
$validator = new CEPValidator(); |
|
49
|
|
|
$validator->initialize($context); |
|
50
|
|
|
$validator->validate('000000000', $constraint); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return ExecutionContextInterface|\PHPUnit_Framework_MockObject_MockObject |
|
55
|
|
|
*/ |
|
56
|
|
|
private function getContext($violations) |
|
57
|
|
|
{ |
|
58
|
|
|
$context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface'); |
|
59
|
|
|
$context->expects($violations)->method('addViolation'); |
|
60
|
|
|
|
|
61
|
|
|
return $context; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|