1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dokobit\Gateway\Tests\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Dokobit\Gateway\Validator\Constraints\Phone; |
6
|
|
|
use Dokobit\Gateway\Validator\Constraints\PhoneValidator; |
7
|
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
8
|
|
|
use Symfony\Component\Validator\Validation; |
9
|
|
|
|
10
|
|
|
class PhoneValidatorTest extends ConstraintValidatorTestCase |
11
|
|
|
{ |
12
|
|
|
protected function getApiVersion() |
13
|
|
|
{ |
14
|
|
|
return Validation::API_VERSION_2_5; |
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
protected function createValidator() |
18
|
|
|
{ |
19
|
|
|
return new PhoneValidator(false); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testNullIsValid() |
23
|
|
|
{ |
24
|
|
|
$this->validator->validate(null, new Phone()); |
25
|
|
|
|
26
|
|
|
$this->assertNoViolation(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testEmptyStringIsValid() |
30
|
|
|
{ |
31
|
|
|
$this->validator->validate('', new Phone()); |
32
|
|
|
|
33
|
|
|
$this->assertNoViolation(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testExpectsStringCompatibleType() |
37
|
|
|
{ |
38
|
|
|
$this->expectException(\Symfony\Component\Validator\Exception\UnexpectedTypeException::class); |
39
|
|
|
$this->validator->validate(new \stdClass(), new Phone()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @dataProvider getValidPhones |
44
|
|
|
*/ |
45
|
|
|
public function testValidPhones($phone) |
46
|
|
|
{ |
47
|
|
|
$this->validator->validate($phone, new Phone()); |
48
|
|
|
|
49
|
|
|
$this->assertNoViolation(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getValidPhones() |
53
|
|
|
{ |
54
|
|
|
return array( |
55
|
|
|
// Lithuania |
56
|
|
|
array('+37060000000'), |
57
|
|
|
// Estonia |
58
|
|
|
array('+3720000000'), |
59
|
|
|
array('+37200000000'), |
60
|
|
|
// // Finland |
61
|
|
|
array('+358409999'), |
62
|
|
|
array('+3584099999'), |
63
|
|
|
array('+35840999999'), |
64
|
|
|
array('+358409999999'), |
65
|
|
|
array('+3584099999999'), |
66
|
|
|
array('+3584579999'), |
67
|
|
|
array('+358509999'), |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @dataProvider getInvalidPhones |
73
|
|
|
*/ |
74
|
|
|
public function testInvalidPhones($phone) |
75
|
|
|
{ |
76
|
|
|
$constraint = new Phone(array( |
77
|
|
|
'message' => 'myMessage', |
78
|
|
|
)); |
79
|
|
|
|
80
|
|
|
$this->validator->validate($phone, $constraint); |
81
|
|
|
|
82
|
|
|
$this->buildViolation('myMessage') |
83
|
|
|
->setParameter('{{ value }}', '"'.$phone.'"') |
84
|
|
|
->setCode(defined('Dokobit\Gateway\Validator\Constraints\Phone::REGEX_FAILED_ERROR')?Phone::REGEX_FAILED_ERROR:null) |
|
|
|
|
85
|
|
|
->assertRaised(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getInvalidPhones() |
89
|
|
|
{ |
90
|
|
|
return array( |
91
|
|
|
array('1'), // too short |
92
|
|
|
array('personal_cde'), // random crap |
93
|
|
|
array('+00000000000'), // invalid |
94
|
|
|
// Lithuania |
95
|
|
|
array('+37000000000'), // invalid |
96
|
|
|
array('+3706000000000'), // invalid |
97
|
|
|
// Estonia |
98
|
|
|
array('+372000000'), |
99
|
|
|
array('+372000000000'), |
100
|
|
|
// // Finland |
101
|
|
|
array('+35840999'), |
102
|
|
|
array('+35840999999999'), |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|