1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pixelpeter\IsoCodesValidation; |
4
|
|
|
|
5
|
|
|
use Validator; |
6
|
|
|
|
7
|
|
|
class IsoCodesValidatorTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Test the correct error messages ist returned |
11
|
|
|
* with all pace holder replaced |
12
|
|
|
* |
13
|
|
|
* @test |
14
|
|
|
* |
15
|
|
|
* @dataProvider invalidDataProvider |
16
|
|
|
*/ |
17
|
|
|
public function validator_returns_correct_error_message($payload, $rules, $expected) |
18
|
|
|
{ |
19
|
|
|
$validator = Validator::make($payload, $rules); |
20
|
|
|
|
21
|
|
|
$this->assertEquals($expected, $validator->errors()->first()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Test each validator returns true for valid data |
26
|
|
|
* |
27
|
|
|
* @test |
28
|
|
|
* |
29
|
|
|
* @dataProvider validDataProvider |
30
|
|
|
* |
31
|
|
|
* @param $payload |
32
|
|
|
* @param $rules |
33
|
|
|
*/ |
34
|
|
|
public function it_validates_correctly($field, $value) |
35
|
|
|
{ |
36
|
|
|
$validator = Validator::make([$field => $value], [$field => $field]); |
37
|
|
|
|
38
|
|
|
$this->assertTrue($validator->passes()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Test each validator with a reference value returns true for valid data |
43
|
|
|
* |
44
|
|
|
* @test |
45
|
|
|
* |
46
|
|
|
* @dataProvider validDataWithReferencesProvider |
47
|
|
|
* |
48
|
|
|
* @param $payload |
49
|
|
|
* @param $rules |
50
|
|
|
*/ |
51
|
|
|
public function it_validates_correctly_with_references($field, $value, $referenceField = '', $referenceValue = '') |
52
|
|
|
{ |
53
|
|
|
$payload = [ |
54
|
|
|
$field => $value, |
55
|
|
|
$referenceField => $referenceValue, |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$rules = [ |
59
|
|
|
$field => "{$field}:{$referenceField}", |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
$validator = Validator::make($payload, $rules); |
63
|
|
|
|
64
|
|
|
$this->assertTrue($validator->passes()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* DataProvider for simple rules |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function validDataProvider() |
73
|
|
|
{ |
74
|
|
|
return include_once __DIR__.'/fixtures/valid.php'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* DataProvider for failing tests |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function invalidDataProvider() |
83
|
|
|
{ |
84
|
|
|
return include_once __DIR__.'/fixtures/invalid.php'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* DataProvider for rules with references |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function validDataWithReferencesProvider() |
93
|
|
|
{ |
94
|
|
|
return include_once __DIR__.'/fixtures/valid_with_references.php'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* DataProvider for arrays with dot notation |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function validDataWithDotNotationProvider() |
103
|
|
|
{ |
104
|
|
|
return include_once __DIR__.'/fixtures/valid_wit_dot_notation.php'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* DataProvider for arrays with dot notation |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function invalidDataWithDotNotationProvider() |
113
|
|
|
{ |
114
|
|
|
return include_once __DIR__.'/fixtures/invalid_wit_dot_notation.php'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Test validator can work on arrays with dot notation |
119
|
|
|
* and returns true for valid data |
120
|
|
|
* |
121
|
|
|
* @test |
122
|
|
|
* |
123
|
|
|
* @dataProvider validDataWithDotNotationProvider |
124
|
|
|
*/ |
125
|
|
|
public function it_validates_correctly_with_dot_notation($data, $rule) |
126
|
|
|
{ |
127
|
|
|
$payload = [ |
128
|
|
|
'data' => $data, |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
$validator = Validator::make($payload, $rule); |
132
|
|
|
|
133
|
|
|
$this->assertTrue($validator->passes()); |
134
|
|
|
$this->assertEmpty($validator->errors()->all()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Test the correct error messages ist returned |
139
|
|
|
* on arrays with dot notation |
140
|
|
|
* |
141
|
|
|
* @test |
142
|
|
|
* |
143
|
|
|
* @dataProvider invalidDataWithDotNotationProvider |
144
|
|
|
*/ |
145
|
|
|
public function validator_returns_correct_error_message_with_dot_notation($data, $rule, $error_message) |
146
|
|
|
{ |
147
|
|
|
$payload = [ |
148
|
|
|
'data' => $data, |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
$validator = Validator::make($payload, $rule); |
152
|
|
|
|
153
|
|
|
$this->assertTrue($validator->fails()); |
154
|
|
|
$this->assertCount(2, $validator->errors()->all()); |
155
|
|
|
$this->assertEquals($error_message[0], $validator->errors()->first()); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|