Completed
Pull Request — master (#167)
by
unknown
02:30
created

AssertTest::testCatchingWithInterface()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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