Completed
Push — master ( d410b1...93d6af )
by Richard
02:03
created

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