Completed
Pull Request — master (#155)
by Raphael
11:38
created

AssertTest::testNullOr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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