Completed
Push — master ( e5f6b6...ec53c2 )
by Richard
01:43
created

testStringifyTruncatesStringValuesLongerThan100CharactersAppropriately()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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