Completed
Push — master ( 81ba3b...57a496 )
by Richard
02:33
created

AssertTest::providesValidE164s()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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