Completed
Push — master ( 01e071...007090 )
by Ehsan
01:46 queued 13s
created

ConstraintTest   F

Complexity

Total Complexity 217

Size/Duplication

Total Lines 3017
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 38

Importance

Changes 0
Metric Value
dl 0
loc 3017
rs 0.5217
c 0
b 0
f 0
wmc 217
lcom 1
cbo 38

How to fix   Complexity   

Complex Class

Complex classes like ConstraintTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ConstraintTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PHPUnit\Framework;
12
13
use PHPUnit\Framework\Constraint\Count;
14
use PHPUnit\Framework\Constraint\IsEmpty;
15
use PHPUnit\Framework\Constraint\SameSize;
16
use PHPUnit\Framework\Constraint\TraversableContains;
17
use PHPUnit\Util\Filter;
18
19
class ConstraintTest extends TestCase
20
{
21
    public function testConstraintArrayHasKey()
22
    {
23
        $constraint = Assert::arrayHasKey(0);
24
25
        $this->assertFalse($constraint->evaluate([], '', true));
26
        $this->assertEquals('has the key 0', $constraint->toString());
27
        $this->assertCount(1, $constraint);
28
29
        try {
30
            $constraint->evaluate([]);
31
        } catch (ExpectationFailedException $e) {
32
            $this->assertEquals(
33
                <<<EOF
34
Failed asserting that an array has the key 0.
35
36
EOF
37
                ,
38
                TestFailure::exceptionToString($e)
39
            );
40
41
            return;
42
        }
43
44
        $this->fail();
45
    }
46
47
    public function testConstraintArrayHasKey2()
48
    {
49
        $constraint = Assert::arrayHasKey(0);
50
51
        try {
52
            $constraint->evaluate([], 'custom message');
53
        } catch (ExpectationFailedException $e) {
54
            $this->assertEquals(
55
                <<<EOF
56
custom message\nFailed asserting that an array has the key 0.
57
58
EOF
59
                ,
60
                TestFailure::exceptionToString($e)
61
            );
62
63
            return;
64
        }
65
66
        $this->fail();
67
    }
68
69
    public function testConstraintArrayNotHasKey()
70
    {
71
        $constraint = Assert::logicalNot(
72
            Assert::arrayHasKey(0)
73
        );
74
75
        $this->assertFalse($constraint->evaluate([0 => 1], '', true));
76
        $this->assertEquals('does not have the key 0', $constraint->toString());
77
        $this->assertCount(1, $constraint);
78
79
        try {
80
            $constraint->evaluate([0 => 1]);
81
        } catch (ExpectationFailedException $e) {
82
            $this->assertEquals(
83
                <<<EOF
84
Failed asserting that an array does not have the key 0.
85
86
EOF
87
                ,
88
                TestFailure::exceptionToString($e)
89
            );
90
91
            return;
92
        }
93
94
        $this->fail();
95
    }
96
97
    public function testConstraintArrayNotHasKey2()
98
    {
99
        $constraint = Assert::logicalNot(
100
            Assert::arrayHasKey(0)
101
        );
102
103
        try {
104
            $constraint->evaluate([0], 'custom message');
105
        } catch (ExpectationFailedException $e) {
106
            $this->assertEquals(
107
                <<<EOF
108
custom message
109
Failed asserting that an array does not have the key 0.
110
111
EOF
112
                ,
113
                TestFailure::exceptionToString($e)
114
            );
115
116
            return;
117
        }
118
119
        $this->fail();
120
    }
121
122
    public function testConstraintIsReadable()
123
    {
124
        $constraint = Assert::isReadable();
125
126
        $this->assertFalse($constraint->evaluate('foo', '', true));
127
        $this->assertEquals('is readable', $constraint->toString());
128
        $this->assertCount(1, $constraint);
129
130
        try {
131
            $constraint->evaluate('foo');
132
        } catch (ExpectationFailedException $e) {
133
            $this->assertEquals(
134
                <<<EOF
135
Failed asserting that "foo" is readable.
136
137
EOF
138
                ,
139
                TestFailure::exceptionToString($e)
140
            );
141
142
            return;
143
        }
144
145
        $this->fail();
146
    }
147
148
    public function testConstraintIsWritable()
149
    {
150
        $constraint = Assert::isWritable();
151
152
        $this->assertFalse($constraint->evaluate('foo', '', true));
153
        $this->assertEquals('is writable', $constraint->toString());
154
        $this->assertCount(1, $constraint);
155
156
        try {
157
            $constraint->evaluate('foo');
158
        } catch (ExpectationFailedException $e) {
159
            $this->assertEquals(
160
                <<<EOF
161
Failed asserting that "foo" is writable.
162
163
EOF
164
                ,
165
                TestFailure::exceptionToString($e)
166
            );
167
168
            return;
169
        }
170
171
        $this->fail();
172
    }
173
174
    public function testConstraintDirectoryExists()
175
    {
176
        $constraint = Assert::directoryExists();
177
178
        $this->assertFalse($constraint->evaluate('foo', '', true));
179
        $this->assertEquals('directory exists', $constraint->toString());
180
        $this->assertCount(1, $constraint);
181
182
        try {
183
            $constraint->evaluate('foo');
184
        } catch (ExpectationFailedException $e) {
185
            $this->assertEquals(
186
                <<<EOF
187
Failed asserting that directory "foo" exists.
188
189
EOF
190
                ,
191
                TestFailure::exceptionToString($e)
192
            );
193
194
            return;
195
        }
196
197
        $this->fail();
198
    }
199
200
    public function testConstraintFileExists()
201
    {
202
        $constraint = Assert::fileExists();
203
204
        $this->assertFalse($constraint->evaluate('foo', '', true));
205
        $this->assertEquals('file exists', $constraint->toString());
206
        $this->assertCount(1, $constraint);
207
208
        try {
209
            $constraint->evaluate('foo');
210
        } catch (ExpectationFailedException $e) {
211
            $this->assertEquals(
212
                <<<EOF
213
Failed asserting that file "foo" exists.
214
215
EOF
216
                ,
217
                TestFailure::exceptionToString($e)
218
            );
219
220
            return;
221
        }
222
223
        $this->fail();
224
    }
225
226
    public function testConstraintFileExists2()
227
    {
228
        $constraint = Assert::fileExists();
229
230
        try {
231
            $constraint->evaluate('foo', 'custom message');
232
        } catch (ExpectationFailedException $e) {
233
            $this->assertEquals(
234
                <<<EOF
235
custom message
236
Failed asserting that file "foo" exists.
237
238
EOF
239
                ,
240
                TestFailure::exceptionToString($e)
241
            );
242
243
            return;
244
        }
245
246
        $this->fail();
247
    }
248
249
    public function testConstraintFileNotExists()
250
    {
251
        $file = \dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ClassWithNonPublicAttributes.php';
252
253
        $constraint = Assert::logicalNot(
254
            Assert::fileExists()
255
        );
256
257
        $this->assertFalse($constraint->evaluate($file, '', true));
258
        $this->assertEquals('file does not exist', $constraint->toString());
259
        $this->assertCount(1, $constraint);
260
261
        try {
262
            $constraint->evaluate($file);
263
        } catch (ExpectationFailedException $e) {
264
            $this->assertEquals(
265
                <<<EOF
266
Failed asserting that file "$file" does not exist.
267
268
EOF
269
                ,
270
                TestFailure::exceptionToString($e)
271
            );
272
273
            return;
274
        }
275
276
        $this->fail();
277
    }
278
279
    public function testConstraintFileNotExists2()
280
    {
281
        $file = \dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ClassWithNonPublicAttributes.php';
282
283
        $constraint = Assert::logicalNot(
284
            Assert::fileExists()
285
        );
286
287
        try {
288
            $constraint->evaluate($file, 'custom message');
289
        } catch (ExpectationFailedException $e) {
290
            $this->assertEquals(
291
                <<<EOF
292
custom message
293
Failed asserting that file "$file" does not exist.
294
295
EOF
296
                ,
297
                TestFailure::exceptionToString($e)
298
            );
299
300
            return;
301
        }
302
303
        $this->fail();
304
    }
305
306
    public function testConstraintGreaterThan()
307
    {
308
        $constraint = Assert::greaterThan(1);
309
310
        $this->assertFalse($constraint->evaluate(0, '', true));
311
        $this->assertTrue($constraint->evaluate(2, '', true));
312
        $this->assertEquals('is greater than 1', $constraint->toString());
313
        $this->assertCount(1, $constraint);
314
315
        try {
316
            $constraint->evaluate(0);
317
        } catch (ExpectationFailedException $e) {
318
            $this->assertEquals(
319
                <<<EOF
320
Failed asserting that 0 is greater than 1.
321
322
EOF
323
                ,
324
                TestFailure::exceptionToString($e)
325
            );
326
327
            return;
328
        }
329
330
        $this->fail();
331
    }
332
333
    public function testConstraintGreaterThan2()
334
    {
335
        $constraint = Assert::greaterThan(1);
336
337
        try {
338
            $constraint->evaluate(0, 'custom message');
339
        } catch (ExpectationFailedException $e) {
340
            $this->assertEquals(
341
                <<<EOF
342
custom message
343
Failed asserting that 0 is greater than 1.
344
345
EOF
346
                ,
347
                TestFailure::exceptionToString($e)
348
            );
349
350
            return;
351
        }
352
353
        $this->fail();
354
    }
355
356
    public function testConstraintNotGreaterThan()
357
    {
358
        $constraint = Assert::logicalNot(
359
            Assert::greaterThan(1)
360
        );
361
362
        $this->assertTrue($constraint->evaluate(1, '', true));
363
        $this->assertEquals('is not greater than 1', $constraint->toString());
364
        $this->assertCount(1, $constraint);
365
366
        try {
367
            $constraint->evaluate(2);
368
        } catch (ExpectationFailedException $e) {
369
            $this->assertEquals(
370
                <<<EOF
371
Failed asserting that 2 is not greater than 1.
372
373
EOF
374
                ,
375
                TestFailure::exceptionToString($e)
376
            );
377
378
            return;
379
        }
380
381
        $this->fail();
382
    }
383
384
    public function testConstraintNotGreaterThan2()
385
    {
386
        $constraint = Assert::logicalNot(
387
            Assert::greaterThan(1)
388
        );
389
390
        try {
391
            $constraint->evaluate(2, 'custom message');
392
        } catch (ExpectationFailedException $e) {
393
            $this->assertEquals(
394
                <<<EOF
395
custom message
396
Failed asserting that 2 is not greater than 1.
397
398
EOF
399
                ,
400
                TestFailure::exceptionToString($e)
401
            );
402
403
            return;
404
        }
405
406
        $this->fail();
407
    }
408
409
    public function testConstraintGreaterThanOrEqual()
410
    {
411
        $constraint = Assert::greaterThanOrEqual(1);
412
413
        $this->assertTrue($constraint->evaluate(1, '', true));
414
        $this->assertFalse($constraint->evaluate(0, '', true));
415
        $this->assertEquals('is equal to 1 or is greater than 1', $constraint->toString());
416
        $this->assertCount(2, $constraint);
417
418
        try {
419
            $constraint->evaluate(0);
420
        } catch (ExpectationFailedException $e) {
421
            $this->assertEquals(
422
                <<<EOF
423
Failed asserting that 0 is equal to 1 or is greater than 1.
424
425
EOF
426
                ,
427
                TestFailure::exceptionToString($e)
428
            );
429
430
            return;
431
        }
432
433
        $this->fail();
434
    }
435
436
    public function testConstraintGreaterThanOrEqual2()
437
    {
438
        $constraint = Assert::greaterThanOrEqual(1);
439
440
        try {
441
            $constraint->evaluate(0, 'custom message');
442
        } catch (ExpectationFailedException $e) {
443
            $this->assertEquals(
444
                <<<EOF
445
custom message
446
Failed asserting that 0 is equal to 1 or is greater than 1.
447
448
EOF
449
                ,
450
                TestFailure::exceptionToString($e)
451
            );
452
453
            return;
454
        }
455
456
        $this->fail();
457
    }
458
459
    public function testConstraintNotGreaterThanOrEqual()
460
    {
461
        $constraint = Assert::logicalNot(
462
            Assert::greaterThanOrEqual(1)
463
        );
464
465
        $this->assertFalse($constraint->evaluate(1, '', true));
466
        $this->assertEquals('not( is equal to 1 or is greater than 1 )', $constraint->toString());
467
        $this->assertCount(2, $constraint);
468
469
        try {
470
            $constraint->evaluate(1);
471
        } catch (ExpectationFailedException $e) {
472
            $this->assertEquals(
473
                <<<EOF
474
Failed asserting that not( 1 is equal to 1 or is greater than 1 ).
475
476
EOF
477
                ,
478
                TestFailure::exceptionToString($e)
479
            );
480
481
            return;
482
        }
483
484
        $this->fail();
485
    }
486
487
    public function testConstraintNotGreaterThanOrEqual2()
488
    {
489
        $constraint = Assert::logicalNot(
490
            Assert::greaterThanOrEqual(1)
491
        );
492
493
        try {
494
            $constraint->evaluate(1, 'custom message');
495
        } catch (ExpectationFailedException $e) {
496
            $this->assertEquals(
497
                <<<EOF
498
custom message
499
Failed asserting that not( 1 is equal to 1 or is greater than 1 ).
500
501
EOF
502
                ,
503
                TestFailure::exceptionToString($e)
504
            );
505
506
            return;
507
        }
508
509
        $this->fail();
510
    }
511
512
    public function testConstraintIsAnything()
513
    {
514
        $constraint = Assert::anything();
515
516
        $this->assertTrue($constraint->evaluate(null, '', true));
517
        $this->assertNull($constraint->evaluate(null));
518
        $this->assertEquals('is anything', $constraint->toString());
519
        $this->assertCount(0, $constraint);
520
    }
521
522
    public function testConstraintNotIsAnything()
523
    {
524
        $constraint = Assert::logicalNot(
525
            Assert::anything()
526
        );
527
528
        $this->assertFalse($constraint->evaluate(null, '', true));
529
        $this->assertEquals('is not anything', $constraint->toString());
530
        $this->assertCount(0, $constraint);
531
532
        try {
533
            $constraint->evaluate(null);
534
        } catch (ExpectationFailedException $e) {
535
            $this->assertEquals(
536
                <<<EOF
537
Failed asserting that null is not anything.
538
539
EOF
540
                ,
541
                TestFailure::exceptionToString($e)
542
            );
543
544
            return;
545
        }
546
547
        $this->fail();
548
    }
549
550
    public function testConstraintIsEqual()
551
    {
552
        $constraint = Assert::equalTo(1);
553
554
        $this->assertTrue($constraint->evaluate(1, '', true));
555
        $this->assertFalse($constraint->evaluate(0, '', true));
556
        $this->assertEquals('is equal to 1', $constraint->toString());
557
        $this->assertCount(1, $constraint);
558
559
        try {
560
            $constraint->evaluate(0);
561
        } catch (ExpectationFailedException $e) {
562
            $this->assertEquals(
563
                <<<EOF
564
Failed asserting that 0 matches expected 1.
565
566
EOF
567
                ,
568
                TestFailure::exceptionToString($e)
569
            );
570
571
            return;
572
        }
573
574
        $this->fail();
575
    }
576
577
    public function isEqualProvider()
578
    {
579
        $a      = new \stdClass;
580
        $a->foo = 'bar';
581
        $b      = new \stdClass;
582
        $ahash  = \spl_object_hash($a);
583
        $bhash  = \spl_object_hash($b);
584
585
        $c               = new \stdClass;
586
        $c->foo          = 'bar';
587
        $c->int          = 1;
588
        $c->array        = [0, [1], [2], 3];
589
        $c->related      = new \stdClass;
590
        $c->related->foo = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk";
591
        $c->self         = $c;
592
        $c->c            = $c;
593
        $d               = new \stdClass;
594
        $d->foo          = 'bar';
595
        $d->int          = 2;
596
        $d->array        = [0, [4], [2], 3];
597
        $d->related      = new \stdClass;
598
        $d->related->foo = "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk";
599
        $d->self         = $d;
600
        $d->c            = $c;
601
602
        $storage1 = new \SplObjectStorage;
603
        $storage1->attach($a);
604
        $storage1->attach($b);
605
        $storage2 = new \SplObjectStorage;
606
        $storage2->attach($b);
607
        $storage1hash = \spl_object_hash($storage1);
608
        $storage2hash = \spl_object_hash($storage2);
609
610
        $dom1                     = new \DOMDocument;
611
        $dom1->preserveWhiteSpace = false;
612
        $dom1->loadXML('<root></root>');
613
        $dom2                     = new \DOMDocument;
614
        $dom2->preserveWhiteSpace = false;
615
        $dom2->loadXML('<root><foo/></root>');
616
617
        $data = [
618
            [1, 0, <<<EOF
619
Failed asserting that 0 matches expected 1.
620
621
EOF
622
            ],
623
            [1.1, 0, <<<EOF
624
Failed asserting that 0 matches expected 1.1.
625
626
EOF
627
            ],
628
            ['a', 'b', <<<EOF
629
Failed asserting that two strings are equal.
630
--- Expected
631
+++ Actual
632
@@ @@
633
-'a'
634
+'b'
635
636
EOF
637
            ],
638
            ["a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk", "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk", <<<EOF
639
Failed asserting that two strings are equal.
640
--- Expected
641
+++ Actual
642
@@ @@
643
 'a\\n
644
-b\\n
645
+p\\n
646
@@ @@
647
 i\\n
648
-j\\n
649
+w\\n
650
 k'
651
652
EOF
653
            ],
654
            [1, [0], <<<EOF
655
Array (...) does not match expected type "integer".
656
657
EOF
658
            ],
659
            [[0], 1, <<<EOF
660
1 does not match expected type "array".
661
662
EOF
663
            ],
664
            [[0], [1], <<<EOF
665
Failed asserting that two arrays are equal.
666
--- Expected
667
+++ Actual
668
@@ @@
669
 Array (
670
-    0 => 0
671
+    0 => 1
672
 )
673
674
EOF
675
            ],
676
            [[true], ['true'], <<<EOF
677
Failed asserting that two arrays are equal.
678
--- Expected
679
+++ Actual
680
@@ @@
681
 Array (
682
-    0 => true
683
+    0 => 'true'
684
 )
685
686
EOF
687
            ],
688
            [[0, [1], [2], 3], [0, [4], [2], 3], <<<EOF
689
Failed asserting that two arrays are equal.
690
--- Expected
691
+++ Actual
692
@@ @@
693
 Array (
694
     0 => 0
695
     1 => Array (
696
-        0 => 1
697
+        0 => 4
698
     )
699
     2 => Array (...)
700
     3 => 3
701
 )
702
703
EOF
704
            ],
705
            [$a, [0], <<<EOF
706
Array (...) does not match expected type "object".
707
708
EOF
709
            ],
710
            [[0], $a, <<<EOF
711
stdClass Object (...) does not match expected type "array".
712
713
EOF
714
            ],
715
            [$a, $b, <<<EOF
716
Failed asserting that two objects are equal.
717
--- Expected
718
+++ Actual
719
@@ @@
720
 stdClass Object (
721
-    'foo' => 'bar'
722
 )
723
724
EOF
725
            ],
726
            [$c, $d, <<<EOF
727
Failed asserting that two objects are equal.
728
--- Expected
729
+++ Actual
730
@@ @@
731
 stdClass Object (
732
     'foo' => 'bar'
733
-    'int' => 1
734
+    'int' => 2
735
     'array' => Array (
736
         0 => 0
737
         1 => Array (
738
-            0 => 1
739
+            0 => 4
740
@@ @@
741
         'foo' => 'a\\n
742
-        b\\n
743
+        p\\n
744
@@ @@
745
         i\\n
746
-        j\\n
747
+        w\\n
748
         k'
749
     )
750
     'self' => stdClass Object (...)
751
     'c' => stdClass Object (...)
752
 )
753
754
EOF
755
            ],
756
            [$dom1, $dom2, <<<EOF
757
Failed asserting that two DOM documents are equal.
758
--- Expected
759
+++ Actual
760
@@ @@
761
 <?xml version="1.0"?>
762
-<root/>
763
+<root>
764
+  <foo/>
765
+</root>
766
767
EOF
768
            ],
769
            [
770
              new \DateTime('2013-03-29 04:13:35', new \DateTimeZone('America/New_York')),
771
              new \DateTime('2013-03-29 04:13:35', new \DateTimeZone('America/Chicago')),
772
              <<<EOF
773
Failed asserting that two DateTime objects are equal.
774
--- Expected
775
+++ Actual
776
@@ @@
777
-2013-03-29T04:13:35.000000-0400
778
+2013-03-29T04:13:35.000000-0500
779
780
EOF
781
            ],
782
        ];
783
784
        if (PHP_MAJOR_VERSION < 7) {
785
            $data[] = [$storage1, $storage2, <<<EOF
786
Failed asserting that two objects are equal.
787
--- Expected
788
+++ Actual
789
@@ @@
790
-SplObjectStorage Object &$storage1hash (
791
-    '$ahash' => Array &0 (
792
-        'obj' => stdClass Object &$ahash (
793
-            'foo' => 'bar'
794
-        )
795
-        'inf' => null
796
-    )
797
-    '$bhash' => Array &1 (
798
+SplObjectStorage Object &$storage2hash (
799
+    '$bhash' => Array &0 (
800
         'obj' => stdClass Object &$bhash ()
801
         'inf' => null
802
     )
803
 )
804
805
EOF
806
            ];
807
        } else {
808
            $data[] = [$storage1, $storage2, <<<EOF
809
Failed asserting that two objects are equal.
810
--- Expected
811
+++ Actual
812
@@ @@
813
-SplObjectStorage Object &$storage1hash (
814
-    '$ahash' => Array &0 (
815
-        'obj' => stdClass Object &$ahash (
816
-            'foo' => 'bar'
817
-        )
818
-        'inf' => null
819
-    )
820
-    '$bhash' => Array &1 (
821
+SplObjectStorage Object &$storage2hash (
822
+    '$bhash' => Array &0 (
823
         'obj' => stdClass Object &$bhash ()
824
         'inf' => null
825
     )
826
 )
827
828
EOF
829
            ];
830
        }
831
832
        return $data;
833
    }
834
835
    /**
836
     * @dataProvider isEqualProvider
837
     */
838
    public function testConstraintIsEqual2($expected, $actual, $message)
839
    {
840
        $constraint = Assert::equalTo($expected);
841
842
        try {
843
            $constraint->evaluate($actual, 'custom message');
844
        } catch (ExpectationFailedException $e) {
845
            $this->assertEquals(
846
                "custom message\n$message",
847
                $this->trimnl(TestFailure::exceptionToString($e))
848
            );
849
850
            return;
851
        }
852
853
        $this->fail();
854
    }
855
856
    public function testConstraintIsNotEqual()
857
    {
858
        $constraint = Assert::logicalNot(
859
            Assert::equalTo(1)
860
        );
861
862
        $this->assertTrue($constraint->evaluate(0, '', true));
863
        $this->assertFalse($constraint->evaluate(1, '', true));
864
        $this->assertEquals('is not equal to 1', $constraint->toString());
865
        $this->assertCount(1, $constraint);
866
867
        try {
868
            $constraint->evaluate(1);
869
        } catch (ExpectationFailedException $e) {
870
            $this->assertEquals(
871
                <<<EOF
872
Failed asserting that 1 is not equal to 1.
873
874
EOF
875
                ,
876
                TestFailure::exceptionToString($e)
877
            );
878
879
            return;
880
        }
881
882
        $this->fail();
883
    }
884
885
    public function testConstraintIsNotEqual2()
886
    {
887
        $constraint = Assert::logicalNot(
888
            Assert::equalTo(1)
889
        );
890
891
        try {
892
            $constraint->evaluate(1, 'custom message');
893
        } catch (ExpectationFailedException $e) {
894
            $this->assertEquals(
895
                <<<EOF
896
custom message
897
Failed asserting that 1 is not equal to 1.
898
899
EOF
900
                ,
901
                TestFailure::exceptionToString($e)
902
            );
903
904
            return;
905
        }
906
907
        $this->fail();
908
    }
909
910
    public function testConstraintIsIdentical()
911
    {
912
        $a = new \stdClass;
913
        $b = new \stdClass;
914
915
        $constraint = Assert::identicalTo($a);
916
917
        $this->assertFalse($constraint->evaluate($b, '', true));
918
        $this->assertTrue($constraint->evaluate($a, '', true));
919
        $this->assertEquals('is identical to an object of class "stdClass"', $constraint->toString());
920
        $this->assertCount(1, $constraint);
921
922
        try {
923
            $constraint->evaluate($b);
924
        } catch (ExpectationFailedException $e) {
925
            $this->assertEquals(
926
                <<<EOF
927
Failed asserting that two variables reference the same object.
928
929
EOF
930
                ,
931
                TestFailure::exceptionToString($e)
932
            );
933
934
            return;
935
        }
936
937
        $this->fail();
938
    }
939
940
    public function testConstraintIsIdentical2()
941
    {
942
        $a = new \stdClass;
943
        $b = new \stdClass;
944
945
        $constraint = Assert::identicalTo($a);
946
947
        try {
948
            $constraint->evaluate($b, 'custom message');
949
        } catch (ExpectationFailedException $e) {
950
            $this->assertEquals(
951
                <<<EOF
952
custom message
953
Failed asserting that two variables reference the same object.
954
955
EOF
956
                ,
957
                TestFailure::exceptionToString($e)
958
            );
959
960
            return;
961
        }
962
963
        $this->fail();
964
    }
965
966
    public function testConstraintIsIdentical3()
967
    {
968
        $constraint = Assert::identicalTo('a');
969
970
        try {
971
            $constraint->evaluate('b', 'custom message');
972
        } catch (ExpectationFailedException $e) {
973
            $this->assertEquals(
974
                <<<EOF
975
custom message
976
Failed asserting that two strings are identical.
977
--- Expected
978
+++ Actual
979
@@ @@
980
-'a'
981
+'b'
982
983
EOF
984
                ,
985
                TestFailure::exceptionToString($e)
986
            );
987
988
            return;
989
        }
990
991
        $this->fail();
992
    }
993
994
    public function testConstraintIsNotIdentical()
995
    {
996
        $a = new \stdClass;
997
        $b = new \stdClass;
998
999
        $constraint = Assert::logicalNot(
1000
            Assert::identicalTo($a)
1001
        );
1002
1003
        $this->assertTrue($constraint->evaluate($b, '', true));
1004
        $this->assertFalse($constraint->evaluate($a, '', true));
1005
        $this->assertEquals('is not identical to an object of class "stdClass"', $constraint->toString());
1006
        $this->assertCount(1, $constraint);
1007
1008
        try {
1009
            $constraint->evaluate($a);
1010
        } catch (ExpectationFailedException $e) {
1011
            $this->assertEquals(
1012
                <<<EOF
1013
Failed asserting that two variables don't reference the same object.
1014
1015
EOF
1016
                ,
1017
                $this->trimnl(TestFailure::exceptionToString($e))
1018
            );
1019
1020
            return;
1021
        }
1022
1023
        $this->fail();
1024
    }
1025
1026
    public function testConstraintIsNotIdentical2()
1027
    {
1028
        $a = new \stdClass;
1029
1030
        $constraint = Assert::logicalNot(
1031
            Assert::identicalTo($a)
1032
        );
1033
1034
        try {
1035
            $constraint->evaluate($a, 'custom message');
1036
        } catch (ExpectationFailedException $e) {
1037
            $this->assertEquals(
1038
                <<<EOF
1039
custom message
1040
Failed asserting that two variables don't reference the same object.
1041
1042
EOF
1043
                ,
1044
                TestFailure::exceptionToString($e)
1045
            );
1046
1047
            return;
1048
        }
1049
1050
        $this->fail();
1051
    }
1052
1053
    public function testConstraintIsNotIdentical3()
1054
    {
1055
        $constraint = Assert::logicalNot(
1056
            Assert::identicalTo('a')
1057
        );
1058
1059
        try {
1060
            $constraint->evaluate('a', 'custom message');
1061
        } catch (ExpectationFailedException $e) {
1062
            $this->assertEquals(
1063
                <<<EOF
1064
custom message
1065
Failed asserting that two strings are not identical.
1066
1067
EOF
1068
                ,
1069
                $this->trimnl(TestFailure::exceptionToString($e))
1070
            );
1071
1072
            return;
1073
        }
1074
1075
        $this->fail();
1076
    }
1077
1078
    public function testConstraintIsInstanceOf()
1079
    {
1080
        $constraint = Assert::isInstanceOf(\Exception::class);
1081
1082
        $this->assertFalse($constraint->evaluate(new \stdClass, '', true));
1083
        $this->assertTrue($constraint->evaluate(new \Exception, '', true));
1084
        $this->assertEquals('is instance of class "Exception"', $constraint->toString());
1085
        $this->assertCount(1, $constraint);
1086
1087
        $interfaceConstraint = Assert::isInstanceOf(\Countable::class);
1088
        $this->assertFalse($interfaceConstraint->evaluate(new \stdClass, '', true));
1089
        $this->assertTrue($interfaceConstraint->evaluate(new \ArrayObject, '', true));
1090
        $this->assertEquals('is instance of interface "Countable"', $interfaceConstraint->toString());
1091
1092
        try {
1093
            $constraint->evaluate(new \stdClass);
1094
        } catch (ExpectationFailedException $e) {
1095
            $this->assertEquals(
1096
                <<<EOF
1097
Failed asserting that stdClass Object () is an instance of class "Exception".
1098
1099
EOF
1100
                ,
1101
                TestFailure::exceptionToString($e)
1102
            );
1103
1104
            return;
1105
        }
1106
1107
        $this->fail();
1108
    }
1109
1110
    public function testConstraintIsInstanceOf2()
1111
    {
1112
        $constraint = Assert::isInstanceOf(\Exception::class);
1113
1114
        try {
1115
            $constraint->evaluate(new \stdClass, 'custom message');
1116
        } catch (ExpectationFailedException $e) {
1117
            $this->assertEquals(
1118
                <<<EOF
1119
custom message
1120
Failed asserting that stdClass Object () is an instance of class "Exception".
1121
1122
EOF
1123
                ,
1124
                TestFailure::exceptionToString($e)
1125
            );
1126
1127
            return;
1128
        }
1129
1130
        $this->fail();
1131
    }
1132
1133
    public function testConstraintIsNotInstanceOf()
1134
    {
1135
        $constraint = Assert::logicalNot(
1136
            Assert::isInstanceOf(\stdClass::class)
1137
        );
1138
1139
        $this->assertFalse($constraint->evaluate(new \stdClass, '', true));
1140
        $this->assertTrue($constraint->evaluate(new Exception, '', true));
1141
        $this->assertEquals('is not instance of class "stdClass"', $constraint->toString());
1142
        $this->assertCount(1, $constraint);
1143
1144
        try {
1145
            $constraint->evaluate(new \stdClass);
1146
        } catch (ExpectationFailedException $e) {
1147
            $this->assertEquals(
1148
                <<<EOF
1149
Failed asserting that stdClass Object () is not an instance of class "stdClass".
1150
1151
EOF
1152
                ,
1153
                TestFailure::exceptionToString($e)
1154
            );
1155
1156
            return;
1157
        }
1158
1159
        $this->fail();
1160
    }
1161
1162
    public function testConstraintIsNotInstanceOf2()
1163
    {
1164
        $constraint = Assert::logicalNot(
1165
            Assert::isInstanceOf(\stdClass::class)
1166
        );
1167
1168
        try {
1169
            $constraint->evaluate(new \stdClass, 'custom message');
1170
        } catch (ExpectationFailedException $e) {
1171
            $this->assertEquals(
1172
                <<<EOF
1173
custom message
1174
Failed asserting that stdClass Object () is not an instance of class "stdClass".
1175
1176
EOF
1177
                ,
1178
                TestFailure::exceptionToString($e)
1179
            );
1180
1181
            return;
1182
        }
1183
1184
        $this->fail();
1185
    }
1186
1187
    public function testConstraintIsType()
1188
    {
1189
        $constraint = Assert::isType('string');
1190
1191
        $this->assertFalse($constraint->evaluate(0, '', true));
1192
        $this->assertTrue($constraint->evaluate('', '', true));
1193
        $this->assertEquals('is of type "string"', $constraint->toString());
1194
        $this->assertCount(1, $constraint);
1195
1196
        try {
1197
            $constraint->evaluate(new \stdClass);
1198
        } catch (ExpectationFailedException $e) {
1199
            $this->assertStringMatchesFormat(
1200
                <<<EOF
1201
Failed asserting that stdClass Object &%x () is of type "string".
1202
1203
EOF
1204
                ,
1205
                $this->trimnl(TestFailure::exceptionToString($e))
1206
            );
1207
1208
            return;
1209
        }
1210
1211
        $this->fail();
1212
    }
1213
1214
    public function testConstraintIsType2()
1215
    {
1216
        $constraint = Assert::isType('string');
1217
1218
        try {
1219
            $constraint->evaluate(new \stdClass, 'custom message');
1220
        } catch (ExpectationFailedException $e) {
1221
            $this->assertStringMatchesFormat(
1222
                <<<EOF
1223
custom message
1224
Failed asserting that stdClass Object &%x () is of type "string".
1225
1226
EOF
1227
                ,
1228
                $this->trimnl(TestFailure::exceptionToString($e))
1229
            );
1230
1231
            return;
1232
        }
1233
1234
        $this->fail();
1235
    }
1236
1237
    public function resources()
1238
    {
1239
        $fh = \fopen(__FILE__, 'r');
1240
        \fclose($fh);
1241
1242
        return [
1243
            'open resource'     => [\fopen(__FILE__, 'r')],
1244
            'closed resource'   => [$fh],
1245
        ];
1246
    }
1247
1248
    /**
1249
     * @dataProvider resources
1250
     */
1251
    public function testConstraintIsResourceTypeEvaluatesCorrectlyWithResources($resource)
1252
    {
1253
        $constraint = Assert::isType('resource');
1254
1255
        $this->assertTrue($constraint->evaluate($resource, '', true));
1256
1257
        @\fclose($resource);
1258
    }
1259
1260
    public function testConstraintIsNotType()
1261
    {
1262
        $constraint = Assert::logicalNot(
1263
            Assert::isType('string')
1264
        );
1265
1266
        $this->assertTrue($constraint->evaluate(0, '', true));
1267
        $this->assertFalse($constraint->evaluate('', '', true));
1268
        $this->assertEquals('is not of type "string"', $constraint->toString());
1269
        $this->assertCount(1, $constraint);
1270
1271
        try {
1272
            $constraint->evaluate('');
1273
        } catch (ExpectationFailedException $e) {
1274
            $this->assertEquals(
1275
                <<<EOF
1276
Failed asserting that '' is not of type "string".
1277
1278
EOF
1279
                ,
1280
                TestFailure::exceptionToString($e)
1281
            );
1282
1283
            return;
1284
        }
1285
1286
        $this->fail();
1287
    }
1288
1289
    public function testConstraintIsNotType2()
1290
    {
1291
        $constraint = Assert::logicalNot(
1292
            Assert::isType('string')
1293
        );
1294
1295
        try {
1296
            $constraint->evaluate('', 'custom message');
1297
        } catch (ExpectationFailedException $e) {
1298
            $this->assertEquals(
1299
                <<<EOF
1300
custom message
1301
Failed asserting that '' is not of type "string".
1302
1303
EOF
1304
                ,
1305
                TestFailure::exceptionToString($e)
1306
            );
1307
1308
            return;
1309
        }
1310
1311
        $this->fail();
1312
    }
1313
1314
    public function testConstraintIsNull()
1315
    {
1316
        $constraint = Assert::isNull();
1317
1318
        $this->assertFalse($constraint->evaluate(0, '', true));
1319
        $this->assertTrue($constraint->evaluate(null, '', true));
1320
        $this->assertEquals('is null', $constraint->toString());
1321
        $this->assertCount(1, $constraint);
1322
1323
        try {
1324
            $constraint->evaluate(0);
1325
        } catch (ExpectationFailedException $e) {
1326
            $this->assertEquals(
1327
                <<<EOF
1328
Failed asserting that 0 is null.
1329
1330
EOF
1331
                ,
1332
                TestFailure::exceptionToString($e)
1333
            );
1334
1335
            return;
1336
        }
1337
1338
        $this->fail();
1339
    }
1340
1341
    public function testConstraintIsNull2()
1342
    {
1343
        $constraint = Assert::isNull();
1344
1345
        try {
1346
            $constraint->evaluate(0, 'custom message');
1347
        } catch (ExpectationFailedException $e) {
1348
            $this->assertEquals(
1349
                <<<EOF
1350
custom message
1351
Failed asserting that 0 is null.
1352
1353
EOF
1354
                ,
1355
                TestFailure::exceptionToString($e)
1356
            );
1357
1358
            return;
1359
        }
1360
1361
        $this->fail();
1362
    }
1363
1364
    public function testConstraintIsNotNull()
1365
    {
1366
        $constraint = Assert::logicalNot(
1367
            Assert::isNull()
1368
        );
1369
1370
        $this->assertFalse($constraint->evaluate(null, '', true));
1371
        $this->assertTrue($constraint->evaluate(0, '', true));
1372
        $this->assertEquals('is not null', $constraint->toString());
1373
        $this->assertCount(1, $constraint);
1374
1375
        try {
1376
            $constraint->evaluate(null);
1377
        } catch (ExpectationFailedException $e) {
1378
            $this->assertEquals(
1379
                <<<EOF
1380
Failed asserting that null is not null.
1381
1382
EOF
1383
                ,
1384
                TestFailure::exceptionToString($e)
1385
            );
1386
1387
            return;
1388
        }
1389
1390
        $this->fail();
1391
    }
1392
1393
    public function testConstraintIsNotNull2()
1394
    {
1395
        $constraint = Assert::logicalNot(
1396
            Assert::isNull()
1397
        );
1398
1399
        try {
1400
            $constraint->evaluate(null, 'custom message');
1401
        } catch (ExpectationFailedException $e) {
1402
            $this->assertEquals(
1403
                <<<EOF
1404
custom message
1405
Failed asserting that null is not null.
1406
1407
EOF
1408
                ,
1409
                TestFailure::exceptionToString($e)
1410
            );
1411
1412
            return;
1413
        }
1414
1415
        $this->fail();
1416
    }
1417
1418
    public function testConstraintLessThan()
1419
    {
1420
        $constraint = Assert::lessThan(1);
1421
1422
        $this->assertTrue($constraint->evaluate(0, '', true));
1423
        $this->assertFalse($constraint->evaluate(1, '', true));
1424
        $this->assertEquals('is less than 1', $constraint->toString());
1425
        $this->assertCount(1, $constraint);
1426
1427
        try {
1428
            $constraint->evaluate(1);
1429
        } catch (ExpectationFailedException $e) {
1430
            $this->assertEquals(
1431
                <<<EOF
1432
Failed asserting that 1 is less than 1.
1433
1434
EOF
1435
                ,
1436
                TestFailure::exceptionToString($e)
1437
            );
1438
1439
            return;
1440
        }
1441
1442
        $this->fail();
1443
    }
1444
1445
    public function testConstraintLessThan2()
1446
    {
1447
        $constraint = Assert::lessThan(1);
1448
1449
        try {
1450
            $constraint->evaluate(1, 'custom message');
1451
        } catch (ExpectationFailedException $e) {
1452
            $this->assertEquals(
1453
                <<<EOF
1454
custom message
1455
Failed asserting that 1 is less than 1.
1456
1457
EOF
1458
                ,
1459
                TestFailure::exceptionToString($e)
1460
            );
1461
1462
            return;
1463
        }
1464
1465
        $this->fail();
1466
    }
1467
1468
    public function testConstraintNotLessThan()
1469
    {
1470
        $constraint = Assert::logicalNot(
1471
            Assert::lessThan(1)
1472
        );
1473
1474
        $this->assertTrue($constraint->evaluate(1, '', true));
1475
        $this->assertFalse($constraint->evaluate(0, '', true));
1476
        $this->assertEquals('is not less than 1', $constraint->toString());
1477
        $this->assertCount(1, $constraint);
1478
1479
        try {
1480
            $constraint->evaluate(0);
1481
        } catch (ExpectationFailedException $e) {
1482
            $this->assertEquals(
1483
                <<<EOF
1484
Failed asserting that 0 is not less than 1.
1485
1486
EOF
1487
                ,
1488
                TestFailure::exceptionToString($e)
1489
            );
1490
1491
            return;
1492
        }
1493
1494
        $this->fail();
1495
    }
1496
1497
    public function testConstraintNotLessThan2()
1498
    {
1499
        $constraint = Assert::logicalNot(
1500
            Assert::lessThan(1)
1501
        );
1502
1503
        try {
1504
            $constraint->evaluate(0, 'custom message');
1505
        } catch (ExpectationFailedException $e) {
1506
            $this->assertEquals(
1507
                <<<EOF
1508
custom message
1509
Failed asserting that 0 is not less than 1.
1510
1511
EOF
1512
                ,
1513
                TestFailure::exceptionToString($e)
1514
            );
1515
1516
            return;
1517
        }
1518
1519
        $this->fail();
1520
    }
1521
1522
    public function testConstraintLessThanOrEqual()
1523
    {
1524
        $constraint = Assert::lessThanOrEqual(1);
1525
1526
        $this->assertTrue($constraint->evaluate(1, '', true));
1527
        $this->assertFalse($constraint->evaluate(2, '', true));
1528
        $this->assertEquals('is equal to 1 or is less than 1', $constraint->toString());
1529
        $this->assertCount(2, $constraint);
1530
1531
        try {
1532
            $constraint->evaluate(2);
1533
        } catch (ExpectationFailedException $e) {
1534
            $this->assertEquals(
1535
                <<<EOF
1536
Failed asserting that 2 is equal to 1 or is less than 1.
1537
1538
EOF
1539
                ,
1540
                TestFailure::exceptionToString($e)
1541
            );
1542
1543
            return;
1544
        }
1545
1546
        $this->fail();
1547
    }
1548
1549
    public function testConstraintCallback()
1550
    {
1551
        $closureReflect = function ($parameter) {
1552
            return $parameter;
1553
        };
1554
1555
        $closureWithoutParameter = function () {
1556
            return true;
1557
        };
1558
1559
        $constraint = Assert::callback($closureWithoutParameter);
1560
        $this->assertTrue($constraint->evaluate('', '', true));
1561
1562
        $constraint = Assert::callback($closureReflect);
1563
        $this->assertTrue($constraint->evaluate(true, '', true));
1564
        $this->assertFalse($constraint->evaluate(false, '', true));
1565
1566
        $callback   = [$this, 'callbackReturningTrue'];
1567
        $constraint = Assert::callback($callback);
1568
        $this->assertTrue($constraint->evaluate(false, '', true));
1569
1570
        $callback   = [self::class, 'staticCallbackReturningTrue'];
1571
        $constraint = Assert::callback($callback);
1572
        $this->assertTrue($constraint->evaluate(null, '', true));
1573
1574
        $this->assertEquals('is accepted by specified callback', $constraint->toString());
1575
    }
1576
1577
    public function testConstraintCallbackFailure()
1578
    {
1579
        $constraint = Assert::callback(function () {
1580
            return false;
1581
        });
1582
1583
        $this->expectException(ExpectationFailedException::class);
1584
        $this->expectExceptionMessage('Failed asserting that \'This fails\' is accepted by specified callback.');
1585
1586
        $constraint->evaluate('This fails');
1587
    }
1588
1589
    public function callbackReturningTrue()
1590
    {
1591
        return true;
1592
    }
1593
1594
    public static function staticCallbackReturningTrue()
1595
    {
1596
        return true;
1597
    }
1598
1599
    public function testConstraintLessThanOrEqual2()
1600
    {
1601
        $constraint = Assert::lessThanOrEqual(1);
1602
1603
        try {
1604
            $constraint->evaluate(2, 'custom message');
1605
        } catch (ExpectationFailedException $e) {
1606
            $this->assertEquals(
1607
                <<<EOF
1608
custom message
1609
Failed asserting that 2 is equal to 1 or is less than 1.
1610
1611
EOF
1612
                ,
1613
                TestFailure::exceptionToString($e)
1614
            );
1615
1616
            return;
1617
        }
1618
1619
        $this->fail();
1620
    }
1621
1622
    public function testConstraintNotLessThanOrEqual()
1623
    {
1624
        $constraint = Assert::logicalNot(
1625
            Assert::lessThanOrEqual(1)
1626
        );
1627
1628
        $this->assertTrue($constraint->evaluate(2, '', true));
1629
        $this->assertFalse($constraint->evaluate(1, '', true));
1630
        $this->assertEquals('not( is equal to 1 or is less than 1 )', $constraint->toString());
1631
        $this->assertCount(2, $constraint);
1632
1633
        try {
1634
            $constraint->evaluate(1);
1635
        } catch (ExpectationFailedException $e) {
1636
            $this->assertEquals(
1637
                <<<EOF
1638
Failed asserting that not( 1 is equal to 1 or is less than 1 ).
1639
1640
EOF
1641
                ,
1642
                TestFailure::exceptionToString($e)
1643
            );
1644
1645
            return;
1646
        }
1647
1648
        $this->fail();
1649
    }
1650
1651
    public function testConstraintNotLessThanOrEqual2()
1652
    {
1653
        $constraint = Assert::logicalNot(
1654
            Assert::lessThanOrEqual(1)
1655
        );
1656
1657
        try {
1658
            $constraint->evaluate(1, 'custom message');
1659
        } catch (ExpectationFailedException $e) {
1660
            $this->assertEquals(
1661
                <<<EOF
1662
custom message
1663
Failed asserting that not( 1 is equal to 1 or is less than 1 ).
1664
1665
EOF
1666
                ,
1667
                TestFailure::exceptionToString($e)
1668
            );
1669
1670
            return;
1671
        }
1672
1673
        $this->fail();
1674
    }
1675
1676
    public function testConstraintClassHasAttribute()
1677
    {
1678
        $constraint = Assert::classHasAttribute('privateAttribute');
1679
1680
        $this->assertTrue($constraint->evaluate(\ClassWithNonPublicAttributes::class, '', true));
1681
        $this->assertFalse($constraint->evaluate(\stdClass::class, '', true));
1682
        $this->assertEquals('has attribute "privateAttribute"', $constraint->toString());
1683
        $this->assertCount(1, $constraint);
1684
1685
        try {
1686
            $constraint->evaluate(\stdClass::class);
1687
        } catch (ExpectationFailedException $e) {
1688
            $this->assertEquals(
1689
                <<<EOF
1690
Failed asserting that class "stdClass" has attribute "privateAttribute".
1691
1692
EOF
1693
                ,
1694
                TestFailure::exceptionToString($e)
1695
            );
1696
1697
            return;
1698
        }
1699
1700
        $this->fail();
1701
    }
1702
1703
    public function testConstraintClassHasAttribute2()
1704
    {
1705
        $constraint = Assert::classHasAttribute('privateAttribute');
1706
1707
        try {
1708
            $constraint->evaluate(\stdClass::class, 'custom message');
1709
        } catch (ExpectationFailedException $e) {
1710
            $this->assertEquals(
1711
                <<<EOF
1712
custom message
1713
Failed asserting that class "stdClass" has attribute "privateAttribute".
1714
1715
EOF
1716
                ,
1717
                TestFailure::exceptionToString($e)
1718
            );
1719
1720
            return;
1721
        }
1722
1723
        $this->fail();
1724
    }
1725
1726
    public function testConstraintClassNotHasAttribute()
1727
    {
1728
        $constraint = Assert::logicalNot(
1729
            Assert::classHasAttribute('privateAttribute')
1730
        );
1731
1732
        $this->assertTrue($constraint->evaluate(\stdClass::class, '', true));
1733
        $this->assertFalse($constraint->evaluate(\ClassWithNonPublicAttributes::class, '', true));
1734
        $this->assertEquals('does not have attribute "privateAttribute"', $constraint->toString());
1735
        $this->assertCount(1, $constraint);
1736
1737
        try {
1738
            $constraint->evaluate(\ClassWithNonPublicAttributes::class);
1739
        } catch (ExpectationFailedException $e) {
1740
            $this->assertEquals(
1741
                <<<EOF
1742
Failed asserting that class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute".
1743
1744
EOF
1745
                ,
1746
                TestFailure::exceptionToString($e)
1747
            );
1748
1749
            return;
1750
        }
1751
1752
        $this->fail();
1753
    }
1754
1755
    public function testConstraintClassNotHasAttribute2()
1756
    {
1757
        $constraint = Assert::logicalNot(
1758
            Assert::classHasAttribute('privateAttribute')
1759
        );
1760
1761
        try {
1762
            $constraint->evaluate(\ClassWithNonPublicAttributes::class, 'custom message');
1763
        } catch (ExpectationFailedException $e) {
1764
            $this->assertEquals(
1765
                <<<EOF
1766
custom message
1767
Failed asserting that class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute".
1768
1769
EOF
1770
                ,
1771
                TestFailure::exceptionToString($e)
1772
            );
1773
1774
            return;
1775
        }
1776
1777
        $this->fail();
1778
    }
1779
1780
    public function testConstraintClassHasStaticAttribute()
1781
    {
1782
        $constraint = Assert::classHasStaticAttribute('privateStaticAttribute');
1783
1784
        $this->assertTrue($constraint->evaluate(\ClassWithNonPublicAttributes::class, '', true));
1785
        $this->assertFalse($constraint->evaluate(\stdClass::class, '', true));
1786
        $this->assertEquals('has static attribute "privateStaticAttribute"', $constraint->toString());
1787
        $this->assertCount(1, $constraint);
1788
1789
        try {
1790
            $constraint->evaluate(\stdClass::class);
1791
        } catch (ExpectationFailedException $e) {
1792
            $this->assertEquals(
1793
                <<<EOF
1794
Failed asserting that class "stdClass" has static attribute "privateStaticAttribute".
1795
1796
EOF
1797
                ,
1798
                TestFailure::exceptionToString($e)
1799
            );
1800
1801
            return;
1802
        }
1803
1804
        $this->fail();
1805
    }
1806
1807
    public function testConstraintClassHasStaticAttribute2()
1808
    {
1809
        $constraint = Assert::classHasStaticAttribute('foo');
1810
1811
        try {
1812
            $constraint->evaluate(\stdClass::class, 'custom message');
1813
        } catch (ExpectationFailedException $e) {
1814
            $this->assertEquals(
1815
                <<<EOF
1816
custom message
1817
Failed asserting that class "stdClass" has static attribute "foo".
1818
1819
EOF
1820
                ,
1821
                TestFailure::exceptionToString($e)
1822
            );
1823
1824
            return;
1825
        }
1826
1827
        $this->fail();
1828
    }
1829
1830
    public function testConstraintClassNotHasStaticAttribute()
1831
    {
1832
        $constraint = Assert::logicalNot(
1833
            Assert::classHasStaticAttribute('privateStaticAttribute')
1834
        );
1835
1836
        $this->assertTrue($constraint->evaluate(\stdClass::class, '', true));
1837
        $this->assertFalse($constraint->evaluate(\ClassWithNonPublicAttributes::class, '', true));
1838
        $this->assertEquals('does not have static attribute "privateStaticAttribute"', $constraint->toString());
1839
        $this->assertCount(1, $constraint);
1840
1841
        try {
1842
            $constraint->evaluate(\ClassWithNonPublicAttributes::class);
1843
        } catch (ExpectationFailedException $e) {
1844
            $this->assertEquals(
1845
                <<<EOF
1846
Failed asserting that class "ClassWithNonPublicAttributes" does not have static attribute "privateStaticAttribute".
1847
1848
EOF
1849
                ,
1850
                TestFailure::exceptionToString($e)
1851
            );
1852
1853
            return;
1854
        }
1855
1856
        $this->fail();
1857
    }
1858
1859
    public function testConstraintClassNotHasStaticAttribute2()
1860
    {
1861
        $constraint = Assert::logicalNot(
1862
            Assert::classHasStaticAttribute('privateStaticAttribute')
1863
        );
1864
1865
        try {
1866
            $constraint->evaluate(\ClassWithNonPublicAttributes::class, 'custom message');
1867
        } catch (ExpectationFailedException $e) {
1868
            $this->assertEquals(
1869
                <<<EOF
1870
custom message
1871
Failed asserting that class "ClassWithNonPublicAttributes" does not have static attribute "privateStaticAttribute".
1872
1873
EOF
1874
                ,
1875
                TestFailure::exceptionToString($e)
1876
            );
1877
1878
            return;
1879
        }
1880
1881
        $this->fail();
1882
    }
1883
1884
    public function testConstraintObjectHasAttribute()
1885
    {
1886
        $constraint = Assert::objectHasAttribute('privateAttribute');
1887
1888
        $this->assertTrue($constraint->evaluate(new \ClassWithNonPublicAttributes, '', true));
1889
        $this->assertFalse($constraint->evaluate(new \stdClass, '', true));
1890
        $this->assertEquals('has attribute "privateAttribute"', $constraint->toString());
1891
        $this->assertCount(1, $constraint);
1892
1893
        try {
1894
            $constraint->evaluate(new \stdClass);
1895
        } catch (ExpectationFailedException $e) {
1896
            $this->assertEquals(
1897
                <<<EOF
1898
Failed asserting that object of class "stdClass" has attribute "privateAttribute".
1899
1900
EOF
1901
                ,
1902
                TestFailure::exceptionToString($e)
1903
            );
1904
1905
            return;
1906
        }
1907
1908
        $this->fail();
1909
    }
1910
1911
    public function testConstraintObjectHasAttribute2()
1912
    {
1913
        $constraint = Assert::objectHasAttribute('privateAttribute');
1914
1915
        try {
1916
            $constraint->evaluate(new \stdClass, 'custom message');
1917
        } catch (ExpectationFailedException $e) {
1918
            $this->assertEquals(
1919
                <<<EOF
1920
custom message
1921
Failed asserting that object of class "stdClass" has attribute "privateAttribute".
1922
1923
EOF
1924
                ,
1925
                TestFailure::exceptionToString($e)
1926
            );
1927
1928
            return;
1929
        }
1930
1931
        $this->fail();
1932
    }
1933
1934
    public function testConstraintObjectNotHasAttribute()
1935
    {
1936
        $constraint = Assert::logicalNot(
1937
            Assert::objectHasAttribute('privateAttribute')
1938
        );
1939
1940
        $this->assertTrue($constraint->evaluate(new \stdClass, '', true));
1941
        $this->assertFalse($constraint->evaluate(new \ClassWithNonPublicAttributes, '', true));
1942
        $this->assertEquals('does not have attribute "privateAttribute"', $constraint->toString());
1943
        $this->assertCount(1, $constraint);
1944
1945
        try {
1946
            $constraint->evaluate(new \ClassWithNonPublicAttributes);
1947
        } catch (ExpectationFailedException $e) {
1948
            $this->assertEquals(
1949
                <<<EOF
1950
Failed asserting that object of class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute".
1951
1952
EOF
1953
                ,
1954
                TestFailure::exceptionToString($e)
1955
            );
1956
1957
            return;
1958
        }
1959
1960
        $this->fail();
1961
    }
1962
1963
    public function testConstraintObjectNotHasAttribute2()
1964
    {
1965
        $constraint = Assert::logicalNot(
1966
            Assert::objectHasAttribute('privateAttribute')
1967
        );
1968
1969
        try {
1970
            $constraint->evaluate(new \ClassWithNonPublicAttributes, 'custom message');
1971
        } catch (ExpectationFailedException $e) {
1972
            $this->assertEquals(
1973
                <<<EOF
1974
custom message
1975
Failed asserting that object of class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute".
1976
1977
EOF
1978
                ,
1979
                TestFailure::exceptionToString($e)
1980
            );
1981
1982
            return;
1983
        }
1984
1985
        $this->fail();
1986
    }
1987
1988
    public function testConstraintRegularExpression()
1989
    {
1990
        $constraint = Assert::matchesRegularExpression('/foo/');
1991
1992
        $this->assertFalse($constraint->evaluate('barbazbar', '', true));
1993
        $this->assertTrue($constraint->evaluate('barfoobar', '', true));
1994
        $this->assertEquals('matches PCRE pattern "/foo/"', $constraint->toString());
1995
        $this->assertCount(1, $constraint);
1996
1997
        try {
1998
            $constraint->evaluate('barbazbar');
1999
        } catch (ExpectationFailedException $e) {
2000
            $this->assertEquals(
2001
                <<<EOF
2002
Failed asserting that 'barbazbar' matches PCRE pattern "/foo/".
2003
2004
EOF
2005
                ,
2006
                TestFailure::exceptionToString($e)
2007
            );
2008
2009
            return;
2010
        }
2011
2012
        $this->fail();
2013
    }
2014
2015
    public function testConstraintRegularExpression2()
2016
    {
2017
        $constraint = Assert::matchesRegularExpression('/foo/');
2018
2019
        try {
2020
            $constraint->evaluate('barbazbar', 'custom message');
2021
        } catch (ExpectationFailedException $e) {
2022
            $this->assertEquals(
2023
                <<<EOF
2024
custom message
2025
Failed asserting that 'barbazbar' matches PCRE pattern "/foo/".
2026
2027
EOF
2028
                ,
2029
                TestFailure::exceptionToString($e)
2030
            );
2031
2032
            return;
2033
        }
2034
2035
        $this->fail();
2036
    }
2037
2038
    public function testConstraintPCRENotMatch()
2039
    {
2040
        $constraint = Assert::logicalNot(
2041
            Assert::matchesRegularExpression('/foo/')
2042
        );
2043
2044
        $this->assertTrue($constraint->evaluate('barbazbar', '', true));
2045
        $this->assertFalse($constraint->evaluate('barfoobar', '', true));
2046
        $this->assertEquals('does not match PCRE pattern "/foo/"', $constraint->toString());
2047
        $this->assertCount(1, $constraint);
2048
2049
        try {
2050
            $constraint->evaluate('barfoobar');
2051
        } catch (ExpectationFailedException $e) {
2052
            $this->assertEquals(
2053
                <<<EOF
2054
Failed asserting that 'barfoobar' does not match PCRE pattern "/foo/".
2055
2056
EOF
2057
                ,
2058
                TestFailure::exceptionToString($e)
2059
            );
2060
2061
            return;
2062
        }
2063
2064
        $this->fail();
2065
    }
2066
2067
    public function testConstraintPCRENotMatch2()
2068
    {
2069
        $constraint = Assert::logicalNot(
2070
            Assert::matchesRegularExpression('/foo/')
2071
        );
2072
2073
        try {
2074
            $constraint->evaluate('barfoobar', 'custom message');
2075
        } catch (ExpectationFailedException $e) {
2076
            $this->assertEquals(
2077
                <<<EOF
2078
custom message
2079
Failed asserting that 'barfoobar' does not match PCRE pattern "/foo/".
2080
2081
EOF
2082
                ,
2083
                TestFailure::exceptionToString($e)
2084
            );
2085
2086
            return;
2087
        }
2088
2089
        $this->fail();
2090
    }
2091
2092
    public function testConstraintStringMatches()
2093
    {
2094
        $constraint = Assert::matches('*%c*');
2095
        $this->assertFalse($constraint->evaluate('**', '', true));
2096
        $this->assertTrue($constraint->evaluate('***', '', true));
2097
        $this->assertEquals('matches PCRE pattern "/^\*.\*$/s"', $constraint->toString());
2098
        $this->assertCount(1, $constraint);
2099
    }
2100
2101
    public function testConstraintStringMatches2()
2102
    {
2103
        $constraint = Assert::matches('*%s*');
2104
        $this->assertFalse($constraint->evaluate('**', '', true));
2105
        $this->assertTrue($constraint->evaluate('***', '', true));
2106
        $this->assertEquals('matches PCRE pattern "/^\*[^\r\n]+\*$/s"', $constraint->toString());
2107
        $this->assertCount(1, $constraint);
2108
    }
2109
2110
    public function testConstraintStringMatches3()
2111
    {
2112
        $constraint = Assert::matches('*%i*');
2113
        $this->assertFalse($constraint->evaluate('**', '', true));
2114
        $this->assertTrue($constraint->evaluate('*0*', '', true));
2115
        $this->assertEquals('matches PCRE pattern "/^\*[+-]?\d+\*$/s"', $constraint->toString());
2116
        $this->assertCount(1, $constraint);
2117
    }
2118
2119
    public function testConstraintStringMatches4()
2120
    {
2121
        $constraint = Assert::matches('*%d*');
2122
        $this->assertFalse($constraint->evaluate('**', '', true));
2123
        $this->assertTrue($constraint->evaluate('*0*', '', true));
2124
        $this->assertEquals('matches PCRE pattern "/^\*\d+\*$/s"', $constraint->toString());
2125
        $this->assertCount(1, $constraint);
2126
    }
2127
2128
    public function testConstraintStringMatches5()
2129
    {
2130
        $constraint = Assert::matches('*%x*');
2131
        $this->assertFalse($constraint->evaluate('**', '', true));
2132
        $this->assertTrue($constraint->evaluate('*0f0f0f*', '', true));
2133
        $this->assertEquals('matches PCRE pattern "/^\*[0-9a-fA-F]+\*$/s"', $constraint->toString());
2134
        $this->assertCount(1, $constraint);
2135
    }
2136
2137
    public function testConstraintStringMatches6()
2138
    {
2139
        $constraint = Assert::matches('*%f*');
2140
        $this->assertFalse($constraint->evaluate('**', '', true));
2141
        $this->assertTrue($constraint->evaluate('*1.0*', '', true));
2142
        $this->assertEquals('matches PCRE pattern "/^\*[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?\*$/s"', $constraint->toString());
2143
        $this->assertCount(1, $constraint);
2144
    }
2145
2146
    public function testConstraintStringStartsWith()
2147
    {
2148
        $constraint = Assert::stringStartsWith('prefix');
2149
2150
        $this->assertFalse($constraint->evaluate('foo', '', true));
2151
        $this->assertTrue($constraint->evaluate('prefixfoo', '', true));
2152
        $this->assertEquals('starts with "prefix"', $constraint->toString());
2153
        $this->assertCount(1, $constraint);
2154
2155
        try {
2156
            $constraint->evaluate('foo');
2157
        } catch (ExpectationFailedException $e) {
2158
            $this->assertEquals(
2159
                <<<EOF
2160
Failed asserting that 'foo' starts with "prefix".
2161
2162
EOF
2163
                ,
2164
                TestFailure::exceptionToString($e)
2165
            );
2166
2167
            return;
2168
        }
2169
2170
        $this->fail();
2171
    }
2172
2173
    public function testConstraintStringStartsWith2()
2174
    {
2175
        $constraint = Assert::stringStartsWith('prefix');
2176
2177
        try {
2178
            $constraint->evaluate('foo', 'custom message');
2179
        } catch (ExpectationFailedException $e) {
2180
            $this->assertEquals(
2181
                <<<EOF
2182
custom message\nFailed asserting that 'foo' starts with "prefix".
2183
2184
EOF
2185
                ,
2186
                TestFailure::exceptionToString($e)
2187
            );
2188
2189
            return;
2190
        }
2191
2192
        $this->fail();
2193
    }
2194
2195
    public function testConstraintStringStartsNotWith()
2196
    {
2197
        $constraint = Assert::logicalNot(
2198
            Assert::stringStartsWith('prefix')
2199
        );
2200
2201
        $this->assertTrue($constraint->evaluate('foo', '', true));
2202
        $this->assertFalse($constraint->evaluate('prefixfoo', '', true));
2203
        $this->assertEquals('starts not with "prefix"', $constraint->toString());
2204
        $this->assertCount(1, $constraint);
2205
2206
        try {
2207
            $constraint->evaluate('prefixfoo');
2208
        } catch (ExpectationFailedException $e) {
2209
            $this->assertEquals(
2210
                <<<EOF
2211
Failed asserting that 'prefixfoo' starts not with "prefix".
2212
2213
EOF
2214
                ,
2215
                TestFailure::exceptionToString($e)
2216
            );
2217
2218
            return;
2219
        }
2220
2221
        $this->fail();
2222
    }
2223
2224
    public function testConstraintStringStartsNotWith2()
2225
    {
2226
        $constraint = Assert::logicalNot(
2227
            Assert::stringStartsWith('prefix')
2228
        );
2229
2230
        try {
2231
            $constraint->evaluate('prefixfoo', 'custom message');
2232
        } catch (ExpectationFailedException $e) {
2233
            $this->assertEquals(
2234
                <<<EOF
2235
custom message
2236
Failed asserting that 'prefixfoo' starts not with "prefix".
2237
2238
EOF
2239
                ,
2240
                TestFailure::exceptionToString($e)
2241
            );
2242
2243
            return;
2244
        }
2245
2246
        $this->fail();
2247
    }
2248
2249
    public function testConstraintStringContains()
2250
    {
2251
        $constraint = Assert::stringContains('foo');
2252
2253
        $this->assertFalse($constraint->evaluate('barbazbar', '', true));
2254
        $this->assertTrue($constraint->evaluate('barfoobar', '', true));
2255
        $this->assertEquals('contains "foo"', $constraint->toString());
2256
        $this->assertCount(1, $constraint);
2257
2258
        try {
2259
            $constraint->evaluate('barbazbar');
2260
        } catch (ExpectationFailedException $e) {
2261
            $this->assertEquals(
2262
                <<<EOF
2263
Failed asserting that 'barbazbar' contains "foo".
2264
2265
EOF
2266
                ,
2267
                TestFailure::exceptionToString($e)
2268
            );
2269
2270
            return;
2271
        }
2272
2273
        $this->fail();
2274
    }
2275
2276
    public function testConstraintStringContainsWhenIgnoreCase()
2277
    {
2278
        $constraint = Assert::stringContains('oryginał', true);
2279
2280
        $this->assertFalse($constraint->evaluate('oryginal', '', true));
2281
        $this->assertTrue($constraint->evaluate('ORYGINAŁ', '', true));
2282
        $this->assertTrue($constraint->evaluate('oryginał', '', true));
2283
        $this->assertEquals('contains "oryginał"', $constraint->toString());
2284
        $this->assertEquals(1, \count($constraint));
2285
2286
        $this->expectException(ExpectationFailedException::class);
2287
2288
        $constraint->evaluate('oryginal');
2289
    }
2290
2291
    public function testConstraintStringContainsForUtf8StringWhenNotIgnoreCase()
2292
    {
2293
        $constraint = Assert::stringContains('oryginał', false);
2294
2295
        $this->assertFalse($constraint->evaluate('oryginal', '', true));
2296
        $this->assertFalse($constraint->evaluate('ORYGINAŁ', '', true));
2297
        $this->assertTrue($constraint->evaluate('oryginał', '', true));
2298
        $this->assertEquals('contains "oryginał"', $constraint->toString());
2299
        $this->assertEquals(1, \count($constraint));
2300
2301
        $this->expectException(ExpectationFailedException::class);
2302
2303
        $constraint->evaluate('oryginal');
2304
    }
2305
2306
    public function testConstraintStringContains2()
2307
    {
2308
        $constraint = Assert::stringContains('foo');
2309
2310
        try {
2311
            $constraint->evaluate('barbazbar', 'custom message');
2312
        } catch (ExpectationFailedException $e) {
2313
            $this->assertEquals(
2314
                <<<EOF
2315
custom message
2316
Failed asserting that 'barbazbar' contains "foo".
2317
2318
EOF
2319
                ,
2320
                TestFailure::exceptionToString($e)
2321
            );
2322
2323
            return;
2324
        }
2325
2326
        $this->fail();
2327
    }
2328
2329
    public function testConstraintStringNotContains()
2330
    {
2331
        $constraint = Assert::logicalNot(
2332
            Assert::stringContains('foo')
2333
        );
2334
2335
        $this->assertTrue($constraint->evaluate('barbazbar', '', true));
2336
        $this->assertFalse($constraint->evaluate('barfoobar', '', true));
2337
        $this->assertEquals('does not contain "foo"', $constraint->toString());
2338
        $this->assertCount(1, $constraint);
2339
2340
        try {
2341
            $constraint->evaluate('barfoobar');
2342
        } catch (ExpectationFailedException $e) {
2343
            $this->assertEquals(
2344
                <<<EOF
2345
Failed asserting that 'barfoobar' does not contain "foo".
2346
2347
EOF
2348
                ,
2349
                TestFailure::exceptionToString($e)
2350
            );
2351
2352
            return;
2353
        }
2354
2355
        $this->fail();
2356
    }
2357
2358
    public function testConstraintStringNotContainsWhenIgnoreCase()
2359
    {
2360
        $constraint = Assert::logicalNot(
2361
            Assert::stringContains('oryginał')
2362
        );
2363
2364
        $this->assertTrue($constraint->evaluate('original', '', true));
2365
        $this->assertFalse($constraint->evaluate('ORYGINAŁ', '', true));
2366
        $this->assertFalse($constraint->evaluate('oryginał', '', true));
2367
        $this->assertEquals('does not contain "oryginał"', $constraint->toString());
2368
        $this->assertEquals(1, \count($constraint));
2369
2370
        $this->expectException(ExpectationFailedException::class);
2371
2372
        $constraint->evaluate('ORYGINAŁ');
2373
    }
2374
2375
    public function testConstraintStringNotContainsForUtf8StringWhenNotIgnoreCase()
2376
    {
2377
        $constraint = Assert::logicalNot(
2378
            Assert::stringContains('oryginał', false)
2379
        );
2380
2381
        $this->assertTrue($constraint->evaluate('original', '', true));
2382
        $this->assertTrue($constraint->evaluate('ORYGINAŁ', '', true));
2383
        $this->assertFalse($constraint->evaluate('oryginał', '', true));
2384
        $this->assertEquals('does not contain "oryginał"', $constraint->toString());
2385
        $this->assertEquals(1, \count($constraint));
2386
2387
        $this->expectException(ExpectationFailedException::class);
2388
2389
        $constraint->evaluate('oryginał');
2390
    }
2391
2392
    public function testConstraintStringNotContains2()
2393
    {
2394
        $constraint = Assert::logicalNot(
2395
            Assert::stringContains('foo')
2396
        );
2397
2398
        try {
2399
            $constraint->evaluate('barfoobar', 'custom message');
2400
        } catch (ExpectationFailedException $e) {
2401
            $this->assertEquals(
2402
                <<<EOF
2403
custom message
2404
Failed asserting that 'barfoobar' does not contain "foo".
2405
2406
EOF
2407
                ,
2408
                TestFailure::exceptionToString($e)
2409
            );
2410
2411
            return;
2412
        }
2413
2414
        $this->fail();
2415
    }
2416
2417
    public function testConstraintStringEndsWith()
2418
    {
2419
        $constraint = Assert::stringEndsWith('suffix');
2420
2421
        $this->assertFalse($constraint->evaluate('foo', '', true));
2422
        $this->assertTrue($constraint->evaluate('foosuffix', '', true));
2423
        $this->assertEquals('ends with "suffix"', $constraint->toString());
2424
        $this->assertCount(1, $constraint);
2425
2426
        try {
2427
            $constraint->evaluate('foo');
2428
        } catch (ExpectationFailedException $e) {
2429
            $this->assertEquals(
2430
                <<<EOF
2431
Failed asserting that 'foo' ends with "suffix".
2432
2433
EOF
2434
                ,
2435
                TestFailure::exceptionToString($e)
2436
            );
2437
2438
            return;
2439
        }
2440
2441
        $this->fail();
2442
    }
2443
2444
    public function testConstraintStringEndsWith2()
2445
    {
2446
        $constraint = Assert::stringEndsWith('suffix');
2447
2448
        try {
2449
            $constraint->evaluate('foo', 'custom message');
2450
        } catch (ExpectationFailedException $e) {
2451
            $this->assertEquals(
2452
                <<<EOF
2453
custom message
2454
Failed asserting that 'foo' ends with "suffix".
2455
2456
EOF
2457
                ,
2458
                TestFailure::exceptionToString($e)
2459
            );
2460
2461
            return;
2462
        }
2463
2464
        $this->fail();
2465
    }
2466
2467
    public function testConstraintStringEndsNotWith()
2468
    {
2469
        $constraint = Assert::logicalNot(
2470
            Assert::stringEndsWith('suffix')
2471
        );
2472
2473
        $this->assertTrue($constraint->evaluate('foo', '', true));
2474
        $this->assertFalse($constraint->evaluate('foosuffix', '', true));
2475
        $this->assertEquals('ends not with "suffix"', $constraint->toString());
2476
        $this->assertCount(1, $constraint);
2477
2478
        try {
2479
            $constraint->evaluate('foosuffix');
2480
        } catch (ExpectationFailedException $e) {
2481
            $this->assertEquals(
2482
                <<<EOF
2483
Failed asserting that 'foosuffix' ends not with "suffix".
2484
2485
EOF
2486
                ,
2487
                TestFailure::exceptionToString($e)
2488
            );
2489
2490
            return;
2491
        }
2492
2493
        $this->fail();
2494
    }
2495
2496
    public function testConstraintStringEndsNotWith2()
2497
    {
2498
        $constraint = Assert::logicalNot(
2499
            Assert::stringEndsWith('suffix')
2500
        );
2501
2502
        try {
2503
            $constraint->evaluate('foosuffix', 'custom message');
2504
        } catch (ExpectationFailedException $e) {
2505
            $this->assertEquals(
2506
                <<<EOF
2507
custom message
2508
Failed asserting that 'foosuffix' ends not with "suffix".
2509
2510
EOF
2511
                ,
2512
                TestFailure::exceptionToString($e)
2513
            );
2514
2515
            return;
2516
        }
2517
2518
        $this->fail();
2519
    }
2520
2521
    public function testConstraintArrayContainsCheckForObjectIdentity()
2522
    {
2523
        // Check for primitive type.
2524
        $constraint = new TraversableContains('foo', true, true);
2525
2526
        $this->assertFalse($constraint->evaluate([0], '', true));
2527
        $this->assertFalse($constraint->evaluate([true], '', true));
2528
2529
        // Default case.
2530
        $constraint = new TraversableContains('foo');
2531
2532
        $this->assertTrue($constraint->evaluate([0], '', true));
2533
        $this->assertTrue($constraint->evaluate([true], '', true));
2534
    }
2535
2536
    public function testConstraintArrayContains()
2537
    {
2538
        $constraint = new TraversableContains('foo');
2539
2540
        $this->assertFalse($constraint->evaluate(['bar'], '', true));
2541
        $this->assertTrue($constraint->evaluate(['foo'], '', true));
2542
        $this->assertEquals("contains 'foo'", $constraint->toString());
2543
        $this->assertCount(1, $constraint);
2544
2545
        try {
2546
            $constraint->evaluate(['bar']);
2547
        } catch (ExpectationFailedException $e) {
2548
            $this->assertEquals(
2549
                <<<EOF
2550
Failed asserting that an array contains 'foo'.
2551
2552
EOF
2553
                ,
2554
                TestFailure::exceptionToString($e)
2555
            );
2556
2557
            return;
2558
        }
2559
2560
        $this->fail();
2561
    }
2562
2563
    public function testConstraintArrayContains2()
2564
    {
2565
        $constraint = new TraversableContains('foo');
2566
2567
        try {
2568
            $constraint->evaluate(['bar'], 'custom message');
2569
        } catch (ExpectationFailedException $e) {
2570
            $this->assertEquals(
2571
                <<<EOF
2572
custom message
2573
Failed asserting that an array contains 'foo'.
2574
2575
EOF
2576
                ,
2577
                TestFailure::exceptionToString($e)
2578
            );
2579
2580
            return;
2581
        }
2582
2583
        $this->fail();
2584
    }
2585
2586
    public function testConstraintArrayNotContains()
2587
    {
2588
        $constraint = Assert::logicalNot(
2589
            new TraversableContains('foo')
2590
        );
2591
2592
        $this->assertTrue($constraint->evaluate(['bar'], '', true));
2593
        $this->assertFalse($constraint->evaluate(['foo'], '', true));
2594
        $this->assertEquals("does not contain 'foo'", $constraint->toString());
2595
        $this->assertCount(1, $constraint);
2596
2597
        try {
2598
            $constraint->evaluate(['foo']);
2599
        } catch (ExpectationFailedException $e) {
2600
            $this->assertEquals(
2601
                <<<EOF
2602
Failed asserting that an array does not contain 'foo'.
2603
2604
EOF
2605
                ,
2606
                TestFailure::exceptionToString($e)
2607
            );
2608
2609
            return;
2610
        }
2611
2612
        $this->fail();
2613
    }
2614
2615
    public function testConstraintArrayNotContains2()
2616
    {
2617
        $constraint = Assert::logicalNot(
2618
            new TraversableContains('foo')
2619
        );
2620
2621
        try {
2622
            $constraint->evaluate(['foo'], 'custom message');
2623
        } catch (ExpectationFailedException $e) {
2624
            $this->assertEquals(
2625
                <<<EOF
2626
custom message
2627
Failed asserting that an array does not contain 'foo'.
2628
2629
EOF
2630
                ,
2631
                TestFailure::exceptionToString($e)
2632
            );
2633
2634
            return;
2635
        }
2636
2637
        $this->fail();
2638
    }
2639
2640
    public function testConstraintSplObjectStorageContains()
2641
    {
2642
        $object     = new \stdClass;
2643
        $constraint = new TraversableContains($object);
2644
        $this->assertStringMatchesFormat('contains stdClass Object &%s ()', $constraint->toString());
2645
2646
        $storage = new \SplObjectStorage;
2647
        $this->assertFalse($constraint->evaluate($storage, '', true));
2648
2649
        $storage->attach($object);
2650
        $this->assertTrue($constraint->evaluate($storage, '', true));
2651
2652
        try {
2653
            $constraint->evaluate(new \SplObjectStorage);
2654
        } catch (ExpectationFailedException $e) {
2655
            $this->assertStringMatchesFormat(
2656
                <<<EOF
2657
Failed asserting that a traversable contains stdClass Object &%x ().
2658
2659
EOF
2660
                ,
2661
                TestFailure::exceptionToString($e)
2662
            );
2663
2664
            return;
2665
        }
2666
2667
        $this->fail();
2668
    }
2669
2670
    public function testConstraintSplObjectStorageContains2()
2671
    {
2672
        $object     = new \stdClass;
2673
        $constraint = new TraversableContains($object);
2674
2675
        try {
2676
            $constraint->evaluate(new \SplObjectStorage, 'custom message');
2677
        } catch (ExpectationFailedException $e) {
2678
            $this->assertStringMatchesFormat(
2679
                <<<EOF
2680
custom message
2681
Failed asserting that a traversable contains stdClass Object &%x ().
2682
2683
EOF
2684
                ,
2685
                TestFailure::exceptionToString($e)
2686
            );
2687
2688
            return;
2689
        }
2690
2691
        $this->fail();
2692
    }
2693
2694
    public function testAttributeEqualTo()
2695
    {
2696
        $object     = new \ClassWithNonPublicAttributes;
2697
        $constraint = Assert::attributeEqualTo('foo', 1);
2698
2699
        $this->assertTrue($constraint->evaluate($object, '', true));
2700
        $this->assertEquals('attribute "foo" is equal to 1', $constraint->toString());
2701
        $this->assertCount(1, $constraint);
2702
2703
        $constraint = Assert::attributeEqualTo('foo', 2);
2704
2705
        $this->assertFalse($constraint->evaluate($object, '', true));
2706
2707
        try {
2708
            $constraint->evaluate($object);
2709
        } catch (ExpectationFailedException $e) {
2710
            $this->assertEquals(
2711
                <<<EOF
2712
Failed asserting that attribute "foo" is equal to 2.
2713
2714
EOF
2715
                ,
2716
                TestFailure::exceptionToString($e)
2717
            );
2718
2719
            return;
2720
        }
2721
2722
        $this->fail();
2723
    }
2724
2725
    public function testAttributeEqualTo2()
2726
    {
2727
        $object     = new \ClassWithNonPublicAttributes;
2728
        $constraint = Assert::attributeEqualTo('foo', 2);
2729
2730
        try {
2731
            $constraint->evaluate($object, 'custom message');
2732
        } catch (ExpectationFailedException $e) {
2733
            $this->assertEquals(
2734
                <<<EOF
2735
custom message\nFailed asserting that attribute "foo" is equal to 2.
2736
2737
EOF
2738
                ,
2739
                TestFailure::exceptionToString($e)
2740
            );
2741
2742
            return;
2743
        }
2744
2745
        $this->fail();
2746
    }
2747
2748
    public function testAttributeNotEqualTo()
2749
    {
2750
        $object     = new \ClassWithNonPublicAttributes;
2751
        $constraint = Assert::logicalNot(
2752
            Assert::attributeEqualTo('foo', 2)
2753
        );
2754
2755
        $this->assertTrue($constraint->evaluate($object, '', true));
2756
        $this->assertEquals('attribute "foo" is not equal to 2', $constraint->toString());
2757
        $this->assertCount(1, $constraint);
2758
2759
        $constraint = Assert::logicalNot(
2760
            Assert::attributeEqualTo('foo', 1)
2761
        );
2762
2763
        $this->assertFalse($constraint->evaluate($object, '', true));
2764
2765
        try {
2766
            $constraint->evaluate($object);
2767
        } catch (ExpectationFailedException $e) {
2768
            $this->assertEquals(
2769
                <<<EOF
2770
Failed asserting that attribute "foo" is not equal to 1.
2771
2772
EOF
2773
                ,
2774
                TestFailure::exceptionToString($e)
2775
            );
2776
2777
            return;
2778
        }
2779
2780
        $this->fail();
2781
    }
2782
2783
    public function testAttributeNotEqualTo2()
2784
    {
2785
        $object     = new \ClassWithNonPublicAttributes;
2786
        $constraint = Assert::logicalNot(
2787
            Assert::attributeEqualTo('foo', 1)
2788
        );
2789
2790
        try {
2791
            $constraint->evaluate($object, 'custom message');
2792
        } catch (ExpectationFailedException $e) {
2793
            $this->assertEquals(
2794
                <<<EOF
2795
custom message\nFailed asserting that attribute "foo" is not equal to 1.
2796
2797
EOF
2798
                ,
2799
                TestFailure::exceptionToString($e)
2800
            );
2801
2802
            return;
2803
        }
2804
2805
        $this->fail();
2806
    }
2807
2808
    public function testConstraintIsEmpty()
2809
    {
2810
        $constraint = new IsEmpty;
2811
2812
        $this->assertFalse($constraint->evaluate(['foo'], '', true));
2813
        $this->assertTrue($constraint->evaluate([], '', true));
2814
        $this->assertFalse($constraint->evaluate(new \ArrayObject(['foo']), '', true));
2815
        $this->assertTrue($constraint->evaluate(new \ArrayObject([]), '', true));
2816
        $this->assertEquals('is empty', $constraint->toString());
2817
        $this->assertCount(1, $constraint);
2818
2819
        try {
2820
            $constraint->evaluate(['foo']);
2821
        } catch (ExpectationFailedException $e) {
2822
            $this->assertEquals(
2823
                <<<EOF
2824
Failed asserting that an array is empty.
2825
2826
EOF
2827
                ,
2828
                TestFailure::exceptionToString($e)
2829
            );
2830
2831
            return;
2832
        }
2833
2834
        $this->fail();
2835
    }
2836
2837
    public function testConstraintIsEmpty2()
2838
    {
2839
        $constraint = new IsEmpty;
2840
2841
        try {
2842
            $constraint->evaluate(['foo'], 'custom message');
2843
        } catch (ExpectationFailedException $e) {
2844
            $this->assertEquals(
2845
                <<<EOF
2846
custom message\nFailed asserting that an array is empty.
2847
2848
EOF
2849
                ,
2850
                TestFailure::exceptionToString($e)
2851
            );
2852
2853
            return;
2854
        }
2855
2856
        $this->fail();
2857
    }
2858
2859
    public function testConstraintCountWithAnArray()
2860
    {
2861
        $constraint = new Count(5);
2862
2863
        $this->assertTrue($constraint->evaluate([1, 2, 3, 4, 5], '', true));
2864
        $this->assertFalse($constraint->evaluate([1, 2, 3, 4], '', true));
2865
    }
2866
2867
    public function testConstraintCountWithAnIteratorWhichDoesNotImplementCountable()
2868
    {
2869
        $constraint = new Count(5);
2870
2871
        $this->assertTrue($constraint->evaluate(new \TestIterator([1, 2, 3, 4, 5]), '', true));
2872
        $this->assertFalse($constraint->evaluate(new \TestIterator([1, 2, 3, 4]), '', true));
2873
    }
2874
2875
    public function testConstraintCountWithAnObjectImplementingCountable()
2876
    {
2877
        $constraint = new Count(5);
2878
2879
        $this->assertTrue($constraint->evaluate(new \ArrayObject([1, 2, 3, 4, 5]), '', true));
2880
        $this->assertFalse($constraint->evaluate(new \ArrayObject([1, 2, 3, 4]), '', true));
2881
    }
2882
2883
    public function testConstraintCountFailing()
2884
    {
2885
        $constraint = new Count(5);
2886
2887
        try {
2888
            $constraint->evaluate([1, 2]);
2889
        } catch (ExpectationFailedException $e) {
2890
            $this->assertEquals(
2891
                <<<EOF
2892
Failed asserting that actual size 2 matches expected size 5.
2893
2894
EOF
2895
                ,
2896
                TestFailure::exceptionToString($e)
2897
            );
2898
2899
            return;
2900
        }
2901
2902
        $this->fail();
2903
    }
2904
2905
    public function testConstraintNotCountFailing()
2906
    {
2907
        $constraint = Assert::logicalNot(
2908
            new Count(2)
2909
        );
2910
2911
        try {
2912
            $constraint->evaluate([1, 2]);
2913
        } catch (ExpectationFailedException $e) {
2914
            $this->assertEquals(
2915
                <<<EOF
2916
Failed asserting that actual size 2 does not match expected size 2.
2917
2918
EOF
2919
                ,
2920
                TestFailure::exceptionToString($e)
2921
            );
2922
2923
            return;
2924
        }
2925
2926
        $this->fail();
2927
    }
2928
2929
    public function testConstraintSameSizeWithAnArray()
2930
    {
2931
        $constraint = new SameSize([1, 2, 3, 4, 5]);
2932
2933
        $this->assertTrue($constraint->evaluate([6, 7, 8, 9, 10], '', true));
2934
        $this->assertFalse($constraint->evaluate([1, 2, 3, 4], '', true));
2935
    }
2936
2937
    public function testConstraintSameSizeWithAnIteratorWhichDoesNotImplementCountable()
2938
    {
2939
        $constraint = new SameSize(new \TestIterator([1, 2, 3, 4, 5]));
2940
2941
        $this->assertTrue($constraint->evaluate(new \TestIterator([6, 7, 8, 9, 10]), '', true));
2942
        $this->assertFalse($constraint->evaluate(new \TestIterator([1, 2, 3, 4]), '', true));
2943
    }
2944
2945
    public function testConstraintSameSizeWithAnObjectImplementingCountable()
2946
    {
2947
        $constraint = new SameSize(new \ArrayObject([1, 2, 3, 4, 5]));
2948
2949
        $this->assertTrue($constraint->evaluate(new \ArrayObject([6, 7, 8, 9, 10]), '', true));
2950
        $this->assertFalse($constraint->evaluate(new \ArrayObject([1, 2, 3, 4]), '', true));
2951
    }
2952
2953
    public function testConstraintSameSizeFailing()
2954
    {
2955
        $constraint = new SameSize([1, 2, 3, 4, 5]);
2956
2957
        try {
2958
            $constraint->evaluate([1, 2]);
2959
        } catch (ExpectationFailedException $e) {
2960
            $this->assertEquals(
2961
                <<<EOF
2962
Failed asserting that actual size 2 matches expected size 5.
2963
2964
EOF
2965
                ,
2966
                TestFailure::exceptionToString($e)
2967
            );
2968
2969
            return;
2970
        }
2971
2972
        $this->fail();
2973
    }
2974
2975
    public function testConstraintNotSameSizeFailing()
2976
    {
2977
        $constraint = Assert::logicalNot(
2978
            new SameSize([1, 2])
2979
        );
2980
2981
        try {
2982
            $constraint->evaluate([3, 4]);
2983
        } catch (ExpectationFailedException $e) {
2984
            $this->assertEquals(
2985
                <<<EOF
2986
Failed asserting that actual size 2 does not match expected size 2.
2987
2988
EOF
2989
                ,
2990
                TestFailure::exceptionToString($e)
2991
            );
2992
2993
            return;
2994
        }
2995
2996
        $this->fail();
2997
    }
2998
2999
    public function testConstraintException()
3000
    {
3001
        $constraint = new Constraint\Exception('FoobarException');
3002
        $exception  = new \DummyException('Test');
3003
        $stackTrace = Filter::getFilteredStacktrace($exception);
3004
3005
        try {
3006
            $constraint->evaluate($exception);
3007
        } catch (ExpectationFailedException $e) {
3008
            $this->assertEquals(
3009
                <<<EOF
3010
Failed asserting that exception of type "DummyException" matches expected exception "FoobarException". Message was: "Test" at
3011
$stackTrace.
3012
3013
EOF
3014
                ,
3015
                TestFailure::exceptionToString($e)
3016
            );
3017
3018
            return;
3019
        }
3020
3021
        $this->fail();
3022
    }
3023
3024
    /**
3025
     * Removes spaces in front of newlines
3026
     *
3027
     * @param string $string
3028
     *
3029
     * @return string
3030
     */
3031
    private function trimnl($string)
3032
    {
3033
        return \preg_replace('/[ ]*\n/', "\n", $string);
3034
    }
3035
}
3036