1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Awurth\SlimValidation\Tests; |
4
|
|
|
|
5
|
|
|
use Awurth\SlimValidation\Validator; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Respect\Validation\Validator as V; |
9
|
|
|
use Slim\Http\Environment; |
10
|
|
|
use Slim\Http\Request; |
11
|
|
|
|
12
|
|
|
class ValidatorTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Validator |
16
|
|
|
*/ |
17
|
|
|
protected $validator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Request |
21
|
|
|
*/ |
22
|
|
|
protected $request; |
23
|
|
|
|
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
$this->request = Request::createFromEnvironment(Environment::mock([ |
27
|
|
|
'QUERY_STRING' => 'username=a_wurth&password=1234' |
28
|
|
|
])); |
29
|
|
|
|
30
|
|
|
$this->validator = new Validator(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @expectedException InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function testValidateWithoutRules() |
37
|
|
|
{ |
38
|
|
|
$this->validator->validate($this->request, [ |
39
|
|
|
'username' |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @expectedException InvalidArgumentException |
45
|
|
|
*/ |
46
|
|
|
public function testValidateWithWrongOptionsType() |
47
|
|
|
{ |
48
|
|
|
$this->validator->validate($this->request, [ |
49
|
|
|
'username' => null |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @expectedException InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
public function testValidateWithWrongRulesType() |
57
|
|
|
{ |
58
|
|
|
$this->validator->validate($this->request, [ |
59
|
|
|
'username' => [ |
60
|
|
|
'rules' => null |
61
|
|
|
] |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testValidate() |
66
|
|
|
{ |
67
|
|
|
$this->validator->validate($this->request, [ |
68
|
|
|
'username' => V::length(6) |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues()); |
72
|
|
|
$this->assertEquals('a_wurth', $this->validator->getValue('username')); |
73
|
|
|
$this->assertTrue($this->validator->isValid()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testValidateWithErrors() |
77
|
|
|
{ |
78
|
|
|
$this->validator->validate($this->request, [ |
79
|
|
|
'username' => V::length(8) |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
$this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues()); |
83
|
|
|
$this->assertEquals('a_wurth', $this->validator->getValue('username')); |
84
|
|
|
$this->assertFalse($this->validator->isValid()); |
85
|
|
|
$this->assertEquals([ |
86
|
|
|
'username' => [ |
87
|
|
|
'length' => '"a_wurth" must have a length greater than 8' |
88
|
|
|
] |
89
|
|
|
], $this->validator->getErrors()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testValidateWithIndexedErrors() |
93
|
|
|
{ |
94
|
|
|
$this->validator->setStoreErrorsWithRules(false); |
95
|
|
|
$this->validator->validate($this->request, [ |
96
|
|
|
'username' => V::length(8) |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
$this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues()); |
100
|
|
|
$this->assertEquals('a_wurth', $this->validator->getValue('username')); |
101
|
|
|
$this->assertFalse($this->validator->isValid()); |
102
|
|
|
$this->assertEquals([ |
103
|
|
|
'username' => [ |
104
|
|
|
'"a_wurth" must have a length greater than 8' |
105
|
|
|
] |
106
|
|
|
], $this->validator->getErrors()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testValidateWithCustomDefaultMessage() |
110
|
|
|
{ |
111
|
|
|
$this->validator->setDefaultMessages([ |
112
|
|
|
'length' => 'Too short!' |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
$this->validator->validate($this->request, [ |
116
|
|
|
'username' => V::length(8) |
117
|
|
|
]); |
118
|
|
|
|
119
|
|
|
$this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues()); |
120
|
|
|
$this->assertEquals('a_wurth', $this->validator->getValue('username')); |
121
|
|
|
$this->assertFalse($this->validator->isValid()); |
122
|
|
|
$this->assertEquals([ |
123
|
|
|
'username' => [ |
124
|
|
|
'length' => 'Too short!' |
125
|
|
|
] |
126
|
|
|
], $this->validator->getErrors()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testValidateWithCustomGlobalMessages() |
130
|
|
|
{ |
131
|
|
|
$this->validator->validate($this->request, [ |
132
|
|
|
'username' => V::length(8), |
133
|
|
|
'password' => V::length(8) |
134
|
|
|
], [ |
135
|
|
|
'length' => 'Too short!' |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
$this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues()); |
139
|
|
|
$this->assertFalse($this->validator->isValid()); |
140
|
|
|
$this->assertEquals([ |
141
|
|
|
'username' => [ |
142
|
|
|
'length' => 'Too short!' |
143
|
|
|
], |
144
|
|
|
'password' => [ |
145
|
|
|
'length' => 'Too short!' |
146
|
|
|
] |
147
|
|
|
], $this->validator->getErrors()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testValidateWithCustomDefaultAndGlobalMessages() |
151
|
|
|
{ |
152
|
|
|
$this->validator->setDefaultMessages([ |
153
|
|
|
'length' => 'Too short!' |
154
|
|
|
]); |
155
|
|
|
|
156
|
|
|
$this->validator->validate($this->request, [ |
157
|
|
|
'username' => V::length(8), |
158
|
|
|
'password' => V::length(8)->alpha() |
159
|
|
|
], [ |
160
|
|
|
'alpha' => 'Only letters are allowed' |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
$this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues()); |
164
|
|
|
$this->assertFalse($this->validator->isValid()); |
165
|
|
|
$this->assertEquals([ |
166
|
|
|
'username' => [ |
167
|
|
|
'length' => 'Too short!' |
168
|
|
|
], |
169
|
|
|
'password' => [ |
170
|
|
|
'length' => 'Too short!', |
171
|
|
|
'alpha' => 'Only letters are allowed' |
172
|
|
|
] |
173
|
|
|
], $this->validator->getErrors()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @expectedException InvalidArgumentException |
178
|
|
|
* @expectedExceptionMessage Expected custom individual messages to be of type array, string given |
179
|
|
|
*/ |
180
|
|
|
public function testValidateWithWrongCustomIndividualMessagesType() |
181
|
|
|
{ |
182
|
|
|
$this->validator->validate($this->request, [ |
183
|
|
|
'username' => [ |
184
|
|
|
'rules' => V::length(8), |
185
|
|
|
'messages' => '' |
186
|
|
|
] |
187
|
|
|
]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testValidateWithCustomIndividualMessage() |
191
|
|
|
{ |
192
|
|
|
$this->validator->validate($this->request, [ |
193
|
|
|
'username' => [ |
194
|
|
|
'rules' => V::length(8), |
195
|
|
|
'messages' => [ |
196
|
|
|
'length' => 'Too short!' |
197
|
|
|
] |
198
|
|
|
], |
199
|
|
|
'password' => V::length(8) |
200
|
|
|
]); |
201
|
|
|
|
202
|
|
|
$this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues()); |
203
|
|
|
$this->assertFalse($this->validator->isValid()); |
204
|
|
|
$this->assertEquals([ |
205
|
|
|
'username' => [ |
206
|
|
|
'length' => 'Too short!' |
207
|
|
|
], |
208
|
|
|
'password' => [ |
209
|
|
|
'length' => '"1234" must have a length greater than 8' |
210
|
|
|
] |
211
|
|
|
], $this->validator->getErrors()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @expectedException InvalidArgumentException |
216
|
|
|
* @expectedExceptionMessage Expected custom message to be of type string, integer given |
217
|
|
|
*/ |
218
|
|
|
public function testValidateWithWrongCustomSingleMessageType() |
219
|
|
|
{ |
220
|
|
|
$this->validator->validate($this->request, [ |
221
|
|
|
'username' => [ |
222
|
|
|
'rules' => V::length(8)->alnum(), |
223
|
|
|
'message' => 10 |
224
|
|
|
] |
225
|
|
|
]); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testValidateWithCustomSingleMessage() |
229
|
|
|
{ |
230
|
|
|
$this->validator->validate($this->request, [ |
231
|
|
|
'username' => [ |
232
|
|
|
'rules' => V::length(8)->alnum(), |
233
|
|
|
'message' => 'Bad username.', |
234
|
|
|
'messages' => [ |
235
|
|
|
'length' => 'Too short!' |
236
|
|
|
] |
237
|
|
|
], |
238
|
|
|
'password' => [ |
239
|
|
|
'rules' => V::length(8), |
240
|
|
|
'messages' => [ |
241
|
|
|
'length' => 'Too short!' |
242
|
|
|
] |
243
|
|
|
] |
244
|
|
|
]); |
245
|
|
|
|
246
|
|
|
$this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues()); |
247
|
|
|
$this->assertFalse($this->validator->isValid()); |
248
|
|
|
$this->assertEquals([ |
249
|
|
|
'username' => [ |
250
|
|
|
'Bad username.' |
251
|
|
|
], |
252
|
|
|
'password' => [ |
253
|
|
|
'length' => 'Too short!' |
254
|
|
|
] |
255
|
|
|
], $this->validator->getErrors()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testIsValidWithErrors() |
259
|
|
|
{ |
260
|
|
|
$this->validator->setErrors(['error']); |
261
|
|
|
|
262
|
|
|
$this->assertFalse($this->validator->isValid()); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function testIsValidWithoutErrors() |
266
|
|
|
{ |
267
|
|
|
$this->validator->setErrors([]); |
268
|
|
|
|
269
|
|
|
$this->assertTrue($this->validator->isValid()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function testAddError() |
273
|
|
|
{ |
274
|
|
|
$this->validator->addError('param', 'message'); |
275
|
|
|
|
276
|
|
|
$this->assertEquals([ |
277
|
|
|
'param' => [ |
278
|
|
|
'message' |
279
|
|
|
] |
280
|
|
|
], $this->validator->getErrors()); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function testAddErrors() |
284
|
|
|
{ |
285
|
|
|
$this->validator->addErrors('param', ['message1', 'message2']); |
286
|
|
|
|
287
|
|
|
$this->assertEquals([ |
288
|
|
|
'param' => [ |
289
|
|
|
'message1', |
290
|
|
|
'message2' |
291
|
|
|
] |
292
|
|
|
], $this->validator->getErrors()); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|