Completed
Pull Request — master (#193)
by Timothy
02:07
created

AssertTest::testValidConstant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 1
1
<?php
2
namespace Assert\Tests;
3
4
use Assert\Assertion;
5
use Assert\AssertionFailedException;
6
7
class AssertTest extends \PHPUnit_Framework_TestCase
8
{
9
    public static function dataInvalidFloat()
10
    {
11
        return array(
12
            array(1),
13
            array(false),
14
            array("test"),
15
            array(null),
16
            array("1.23"),
17
            array("10"),
18
        );
19
    }
20
21
    /**
22
     * @dataProvider dataInvalidFloat
23
     */
24
    public function testInvalidFloat($nonFloat)
25
    {
26
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FLOAT);
27
        Assertion::float($nonFloat);
28
    }
29
30
    public function testValidFloat()
31
    {
32
        $this->assertTrue(Assertion::float(1.0));
33
        $this->assertTrue(Assertion::float(0.1));
34
        $this->assertTrue(Assertion::float(-1.1));
35
    }
36
37
    public static function dataInvalidInteger()
38
    {
39
        return array(
40
            array(1.23),
41
            array(false),
42
            array("test"),
43
            array(null),
44
            array("1.23"),
45
            array("10"),
46
            array(new \DateTime()),
47
        );
48
    }
49
50
    /**
51
     * @dataProvider dataInvalidInteger
52
     */
53
    public function testInvalidInteger($nonInteger)
54
    {
55
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTEGER);
56
        Assertion::integer($nonInteger);
57
    }
58
59
    public function testValidInteger()
60
    {
61
        $this->assertTrue(Assertion::integer(10));
62
        $this->assertTrue(Assertion::integer(0));
63
    }
64
65
    public function testValidIntegerish()
66
    {
67
        $this->assertTrue(Assertion::integerish(10));
68
        $this->assertTrue(Assertion::integerish("10"));
69
    }
70
71
    public static function dataInvalidIntegerish()
72
    {
73
        return array(
74
            array(1.23),
75
            array(false),
76
            array("test"),
77
            array(null),
78
            array("1.23"),
79
        );
80
    }
81
82
    /**
83
     * @dataProvider dataInvalidIntegerish
84
     */
85
    public function testInvalidIntegerish($nonInteger)
86
    {
87
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTEGERISH);
88
        Assertion::integerish($nonInteger);
89
    }
90
91
    public function testValidBoolean()
92
    {
93
        $this->assertTrue(Assertion::boolean(true));
94
        $this->assertTrue(Assertion::boolean(false));
95
    }
96
97
    public function testInvalidBoolean()
98
    {
99
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BOOLEAN);
100
        Assertion::boolean(1);
101
    }
102
103
    public function testInvalidScalar()
104
    {
105
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SCALAR);
106
        Assertion::scalar(new \stdClass);
107
    }
108
109
    public function testValidScalar()
110
    {
111
        $this->assertTrue(Assertion::scalar("foo"));
112
        $this->assertTrue(Assertion::scalar(52));
113
        $this->assertTrue(Assertion::scalar(12.34));
114
        $this->assertTrue(Assertion::scalar(false));
115
    }
116
117
    public static function dataInvalidNotEmpty()
118
    {
119
        return array(
120
            array(""),
121
            array(false),
122
            array(0),
123
            array(null),
124
            array( array() ),
125
        );
126
    }
127
128
    /**
129
     * @dataProvider dataInvalidNotEmpty
130
     */
131
    public function testInvalidNotEmpty($value)
132
    {
133
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_EMPTY);
134
        Assertion::notEmpty($value);
135
    }
136
137
    public function testNotEmpty()
138
    {
139
        $this->assertTrue(Assertion::notEmpty("test"));
140
        $this->assertTrue(Assertion::notEmpty(1));
141
        $this->assertTrue(Assertion::notEmpty(true));
142
        $this->assertTrue(Assertion::notEmpty(array("foo")));
143
    }
144
145
    public function testEmpty()
146
    {
147
        $this->assertTrue(Assertion::noContent(""));
148
        $this->assertTrue(Assertion::noContent(0));
149
        $this->assertTrue(Assertion::noContent(false));
150
        $this->assertTrue(Assertion::noContent(array()));
151
    }
152
153
    public static function dataInvalidEmpty()
154
    {
155
        return array(
156
            array("foo"),
157
            array(true),
158
            array(12),
159
            array( array('foo') ),
160
            array( new \stdClass() ),
161
        );
162
    }
163
164
    /**
165
     * @dataProvider dataInvalidEmpty
166
     */
167
    public function testInvalidEmpty($value)
168
    {
169
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NOT_EMPTY);
170
        Assertion::noContent($value);
171
    }
172
173
    public static function dataInvalidNull()
174
    {
175
        return array(
176
            array("foo"),
177
            array(""),
178
            array(false),
179
            array(12),
180
            array(0),
181
            array(array()),
182
        );
183
    }
184
185
    /**
186
     * @dataProvider dataInvalidNull
187
     */
188
    public function testInvalidNull($value)
189
    {
190
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NOT_NULL);
191
        Assertion::null($value);
192
    }
193
194
    public function testNull()
195
    {
196
        $this->assertTrue(Assertion::null(null));
197
    }
198
199
    public function testNotNull()
200
    {
201
        $this->assertTrue(Assertion::notNull("1"));
202
        $this->assertTrue(Assertion::notNull(1));
203
        $this->assertTrue(Assertion::notNull(0));
204
        $this->assertTrue(Assertion::notNull(array()));
205
        $this->assertTrue(Assertion::notNull(false));
206
    }
207
208
    public function testInvalidNotNull()
209
    {
210
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NULL);
211
        Assertion::notNull(null);
212
    }
213
214
    public function testString()
215
    {
216
        $this->assertTrue(Assertion::string("test-string"));
217
        $this->assertTrue(Assertion::string(""));
218
    }
219
220
    /**
221
     * @dataProvider dataInvalidString
222
     */
223
    public function testInvalidString($invalidString)
224
    {
225
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING);
226
        Assertion::string($invalidString);
227
    }
228
229
    public static function dataInvalidString()
230
    {
231
        return array(
232
            array(1.23),
233
            array(false),
234
            array(new \ArrayObject),
235
            array(null),
236
            array(10),
237
            array(true),
238
        );
239
    }
240
241
    public function testValidRegex()
242
    {
243
        $this->assertTrue(Assertion::regex('some string', '/.*/'));
244
    }
245
246
    public function testInvalidRegex()
247
    {
248
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_REGEX);
249
        Assertion::regex("foo", "(bar)");
250
    }
251
252
    public function testInvalidRegexValueNotString()
253
    {
254
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING);
255
        Assertion::regex(array("foo"), "(bar)");
256
    }
257
258
    public function testInvalidMinLength()
259
    {
260
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MIN_LENGTH);
261
        Assertion::minLength("foo", 4);
262
    }
263
264
    public function testValidMinLength()
265
    {
266
        $this->assertTrue(Assertion::minLength("foo", 3));
267
        $this->assertTrue(Assertion::minLength("foo", 1));
268
        $this->assertTrue(Assertion::minLength("foo", 0));
269
        $this->assertTrue(Assertion::minLength("", 0));
270
        $this->assertTrue(Assertion::minLength("址址", 2));
271
    }
272
273
    public function testInvalidMaxLength()
274
    {
275
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MAX_LENGTH);
276
        Assertion::maxLength("foo", 2);
277
    }
278
279
    public function testValidMaxLength()
280
    {
281
        $this->assertTrue(Assertion::maxLength("foo", 10));
282
        $this->assertTrue(Assertion::maxLength("foo", 3));
283
        $this->assertTrue(Assertion::maxLength("", 0));
284
        $this->assertTrue(Assertion::maxLength("址址", 2));
285
    }
286
287
    public function testInvalidBetweenLengthMin()
288
    {
289
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MIN_LENGTH);
290
        Assertion::betweenLength("foo", 4, 100);
291
    }
292
293
    public function testInvalidBetweenLengthMax()
294
    {
295
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MAX_LENGTH);
296
        Assertion::betweenLength("foo", 0, 2);
297
    }
298
299
    public function testValidBetweenLength()
300
    {
301
        $this->assertTrue(Assertion::betweenLength("foo", 0, 3));
302
        $this->assertTrue(Assertion::betweenLength("址址", 2, 2));
303
    }
304
305
    public function testInvalidStartsWith()
306
    {
307
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_START);
308
        Assertion::startsWith("foo", "bar");
309
    }
310
311
    public function testInvalidStartsWithDueToWrongEncoding()
312
    {
313
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_START);
314
        Assertion::startsWith("址", "址址", null, null, 'ASCII');
315
    }
316
317
    public function testValidStartsWith()
318
    {
319
        $this->assertTrue(Assertion::startsWith("foo", "foo"));
320
        $this->assertTrue(Assertion::startsWith("foo", "fo"));
321
        $this->assertTrue(Assertion::startsWith("foo", "f"));
322
        $this->assertTrue(Assertion::startsWith("址foo", "址"));
323
    }
324
325
    public function testInvalidEndsWith()
326
    {
327
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END);
328
        Assertion::endsWith("foo", "bar");
329
    }
330
331
    public function testInvalidEndsWithDueToWrongEncoding()
332
    {
333
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END);
334
        Assertion::endsWith("址", "址址", null, null, 'ASCII');
335
    }
336
337
    public function testValidEndsWith()
338
    {
339
        $this->assertTrue(Assertion::endsWith("foo", "foo"));
340
        $this->assertTrue(Assertion::endsWith("sonderbar", "bar"));
341
        $this->assertTrue(Assertion::endsWith("opp", "p"));
342
        $this->assertTrue(Assertion::endsWith("foo址", "址"));
343
    }
344
345
    public function testInvalidContains()
346
    {
347
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_CONTAINS);
348
        Assertion::contains("foo", "bar");
349
    }
350
351
    public function testValidContains()
352
    {
353
        $this->assertTrue(Assertion::contains("foo", "foo"));
354
        $this->assertTrue(Assertion::contains("foo", "oo"));
355
    }
356
357
    public function testInvalidChoice()
358
    {
359
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE);
360
        Assertion::choice("foo", array("bar", "baz"));
361
    }
362
363
    public function testValidChoice()
364
    {
365
        $this->assertTrue(Assertion::choice("foo", array("foo")));
366
    }
367
368
    public function testInvalidInArray()
369
    {
370
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE);
371
        Assertion::inArray("bar", array("baz"));
372
    }
373
374
    public function testValidInArray()
375
    {
376
        $this->assertTrue(Assertion::inArray("foo", array("foo")));
377
    }
378
379
    public function testInvalidNumeric()
380
    {
381
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NUMERIC);
382
        Assertion::numeric("foo");
383
    }
384
385
    public function testValidNumeric()
386
    {
387
        $this->assertTrue(Assertion::numeric("1"));
388
        $this->assertTrue(Assertion::numeric(1));
389
        $this->assertTrue(Assertion::numeric(1.23));
390
    }
391
392
    public static function dataInvalidArray()
393
    {
394
        return array(
395
            array(null),
396
            array(false),
397
            array("test"),
398
            array(1),
399
            array(1.23),
400
            array(new \stdClass),
401
            array(fopen('php://memory', 'r')),
402
        );
403
    }
404
405
    /**
406
     * @dataProvider dataInvalidArray
407
     */
408
    public function testInvalidArray($value)
409
    {
410
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_ARRAY);
411
        Assertion::isArray($value);
412
    }
413
414
    public function testValidArray()
415
    {
416
        $this->assertTrue(Assertion::isArray(array()));
417
        $this->assertTrue(Assertion::isArray(array(1, 2, 3)));
418
        $this->assertTrue(Assertion::isArray(array(array(), array())));
419
    }
420
421
    public function testInvalidKeyExists()
422
    {
423
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_EXISTS);
424
        Assertion::keyExists(array("foo" => "bar"), "baz");
425
    }
426
427
    public function testValidKeyExists()
428
    {
429
        $this->assertTrue(Assertion::keyExists(array("foo" => "bar"), "foo"));
430
    }
431
432
    public function testInvalidKeyNotExists()
433
    {
434
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_NOT_EXISTS);
435
        Assertion::keyNotExists(array("foo" => "bar"), "foo");
436
    }
437
438
    public function testValidKeyNotExists()
439
    {
440
        $this->assertTrue(Assertion::keyNotExists(array("foo" => "bar"), "baz"));
441
    }
442
443
    public static function dataInvalidNotBlank()
444
    {
445
        return array(
446
            array(""),
447
            array(" "),
448
            array("\t"),
449
            array("\n"),
450
            array("\r"),
451
            array(false),
452
            array(null),
453
            array( array() ),
454
        );
455
    }
456
457
    /**
458
     * @dataProvider dataInvalidNotBlank
459
     */
460
    public function testInvalidNotBlank($notBlank)
461
    {
462
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_BLANK);
463
        Assertion::notBlank($notBlank);
464
    }
465
466
    public function testValidNotBlank()
467
    {
468
        $this->assertTrue(Assertion::notBlank("foo"));
469
    }
470
471
    public function testInvalidNotInstanceOf()
472
    {
473
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_INSTANCE_OF);
474
        Assertion::notIsInstanceOf(new \stdClass, 'stdClass');
475
    }
476
477
    public function testValidNotIsInstanceOf()
478
    {
479
        $this->assertTrue(Assertion::notIsInstanceOf(new \stdClass, 'PDO'));
480
    }
481
482
    public function testInvalidInstanceOf()
483
    {
484
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INSTANCE_OF);
485
        Assertion::isInstanceOf(new \stdClass, 'PDO');
486
    }
487
488
    public function testValidInstanceOf()
489
    {
490
        $this->assertTrue(Assertion::isInstanceOf(new \stdClass, 'stdClass'));
491
    }
492
493
    public function testInvalidSubclassOf()
494
    {
495
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SUBCLASS_OF);
496
        Assertion::subclassOf(new \stdClass, 'PDO');
497
    }
498
499
    public function testValidSubclassOf()
500
    {
501
        $this->assertTrue(Assertion::subclassOf(new ChildStdClass, 'stdClass'));
502
    }
503
504
    public function testInvalidRange()
505
    {
506
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_RANGE);
507
        Assertion::range(1, 2, 3);
508
        Assertion::range(1.5, 2, 3);
509
    }
510
511
    public function testValidRange()
512
    {
513
        $this->assertTrue(Assertion::range(1, 1, 2));
514
        $this->assertTrue(Assertion::range(2, 1, 2));
515
        $this->assertTrue(Assertion::range(2, 0, 100));
516
        $this->assertTrue(Assertion::range(2.5, 2.25, 2.75));
517
    }
518
519
    public function testInvalidEmail()
520
    {
521
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_EMAIL);
522
        Assertion::email("foo");
523
    }
524
525
    public function testValidEmail()
526
    {
527
        $this->assertTrue(Assertion::email("[email protected]"));
528
    }
529
530
    /**
531
     * @dataProvider dataInvalidUrl
532
     */
533
    public function testInvalidUrl($url)
534
    {
535
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_URL);
536
537
        Assertion::url($url);
538
    }
539
540
    public static function dataInvalidUrl()
541
    {
542
        return array(
543
            array('google.com'),
544
            array('://google.com'),
545
            array('http ://google.com'),
546
            array('http:/google.com'),
547
            array('http://goog_le.com'),
548
            array('http://google.com::aa'),
549
            array('http://google.com:aa'),
550
            array('ftp://google.fr'),
551
            array('faked://google.fr'),
552
            array('http://127.0.0.1:aa/'),
553
            array('ftp://[::1]/'),
554
            array('http://[::1'),
555
            array('http://hello.☎/'),
556
            array('http://:[email protected]'),
557
            array('http://:password@@symfony.com'),
558
            array('http://username:passwordsymfony.com'),
559
            array('http://usern@me:[email protected]'),
560
        );
561
    }
562
563
    /**
564
     * @dataProvider dataValidUrl
565
     */
566
    public function testValidUrl($url)
567
    {
568
        $this->assertTrue(Assertion::url($url));
569
    }
570
571
    public static function dataValidUrl()
572
    {
573
        return array(
574
            array('http://a.pl'),
575
            array('http://www.google.com'),
576
            array('http://www.google.com.'),
577
            array('http://www.google.museum'),
578
            array('https://google.com/'),
579
            array('https://google.com:80/'),
580
            array('http://www.example.coop/'),
581
            array('http://www.test-example.com/'),
582
            array('http://www.symfony.com/'),
583
            array('http://symfony.fake/blog/'),
584
            array('http://symfony.com/?'),
585
            array('http://symfony.com/search?type=&q=url+validator'),
586
            array('http://symfony.com/#'),
587
            array('http://symfony.com/#?'),
588
            array('http://www.symfony.com/doc/current/book/validation.html#supported-constraints'),
589
            array('http://very.long.domain.name.com/'),
590
            array('http://localhost/'),
591
            array('http://myhost123/'),
592
            array('http://127.0.0.1/'),
593
            array('http://127.0.0.1:80/'),
594
            array('http://[::1]/'),
595
            array('http://[::1]:80/'),
596
            array('http://[1:2:3::4:5:6:7]/'),
597
            array('http://sãopaulo.com/'),
598
            array('http://xn--sopaulo-xwa.com/'),
599
            array('http://sãopaulo.com.br/'),
600
            array('http://xn--sopaulo-xwa.com.br/'),
601
            array('http://пример.испытание/'),
602
            array('http://xn--e1afmkfd.xn--80akhbyknj4f/'),
603
            array('http://مثال.إختبار/'),
604
            array('http://xn--mgbh0fb.xn--kgbechtv/'),
605
            array('http://例子.测试/'),
606
            array('http://xn--fsqu00a.xn--0zwm56d/'),
607
            array('http://例子.測試/'),
608
            array('http://xn--fsqu00a.xn--g6w251d/'),
609
            array('http://例え.テスト/'),
610
            array('http://xn--r8jz45g.xn--zckzah/'),
611
            array('http://مثال.آزمایشی/'),
612
            array('http://xn--mgbh0fb.xn--hgbk6aj7f53bba/'),
613
            array('http://실례.테스트/'),
614
            array('http://xn--9n2bp8q.xn--9t4b11yi5a/'),
615
            array('http://العربية.idn.icann.org/'),
616
            array('http://xn--ogb.idn.icann.org/'),
617
            array('http://xn--e1afmkfd.xn--80akhbyknj4f.xn--e1afmkfd/'),
618
            array('http://xn--espaa-rta.xn--ca-ol-fsay5a/'),
619
            array('http://xn--d1abbgf6aiiy.xn--p1ai/'),
620
            array('http://☎.com/'),
621
            array('http://username:[email protected]'),
622
            array('http://[email protected]'),
623
            array('http://symfony.com?'),
624
            array('http://symfony.com?query=1'),
625
            array('http://symfony.com/?query=1'),
626
            array('http://symfony.com#'),
627
            array('http://symfony.com#fragment'),
628
            array('http://symfony.com/#fragment'),
629
        );
630
    }
631
632
    public function testInvalidDigit()
633
    {
634
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DIGIT);
635
        Assertion::digit(-1);
636
    }
637
638
    public function testValidDigit()
639
    {
640
        $this->assertTrue(Assertion::digit(1));
641
        $this->assertTrue(Assertion::digit(0));
642
        $this->assertTrue(Assertion::digit("0"));
643
    }
644
645
    public function testValidAlnum()
646
    {
647
        $this->assertTrue(Assertion::alnum("a"));
648
        $this->assertTrue(Assertion::alnum("a1"));
649
        $this->assertTrue(Assertion::alnum("aasdf1234"));
650
        $this->assertTrue(Assertion::alnum("a1b2c3"));
651
    }
652
653
    public function testInvalidAlnum()
654
    {
655
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_ALNUM);
656
        Assertion::alnum("1a");
657
    }
658
659
    public function testValidTrue()
660
    {
661
        $this->assertTrue(Assertion::true(1 == 1));
662
    }
663
664
    public function testInvalidTrue()
665
    {
666
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE);
667
        Assertion::true(false);
668
    }
669
670
    public function testValidFalse()
671
    {
672
        $this->assertTrue(Assertion::false(1 == 0));
673
    }
674
675
    public function testInvalidFalse()
676
    {
677
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FALSE);
678
        Assertion::false(true);
679
    }
680
681
    public function testInvalidClass()
682
    {
683
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CLASS);
684
        Assertion::classExists("Foo");
685
    }
686
687
    public function testValidClass()
688
    {
689
        $this->assertTrue(Assertion::classExists("\\Exception"));
690
    }
691
692
    public function testSame()
693
    {
694
        $this->assertTrue(Assertion::same(1, 1));
695
        $this->assertTrue(Assertion::same("foo", "foo"));
696
        $this->assertTrue(Assertion::same($obj = new \stdClass(), $obj));
697
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SAME);
698
        Assertion::same(new \stdClass(), new \stdClass());
699
    }
700
701
    public function testEq()
702
    {
703
        $this->assertTrue(Assertion::eq(1, "1"));
704
        $this->assertTrue(Assertion::eq("foo", true));
705
        $this->assertTrue(Assertion::eq($obj = new \stdClass(), $obj));
706
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_EQ);
707
        Assertion::eq("2", 1);
708
    }
709
710
    public function testNotEq()
711
    {
712
        $this->assertTrue(Assertion::notEq("1", false));
713
        $this->assertTrue(Assertion::notEq(new \stdClass(), array()));
714
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_EQ);
715
        Assertion::notEq("1", 1);
716
    }
717
718
    public function testNotSame()
719
    {
720
        $this->assertTrue(Assertion::notSame("1", 2));
721
        $this->assertTrue(Assertion::notSame(new \stdClass(), array()));
722
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_SAME);
723
        Assertion::notSame(1, 1);
724
    }
725
726
    public function testNotInArray()
727
    {
728
        $this->assertTrue(Assertion::notInArray(6, range(1, 5)));
729
730
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_VALUE_IN_ARRAY);
731
        Assertion::notInArray(1, range(1, 5));
732
        Assertion::notInArray(range('a', 'c'), range('a', 'd'));
733
    }
734
735
    public function testMin()
736
    {
737
        $this->assertTrue(Assertion::min(1, 1));
738
        $this->assertTrue(Assertion::min(2, 1));
739
        $this->assertTrue(Assertion::min(2.5, 1));
740
741
        try {
742
            Assertion::min(0, 1);
743
            $this->fail('Expected exception `Assert\AssertionFailedException` not thrown');
744
        } catch (AssertionFailedException $e) {
745
            $this->assertEquals(Assertion::INVALID_MIN, $e->getCode());
746
            $this->assertEquals('Number "0" was expected to be at least "1".', $e->getMessage());
747
        }
748
749
        try {
750
            Assertion::min(0.5, 2.5);
751
            $this->fail('Expected exception `Assert\AssertionFailedException` not thrown');
752
        } catch (AssertionFailedException $e) {
753
            $this->assertEquals(Assertion::INVALID_MIN, $e->getCode());
754
            $this->assertEquals('Number "0.5" was expected to be at least "2.5".', $e->getMessage());
755
        }
756
    }
757
758
    public function testMax()
759
    {
760
        $this->assertTrue(Assertion::max(1, 1));
761
        $this->assertTrue(Assertion::max(0.5, 1));
762
        $this->assertTrue(Assertion::max(0, 1));
763
764
        try {
765
            Assertion::max(2, 1);
766
            $this->fail('Expected exception `Assert\AssertionFailedException` not thrown');
767
        } catch (AssertionFailedException $e) {
768
            $this->assertEquals(Assertion::INVALID_MAX, $e->getCode());
769
            $this->assertEquals('Number "2" was expected to be at most "1".', $e->getMessage());
770
        }
771
772
        try {
773
            Assertion::max(2.5, 0.5);
774
            $this->fail('Expected exception `Assert\AssertionFailedException` not thrown');
775
        } catch (AssertionFailedException $e) {
776
            $this->assertEquals(Assertion::INVALID_MAX, $e->getCode());
777
            $this->assertEquals('Number "2.5" was expected to be at most "0.5".', $e->getMessage());
778
        }
779
    }
780
781
    public function testNullOr()
782
    {
783
        $this->assertTrue(Assertion::nullOrMax(null, 1));
784
        $this->assertTrue(Assertion::nullOrMax(null, 2));
785
    }
786
787
    public function testNullOrWithNoValueThrows()
788
    {
789
        $this->setExpectedException('BadMethodCallException');
790
        Assertion::nullOrMax();
0 ignored issues
show
Bug introduced by
The call to nullOrMax() misses some required arguments starting with $value.
Loading history...
791
    }
792
793
    public function testLength()
794
    {
795
        $this->assertTrue(Assertion::length("asdf", 4));
796
        $this->assertTrue(Assertion::length("", 0));
797
    }
798
799
    public static function dataLengthUtf8Characters()
800
    {
801
        return array(
802
            array("址", 1),
803
            array("ل", 1),
804
        );
805
    }
806
807
    /**
808
     * @dataProvider dataLengthUtf8Characters
809
     */
810
    public function testLenghtUtf8Characters($value, $expected)
811
    {
812
        $this->assertTrue(Assertion::length($value, $expected));
813
    }
814
815
    public function testLengthFailed()
816
    {
817
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LENGTH);
818
        Assertion::length("asdf", 3);
819
    }
820
821
    public function testLengthFailedForWrongEncoding()
822
    {
823
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LENGTH);
824
        Assertion::length("址", 1, null, null, 'ASCII');
825
    }
826
827
    public function testLengthValidForGivenEncoding()
828
    {
829
        $this->assertTrue(Assertion::length("址", 1, null, null, 'utf8'));
830
    }
831
832
    public function testFile()
833
    {
834
        $this->assertTrue(Assertion::file(__FILE__));
835
    }
836
837
    public function testFileWithEmptyFilename()
838
    {
839
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_EMPTY);
840
        Assertion::file("");
841
    }
842
843
    public function testFileDoesNotExists()
844
    {
845
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FILE);
846
        Assertion::file(__DIR__ . '/does-not-exists');
847
    }
848
849
    public function testDirectory()
850
    {
851
        $this->assertTrue(Assertion::directory(__DIR__));
852
853
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DIRECTORY);
854
        Assertion::directory(__DIR__ . '/does-not-exist');
855
    }
856
857
    public function testReadable()
858
    {
859
        $this->assertTrue(Assertion::readable(__FILE__));
860
861
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_READABLE);
862
        Assertion::readable(__DIR__ . '/does-not-exist');
863
    }
864
865
    public function testWriteable()
866
    {
867
        $this->assertTrue(Assertion::writeable(sys_get_temp_dir()));
868
869
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_WRITEABLE);
870
        Assertion::writeable(__DIR__ . '/does-not-exist');
871
    }
872
873
    /**
874
     * @expectedException \BadMethodCallException
875
     * @expectedExceptionMessage No assertion
876
     */
877
    public function testFailedNullOrMethodCall()
878
    {
879
        Assertion::NullOrAssertionDoesNotExist();
880
    }
881
882
    public function testImplementsInterface()
883
    {
884
        $this->assertTrue(Assertion::implementsInterface(
885
            '\ArrayIterator',
886
            '\Traversable'
887
        ));
888
889
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INTERFACE_NOT_IMPLEMENTED);
890
        Assertion::implementsInterface(
891
            '\Exception',
892
            '\Traversable'
893
        );
894
    }
895
896
    public function testImplementsInterfaceWithClassObject()
897
    {
898
        $class = new \ArrayObject();
899
900
        $this->assertTrue(Assertion::implementsInterface(
901
            $class,
902
            '\Traversable'
903
        ));
904
905
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INTERFACE_NOT_IMPLEMENTED);
906
        Assertion::implementsInterface(
907
            $class,
908
            '\SplObserver'
909
        );
910
    }
911
912
    /**
913
     * @dataProvider isJsonStringDataprovider
914
     */
915
    public function testIsJsonString($content)
916
    {
917
        $this->assertTrue(Assertion::isJsonString($content));
918
    }
919
920
    public static function isJsonStringDataprovider()
921
    {
922
        return array(
923
            '»null« value' => array(json_encode(null)),
924
            '»false« value' => array(json_encode(false)),
925
            'array value' => array('["false"]'),
926
            'object value' => array('{"tux":"false"}'),
927
        );
928
    }
929
930
    /**
931
     * @dataProvider isJsonStringInvalidStringDataprovider
932
     */
933
    public function testIsJsonStringExpectingException($invalidString)
934
    {
935
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_JSON_STRING);
936
        Assertion::isJsonString($invalidString);
937
    }
938
939
    public static function isJsonStringInvalidStringDataprovider()
940
    {
941
        return array(
942
            'no json string' => array('invalid json encoded string'),
943
            'error in json string' => array('{invalid json encoded string}'),
944
        );
945
    }
946
947
    /**
948
     * @dataProvider providesValidUuids
949
     */
950
    public function testValidUuids($uuid)
951
    {
952
        $this->assertTrue(Assertion::uuid($uuid));
953
    }
954
955
    /**
956
     * @dataProvider providesInvalidUuids
957
     */
958
    public function testInvalidUuids($uuid)
959
    {
960
        $this->setExpectedException('Assert\InvalidArgumentException');
961
        Assertion::uuid($uuid);
962
    }
963
964
    public static function providesValidUuids()
965
    {
966
        return array(
967
            array('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'),
968
            array('ff6f8cb0-c57d-21e1-9b21-0800200c9a66'),
969
            array('ff6f8cb0-c57d-31e1-9b21-0800200c9a66'),
970
            array('ff6f8cb0-c57d-41e1-9b21-0800200c9a66'),
971
            array('ff6f8cb0-c57d-51e1-9b21-0800200c9a66'),
972
            array('FF6F8CB0-C57D-11E1-9B21-0800200C9A66'),
973
        );
974
    }
975
976
    public static function providesInvalidUuids()
977
    {
978
        return array(
979
            array('zf6f8cb0-c57d-11e1-9b21-0800200c9a66'),
980
            array('af6f8cb0c57d11e19b210800200c9a66'),
981
            array('ff6f8cb0-c57da-51e1-9b21-0800200c9a66'),
982
            array('af6f8cb-c57d-11e1-9b21-0800200c9a66'),
983
            array('3f6f8cb0-c57d-11e1-9b21-0800200c9a6'),
984
        );
985
    }
986
987
    /**
988
     * @dataProvider providesValidE164s
989
     */
990
    public function testValidE164s($e164)
991
    {
992
        $this->assertTrue(Assertion::e164($e164));
993
    }
994
995
    /**
996
     * @dataProvider providesInvalidE164s
997
     */
998
    public function testInvalidE164s($e164)
999
    {
1000
        $this->setExpectedException('Assert\InvalidArgumentException');
1001
        Assertion::e164($e164);
1002
    }
1003
1004
    public static function providesValidE164s()
1005
    {
1006
        return array(
1007
            array('+33626525690'),
1008
            array('33626525690'),
1009
            array('+16174552211'),
1010
        );
1011
    }
1012
1013
    public static function providesInvalidE164s()
1014
    {
1015
        return array(
1016
            array('+3362652569e'),
1017
            array('+3361231231232652569'),
1018
        );
1019
    }
1020
1021
    public function testValidNotEmptyKey()
1022
    {
1023
        $this->assertTrue(Assertion::notEmptyKey(array('keyExists' => 'notEmpty'), 'keyExists'));
1024
    }
1025
1026
    /**
1027
     * @dataProvider invalidNotEmptyKeyDataprovider
1028
     */
1029
    public function testInvalidNotEmptyKey($invalidArray, $key)
1030
    {
1031
        $this->setExpectedException('Assert\InvalidArgumentException');
1032
        Assertion::notEmptyKey($invalidArray, $key);
1033
    }
1034
1035
    public static function invalidNotEmptyKeyDataprovider()
1036
    {
1037
        return array(
1038
            'empty'          => array(array('keyExists' => ''), 'keyExists'),
1039
            'key not exists' => array(array('key' => 'notEmpty'), 'keyNotExists')
1040
        );
1041
    }
1042
1043
    public function testAllWithSimpleAssertion()
1044
    {
1045
        $this->assertTrue(Assertion::allTrue(array(true, true)));
1046
    }
1047
1048
    public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion()
1049
    {
1050
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE);
1051
        Assertion::allTrue(array(true, false));
1052
    }
1053
1054
    public function testAllWithComplexAssertion()
1055
    {
1056
        $this->assertTrue(Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'stdClass'));
1057
    }
1058
1059
    public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion()
1060
    {
1061
        $this->setExpectedException('Assert\AssertionFailedException', 'Assertion failed', Assertion::INVALID_INSTANCE_OF);
1062
1063
        Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'PDO', 'Assertion failed', 'foos');
1064
    }
1065
1066
    public function testAllWithNoValueThrows()
1067
    {
1068
        $this->setExpectedException('BadMethodCallException');
1069
        Assertion::allTrue();
0 ignored issues
show
Bug introduced by
The call to allTrue() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
1070
    }
1071
1072
    public function testValidCount()
1073
    {
1074
        $this->assertTrue(Assertion::count(array('Hi'), 1));
1075
        $this->assertTrue(Assertion::count(new OneCountable(), 1));
1076
    }
1077
1078
    public static function dataInvalidCount()
1079
    {
1080
        return array(
1081
            array(array('Hi', 'There'), 3),
1082
            array(new OneCountable(), 2),
1083
        );
1084
    }
1085
1086
    /**
1087
     * @dataProvider dataInvalidCount
1088
     */
1089
    public function testInvalidCount($countable, $count)
1090
    {
1091
        $this->setExpectedException('Assert\AssertionFailedException', 'List does not contain exactly "'.$count.'" elements.', Assertion::INVALID_COUNT);
1092
        Assertion::count($countable, $count);
1093
    }
1094
1095
    public function testChoicesNotEmpty()
1096
    {
1097
        $this->assertTrue(Assertion::choicesNotEmpty(
1098
            array('tux' => 'linux', 'Gnu' => 'dolphin'),
1099
            array('tux')
1100
        ));
1101
    }
1102
1103
    /**
1104
     * @dataProvider invalidChoicesProvider
1105
     */
1106
    public function testChoicesNotEmptyExpectingException($values, $choices, $exceptionCode)
1107
    {
1108
        $this->setExpectedException('Assert\AssertionFailedException', null, $exceptionCode);
1109
        Assertion::choicesNotEmpty(
1110
            $values,
1111
            $choices
1112
        );
1113
    }
1114
1115
    public function invalidChoicesProvider()
1116
    {
1117
        return array(
1118
            'empty values' => array(array(), array('tux'), Assertion::VALUE_EMPTY),
1119
            'empty recodes in $values' => array(array('tux' => ''), array('tux'), Assertion::VALUE_EMPTY),
1120
            'choice not found in values' => array(array('tux' => ''), array('invalidChoice'), Assertion::INVALID_KEY_ISSET),
1121
        );
1122
    }
1123
1124
    public function testIsObject()
1125
    {
1126
        $this->assertTrue(Assertion::isObject(new \stdClass));
1127
    }
1128
1129
    public function testIsObjectExpectingException()
1130
    {
1131
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_OBJECT);
1132
        Assertion::isObject('notAnObject');
1133
    }
1134
1135
    public function testMethodExists()
1136
    {
1137
        $this->assertTrue(Assertion::methodExists('methodExists', new Assertion()));
1138
    }
1139
1140
    public function testMethodExistsFailure()
1141
    {
1142
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_METHOD);
1143
        Assertion::methodExists('methodNotExists', new Assertion());
1144
    }
1145
1146
    /**
1147
     * @test
1148
     */
1149
    public function it_passes_values_and_constraints_to_exception()
0 ignored issues
show
Coding Style introduced by
Method name "AssertTest::it_passes_values_and_constraints_to_exception" is not in camel caps format
Loading history...
1150
    {
1151
        try {
1152
            Assertion::range(0, 10, 20);
1153
1154
            $this->fail('Exception expected');
1155
        } catch (AssertionFailedException $e) {
1156
            $this->assertEquals(0, $e->getValue());
1157
            $this->assertEquals(array('min' => 10, 'max' => 20), $e->getConstraints());
1158
        }
1159
    }
1160
1161
    public function testLessThan()
1162
    {
1163
        $this->assertTrue(Assertion::lessThan(1, 2));
1164
        $this->assertTrue(Assertion::lessThan('aaa', 'bbb'));
1165
        $this->assertTrue(Assertion::lessThan('aaa', 'aaaa'));
1166
        $this->assertTrue(Assertion::lessThan(new \DateTime('today'), new \DateTime('tomorrow')));
1167
    }
1168
1169
    public function invalidLessProvider()
1170
    {
1171
        return array(
1172
            array(2, 1),
1173
            array(2, 2),
1174
            array('aaa', 'aaa'),
1175
            array('aaaa', 'aaa'),
1176
            array(new \DateTime('today'), new \DateTime('yesterday')),
1177
            array(new \DateTime('today'), new \DateTime('today')),
1178
        );
1179
    }
1180
1181
    /**
1182
     * @dataProvider invalidLessProvider
1183
     */
1184
    public function testLessThanThrowsException($value, $limit)
1185
    {
1186
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LESS);
1187
        Assertion::lessThan($value, $limit);
1188
    }
1189
1190
    public function testLessOrEqualThan()
1191
    {
1192
        $this->assertTrue(Assertion::lessOrEqualThan(1, 2));
1193
        $this->assertTrue(Assertion::lessOrEqualThan(1, 1));
1194
        $this->assertTrue(Assertion::lessOrEqualThan('aaa', 'bbb'));
1195
        $this->assertTrue(Assertion::lessOrEqualThan('aaa', 'aaaa'));
1196
        $this->assertTrue(Assertion::lessOrEqualThan('aaa', 'aaa'));
1197
        $this->assertTrue(Assertion::lessOrEqualThan(new \DateTime('today'), new \DateTime('tomorrow')));
1198
        $this->assertTrue(Assertion::lessOrEqualThan(new \DateTime('today'), new \DateTime('today')));
1199
    }
1200
1201
    public function invalidLessOrEqualProvider()
1202
    {
1203
        return array(
1204
            array(2, 1),
1205
            array('aaaa', 'aaa'),
1206
            array(new \DateTime('today'), new \DateTime('yesterday')),
1207
        );
1208
    }
1209
1210
    /**
1211
     * @dataProvider invalidLessOrEqualProvider
1212
     */
1213
    public function testLessOrEqualThanThrowsException($value, $limit)
1214
    {
1215
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LESS_OR_EQUAL);
1216
        Assertion::lessOrEqualThan($value, $limit);
1217
    }
1218
1219
    public function testGreaterThan()
1220
    {
1221
        $this->assertTrue(Assertion::greaterThan(2, 1));
1222
        $this->assertTrue(Assertion::greaterThan('bbb', 'aaa'));
1223
        $this->assertTrue(Assertion::greaterThan('aaaa', 'aaa'));
1224
        $this->assertTrue(Assertion::greaterThan(new \DateTime('tomorrow'), new \DateTime('today')));
1225
    }
1226
1227
    public function invalidGreaterProvider()
1228
    {
1229
        return array(
1230
            array(1, 2),
1231
            array(2, 2),
1232
            array('aaa', 'aaa'),
1233
            array('aaa', 'aaaa'),
1234
            array(new \DateTime('yesterday'), new \DateTime('today')),
1235
            array(new \DateTime('today'), new \DateTime('today')),
1236
        );
1237
    }
1238
1239
    /**
1240
     * @dataProvider validDateProvider
1241
     */
1242
    public function testValidDate($value, $format)
1243
    {
1244
        $this->assertTrue(Assertion::date($value, $format));
1245
    }
1246
1247
    public function validDateProvider()
1248
    {
1249
        return array(
1250
            array('2012-03-13', 'Y-m-d'),
1251
            array('29.02.2012 12:03:36.432563', 'd.m.Y H:i:s.u'),
1252
            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'),
1253
            array('1439486158', 'U')
1254
        );
1255
    }
1256
1257
    /**
1258
     * @dataProvider invalidGreaterProvider
1259
     */
1260
    public function testGreaterThanThrowsException($value, $limit)
1261
    {
1262
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_GREATER);
1263
        Assertion::greaterThan($value, $limit);
1264
    }
1265
1266
    public function testGreaterOrEqualThan()
1267
    {
1268
        $this->assertTrue(Assertion::greaterOrEqualThan(2, 1));
1269
        $this->assertTrue(Assertion::greaterOrEqualThan(1, 1));
1270
        $this->assertTrue(Assertion::greaterOrEqualThan('bbb', 'aaa'));
1271
        $this->assertTrue(Assertion::greaterOrEqualThan('aaaa', 'aaa'));
1272
        $this->assertTrue(Assertion::greaterOrEqualThan('aaa', 'aaa'));
1273
        $this->assertTrue(Assertion::greaterOrEqualThan(new \DateTime('tomorrow'), new \DateTime('today')));
1274
        $this->assertTrue(Assertion::greaterOrEqualThan(new \DateTime('today'), new \DateTime('today')));
1275
    }
1276
1277
    public function invalidGreaterOrEqualProvider()
1278
    {
1279
        return array(
1280
            array(1, 2),
1281
            array('aaa', 'aaaa'),
1282
            array(new \DateTime('yesterday'), new \DateTime('tomorrow')),
1283
        );
1284
    }
1285
1286
    /**
1287
     * @dataProvider invalidGreaterOrEqualProvider
1288
     *
1289
     * @param mixed $value
1290
     * @param mixed $limit
1291
     */
1292
    public function testGreaterOrEqualThanThrowsException($value, $limit)
1293
    {
1294
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_GREATER_OR_EQUAL);
1295
        Assertion::greaterOrEqualThan($value, $limit);
1296
    }
1297
1298
    /**
1299
     * @dataProvider invalidDateProvider
1300
     */
1301
    public function testInvalidDate($value, $format)
1302
    {
1303
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DATE);
1304
        Assertion::date($value, $format);
1305
    }
1306
1307
    public function invalidDateProvider()
1308
    {
1309
        return array(
1310
            array('this is not the date', 'Y-m-d'),
1311
            array('2011-02-29', 'Y-m-d'),
1312
            array('2012.02.29 12:60:36.432563', 'Y.m.d H:i:s.u')
1313
        );
1314
    }
1315
1316
    public function testValidTraversable()
1317
    {
1318
        $this->assertTrue(Assertion::isTraversable(new \ArrayObject));
1319
    }
1320
1321
    public function testInvalidTraversable()
1322
    {
1323
        $this->setExpectedException('Assert\InvalidArgumentException', null, Assertion::INVALID_TRAVERSABLE);
1324
        Assertion::isTraversable('not traversable');
1325
    }
1326
1327
    public function testValidArrayAccessible()
1328
    {
1329
        $this->assertTrue(Assertion::isArrayAccessible(new \ArrayObject));
1330
    }
1331
1332
    public function testInvalidArrayAccessible()
1333
    {
1334
        $this->setExpectedException('Assert\InvalidArgumentException', null, Assertion::INVALID_ARRAY_ACCESSIBLE);
1335
        Assertion::isArrayAccessible('not array accessible');
1336
    }
1337
1338
    public function testInvalidCallable()
1339
    {
1340
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CALLABLE);
1341
        Assertion::isCallable("nonExistingFunction");
1342
    }
1343
1344
    public function testValidCallable()
1345
    {
1346
        $this->assertTrue(Assertion::isCallable('\is_callable'));
1347
        $this->assertTrue(Assertion::isCallable(__NAMESPACE__ . "\\someCallable"));
1348
        $this->assertTrue(Assertion::isCallable(array(__NAMESPACE__ . "\\OneCountable", "count")));
1349
        $this->assertTrue(Assertion::isCallable(function () {
1350
        }));
1351
    }
1352
1353
    public function testInvalidSatisfy()
1354
    {
1355
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SATISFY);
1356
        Assertion::satisfy(null, function ($value) {
1357
            return !is_null($value);
1358
        });
1359
    }
1360
1361
    public function testValidSatisfy()
1362
    {
1363
        // Should not fail with true return
1364
        $this->assertTrue(Assertion::satisfy(null, function ($value) {
1365
            return is_null($value);
1366
        }));
1367
1368
        // Should not fail with void return
1369
        $this->assertTrue(Assertion::satisfy(true, function ($value) {
1370
            if (!is_bool($value)) {
1371
                return false;
1372
            }
1373
        }));
1374
    }
1375
1376
    /**
1377
     * @dataProvider validIpProvider
1378
     */
1379
    public function testValidIp($value)
1380
    {
1381
        $this->assertTrue(Assertion::ip($value));
1382
    }
1383
1384
    public function validIpProvider()
1385
    {
1386
        return array(
1387
            array('0.0.0.0'),
1388
            array('14.32.152.216'),
1389
            array('255.255.255.255'),
1390
            array('2001:db8:85a3:8d3:1319:8a2e:370:7348'),
1391
        );
1392
    }
1393
1394
    /**
1395
     * @dataProvider invalidIpProvider
1396
     */
1397
    public function testInvalidIp($value, $flag = null)
1398
    {
1399
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_IP);
1400
        Assertion::ip($value, $flag);
1401
    }
1402
1403
    public function invalidIpProvider()
1404
    {
1405
        return array(
1406
            array('invalid ip address'),
1407
            array('14.32.152,216'),
1408
            array('14.32.256.216'),
1409
            array('192.168.0.10', FILTER_FLAG_NO_PRIV_RANGE),
1410
            array('127.0.0.1', FILTER_FLAG_NO_RES_RANGE),
1411
            array('2001:db8:85a3:8d3:1319:8g2e:370:7348'),
1412
            array('fdb9:75b9:9e69:5d08:1:1:1:1', FILTER_FLAG_NO_PRIV_RANGE),
1413
        );
1414
    }
1415
1416
    public function testValidIpv4()
1417
    {
1418
        $this->assertTrue(Assertion::ipv4('109.188.127.26'));
1419
    }
1420
1421
    public function testInvalidIpv4()
1422
    {
1423
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_IP);
1424
        Assertion::ipv4('2001:db8:85a3:8d3:1319:8a2e:370:7348');
1425
    }
1426
1427
    public function testValidIpv6()
1428
    {
1429
        $this->assertTrue(Assertion::ipv6('2001:db8:85a3:8d3:1319:8a2e:370:7348'));
1430
    }
1431
1432
    public function testInvalidIpv6()
1433
    {
1434
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_IP);
1435
        Assertion::ipv6('109.188.127.26');
1436
    }
1437
1438
    public function testInvalidInterfaceExists()
1439
    {
1440
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTERFACE);
1441
        Assertion::interfaceExists("Foo");
1442
    }
1443
1444
    public function testValidInterfaceExists()
1445
    {
1446
        $this->assertTrue(Assertion::interfaceExists("\\Countable"));
1447
    }
1448
1449
    /**
1450
     * @dataProvider providerInvalidBetween
1451
     *
1452
     * @param mixed $value
1453
     * @param mixed $lowerLimit
1454
     * @param mixed $upperLimit
1455
     */
1456
    public function testInvalidBetween($value, $lowerLimit, $upperLimit)
1457
    {
1458
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BETWEEN);
1459
1460
        Assertion::between($value, $lowerLimit, $upperLimit);
1461
    }
1462
1463
    /**
1464
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
1465
     */
1466
    public function providerInvalidBetween()
1467
    {
1468
        return array(
1469
            array(1, 2, 3),
1470
            array(3, 1, 2),
1471
            array('aaa', 'bbb', 'ccc'),
1472
            array('ddd', 'bbb', 'ccc'),
1473
            array(new \DateTime('yesterday'), new \DateTime('today'), new \DateTime('tomorrow')),
1474
            array(new \DateTime('tomorrow'), new \DateTime('yesterday'), new \DateTime('today')),
1475
        );
1476
    }
1477
1478
    /**
1479
     * @dataProvider providerValidBetween
1480
     *
1481
     * @param mixed $value
1482
     * @param mixed $lowerLimit
1483
     * @param mixed $upperLimit
1484
     */
1485
    public function testValidBetween($value, $lowerLimit, $upperLimit)
1486
    {
1487
        $this->assertTrue(Assertion::between($value, $lowerLimit, $upperLimit));
1488
    }
1489
1490
    /**
1491
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
1492
     */
1493
    public function providerValidBetween()
1494
    {
1495
        return array(
1496
            array(2, 1, 3),
1497
            array(1, 1, 1),
1498
            array('bbb', 'aaa', 'ccc'),
1499
            array('aaa', 'aaa', 'aaa'),
1500
            array(new \DateTime('today'), new \DateTime('yesterday'), new \DateTime('tomorrow')),
1501
            array(new \DateTime('today'), new \DateTime('today'), new \DateTime('today')),
1502
        );
1503
    }
1504
1505
    /**
1506
     * @dataProvider providerInvalidBetweenExclusive
1507
     *
1508
     * @param mixed $value
1509
     * @param mixed $lowerLimit
1510
     * @param mixed $upperLimit
1511
     */
1512
    public function testInvalidBetweenExclusive($value, $lowerLimit, $upperLimit)
1513
    {
1514
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BETWEEN_EXCLUSIVE);
1515
1516
        Assertion::betweenExclusive($value, $lowerLimit, $upperLimit);
1517
    }
1518
1519
    /**
1520
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
1521
     */
1522
    public function providerInvalidBetweenExclusive()
1523
    {
1524
        return array(
1525
            array(1, 1, 2),
1526
            array(2, 1, 2),
1527
            array('aaa', 'aaa', 'bbb'),
1528
            array('bbb', 'aaa', 'bbb'),
1529
            array(new \DateTime('today'), new \DateTime('today'), new \DateTime('tomorrow')),
1530
            array(new \DateTime('tomorrow'), new \DateTime('today'), new \DateTime('tomorrow')),
1531
        );
1532
    }
1533
1534
    /**
1535
     * @dataProvider providerValidBetweenExclusive
1536
     *
1537
     * @param mixed $value
1538
     * @param mixed $lowerLimit
1539
     * @param mixed $upperLimit
1540
     */
1541
    public function testValidBetweenExclusive($value, $lowerLimit, $upperLimit)
1542
    {
1543
        $this->assertTrue(Assertion::betweenExclusive($value, $lowerLimit, $upperLimit));
1544
    }
1545
1546
    /**
1547
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
1548
     */
1549
    public function providerValidBetweenExclusive()
1550
    {
1551
        return array(
1552
            array(2, 1, 3),
1553
            array('bbb', 'aaa', 'ccc'),
1554
            array(new \DateTime('today'), new \DateTime('yesterday'), new \DateTime('tomorrow')),
1555
        );
1556
    }
1557
1558
    public function testStringifyTruncatesStringValuesLongerThan100CharactersAppropriately()
1559
    {
1560
        $string = str_repeat('1234567890', 11);
1561
1562
        $this->setExpectedException('Assert\AssertionFailedException', '1234567...', Assertion::INVALID_FLOAT);
1563
1564
        $this->assertTrue(Assertion::float($string));
1565
    }
1566
1567
    public function testStringifyReportsResourceType()
1568
    {
1569
        $this->setExpectedException('Assert\AssertionFailedException', 'stream', Assertion::INVALID_FLOAT);
1570
1571
        $this->assertTrue(Assertion::float(fopen('php://stdin', 'rb')));
1572
    }
1573
1574
    public function testValidConstant()
1575
    {
1576
        $this->assertTrue(Assertion::defined('PHP_VERSION'));
1577
    }
1578
1579
    /**
1580
     * @expectedException \Assert\InvalidArgumentException
1581
     */
1582
    public function testInvalidConstant()
1583
    {
1584
        Assertion::defined('NOT_A_CONSTANT');
1585
    }
1586
}
1587
1588
class ChildStdClass extends \stdClass
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1589
{
1590
}
1591
1592
class OneCountable implements \Countable
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1593
{
1594
    public function count()
1595
    {
1596
        return 1;
1597
    }
1598
}
1599
1600
function someCallable()
1601
{
1602
}
1603