1
|
|
|
<?php |
2
|
|
|
namespace Assert\Tests; |
3
|
|
|
|
4
|
|
|
use Assert\Assertion; |
5
|
|
|
use Assert\AssertionFailedException; |
6
|
|
|
use Webmozart\Assert\Assert; |
7
|
|
|
|
8
|
|
|
class AssertTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
public static function dataInvalidFloat() |
11
|
|
|
{ |
12
|
|
|
return array( |
13
|
|
|
array(1), |
14
|
|
|
array(false), |
15
|
|
|
array("test"), |
16
|
|
|
array(null), |
17
|
|
|
array("1.23"), |
18
|
|
|
array("10"), |
19
|
|
|
); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @dataProvider dataInvalidFloat |
24
|
|
|
*/ |
25
|
|
|
public function testInvalidFloat($nonFloat) |
26
|
|
|
{ |
27
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FLOAT); |
28
|
|
|
Assertion::float($nonFloat); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testValidFloat() |
32
|
|
|
{ |
33
|
|
|
Assertion::float(1.0); |
34
|
|
|
Assertion::float(0.1); |
35
|
|
|
Assertion::float(-1.1); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function dataInvalidInteger() |
39
|
|
|
{ |
40
|
|
|
return array( |
41
|
|
|
array(1.23), |
42
|
|
|
array(false), |
43
|
|
|
array("test"), |
44
|
|
|
array(null), |
45
|
|
|
array("1.23"), |
46
|
|
|
array("10"), |
47
|
|
|
array(new \DateTime()), |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider dataInvalidInteger |
53
|
|
|
*/ |
54
|
|
|
public function testInvalidInteger($nonInteger) |
55
|
|
|
{ |
56
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTEGER); |
57
|
|
|
Assertion::integer($nonInteger); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testValidInteger() |
61
|
|
|
{ |
62
|
|
|
Assertion::integer(10); |
63
|
|
|
Assertion::integer(0); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testValidIntegerish() |
67
|
|
|
{ |
68
|
|
|
Assertion::integerish(10); |
69
|
|
|
Assertion::integerish("10"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function dataInvalidIntegerish() |
73
|
|
|
{ |
74
|
|
|
return array( |
75
|
|
|
array(1.23), |
76
|
|
|
array(false), |
77
|
|
|
array("test"), |
78
|
|
|
array(null), |
79
|
|
|
array("1.23"), |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @dataProvider dataInvalidIntegerish |
85
|
|
|
*/ |
86
|
|
|
public function testInvalidIntegerish($nonInteger) |
87
|
|
|
{ |
88
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTEGERISH); |
89
|
|
|
Assertion::integerish($nonInteger); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testValidBoolean() |
93
|
|
|
{ |
94
|
|
|
Assertion::boolean(true); |
95
|
|
|
Assertion::boolean(false); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testInvalidBoolean() |
99
|
|
|
{ |
100
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BOOLEAN); |
101
|
|
|
Assertion::boolean(1); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testInvalidScalar() |
105
|
|
|
{ |
106
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SCALAR); |
107
|
|
|
Assertion::scalar(new \stdClass); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testValidScalar() |
111
|
|
|
{ |
112
|
|
|
Assertion::scalar("foo"); |
113
|
|
|
Assertion::scalar(52); |
114
|
|
|
Assertion::scalar(12.34); |
115
|
|
|
Assertion::scalar(false); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function dataInvalidNotEmpty() |
119
|
|
|
{ |
120
|
|
|
return array( |
121
|
|
|
array(""), |
122
|
|
|
array(false), |
123
|
|
|
array(0), |
124
|
|
|
array(null), |
125
|
|
|
array( array() ), |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @dataProvider dataInvalidNotEmpty |
131
|
|
|
*/ |
132
|
|
|
public function testInvalidNotEmpty($value) |
133
|
|
|
{ |
134
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_EMPTY); |
135
|
|
|
Assertion::notEmpty($value); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testNotEmpty() |
139
|
|
|
{ |
140
|
|
|
Assertion::notEmpty("test"); |
141
|
|
|
Assertion::notEmpty(1); |
142
|
|
|
Assertion::notEmpty(true); |
143
|
|
|
Assertion::notEmpty( array("foo") ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testEmpty() |
147
|
|
|
{ |
148
|
|
|
Assertion::noContent(""); |
149
|
|
|
Assertion::noContent(0); |
150
|
|
|
Assertion::noContent(false); |
151
|
|
|
Assertion::noContent( array() ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public static function dataInvalidEmpty() |
155
|
|
|
{ |
156
|
|
|
return array( |
157
|
|
|
array("foo"), |
158
|
|
|
array(true), |
159
|
|
|
array(12), |
160
|
|
|
array( array('foo') ), |
161
|
|
|
array( new \stdClass() ), |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @dataProvider dataInvalidEmpty |
167
|
|
|
*/ |
168
|
|
|
public function testInvalidEmpty($value) |
169
|
|
|
{ |
170
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NOT_EMPTY); |
171
|
|
|
Assertion::noContent($value); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public static function dataInvalidNull() |
175
|
|
|
{ |
176
|
|
|
return array( |
177
|
|
|
array("foo"), |
178
|
|
|
array(""), |
179
|
|
|
array(false), |
180
|
|
|
array(12), |
181
|
|
|
array(0), |
182
|
|
|
array(array()), |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @dataProvider dataInvalidNull |
188
|
|
|
*/ |
189
|
|
|
public function testInvalidNull($value) |
190
|
|
|
{ |
191
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NOT_NULL); |
192
|
|
|
Assertion::null($value); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testNull() |
196
|
|
|
{ |
197
|
|
|
Assertion::null(null); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testNotNull() |
201
|
|
|
{ |
202
|
|
|
Assertion::notNull("1"); |
203
|
|
|
Assertion::notNull(1); |
204
|
|
|
Assertion::notNull(0); |
205
|
|
|
Assertion::notNull(array()); |
206
|
|
|
Assertion::notNull(false); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testInvalidNotNull() |
210
|
|
|
{ |
211
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NULL); |
212
|
|
|
Assertion::notNull(null); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function testString() |
216
|
|
|
{ |
217
|
|
|
Assertion::string("test-string"); |
218
|
|
|
Assertion::string(""); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @dataProvider dataInvalidString |
223
|
|
|
*/ |
224
|
|
|
public function testInvalidString($invalidString) |
225
|
|
|
{ |
226
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING); |
227
|
|
|
Assertion::string($invalidString); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public static function dataInvalidString() |
231
|
|
|
{ |
232
|
|
|
return array( |
233
|
|
|
array(1.23), |
234
|
|
|
array(false), |
235
|
|
|
array(new \ArrayObject), |
236
|
|
|
array(null), |
237
|
|
|
array(10), |
238
|
|
|
array(true), |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testInvalidRegex() |
243
|
|
|
{ |
244
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_REGEX); |
245
|
|
|
Assertion::regex("foo", "(bar)"); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testInvalidRegexValueNotString() |
249
|
|
|
{ |
250
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING); |
251
|
|
|
Assertion::regex(array("foo"), "(bar)"); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function testInvalidMinLength() |
255
|
|
|
{ |
256
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MIN_LENGTH); |
257
|
|
|
Assertion::minLength("foo", 4); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function testValidMinLength() |
261
|
|
|
{ |
262
|
|
|
Assertion::minLength("foo", 3); |
263
|
|
|
Assertion::minLength("foo", 1); |
264
|
|
|
Assertion::minLength("foo", 0); |
265
|
|
|
Assertion::minLength("", 0); |
266
|
|
|
Assertion::minLength("址址", 2); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testInvalidMaxLength() |
270
|
|
|
{ |
271
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MAX_LENGTH); |
272
|
|
|
Assertion::maxLength("foo", 2); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testValidMaxLength() |
276
|
|
|
{ |
277
|
|
|
Assertion::maxLength("foo", 10); |
278
|
|
|
Assertion::maxLength("foo", 3); |
279
|
|
|
Assertion::maxLength("", 0); |
280
|
|
|
Assertion::maxLength("址址", 2); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function testInvalidBetweenLengthMin() |
284
|
|
|
{ |
285
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MIN_LENGTH); |
286
|
|
|
Assertion::betweenLength("foo", 4, 100); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function testInvalidBetweenLengthMax() |
290
|
|
|
{ |
291
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MAX_LENGTH); |
292
|
|
|
Assertion::betweenLength("foo", 0, 2); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function testValidBetweenLength() |
296
|
|
|
{ |
297
|
|
|
Assertion::betweenLength("foo", 0, 3); |
298
|
|
|
Assertion::betweenLength("址址", 2, 2); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function testInvalidStartsWith() |
302
|
|
|
{ |
303
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_START); |
304
|
|
|
Assertion::startsWith("foo", "bar"); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function testInvalidStartsWithDueToWrongEncoding() |
308
|
|
|
{ |
309
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_START); |
310
|
|
|
Assertion::startsWith("址", "址址", null, null, 'ASCII'); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function testValidStartsWith() |
314
|
|
|
{ |
315
|
|
|
Assertion::startsWith("foo", "foo"); |
316
|
|
|
Assertion::startsWith("foo", "fo"); |
317
|
|
|
Assertion::startsWith("foo", "f"); |
318
|
|
|
Assertion::startsWith("址foo", "址"); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
public function testInvalidEndsWith() |
322
|
|
|
{ |
323
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END); |
324
|
|
|
Assertion::endsWith("foo", "bar"); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function testInvalidEndsWithDueToWrongEncoding() |
328
|
|
|
{ |
329
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END); |
330
|
|
|
Assertion::endsWith("址", "址址", null, null, 'ASCII'); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function testValidEndsWith() |
334
|
|
|
{ |
335
|
|
|
Assertion::endsWith("foo", "foo"); |
336
|
|
|
Assertion::endsWith("sonderbar", "bar"); |
337
|
|
|
Assertion::endsWith("opp", "p"); |
338
|
|
|
Assertion::endsWith("foo址", "址"); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
public function testInvalidContains() |
342
|
|
|
{ |
343
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_CONTAINS); |
344
|
|
|
Assertion::contains("foo", "bar"); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
public function testValidContains() |
348
|
|
|
{ |
349
|
|
|
Assertion::contains("foo", "foo"); |
350
|
|
|
Assertion::contains("foo", "oo"); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function testInvalidChoice() |
354
|
|
|
{ |
355
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE); |
356
|
|
|
Assertion::choice("foo", array("bar", "baz")); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
public function testValidChoice() |
360
|
|
|
{ |
361
|
|
|
Assertion::choice("foo", array("foo")); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function testInvalidInArray() |
365
|
|
|
{ |
366
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE); |
367
|
|
|
Assertion::inArray("bar", array("baz")); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
public function testValidInArray() |
371
|
|
|
{ |
372
|
|
|
Assertion::inArray("foo", array("foo")); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
public function testInvalidNumeric() |
376
|
|
|
{ |
377
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NUMERIC); |
378
|
|
|
Assertion::numeric("foo"); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
public function testValidNumeric() |
382
|
|
|
{ |
383
|
|
|
Assertion::numeric("1"); |
384
|
|
|
Assertion::numeric(1); |
385
|
|
|
Assertion::numeric(1.23); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public static function dataInvalidArray() |
389
|
|
|
{ |
390
|
|
|
return array( |
391
|
|
|
array(null), |
392
|
|
|
array(false), |
393
|
|
|
array("test"), |
394
|
|
|
array(1), |
395
|
|
|
array(1.23), |
396
|
|
|
array(new \stdClass), |
397
|
|
|
array(fopen('php://memory', 'r')), |
398
|
|
|
); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @dataProvider dataInvalidArray |
403
|
|
|
*/ |
404
|
|
|
public function testInvalidArray($value) |
405
|
|
|
{ |
406
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_ARRAY); |
407
|
|
|
Assertion::isArray($value); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
public function testValidArray() |
411
|
|
|
{ |
412
|
|
|
Assertion::isArray(array()); |
413
|
|
|
Assertion::isArray(array(1,2,3)); |
414
|
|
|
Assertion::isArray(array(array(),array())); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
public function testInvalidKeyExists() |
418
|
|
|
{ |
419
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_EXISTS); |
420
|
|
|
Assertion::keyExists(array("foo" => "bar"), "baz"); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
public function testValidKeyExists() |
424
|
|
|
{ |
425
|
|
|
Assertion::keyExists(array("foo" => "bar"), "foo"); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
public function testInvalidKeyNotExists() |
429
|
|
|
{ |
430
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_NOT_EXISTS); |
431
|
|
|
Assertion::keyNotExists(array("foo" => "bar"), "foo"); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
public function testValidKeyNotExists() |
435
|
|
|
{ |
436
|
|
|
Assertion::keyNotExists(array("foo" => "bar"), "baz"); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
public static function dataInvalidNotBlank() |
440
|
|
|
{ |
441
|
|
|
return array( |
442
|
|
|
array(""), |
443
|
|
|
array(" "), |
444
|
|
|
array("\t"), |
445
|
|
|
array("\n"), |
446
|
|
|
array("\r"), |
447
|
|
|
array(false), |
448
|
|
|
array(null), |
449
|
|
|
array( array() ), |
450
|
|
|
); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* @dataProvider dataInvalidNotBlank |
455
|
|
|
*/ |
456
|
|
|
public function testInvalidNotBlank($notBlank) |
457
|
|
|
{ |
458
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_BLANK); |
459
|
|
|
Assertion::notBlank($notBlank); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
public function testValidNotBlank() |
463
|
|
|
{ |
464
|
|
|
Assertion::notBlank("foo"); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
public function testInvalidNotInstanceOf() |
468
|
|
|
{ |
469
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_INSTANCE_OF); |
470
|
|
|
Assertion::notIsInstanceOf(new \stdClass, 'stdClass'); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
public function testValidNotIsInstanceOf() |
474
|
|
|
{ |
475
|
|
|
Assertion::notIsInstanceOf(new \stdClass, 'PDO'); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
public function testInvalidInstanceOf() |
479
|
|
|
{ |
480
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INSTANCE_OF); |
481
|
|
|
Assertion::isInstanceOf(new \stdClass, 'PDO'); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
public function testValidInstanceOf() |
485
|
|
|
{ |
486
|
|
|
Assertion::isInstanceOf(new \stdClass, 'stdClass'); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
public function testInvalidSubclassOf() |
490
|
|
|
{ |
491
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SUBCLASS_OF); |
492
|
|
|
Assertion::subclassOf(new \stdClass, 'PDO'); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
public function testValidSubclassOf() |
496
|
|
|
{ |
497
|
|
|
Assertion::subclassOf(new ChildStdClass, 'stdClass'); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
public function testInvalidRange() |
501
|
|
|
{ |
502
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_RANGE); |
503
|
|
|
Assertion::range(1, 2, 3); |
504
|
|
|
Assertion::range(1.5, 2, 3); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
public function testValidRange() |
508
|
|
|
{ |
509
|
|
|
Assertion::range(1, 1, 2); |
510
|
|
|
Assertion::range(2, 1, 2); |
511
|
|
|
Assertion::range(2, 0, 100); |
512
|
|
|
Assertion::range(2.5, 2.25, 2.75); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
public function testInvalidEmail() |
516
|
|
|
{ |
517
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_EMAIL); |
518
|
|
|
Assertion::email("foo"); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
public function testValidEmail() |
522
|
|
|
{ |
523
|
|
|
Assertion::email("[email protected]"); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* @dataProvider dataInvalidUrl |
528
|
|
|
*/ |
529
|
|
|
public function testInvalidUrl($url) |
530
|
|
|
{ |
531
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_URL); |
532
|
|
|
|
533
|
|
|
Assertion::url($url); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
public static function dataInvalidUrl() |
537
|
|
|
{ |
538
|
|
|
return array( |
539
|
|
|
'null value' => array(""), |
540
|
|
|
'empty string' => array(" "), |
541
|
|
|
'no scheme' => array("url.de"), |
542
|
|
|
'unsupported scheme' => array("git://url.de"), |
543
|
|
|
'Http with query (no / between tld und ?)' => array("http://example.org?do=something"), |
544
|
|
|
'Http with query and port (no / between port und ?)' => array("http://example.org:8080?do=something"), |
545
|
|
|
); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* @dataProvider dataValidUrl |
550
|
|
|
*/ |
551
|
|
|
public function testValidUrl($url) |
552
|
|
|
{ |
553
|
|
|
Assertion::url($url); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
public static function dataValidUrl() |
557
|
|
|
{ |
558
|
|
|
return array( |
559
|
|
|
'straight with Http' => array("http://example.org"), |
560
|
|
|
'Http with path' => array("http://example.org/do/something"), |
561
|
|
|
'Http with query' => array("http://example.org/index.php?do=something"), |
562
|
|
|
'Http with port' => array("http://example.org:8080"), |
563
|
|
|
'Http with all possibilities' => array("http://example.org:8080/do/something/index.php?do=something"), |
564
|
|
|
'straight with Https' => array("https://example.org"), |
565
|
|
|
); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
public function testInvalidDigit() |
569
|
|
|
{ |
570
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DIGIT); |
571
|
|
|
Assertion::digit(-1); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
public function testValidDigit() |
575
|
|
|
{ |
576
|
|
|
Assertion::digit(1); |
577
|
|
|
Assertion::digit(0); |
578
|
|
|
Assertion::digit("0"); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
public function testValidAlnum() |
582
|
|
|
{ |
583
|
|
|
Assertion::alnum("a"); |
584
|
|
|
Assertion::alnum("a1"); |
585
|
|
|
Assertion::alnum("aasdf1234"); |
586
|
|
|
Assertion::alnum("a1b2c3"); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
public function testInvalidAlnum() |
590
|
|
|
{ |
591
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_ALNUM); |
592
|
|
|
Assertion::alnum("1a"); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
public function testValidTrue() |
596
|
|
|
{ |
597
|
|
|
Assertion::true(1 == 1); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
public function testInvalidTrue() |
601
|
|
|
{ |
602
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE); |
603
|
|
|
Assertion::true(false); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
public function testValidFalse() |
607
|
|
|
{ |
608
|
|
|
Assertion::false(1 == 0); |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
public function testInvalidFalse() |
612
|
|
|
{ |
613
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FALSE); |
614
|
|
|
Assertion::false(true); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
public function testInvalidClass() |
618
|
|
|
{ |
619
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CLASS); |
620
|
|
|
Assertion::classExists("Foo"); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
public function testValidClass() |
624
|
|
|
{ |
625
|
|
|
Assertion::classExists("\\Exception"); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
public function testSame() |
629
|
|
|
{ |
630
|
|
|
Assertion::same(1,1); |
631
|
|
|
Assertion::same("foo","foo"); |
632
|
|
|
Assertion::same($obj = new \stdClass(), $obj); |
633
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SAME); |
634
|
|
|
Assertion::same(new \stdClass(), new \stdClass()); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
public function testEq() |
638
|
|
|
{ |
639
|
|
|
Assertion::eq(1,"1"); |
640
|
|
|
Assertion::eq("foo",true); |
641
|
|
|
Assertion::eq($obj = new \stdClass(), $obj); |
642
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_EQ); |
643
|
|
|
Assertion::eq("2", 1); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
public function testNotEq() |
647
|
|
|
{ |
648
|
|
|
Assertion::notEq("1", false); |
649
|
|
|
Assertion::notEq(new \stdClass(), array()); |
650
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_EQ); |
651
|
|
|
Assertion::notEq("1", 1); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
public function testNotSame() |
655
|
|
|
{ |
656
|
|
|
Assertion::notSame("1", 2); |
657
|
|
|
Assertion::notSame(new \stdClass(), array()); |
658
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_SAME); |
659
|
|
|
Assertion::notSame(1, 1); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
public function testNotInArray() |
663
|
|
|
{ |
664
|
|
|
Assertion::notInArray(6, range(1, 5)); |
665
|
|
|
|
666
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_VALUE_IN_ARRAY); |
667
|
|
|
Assertion::notInArray(1, range(1, 5)); |
668
|
|
|
Assertion::notInArray(range('a', 'c'), range('a', 'd')); |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
public function testMin() |
672
|
|
|
{ |
673
|
|
|
Assertion::min(1, 1); |
674
|
|
|
Assertion::min(2, 1); |
675
|
|
|
Assertion::min(2.5, 1); |
676
|
|
|
|
677
|
|
|
try { |
678
|
|
|
Assertion::min(0, 1); |
679
|
|
|
$this->fail('Expected exception `Assert\AssertionFailedException` not thrown'); |
680
|
|
|
} catch (AssertionFailedException $e){ |
681
|
|
|
$this->assertEquals(Assertion::INVALID_MIN, $e->getCode()); |
682
|
|
|
$this->assertEquals('Number "0" was expected to be at least "1".', $e->getMessage()); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
try { |
686
|
|
|
Assertion::min(0.5, 2.5); |
687
|
|
|
$this->fail('Expected exception `Assert\AssertionFailedException` not thrown'); |
688
|
|
|
} catch (AssertionFailedException $e){ |
689
|
|
|
$this->assertEquals(Assertion::INVALID_MIN, $e->getCode()); |
690
|
|
|
$this->assertEquals('Number "0.5" was expected to be at least "2.5".', $e->getMessage()); |
691
|
|
|
} |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
public function testMax() |
695
|
|
|
{ |
696
|
|
|
Assertion::max(1, 1); |
697
|
|
|
Assertion::max(0.5, 1); |
698
|
|
|
Assertion::max(0, 1); |
699
|
|
|
|
700
|
|
|
try { |
701
|
|
|
Assertion::max(2, 1); |
702
|
|
|
$this->fail('Expected exception `Assert\AssertionFailedException` not thrown'); |
703
|
|
|
} catch (AssertionFailedException $e){ |
704
|
|
|
$this->assertEquals(Assertion::INVALID_MAX, $e->getCode()); |
705
|
|
|
$this->assertEquals('Number "2" was expected to be at most "1".', $e->getMessage()); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
try { |
709
|
|
|
Assertion::max(2.5, 0.5); |
710
|
|
|
$this->fail('Expected exception `Assert\AssertionFailedException` not thrown'); |
711
|
|
|
} catch (AssertionFailedException $e){ |
712
|
|
|
$this->assertEquals(Assertion::INVALID_MAX, $e->getCode()); |
713
|
|
|
$this->assertEquals('Number "2.5" was expected to be at most "0.5".', $e->getMessage()); |
714
|
|
|
} |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
public function testNullOr() |
718
|
|
|
{ |
719
|
|
|
Assertion::nullOrMax(null, 1); |
720
|
|
|
Assertion::nullOrMax(null, 2); |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
public function testNullOrWithNoValueThrows() |
724
|
|
|
{ |
725
|
|
|
$this->setExpectedException('BadMethodCallException'); |
726
|
|
|
Assertion::nullOrMax(); |
|
|
|
|
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
public function testLength() |
730
|
|
|
{ |
731
|
|
|
Assertion::length("asdf", 4); |
732
|
|
|
Assertion::length("", 0); |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
public static function dataLengthUtf8Characters() |
736
|
|
|
{ |
737
|
|
|
return array( |
738
|
|
|
array("址", 1), |
739
|
|
|
array("ل", 1), |
740
|
|
|
); |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* @dataProvider dataLengthUtf8Characters |
745
|
|
|
*/ |
746
|
|
|
public function testLenghtUtf8Characters($value, $expected) |
747
|
|
|
{ |
748
|
|
|
Assertion::length($value, $expected); |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
public function testLengthFailed() |
752
|
|
|
{ |
753
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LENGTH); |
754
|
|
|
Assertion::length("asdf", 3); |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
public function testLengthFailedForWrongEncoding() |
758
|
|
|
{ |
759
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LENGTH); |
760
|
|
|
Assertion::length("址", 1, null, null, 'ASCII'); |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
public function testLengthValidForGivenEncoding() |
764
|
|
|
{ |
765
|
|
|
Assertion::length("址", 1, null, null, 'utf8'); |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
public function testFile() |
769
|
|
|
{ |
770
|
|
|
Assertion::file(__FILE__); |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
public function testFileWithEmptyFilename() |
774
|
|
|
{ |
775
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_EMPTY); |
776
|
|
|
Assertion::file(""); |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
public function testFileDoesNotExists() |
780
|
|
|
{ |
781
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FILE); |
782
|
|
|
Assertion::file(__DIR__ . '/does-not-exists'); |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
public function testDirectory() |
786
|
|
|
{ |
787
|
|
|
Assertion::directory(__DIR__); |
788
|
|
|
|
789
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DIRECTORY); |
790
|
|
|
Assertion::directory(__DIR__ . '/does-not-exist'); |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
public function testReadable() |
794
|
|
|
{ |
795
|
|
|
Assertion::readable(__FILE__); |
796
|
|
|
|
797
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_READABLE); |
798
|
|
|
Assertion::readable(__DIR__ . '/does-not-exist'); |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
public function testWriteable() |
802
|
|
|
{ |
803
|
|
|
Assertion::writeable(sys_get_temp_dir()); |
804
|
|
|
|
805
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_WRITEABLE); |
806
|
|
|
Assertion::writeable(__DIR__ . '/does-not-exist'); |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
/** |
810
|
|
|
* @expectedException \BadMethodCallException |
811
|
|
|
* @expectedExceptionMessage No assertion |
812
|
|
|
*/ |
813
|
|
|
public function testFailedNullOrMethodCall() |
814
|
|
|
{ |
815
|
|
|
Assertion::NullOrAssertionDoesNotExist(); |
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
public function testImplementsInterface() |
819
|
|
|
{ |
820
|
|
|
Assertion::implementsInterface( |
821
|
|
|
'\ArrayIterator', |
822
|
|
|
'\Traversable' |
823
|
|
|
); |
824
|
|
|
|
825
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INTERFACE_NOT_IMPLEMENTED); |
826
|
|
|
Assertion::implementsInterface( |
827
|
|
|
'\Exception', |
828
|
|
|
'\Traversable' |
829
|
|
|
); |
830
|
|
|
} |
831
|
|
|
|
832
|
|
|
public function testImplementsInterfaceWithClassObject() |
833
|
|
|
{ |
834
|
|
|
$class = new \ArrayObject(); |
835
|
|
|
|
836
|
|
|
Assertion::implementsInterface( |
837
|
|
|
$class, |
838
|
|
|
'\Traversable' |
839
|
|
|
); |
840
|
|
|
|
841
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INTERFACE_NOT_IMPLEMENTED); |
842
|
|
|
Assertion::implementsInterface( |
843
|
|
|
$class, |
844
|
|
|
'\SplObserver' |
845
|
|
|
); |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* @dataProvider isJsonStringDataprovider |
850
|
|
|
*/ |
851
|
|
|
public function testIsJsonString($content) |
852
|
|
|
{ |
853
|
|
|
Assertion::isJsonString($content); |
854
|
|
|
} |
855
|
|
|
|
856
|
|
|
public static function isJsonStringDataprovider() |
857
|
|
|
{ |
858
|
|
|
return array( |
859
|
|
|
'»null« value' => array(json_encode(null)), |
860
|
|
|
'»false« value' => array(json_encode(false)), |
861
|
|
|
'array value' => array('["false"]'), |
862
|
|
|
'object value' => array('{"tux":"false"}'), |
863
|
|
|
); |
864
|
|
|
} |
865
|
|
|
|
866
|
|
|
/** |
867
|
|
|
* @dataProvider isJsonStringInvalidStringDataprovider |
868
|
|
|
*/ |
869
|
|
|
public function testIsJsonStringExpectingException($invalidString) |
870
|
|
|
{ |
871
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_JSON_STRING); |
872
|
|
|
Assertion::isJsonString($invalidString); |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
public static function isJsonStringInvalidStringDataprovider() |
876
|
|
|
{ |
877
|
|
|
return array( |
878
|
|
|
'no json string' => array('invalid json encoded string'), |
879
|
|
|
'error in json string' => array('{invalid json encoded string}'), |
880
|
|
|
); |
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
/** |
884
|
|
|
* @dataProvider providesValidUuids |
885
|
|
|
*/ |
886
|
|
|
public function testValidUuids($uuid) |
887
|
|
|
{ |
888
|
|
|
Assertion::uuid($uuid); |
889
|
|
|
} |
890
|
|
|
|
891
|
|
|
/** |
892
|
|
|
* @dataProvider providesInvalidUuids |
893
|
|
|
*/ |
894
|
|
|
public function testInvalidUuids($uuid) |
895
|
|
|
{ |
896
|
|
|
$this->setExpectedException('Assert\InvalidArgumentException'); |
897
|
|
|
Assertion::uuid($uuid); |
898
|
|
|
} |
899
|
|
|
|
900
|
|
|
static public function providesValidUuids() |
|
|
|
|
901
|
|
|
{ |
902
|
|
|
return array( |
903
|
|
|
array('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'), |
904
|
|
|
array('ff6f8cb0-c57d-21e1-9b21-0800200c9a66'), |
905
|
|
|
array('ff6f8cb0-c57d-31e1-9b21-0800200c9a66'), |
906
|
|
|
array('ff6f8cb0-c57d-41e1-9b21-0800200c9a66'), |
907
|
|
|
array('ff6f8cb0-c57d-51e1-9b21-0800200c9a66'), |
908
|
|
|
array('FF6F8CB0-C57D-11E1-9B21-0800200C9A66'), |
909
|
|
|
); |
910
|
|
|
} |
911
|
|
|
|
912
|
|
|
static public function providesInvalidUuids() |
|
|
|
|
913
|
|
|
{ |
914
|
|
|
return array( |
915
|
|
|
array('zf6f8cb0-c57d-11e1-9b21-0800200c9a66'), |
916
|
|
|
array('af6f8cb0c57d11e19b210800200c9a66'), |
917
|
|
|
array('ff6f8cb0-c57da-51e1-9b21-0800200c9a66'), |
918
|
|
|
array('af6f8cb-c57d-11e1-9b21-0800200c9a66'), |
919
|
|
|
array('3f6f8cb0-c57d-11e1-9b21-0800200c9a6'), |
920
|
|
|
); |
921
|
|
|
} |
922
|
|
|
|
923
|
|
|
/** |
924
|
|
|
* @dataProvider providesValidE164s |
925
|
|
|
*/ |
926
|
|
|
public function testValidE164s($e164) |
927
|
|
|
{ |
928
|
|
|
Assertion::e164($e164); |
929
|
|
|
} |
930
|
|
|
|
931
|
|
|
/** |
932
|
|
|
* @dataProvider providesInvalidE164s |
933
|
|
|
*/ |
934
|
|
|
public function testInvalidE164s($e164) |
935
|
|
|
{ |
936
|
|
|
$this->setExpectedException('Assert\InvalidArgumentException'); |
937
|
|
|
Assertion::e164($e164); |
938
|
|
|
} |
939
|
|
|
|
940
|
|
|
static public function providesValidE164s() |
|
|
|
|
941
|
|
|
{ |
942
|
|
|
return array( |
943
|
|
|
array('+33626525690'), |
944
|
|
|
array('33626525690'), |
945
|
|
|
array('+16174552211'), |
946
|
|
|
); |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
static public function providesInvalidE164s() |
|
|
|
|
950
|
|
|
{ |
951
|
|
|
return array( |
952
|
|
|
array('+3362652569e'), |
953
|
|
|
array('+3361231231232652569'), |
954
|
|
|
); |
955
|
|
|
} |
956
|
|
|
|
957
|
|
|
public function testValidNotEmptyKey() |
958
|
|
|
{ |
959
|
|
|
Assertion::notEmptyKey(array('keyExists' => 'notEmpty'), 'keyExists'); |
960
|
|
|
} |
961
|
|
|
|
962
|
|
|
/** |
963
|
|
|
* @dataProvider invalidNotEmptyKeyDataprovider |
964
|
|
|
*/ |
965
|
|
|
public function testInvalidNotEmptyKey($invalidArray, $key) |
966
|
|
|
{ |
967
|
|
|
$this->setExpectedException('Assert\InvalidArgumentException'); |
968
|
|
|
Assertion::notEmptyKey($invalidArray, $key); |
969
|
|
|
} |
970
|
|
|
|
971
|
|
|
public static function invalidNotEmptyKeyDataprovider() |
972
|
|
|
{ |
973
|
|
|
return array( |
974
|
|
|
'empty' => array(array('keyExists' => ''), 'keyExists'), |
975
|
|
|
'key not exists' => array(array('key' => 'notEmpty'), 'keyNotExists') |
976
|
|
|
); |
977
|
|
|
} |
978
|
|
|
|
979
|
|
|
public function testAllWithSimpleAssertion() |
980
|
|
|
{ |
981
|
|
|
Assertion::allTrue(array(true, true)); |
982
|
|
|
} |
983
|
|
|
|
984
|
|
|
public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion() |
985
|
|
|
{ |
986
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE); |
987
|
|
|
Assertion::allTrue(array(true, false)); |
988
|
|
|
} |
989
|
|
|
|
990
|
|
|
public function testAllWithComplexAssertion() |
991
|
|
|
{ |
992
|
|
|
Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'stdClass'); |
993
|
|
|
} |
994
|
|
|
|
995
|
|
|
public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion() |
996
|
|
|
{ |
997
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', 'Assertion failed', Assertion::INVALID_INSTANCE_OF); |
998
|
|
|
|
999
|
|
|
Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'PDO', 'Assertion failed', 'foos'); |
1000
|
|
|
} |
1001
|
|
|
|
1002
|
|
|
public function testAllWithNoValueThrows() |
1003
|
|
|
{ |
1004
|
|
|
$this->setExpectedException('BadMethodCallException'); |
1005
|
|
|
Assertion::allTrue(); |
|
|
|
|
1006
|
|
|
} |
1007
|
|
|
|
1008
|
|
|
public function testValidCount() |
1009
|
|
|
{ |
1010
|
|
|
Assertion::count(array('Hi'), 1); |
1011
|
|
|
Assertion::count(new OneCountable(), 1); |
1012
|
|
|
} |
1013
|
|
|
|
1014
|
|
|
public static function dataInvalidCount() |
1015
|
|
|
{ |
1016
|
|
|
return array( |
1017
|
|
|
array(array('Hi', 'There'), 3), |
1018
|
|
|
array(new OneCountable(), 2), |
1019
|
|
|
); |
1020
|
|
|
} |
1021
|
|
|
|
1022
|
|
|
/** |
1023
|
|
|
* @dataProvider dataInvalidCount |
1024
|
|
|
*/ |
1025
|
|
|
public function testInvalidCount($countable, $count) |
1026
|
|
|
{ |
1027
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', 'List does not contain exactly "'.$count.'" elements.', Assertion::INVALID_COUNT); |
1028
|
|
|
Assertion::count($countable, $count); |
1029
|
|
|
} |
1030
|
|
|
|
1031
|
|
|
public function testChoicesNotEmpty() |
1032
|
|
|
{ |
1033
|
|
|
Assertion::choicesNotEmpty( |
1034
|
|
|
array('tux' => 'linux', 'Gnu' => 'dolphin'), |
1035
|
|
|
array('tux') |
1036
|
|
|
); |
1037
|
|
|
} |
1038
|
|
|
|
1039
|
|
|
/** |
1040
|
|
|
* @dataProvider invalidChoicesProvider |
1041
|
|
|
*/ |
1042
|
|
|
public function testChoicesNotEmptyExpectingException($values, $choices, $exceptionCode) |
1043
|
|
|
{ |
1044
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, $exceptionCode); |
1045
|
|
|
Assertion::choicesNotEmpty( |
1046
|
|
|
$values, |
1047
|
|
|
$choices |
1048
|
|
|
); |
1049
|
|
|
} |
1050
|
|
|
|
1051
|
|
|
public function invalidChoicesProvider() |
1052
|
|
|
{ |
1053
|
|
|
return array( |
1054
|
|
|
'empty values' => array(array(), array('tux'), Assertion::VALUE_EMPTY), |
1055
|
|
|
'empty recodes in $values' => array(array('tux' => ''), array('tux'), Assertion::VALUE_EMPTY), |
1056
|
|
|
'choice not found in values' => array(array('tux' => ''), array('invalidChoice'), Assertion::INVALID_KEY_ISSET), |
1057
|
|
|
); |
1058
|
|
|
} |
1059
|
|
|
|
1060
|
|
|
public function testIsObject() |
1061
|
|
|
{ |
1062
|
|
|
Assertion::isObject(new \stdClass); |
1063
|
|
|
} |
1064
|
|
|
|
1065
|
|
|
public function testIsObjectExpectingException() |
1066
|
|
|
{ |
1067
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_OBJECT); |
1068
|
|
|
Assertion::isObject('notAnObject'); |
1069
|
|
|
} |
1070
|
|
|
|
1071
|
|
|
public function testMethodExists() |
1072
|
|
|
{ |
1073
|
|
|
Assertion::methodExists('methodExists', new Assertion()); |
1074
|
|
|
} |
1075
|
|
|
|
1076
|
|
|
public function testMethodExistsFailure() |
1077
|
|
|
{ |
1078
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_METHOD); |
1079
|
|
|
Assertion::methodExists('methodNotExists', new Assertion()); |
1080
|
|
|
} |
1081
|
|
|
|
1082
|
|
|
/** |
1083
|
|
|
* @test |
1084
|
|
|
*/ |
1085
|
|
|
public function it_passes_values_and_constraints_to_exception() |
|
|
|
|
1086
|
|
|
{ |
1087
|
|
|
try { |
1088
|
|
|
Assertion::range(0, 10, 20); |
1089
|
|
|
|
1090
|
|
|
$this->fail('Exception expected'); |
1091
|
|
|
} catch (AssertionFailedException $e) { |
1092
|
|
|
$this->assertEquals(0, $e->getValue()); |
1093
|
|
|
$this->assertEquals(array('min' => 10, 'max' => 20), $e->getConstraints()); |
1094
|
|
|
} |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
|
|
public function testLessThan() |
1098
|
|
|
{ |
1099
|
|
|
Assertion::lessThan(1, 2); |
1100
|
|
|
Assertion::lessThan('aaa', 'bbb'); |
1101
|
|
|
Assertion::lessThan('aaa', 'aaaa'); |
1102
|
|
|
Assertion::lessThan(new \DateTime('today'), new \DateTime('tomorrow')); |
1103
|
|
|
} |
1104
|
|
|
|
1105
|
|
|
public function invalidLessProvider() |
1106
|
|
|
{ |
1107
|
|
|
return array( |
1108
|
|
|
array(2, 1), |
1109
|
|
|
array(2, 2), |
1110
|
|
|
array('aaa', 'aaa'), |
1111
|
|
|
array('aaaa', 'aaa'), |
1112
|
|
|
array(new \DateTime('today'), new \DateTime('yesterday')), |
1113
|
|
|
array(new \DateTime('today'), new \DateTime('today')), |
1114
|
|
|
); |
1115
|
|
|
} |
1116
|
|
|
|
1117
|
|
|
/** |
1118
|
|
|
* @dataProvider invalidLessProvider |
1119
|
|
|
*/ |
1120
|
|
|
public function testLessThanThrowsException($value, $limit) |
1121
|
|
|
{ |
1122
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LESS); |
1123
|
|
|
Assertion::lessThan($value, $limit); |
1124
|
|
|
} |
1125
|
|
|
|
1126
|
|
|
public function testLessOrEqualThan() |
1127
|
|
|
{ |
1128
|
|
|
Assertion::lessOrEqualThan(1, 2); |
1129
|
|
|
Assertion::lessOrEqualThan(1, 1); |
1130
|
|
|
Assertion::lessOrEqualThan('aaa', 'bbb'); |
1131
|
|
|
Assertion::lessOrEqualThan('aaa', 'aaaa'); |
1132
|
|
|
Assertion::lessOrEqualThan('aaa', 'aaa'); |
1133
|
|
|
Assertion::lessOrEqualThan(new \DateTime('today'), new \DateTime('tomorrow')); |
1134
|
|
|
Assertion::lessOrEqualThan(new \DateTime('today'), new \DateTime('today')); |
1135
|
|
|
} |
1136
|
|
|
|
1137
|
|
|
public function invalidLessOrEqualProvider() |
1138
|
|
|
{ |
1139
|
|
|
return array( |
1140
|
|
|
array(2, 1), |
1141
|
|
|
array('aaaa', 'aaa'), |
1142
|
|
|
array(new \DateTime('today'), new \DateTime('yesterday')), |
1143
|
|
|
); |
1144
|
|
|
} |
1145
|
|
|
|
1146
|
|
|
/** |
1147
|
|
|
* @dataProvider invalidLessOrEqualProvider |
1148
|
|
|
*/ |
1149
|
|
|
public function testLessOrEqualThanThrowsException($value, $limit) |
1150
|
|
|
{ |
1151
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LESS_OR_EQUAL); |
1152
|
|
|
Assertion::lessOrEqualThan($value, $limit); |
1153
|
|
|
} |
1154
|
|
|
|
1155
|
|
|
public function testGreaterThan() |
1156
|
|
|
{ |
1157
|
|
|
Assertion::greaterThan(2, 1); |
1158
|
|
|
Assertion::greaterThan('bbb', 'aaa'); |
1159
|
|
|
Assertion::greaterThan('aaaa', 'aaa'); |
1160
|
|
|
Assertion::greaterThan(new \DateTime('tomorrow'), new \DateTime('today')); |
1161
|
|
|
} |
1162
|
|
|
|
1163
|
|
|
public function invalidGreaterProvider() |
1164
|
|
|
{ |
1165
|
|
|
return array( |
1166
|
|
|
array(1, 2), |
1167
|
|
|
array(2, 2), |
1168
|
|
|
array('aaa', 'aaa'), |
1169
|
|
|
array('aaa', 'aaaa'), |
1170
|
|
|
array(new \DateTime('yesterday'), new \DateTime('today')), |
1171
|
|
|
array(new \DateTime('today'), new \DateTime('today')), |
1172
|
|
|
); |
1173
|
|
|
} |
1174
|
|
|
|
1175
|
|
|
/** |
1176
|
|
|
* @dataProvider validDateProvider |
1177
|
|
|
*/ |
1178
|
|
|
public function testValidDate($value, $format) |
1179
|
|
|
{ |
1180
|
|
|
Assertion::date($value, $format); |
1181
|
|
|
} |
1182
|
|
|
|
1183
|
|
|
public function validDateProvider() |
1184
|
|
|
{ |
1185
|
|
|
return array( |
1186
|
|
|
array('2012-03-13', 'Y-m-d'), |
1187
|
|
|
array('29.02.2012 12:03:36.432563', 'd.m.Y H:i:s.u'), |
1188
|
|
|
array('13.08.2015 17:08:23 Thu Thursday th 224 August Aug 8 15 17 432563 UTC UTC', 'd.m.Y H:i:s D l S z F M n y H u e T'), |
1189
|
|
|
array('1439486158', 'U') |
1190
|
|
|
); |
1191
|
|
|
} |
1192
|
|
|
|
1193
|
|
|
/** |
1194
|
|
|
* @dataProvider invalidGreaterProvider |
1195
|
|
|
*/ |
1196
|
|
|
public function testGreaterThanThrowsException($value, $limit) |
1197
|
|
|
{ |
1198
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_GREATER); |
1199
|
|
|
Assertion::greaterThan($value, $limit); |
1200
|
|
|
} |
1201
|
|
|
|
1202
|
|
|
public function testGreaterOrEqualThan() |
1203
|
|
|
{ |
1204
|
|
|
Assertion::greaterOrEqualThan(2, 1); |
1205
|
|
|
Assertion::greaterOrEqualThan(1, 1); |
1206
|
|
|
Assertion::greaterOrEqualThan('bbb', 'aaa'); |
1207
|
|
|
Assertion::greaterOrEqualThan('aaaa', 'aaa'); |
1208
|
|
|
Assertion::greaterOrEqualThan('aaa', 'aaa'); |
1209
|
|
|
Assertion::greaterOrEqualThan(new \DateTime('tomorrow'), new \DateTime('today')); |
1210
|
|
|
Assertion::greaterOrEqualThan(new \DateTime('today'), new \DateTime('today')); |
1211
|
|
|
} |
1212
|
|
|
|
1213
|
|
|
public function invalidGreaterOrEqualProvider() |
1214
|
|
|
{ |
1215
|
|
|
return array( |
1216
|
|
|
array(1, 2), |
1217
|
|
|
array('aaa', 'aaaa'), |
1218
|
|
|
array(new \DateTime('yesterday'), new \DateTime('tomorrow')), |
1219
|
|
|
); |
1220
|
|
|
} |
1221
|
|
|
|
1222
|
|
|
/** |
1223
|
|
|
* @dataProvider invalidGreaterOrEqualProvider |
1224
|
|
|
* |
1225
|
|
|
* @param mixed $value |
1226
|
|
|
* @param mixed $limit |
1227
|
|
|
*/ |
1228
|
|
|
public function testGreaterOrEqualThanThrowsException($value, $limit) |
1229
|
|
|
{ |
1230
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_GREATER_OR_EQUAL); |
1231
|
|
|
Assertion::greaterOrEqualThan($value, $limit); |
1232
|
|
|
} |
1233
|
|
|
|
1234
|
|
|
/** |
1235
|
|
|
* @dataProvider invalidDateProvider |
1236
|
|
|
*/ |
1237
|
|
|
public function testInvalidDate($value, $format) |
1238
|
|
|
{ |
1239
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DATE); |
1240
|
|
|
Assertion::date($value, $format); |
1241
|
|
|
} |
1242
|
|
|
|
1243
|
|
|
public function invalidDateProvider() |
1244
|
|
|
{ |
1245
|
|
|
return array( |
1246
|
|
|
array('this is not the date', 'Y-m-d'), |
1247
|
|
|
array('2011-02-29', 'Y-m-d'), |
1248
|
|
|
array('2012.02.29 12:60:36.432563', 'Y.m.d H:i:s.u') |
1249
|
|
|
); |
1250
|
|
|
} |
1251
|
|
|
|
1252
|
|
|
public function testInvalidTraversable() |
1253
|
|
|
{ |
1254
|
|
|
$this->setExpectedException('Assert\InvalidArgumentException', null, Assertion::INVALID_TRAVERSABLE); |
1255
|
|
|
Assertion::isTraversable('not traversable'); |
1256
|
|
|
} |
1257
|
|
|
|
1258
|
|
|
public function testInvalidArrayAccessible() |
1259
|
|
|
{ |
1260
|
|
|
$this->setExpectedException('Assert\InvalidArgumentException', null, Assertion::INVALID_ARRAY_ACCESSIBLE); |
1261
|
|
|
Assertion::isArrayAccessible('not array accessible'); |
1262
|
|
|
} |
1263
|
|
|
|
1264
|
|
|
public function testInvalidCallable() |
1265
|
|
|
{ |
1266
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CALLABLE); |
1267
|
|
|
Assertion::isCallable("nonExistingFunction"); |
1268
|
|
|
} |
1269
|
|
|
|
1270
|
|
|
public function testValidCallable() |
1271
|
|
|
{ |
1272
|
|
|
Assertion::isCallable('\is_callable'); |
1273
|
|
|
Assertion::isCallable(__NAMESPACE__ . "\\someCallable"); |
1274
|
|
|
Assertion::isCallable(array(__NAMESPACE__ . "\\OneCountable", "count")); |
1275
|
|
|
Assertion::isCallable(function () { |
1276
|
|
|
}); |
1277
|
|
|
} |
1278
|
|
|
|
1279
|
|
|
public function testInvalidSatisfy() |
1280
|
|
|
{ |
1281
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SATISFY); |
1282
|
|
|
Assertion::satisfy(null, function ($value) { |
1283
|
|
|
return !is_null($value); |
1284
|
|
|
}); |
1285
|
|
|
} |
1286
|
|
|
|
1287
|
|
|
public function testValidSatisfy() |
1288
|
|
|
{ |
1289
|
|
|
// Should not fail with true return |
1290
|
|
|
Assertion::satisfy(null, function ($value) { |
1291
|
|
|
return is_null($value); |
1292
|
|
|
}); |
1293
|
|
|
|
1294
|
|
|
// Should not fail with void return |
1295
|
|
|
Assertion::satisfy(true, function ($value) { |
1296
|
|
|
if (!is_bool($value)) { |
1297
|
|
|
return false; |
1298
|
|
|
} |
1299
|
|
|
}); |
1300
|
|
|
} |
1301
|
|
|
|
1302
|
|
|
/** |
1303
|
|
|
* @dataProvider validIpProvider |
1304
|
|
|
*/ |
1305
|
|
|
public function testValidIp($value) { |
1306
|
|
|
Assertion::ip($value); |
1307
|
|
|
} |
1308
|
|
|
|
1309
|
|
|
public function validIpProvider() { |
1310
|
|
|
return array( |
1311
|
|
|
array('0.0.0.0'), |
1312
|
|
|
array('14.32.152.216'), |
1313
|
|
|
array('255.255.255.255'), |
1314
|
|
|
array('2001:db8:85a3:8d3:1319:8a2e:370:7348'), |
1315
|
|
|
); |
1316
|
|
|
} |
1317
|
|
|
|
1318
|
|
|
/** |
1319
|
|
|
* @dataProvider invalidIpProvider |
1320
|
|
|
*/ |
1321
|
|
|
public function testInvalidIp($value, $flag = null) { |
1322
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_IP); |
1323
|
|
|
Assertion::ip($value, $flag); |
1324
|
|
|
} |
1325
|
|
|
|
1326
|
|
|
public function invalidIpProvider() { |
1327
|
|
|
return array( |
1328
|
|
|
array('invalid ip address'), |
1329
|
|
|
array('14.32.152,216'), |
1330
|
|
|
array('14.32.256.216'), |
1331
|
|
|
array('192.168.0.10', FILTER_FLAG_NO_PRIV_RANGE), |
1332
|
|
|
array('127.0.0.1', FILTER_FLAG_NO_RES_RANGE), |
1333
|
|
|
array('2001:db8:85a3:8d3:1319:8g2e:370:7348'), |
1334
|
|
|
array('fdb9:75b9:9e69:5d08:1:1:1:1', FILTER_FLAG_NO_PRIV_RANGE), |
1335
|
|
|
); |
1336
|
|
|
} |
1337
|
|
|
|
1338
|
|
|
public function testValidIpv4() { |
1339
|
|
|
Assertion::ipv4('109.188.127.26'); |
1340
|
|
|
} |
1341
|
|
|
|
1342
|
|
|
public function testInvalidIpv4() { |
1343
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_IP); |
1344
|
|
|
Assertion::ipv4('2001:db8:85a3:8d3:1319:8a2e:370:7348'); |
1345
|
|
|
} |
1346
|
|
|
|
1347
|
|
|
public function testValidIpv6() { |
1348
|
|
|
Assertion::ipv6('2001:db8:85a3:8d3:1319:8a2e:370:7348'); |
1349
|
|
|
} |
1350
|
|
|
|
1351
|
|
|
public function testInvalidIpv6() |
1352
|
|
|
{ |
1353
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_IP); |
1354
|
|
|
Assertion::ipv6('109.188.127.26'); |
1355
|
|
|
} |
1356
|
|
|
|
1357
|
|
|
public function testInvalidInterfaceExists() |
1358
|
|
|
{ |
1359
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTERFACE); |
1360
|
|
|
Assertion::interfaceExists("Foo"); |
1361
|
|
|
} |
1362
|
|
|
|
1363
|
|
|
public function testValidInterfaceExists() |
1364
|
|
|
{ |
1365
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTERFACE); |
1366
|
|
|
Assertion::interfaceExists("\\Countable"); |
1367
|
|
|
} |
1368
|
|
|
|
1369
|
|
|
/** |
1370
|
|
|
* @dataProvider providerInvalidBetween |
1371
|
|
|
* |
1372
|
|
|
* @param mixed $value |
1373
|
|
|
* @param mixed $lowerLimit |
1374
|
|
|
* @param mixed $upperLimit |
1375
|
|
|
*/ |
1376
|
|
|
public function testInvalidBetween($value, $lowerLimit, $upperLimit) |
1377
|
|
|
{ |
1378
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BETWEEN); |
1379
|
|
|
|
1380
|
|
|
Assertion::between($value, $lowerLimit, $upperLimit); |
1381
|
|
|
} |
1382
|
|
|
|
1383
|
|
|
/** |
1384
|
|
|
* @return array |
|
|
|
|
1385
|
|
|
*/ |
1386
|
|
|
public function providerInvalidBetween() |
1387
|
|
|
{ |
1388
|
|
|
return array( |
1389
|
|
|
array(1, 2, 3), |
1390
|
|
|
array(3, 1, 2), |
1391
|
|
|
array('aaa', 'bbb', 'ccc'), |
1392
|
|
|
array('ddd', 'bbb', 'ccc'), |
1393
|
|
|
array(new \DateTime('yesterday'), new \DateTime('today'), new \DateTime('tomorrow')), |
1394
|
|
|
array(new \DateTime('tomorrow'), new \DateTime('yesterday'), new \DateTime('today')), |
1395
|
|
|
); |
1396
|
|
|
} |
1397
|
|
|
|
1398
|
|
|
/** |
1399
|
|
|
* @dataProvider providerValidBetween |
1400
|
|
|
* |
1401
|
|
|
* @param mixed $value |
1402
|
|
|
* @param mixed $lowerLimit |
1403
|
|
|
* @param mixed $upperLimit |
1404
|
|
|
*/ |
1405
|
|
|
public function testValidBetween($value, $lowerLimit, $upperLimit) |
1406
|
|
|
{ |
1407
|
|
|
$this->assertNull(Assertion::between($value, $lowerLimit, $upperLimit)); |
1408
|
|
|
} |
1409
|
|
|
|
1410
|
|
|
/** |
1411
|
|
|
* @return array |
|
|
|
|
1412
|
|
|
*/ |
1413
|
|
|
public function providerValidBetween() |
1414
|
|
|
{ |
1415
|
|
|
return array( |
1416
|
|
|
array(2, 1, 3), |
1417
|
|
|
array(1, 1, 1), |
1418
|
|
|
array('bbb', 'aaa', 'ccc'), |
1419
|
|
|
array('aaa', 'aaa', 'aaa'), |
1420
|
|
|
array(new \DateTime('today'), new \DateTime('yesterday'), new \DateTime('tomorrow')), |
1421
|
|
|
array(new \DateTime('today'), new \DateTime('today'), new \DateTime('today')), |
1422
|
|
|
); |
1423
|
|
|
} |
1424
|
|
|
|
1425
|
|
|
/** |
1426
|
|
|
* @dataProvider providerInvalidBetweenExclusive |
1427
|
|
|
* |
1428
|
|
|
* @param mixed $value |
1429
|
|
|
* @param mixed $lowerLimit |
1430
|
|
|
* @param mixed $upperLimit |
1431
|
|
|
*/ |
1432
|
|
|
public function testInvalidBetweenExclusive($value, $lowerLimit, $upperLimit) |
1433
|
|
|
{ |
1434
|
|
|
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BETWEEN_EXCLUSIVE); |
1435
|
|
|
|
1436
|
|
|
Assertion::betweenExclusive($value, $lowerLimit, $upperLimit); |
1437
|
|
|
} |
1438
|
|
|
|
1439
|
|
|
/** |
1440
|
|
|
* @return array |
|
|
|
|
1441
|
|
|
*/ |
1442
|
|
|
public function providerInvalidBetweenExclusive() |
1443
|
|
|
{ |
1444
|
|
|
return array( |
1445
|
|
|
array(1, 1, 2), |
1446
|
|
|
array(2, 1, 2), |
1447
|
|
|
array('aaa', 'aaa', 'bbb'), |
1448
|
|
|
array('bbb', 'aaa', 'bbb'), |
1449
|
|
|
array(new \DateTime('today'), new \DateTime('today'), new \DateTime('tomorrow')), |
1450
|
|
|
array(new \DateTime('tomorrow'), new \DateTime('today'), new \DateTime('tomorrow')), |
1451
|
|
|
); |
1452
|
|
|
} |
1453
|
|
|
|
1454
|
|
|
/** |
1455
|
|
|
* @dataProvider providerValidBetweenExclusive |
1456
|
|
|
* |
1457
|
|
|
* @param mixed $value |
1458
|
|
|
* @param mixed $lowerLimit |
1459
|
|
|
* @param mixed $upperLimit |
1460
|
|
|
*/ |
1461
|
|
|
public function testValidBetweenExclusive($value, $lowerLimit, $upperLimit) |
1462
|
|
|
{ |
1463
|
|
|
$this->assertNull(Assertion::betweenExclusive($value, $lowerLimit, $upperLimit)); |
1464
|
|
|
} |
1465
|
|
|
|
1466
|
|
|
/** |
1467
|
|
|
* @return array |
|
|
|
|
1468
|
|
|
*/ |
1469
|
|
|
public function providerValidBetweenExclusive() |
1470
|
|
|
{ |
1471
|
|
|
return array( |
1472
|
|
|
array(2, 1, 3), |
1473
|
|
|
array('bbb', 'aaa', 'ccc'), |
1474
|
|
|
array(new \DateTime('today'), new \DateTime('yesterday'), new \DateTime('tomorrow')), |
1475
|
|
|
); |
1476
|
|
|
} |
1477
|
|
|
} |
1478
|
|
|
|
1479
|
|
|
class ChildStdClass extends \stdClass |
|
|
|
|
1480
|
|
|
{ |
1481
|
|
|
|
1482
|
|
|
} |
1483
|
|
|
|
1484
|
|
|
class OneCountable implements \Countable |
|
|
|
|
1485
|
|
|
{ |
1486
|
|
|
public function count() |
1487
|
|
|
{ |
1488
|
|
|
return 1; |
1489
|
|
|
} |
1490
|
|
|
} |
1491
|
|
|
|
1492
|
|
|
function someCallable() |
1493
|
|
|
{ |
1494
|
|
|
|
1495
|
|
|
} |
1496
|
|
|
|