ConstraintTest::testConstraintArrayNotContains()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 0
dl 0
loc 27
rs 9.7333
c 0
b 0
f 0
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
namespace PHPUnit\Framework;
11
12
use PHPUnit\Framework\Constraint\Count;
13
use PHPUnit\Framework\Constraint\SameSize;
14
use PHPUnit\Framework\Constraint\TraversableContains;
15
use PHPUnit\Util\Filter;
16
17
class ConstraintTest extends TestCase
18
{
19
    public function testConstraintArrayNotHasKey(): void
20
    {
21
        $constraint = Assert::logicalNot(
22
            Assert::arrayHasKey(0)
23
        );
24
25
        $this->assertFalse($constraint->evaluate([0 => 1], '', true));
26
        $this->assertEquals('does not have the key 0', $constraint->toString());
27
        $this->assertCount(1, $constraint);
28
29
        try {
30
            $constraint->evaluate([0 => 1]);
31
        } catch (ExpectationFailedException $e) {
32
            $this->assertEquals(
33
                <<<EOF
34
Failed asserting that an array does not have 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 testConstraintArrayNotHasKey2(): void
48
    {
49
        $constraint = Assert::logicalNot(
50
            Assert::arrayHasKey(0)
51
        );
52
53
        try {
54
            $constraint->evaluate([0], 'custom message');
55
        } catch (ExpectationFailedException $e) {
56
            $this->assertEquals(
57
                <<<EOF
58
custom message
59
Failed asserting that an array does not have the key 0.
60
61
EOF
62
                ,
63
                TestFailure::exceptionToString($e)
64
            );
65
66
            return;
67
        }
68
69
        $this->fail();
70
    }
71
72
    public function testConstraintFileNotExists(): void
73
    {
74
        $file = TEST_FILES_PATH . 'ClassWithNonPublicAttributes.php';
75
76
        $constraint = Assert::logicalNot(
77
            Assert::fileExists()
78
        );
79
80
        $this->assertFalse($constraint->evaluate($file, '', true));
81
        $this->assertEquals('file does not exist', $constraint->toString());
82
        $this->assertCount(1, $constraint);
83
84
        try {
85
            $constraint->evaluate($file);
86
        } catch (ExpectationFailedException $e) {
87
            $this->assertEquals(
88
                <<<EOF
89
Failed asserting that file "$file" does not exist.
90
91
EOF
92
                ,
93
                TestFailure::exceptionToString($e)
94
            );
95
96
            return;
97
        }
98
99
        $this->fail();
100
    }
101
102
    public function testConstraintFileNotExists2(): void
103
    {
104
        $file = TEST_FILES_PATH . 'ClassWithNonPublicAttributes.php';
105
106
        $constraint = Assert::logicalNot(
107
            Assert::fileExists()
108
        );
109
110
        try {
111
            $constraint->evaluate($file, 'custom message');
112
        } catch (ExpectationFailedException $e) {
113
            $this->assertEquals(
114
                <<<EOF
115
custom message
116
Failed asserting that file "$file" does not exist.
117
118
EOF
119
                ,
120
                TestFailure::exceptionToString($e)
121
            );
122
123
            return;
124
        }
125
126
        $this->fail();
127
    }
128
129
    public function testConstraintNotGreaterThan(): void
130
    {
131
        $constraint = Assert::logicalNot(
132
            Assert::greaterThan(1)
133
        );
134
135
        $this->assertTrue($constraint->evaluate(1, '', true));
136
        $this->assertEquals('is not greater than 1', $constraint->toString());
137
        $this->assertCount(1, $constraint);
138
139
        try {
140
            $constraint->evaluate(2);
141
        } catch (ExpectationFailedException $e) {
142
            $this->assertEquals(
143
                <<<EOF
144
Failed asserting that 2 is not greater than 1.
145
146
EOF
147
                ,
148
                TestFailure::exceptionToString($e)
149
            );
150
151
            return;
152
        }
153
154
        $this->fail();
155
    }
156
157
    public function testConstraintNotGreaterThan2(): void
158
    {
159
        $constraint = Assert::logicalNot(
160
            Assert::greaterThan(1)
161
        );
162
163
        try {
164
            $constraint->evaluate(2, 'custom message');
165
        } catch (ExpectationFailedException $e) {
166
            $this->assertEquals(
167
                <<<EOF
168
custom message
169
Failed asserting that 2 is not greater than 1.
170
171
EOF
172
                ,
173
                TestFailure::exceptionToString($e)
174
            );
175
176
            return;
177
        }
178
179
        $this->fail();
180
    }
181
182
    public function testConstraintGreaterThanOrEqual(): void
183
    {
184
        $constraint = Assert::greaterThanOrEqual(1);
185
186
        $this->assertTrue($constraint->evaluate(1, '', true));
187
        $this->assertFalse($constraint->evaluate(0, '', true));
188
        $this->assertEquals('is equal to 1 or is greater than 1', $constraint->toString());
189
        $this->assertCount(2, $constraint);
190
191
        try {
192
            $constraint->evaluate(0);
193
        } catch (ExpectationFailedException $e) {
194
            $this->assertEquals(
195
                <<<EOF
196
Failed asserting that 0 is equal to 1 or is greater than 1.
197
198
EOF
199
                ,
200
                TestFailure::exceptionToString($e)
201
            );
202
203
            return;
204
        }
205
206
        $this->fail();
207
    }
208
209
    public function testConstraintGreaterThanOrEqual2(): void
210
    {
211
        $constraint = Assert::greaterThanOrEqual(1);
212
213
        try {
214
            $constraint->evaluate(0, 'custom message');
215
        } catch (ExpectationFailedException $e) {
216
            $this->assertEquals(
217
                <<<EOF
218
custom message
219
Failed asserting that 0 is equal to 1 or is greater than 1.
220
221
EOF
222
                ,
223
                TestFailure::exceptionToString($e)
224
            );
225
226
            return;
227
        }
228
229
        $this->fail();
230
    }
231
232
    public function testConstraintNotGreaterThanOrEqual(): void
233
    {
234
        $constraint = Assert::logicalNot(
235
            Assert::greaterThanOrEqual(1)
236
        );
237
238
        $this->assertFalse($constraint->evaluate(1, '', true));
239
        $this->assertEquals('not( is equal to 1 or is greater than 1 )', $constraint->toString());
240
        $this->assertCount(2, $constraint);
241
242
        try {
243
            $constraint->evaluate(1);
244
        } catch (ExpectationFailedException $e) {
245
            $this->assertEquals(
246
                <<<EOF
247
Failed asserting that not( 1 is equal to 1 or is greater than 1 ).
248
249
EOF
250
                ,
251
                TestFailure::exceptionToString($e)
252
            );
253
254
            return;
255
        }
256
257
        $this->fail();
258
    }
259
260
    public function testConstraintNotGreaterThanOrEqual2(): void
261
    {
262
        $constraint = Assert::logicalNot(
263
            Assert::greaterThanOrEqual(1)
264
        );
265
266
        try {
267
            $constraint->evaluate(1, 'custom message');
268
        } catch (ExpectationFailedException $e) {
269
            $this->assertEquals(
270
                <<<EOF
271
custom message
272
Failed asserting that not( 1 is equal to 1 or is greater than 1 ).
273
274
EOF
275
                ,
276
                TestFailure::exceptionToString($e)
277
            );
278
279
            return;
280
        }
281
282
        $this->fail();
283
    }
284
285
    public function testConstraintIsAnything(): void
286
    {
287
        $constraint = Assert::anything();
288
289
        $this->assertTrue($constraint->evaluate(null, '', true));
290
        $this->assertNull($constraint->evaluate(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $constraint->evaluate(null) targeting PHPUnit\Framework\Constr...\IsAnything::evaluate() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
291
        $this->assertEquals('is anything', $constraint->toString());
292
        $this->assertCount(0, $constraint);
293
    }
294
295
    public function testConstraintNotIsAnything(): void
296
    {
297
        $constraint = Assert::logicalNot(
298
            Assert::anything()
299
        );
300
301
        $this->assertFalse($constraint->evaluate(null, '', true));
302
        $this->assertEquals('is not anything', $constraint->toString());
303
        $this->assertCount(0, $constraint);
304
305
        try {
306
            $constraint->evaluate(null);
307
        } catch (ExpectationFailedException $e) {
308
            $this->assertEquals(
309
                <<<EOF
310
Failed asserting that null is not anything.
311
312
EOF
313
                ,
314
                TestFailure::exceptionToString($e)
315
            );
316
317
            return;
318
        }
319
320
        $this->fail();
321
    }
322
323
    public function testConstraintIsNotEqual(): void
324
    {
325
        $constraint = Assert::logicalNot(
326
            Assert::equalTo(1)
327
        );
328
329
        $this->assertTrue($constraint->evaluate(0, '', true));
330
        $this->assertFalse($constraint->evaluate(1, '', true));
331
        $this->assertEquals('is not equal to 1', $constraint->toString());
332
        $this->assertCount(1, $constraint);
333
334
        try {
335
            $constraint->evaluate(1);
336
        } catch (ExpectationFailedException $e) {
337
            $this->assertEquals(
338
                <<<EOF
339
Failed asserting that 1 is not equal to 1.
340
341
EOF
342
                ,
343
                TestFailure::exceptionToString($e)
344
            );
345
346
            return;
347
        }
348
349
        $this->fail();
350
    }
351
352
    public function testConstraintIsNotEqual2(): void
353
    {
354
        $constraint = Assert::logicalNot(
355
            Assert::equalTo(1)
356
        );
357
358
        try {
359
            $constraint->evaluate(1, 'custom message');
360
        } catch (ExpectationFailedException $e) {
361
            $this->assertEquals(
362
                <<<EOF
363
custom message
364
Failed asserting that 1 is not equal to 1.
365
366
EOF
367
                ,
368
                TestFailure::exceptionToString($e)
369
            );
370
371
            return;
372
        }
373
374
        $this->fail();
375
    }
376
377
    public function testConstraintIsNotIdentical(): void
378
    {
379
        $a = new \stdClass;
380
        $b = new \stdClass;
381
382
        $constraint = Assert::logicalNot(
383
            Assert::identicalTo($a)
384
        );
385
386
        $this->assertTrue($constraint->evaluate($b, '', true));
387
        $this->assertFalse($constraint->evaluate($a, '', true));
388
        $this->assertEquals('is not identical to an object of class "stdClass"', $constraint->toString());
389
        $this->assertCount(1, $constraint);
390
391
        try {
392
            $constraint->evaluate($a);
393
        } catch (ExpectationFailedException $e) {
394
            $this->assertEquals(
395
                <<<EOF
396
Failed asserting that two variables don't reference the same object.
397
398
EOF
399
                ,
400
                $this->trimnl(TestFailure::exceptionToString($e))
401
            );
402
403
            return;
404
        }
405
406
        $this->fail();
407
    }
408
409
    public function testConstraintIsNotIdentical2(): void
410
    {
411
        $a = new \stdClass;
412
413
        $constraint = Assert::logicalNot(
414
            Assert::identicalTo($a)
415
        );
416
417
        try {
418
            $constraint->evaluate($a, 'custom message');
419
        } catch (ExpectationFailedException $e) {
420
            $this->assertEquals(
421
                <<<EOF
422
custom message
423
Failed asserting that two variables don't reference the same object.
424
425
EOF
426
                ,
427
                TestFailure::exceptionToString($e)
428
            );
429
430
            return;
431
        }
432
433
        $this->fail();
434
    }
435
436
    public function testConstraintIsNotIdentical3(): void
437
    {
438
        $constraint = Assert::logicalNot(
439
            Assert::identicalTo('a')
440
        );
441
442
        try {
443
            $constraint->evaluate('a', 'custom message');
444
        } catch (ExpectationFailedException $e) {
445
            $this->assertEquals(
446
                <<<EOF
447
custom message
448
Failed asserting that two strings are not identical.
449
450
EOF
451
                ,
452
                $this->trimnl(TestFailure::exceptionToString($e))
453
            );
454
455
            return;
456
        }
457
458
        $this->fail();
459
    }
460
461
    public function testConstraintIsInstanceOf(): void
462
    {
463
        $constraint = Assert::isInstanceOf(\Exception::class);
464
465
        $this->assertFalse($constraint->evaluate(new \stdClass, '', true));
466
        $this->assertTrue($constraint->evaluate(new \Exception, '', true));
467
        $this->assertEquals('is instance of class "Exception"', $constraint->toString());
468
        $this->assertCount(1, $constraint);
469
470
        $interfaceConstraint = Assert::isInstanceOf(\Countable::class);
471
        $this->assertFalse($interfaceConstraint->evaluate(new \stdClass, '', true));
472
        $this->assertTrue($interfaceConstraint->evaluate(new \ArrayObject, '', true));
473
        $this->assertEquals('is instance of interface "Countable"', $interfaceConstraint->toString());
474
475
        try {
476
            $constraint->evaluate(new \stdClass);
477
        } catch (ExpectationFailedException $e) {
478
            $this->assertEquals(
479
                <<<EOF
480
Failed asserting that stdClass Object () is an instance of class "Exception".
481
482
EOF
483
                ,
484
                TestFailure::exceptionToString($e)
485
            );
486
487
            return;
488
        }
489
490
        $this->fail();
491
    }
492
493
    public function testConstraintIsInstanceOf2(): void
494
    {
495
        $constraint = Assert::isInstanceOf(\Exception::class);
496
497
        try {
498
            $constraint->evaluate(new \stdClass, 'custom message');
499
        } catch (ExpectationFailedException $e) {
500
            $this->assertEquals(
501
                <<<EOF
502
custom message
503
Failed asserting that stdClass Object () is an instance of class "Exception".
504
505
EOF
506
                ,
507
                TestFailure::exceptionToString($e)
508
            );
509
510
            return;
511
        }
512
513
        $this->fail();
514
    }
515
516
    public function testConstraintIsNotInstanceOf(): void
517
    {
518
        $constraint = Assert::logicalNot(
519
            Assert::isInstanceOf(\stdClass::class)
520
        );
521
522
        $this->assertFalse($constraint->evaluate(new \stdClass, '', true));
523
        $this->assertTrue($constraint->evaluate(new Exception, '', true));
524
        $this->assertEquals('is not instance of class "stdClass"', $constraint->toString());
525
        $this->assertCount(1, $constraint);
526
527
        try {
528
            $constraint->evaluate(new \stdClass);
529
        } catch (ExpectationFailedException $e) {
530
            $this->assertEquals(
531
                <<<EOF
532
Failed asserting that stdClass Object () is not an instance of class "stdClass".
533
534
EOF
535
                ,
536
                TestFailure::exceptionToString($e)
537
            );
538
539
            return;
540
        }
541
542
        $this->fail();
543
    }
544
545
    public function testConstraintIsNotInstanceOf2(): void
546
    {
547
        $constraint = Assert::logicalNot(
548
            Assert::isInstanceOf(\stdClass::class)
549
        );
550
551
        try {
552
            $constraint->evaluate(new \stdClass, 'custom message');
553
        } catch (ExpectationFailedException $e) {
554
            $this->assertEquals(
555
                <<<EOF
556
custom message
557
Failed asserting that stdClass Object () is not an instance of class "stdClass".
558
559
EOF
560
                ,
561
                TestFailure::exceptionToString($e)
562
            );
563
564
            return;
565
        }
566
567
        $this->fail();
568
    }
569
570
    public function testConstraintIsNotType(): void
571
    {
572
        $constraint = Assert::logicalNot(
573
            Assert::isType('string')
574
        );
575
576
        $this->assertTrue($constraint->evaluate(0, '', true));
577
        $this->assertFalse($constraint->evaluate('', '', true));
578
        $this->assertEquals('is not of type "string"', $constraint->toString());
579
        $this->assertCount(1, $constraint);
580
581
        try {
582
            $constraint->evaluate('');
583
        } catch (ExpectationFailedException $e) {
584
            $this->assertEquals(
585
                <<<EOF
586
Failed asserting that '' is not of type "string".
587
588
EOF
589
                ,
590
                TestFailure::exceptionToString($e)
591
            );
592
593
            return;
594
        }
595
596
        $this->fail();
597
    }
598
599
    public function testConstraintIsNotType2(): void
600
    {
601
        $constraint = Assert::logicalNot(
602
            Assert::isType('string')
603
        );
604
605
        try {
606
            $constraint->evaluate('', 'custom message');
607
        } catch (ExpectationFailedException $e) {
608
            $this->assertEquals(
609
                <<<EOF
610
custom message
611
Failed asserting that '' is not of type "string".
612
613
EOF
614
                ,
615
                TestFailure::exceptionToString($e)
616
            );
617
618
            return;
619
        }
620
621
        $this->fail();
622
    }
623
624
    public function testConstraintIsNotNull(): void
625
    {
626
        $constraint = Assert::logicalNot(
627
            Assert::isNull()
628
        );
629
630
        $this->assertFalse($constraint->evaluate(null, '', true));
631
        $this->assertTrue($constraint->evaluate(0, '', true));
632
        $this->assertEquals('is not null', $constraint->toString());
633
        $this->assertCount(1, $constraint);
634
635
        try {
636
            $constraint->evaluate(null);
637
        } catch (ExpectationFailedException $e) {
638
            $this->assertEquals(
639
                <<<EOF
640
Failed asserting that null is not null.
641
642
EOF
643
                ,
644
                TestFailure::exceptionToString($e)
645
            );
646
647
            return;
648
        }
649
650
        $this->fail();
651
    }
652
653
    public function testConstraintIsNotNull2(): void
654
    {
655
        $constraint = Assert::logicalNot(
656
            Assert::isNull()
657
        );
658
659
        try {
660
            $constraint->evaluate(null, 'custom message');
661
        } catch (ExpectationFailedException $e) {
662
            $this->assertEquals(
663
                <<<EOF
664
custom message
665
Failed asserting that null is not null.
666
667
EOF
668
                ,
669
                TestFailure::exceptionToString($e)
670
            );
671
672
            return;
673
        }
674
675
        $this->fail();
676
    }
677
678
    public function testConstraintNotLessThan(): void
679
    {
680
        $constraint = Assert::logicalNot(
681
            Assert::lessThan(1)
682
        );
683
684
        $this->assertTrue($constraint->evaluate(1, '', true));
685
        $this->assertFalse($constraint->evaluate(0, '', true));
686
        $this->assertEquals('is not less than 1', $constraint->toString());
687
        $this->assertCount(1, $constraint);
688
689
        try {
690
            $constraint->evaluate(0);
691
        } catch (ExpectationFailedException $e) {
692
            $this->assertEquals(
693
                <<<EOF
694
Failed asserting that 0 is not less than 1.
695
696
EOF
697
                ,
698
                TestFailure::exceptionToString($e)
699
            );
700
701
            return;
702
        }
703
704
        $this->fail();
705
    }
706
707
    public function testConstraintNotLessThan2(): void
708
    {
709
        $constraint = Assert::logicalNot(
710
            Assert::lessThan(1)
711
        );
712
713
        try {
714
            $constraint->evaluate(0, 'custom message');
715
        } catch (ExpectationFailedException $e) {
716
            $this->assertEquals(
717
                <<<EOF
718
custom message
719
Failed asserting that 0 is not less than 1.
720
721
EOF
722
                ,
723
                TestFailure::exceptionToString($e)
724
            );
725
726
            return;
727
        }
728
729
        $this->fail();
730
    }
731
732
    public function testConstraintLessThanOrEqual(): void
733
    {
734
        $constraint = Assert::lessThanOrEqual(1);
735
736
        $this->assertTrue($constraint->evaluate(1, '', true));
737
        $this->assertFalse($constraint->evaluate(2, '', true));
738
        $this->assertEquals('is equal to 1 or is less than 1', $constraint->toString());
739
        $this->assertCount(2, $constraint);
740
741
        try {
742
            $constraint->evaluate(2);
743
        } catch (ExpectationFailedException $e) {
744
            $this->assertEquals(
745
                <<<EOF
746
Failed asserting that 2 is equal to 1 or is less than 1.
747
748
EOF
749
                ,
750
                TestFailure::exceptionToString($e)
751
            );
752
753
            return;
754
        }
755
756
        $this->fail();
757
    }
758
759
    public function testConstraintLessThanOrEqual2(): void
760
    {
761
        $constraint = Assert::lessThanOrEqual(1);
762
763
        try {
764
            $constraint->evaluate(2, 'custom message');
765
        } catch (ExpectationFailedException $e) {
766
            $this->assertEquals(
767
                <<<EOF
768
custom message
769
Failed asserting that 2 is equal to 1 or is less than 1.
770
771
EOF
772
                ,
773
                TestFailure::exceptionToString($e)
774
            );
775
776
            return;
777
        }
778
779
        $this->fail();
780
    }
781
782
    public function testConstraintNotLessThanOrEqual(): void
783
    {
784
        $constraint = Assert::logicalNot(
785
            Assert::lessThanOrEqual(1)
786
        );
787
788
        $this->assertTrue($constraint->evaluate(2, '', true));
789
        $this->assertFalse($constraint->evaluate(1, '', true));
790
        $this->assertEquals('not( is equal to 1 or is less than 1 )', $constraint->toString());
791
        $this->assertCount(2, $constraint);
792
793
        try {
794
            $constraint->evaluate(1);
795
        } catch (ExpectationFailedException $e) {
796
            $this->assertEquals(
797
                <<<EOF
798
Failed asserting that not( 1 is equal to 1 or is less than 1 ).
799
800
EOF
801
                ,
802
                TestFailure::exceptionToString($e)
803
            );
804
805
            return;
806
        }
807
808
        $this->fail();
809
    }
810
811
    public function testConstraintNotLessThanOrEqual2(): void
812
    {
813
        $constraint = Assert::logicalNot(
814
            Assert::lessThanOrEqual(1)
815
        );
816
817
        try {
818
            $constraint->evaluate(1, 'custom message');
819
        } catch (ExpectationFailedException $e) {
820
            $this->assertEquals(
821
                <<<EOF
822
custom message
823
Failed asserting that not( 1 is equal to 1 or is less than 1 ).
824
825
EOF
826
                ,
827
                TestFailure::exceptionToString($e)
828
            );
829
830
            return;
831
        }
832
833
        $this->fail();
834
    }
835
836
    public function testConstraintClassNotHasAttribute(): void
837
    {
838
        $constraint = Assert::logicalNot(
839
            Assert::classHasAttribute('privateAttribute')
840
        );
841
842
        $this->assertTrue($constraint->evaluate(\stdClass::class, '', true));
843
        $this->assertFalse($constraint->evaluate(\ClassWithNonPublicAttributes::class, '', true));
844
        $this->assertEquals('does not have attribute "privateAttribute"', $constraint->toString());
845
        $this->assertCount(1, $constraint);
846
847
        try {
848
            $constraint->evaluate(\ClassWithNonPublicAttributes::class);
849
        } catch (ExpectationFailedException $e) {
850
            $this->assertEquals(
851
                <<<EOF
852
Failed asserting that class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute".
853
854
EOF
855
                ,
856
                TestFailure::exceptionToString($e)
857
            );
858
859
            return;
860
        }
861
862
        $this->fail();
863
    }
864
865
    public function testConstraintClassNotHasAttribute2(): void
866
    {
867
        $constraint = Assert::logicalNot(
868
            Assert::classHasAttribute('privateAttribute')
869
        );
870
871
        try {
872
            $constraint->evaluate(\ClassWithNonPublicAttributes::class, 'custom message');
873
        } catch (ExpectationFailedException $e) {
874
            $this->assertEquals(
875
                <<<EOF
876
custom message
877
Failed asserting that class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute".
878
879
EOF
880
                ,
881
                TestFailure::exceptionToString($e)
882
            );
883
884
            return;
885
        }
886
887
        $this->fail();
888
    }
889
890
    public function testConstraintClassNotHasStaticAttribute(): void
891
    {
892
        $constraint = Assert::logicalNot(
893
            Assert::classHasStaticAttribute('privateStaticAttribute')
894
        );
895
896
        $this->assertTrue($constraint->evaluate(\stdClass::class, '', true));
897
        $this->assertFalse($constraint->evaluate(\ClassWithNonPublicAttributes::class, '', true));
898
        $this->assertEquals('does not have static attribute "privateStaticAttribute"', $constraint->toString());
899
        $this->assertCount(1, $constraint);
900
901
        try {
902
            $constraint->evaluate(\ClassWithNonPublicAttributes::class);
903
        } catch (ExpectationFailedException $e) {
904
            $this->assertEquals(
905
                <<<EOF
906
Failed asserting that class "ClassWithNonPublicAttributes" does not have static attribute "privateStaticAttribute".
907
908
EOF
909
                ,
910
                TestFailure::exceptionToString($e)
911
            );
912
913
            return;
914
        }
915
916
        $this->fail();
917
    }
918
919
    public function testConstraintClassNotHasStaticAttribute2(): void
920
    {
921
        $constraint = Assert::logicalNot(
922
            Assert::classHasStaticAttribute('privateStaticAttribute')
923
        );
924
925
        try {
926
            $constraint->evaluate(\ClassWithNonPublicAttributes::class, 'custom message');
927
        } catch (ExpectationFailedException $e) {
928
            $this->assertEquals(
929
                <<<EOF
930
custom message
931
Failed asserting that class "ClassWithNonPublicAttributes" does not have static attribute "privateStaticAttribute".
932
933
EOF
934
                ,
935
                TestFailure::exceptionToString($e)
936
            );
937
938
            return;
939
        }
940
941
        $this->fail();
942
    }
943
944
    public function testConstraintObjectNotHasAttribute(): void
945
    {
946
        $constraint = Assert::logicalNot(
947
            Assert::objectHasAttribute('privateAttribute')
948
        );
949
950
        $this->assertTrue($constraint->evaluate(new \stdClass, '', true));
951
        $this->assertFalse($constraint->evaluate(new \ClassWithNonPublicAttributes, '', true));
952
        $this->assertEquals('does not have attribute "privateAttribute"', $constraint->toString());
953
        $this->assertCount(1, $constraint);
954
955
        try {
956
            $constraint->evaluate(new \ClassWithNonPublicAttributes);
957
        } catch (ExpectationFailedException $e) {
958
            $this->assertEquals(
959
                <<<EOF
960
Failed asserting that object of class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute".
961
962
EOF
963
                ,
964
                TestFailure::exceptionToString($e)
965
            );
966
967
            return;
968
        }
969
970
        $this->fail();
971
    }
972
973
    public function testConstraintObjectNotHasAttribute2(): void
974
    {
975
        $constraint = Assert::logicalNot(
976
            Assert::objectHasAttribute('privateAttribute')
977
        );
978
979
        try {
980
            $constraint->evaluate(new \ClassWithNonPublicAttributes, 'custom message');
981
        } catch (ExpectationFailedException $e) {
982
            $this->assertEquals(
983
                <<<EOF
984
custom message
985
Failed asserting that object of class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute".
986
987
EOF
988
                ,
989
                TestFailure::exceptionToString($e)
990
            );
991
992
            return;
993
        }
994
995
        $this->fail();
996
    }
997
998
    public function testConstraintPCRENotMatch(): void
999
    {
1000
        $constraint = Assert::logicalNot(
1001
            Assert::matchesRegularExpression('/foo/')
1002
        );
1003
1004
        $this->assertTrue($constraint->evaluate('barbazbar', '', true));
1005
        $this->assertFalse($constraint->evaluate('barfoobar', '', true));
1006
        $this->assertEquals('does not match PCRE pattern "/foo/"', $constraint->toString());
1007
        $this->assertCount(1, $constraint);
1008
1009
        try {
1010
            $constraint->evaluate('barfoobar');
1011
        } catch (ExpectationFailedException $e) {
1012
            $this->assertEquals(
1013
                <<<EOF
1014
Failed asserting that 'barfoobar' does not match PCRE pattern "/foo/".
1015
1016
EOF
1017
                ,
1018
                TestFailure::exceptionToString($e)
1019
            );
1020
1021
            return;
1022
        }
1023
1024
        $this->fail();
1025
    }
1026
1027
    public function testConstraintPCRENotMatch2(): void
1028
    {
1029
        $constraint = Assert::logicalNot(
1030
            Assert::matchesRegularExpression('/foo/')
1031
        );
1032
1033
        try {
1034
            $constraint->evaluate('barfoobar', 'custom message');
1035
        } catch (ExpectationFailedException $e) {
1036
            $this->assertEquals(
1037
                <<<EOF
1038
custom message
1039
Failed asserting that 'barfoobar' does not match PCRE pattern "/foo/".
1040
1041
EOF
1042
                ,
1043
                TestFailure::exceptionToString($e)
1044
            );
1045
1046
            return;
1047
        }
1048
1049
        $this->fail();
1050
    }
1051
1052
    public function testConstraintStringStartsNotWith(): void
1053
    {
1054
        $constraint = Assert::logicalNot(
1055
            Assert::stringStartsWith('prefix')
1056
        );
1057
1058
        $this->assertTrue($constraint->evaluate('foo', '', true));
1059
        $this->assertFalse($constraint->evaluate('prefixfoo', '', true));
1060
        $this->assertEquals('starts not with "prefix"', $constraint->toString());
1061
        $this->assertCount(1, $constraint);
1062
1063
        try {
1064
            $constraint->evaluate('prefixfoo');
1065
        } catch (ExpectationFailedException $e) {
1066
            $this->assertEquals(
1067
                <<<EOF
1068
Failed asserting that 'prefixfoo' starts not with "prefix".
1069
1070
EOF
1071
                ,
1072
                TestFailure::exceptionToString($e)
1073
            );
1074
1075
            return;
1076
        }
1077
1078
        $this->fail();
1079
    }
1080
1081
    public function testConstraintStringStartsNotWith2(): void
1082
    {
1083
        $constraint = Assert::logicalNot(
1084
            Assert::stringStartsWith('prefix')
1085
        );
1086
1087
        try {
1088
            $constraint->evaluate('prefixfoo', 'custom message');
1089
        } catch (ExpectationFailedException $e) {
1090
            $this->assertEquals(
1091
                <<<EOF
1092
custom message
1093
Failed asserting that 'prefixfoo' starts not with "prefix".
1094
1095
EOF
1096
                ,
1097
                TestFailure::exceptionToString($e)
1098
            );
1099
1100
            return;
1101
        }
1102
1103
        $this->fail();
1104
    }
1105
1106
    public function testConstraintStringNotContains(): void
1107
    {
1108
        $constraint = Assert::logicalNot(
1109
            Assert::stringContains('foo')
1110
        );
1111
1112
        $this->assertTrue($constraint->evaluate('barbazbar', '', true));
1113
        $this->assertFalse($constraint->evaluate('barfoobar', '', true));
1114
        $this->assertEquals('does not contain "foo"', $constraint->toString());
1115
        $this->assertCount(1, $constraint);
1116
1117
        try {
1118
            $constraint->evaluate('barfoobar');
1119
        } catch (ExpectationFailedException $e) {
1120
            $this->assertEquals(
1121
                <<<EOF
1122
Failed asserting that 'barfoobar' does not contain "foo".
1123
1124
EOF
1125
                ,
1126
                TestFailure::exceptionToString($e)
1127
            );
1128
1129
            return;
1130
        }
1131
1132
        $this->fail();
1133
    }
1134
1135
    public function testConstraintStringNotContainsWhenIgnoreCase(): void
1136
    {
1137
        $constraint = Assert::logicalNot(
1138
            Assert::stringContains('oryginał')
1139
        );
1140
1141
        $this->assertTrue($constraint->evaluate('original', '', true));
1142
        $this->assertFalse($constraint->evaluate('ORYGINAŁ', '', true));
1143
        $this->assertFalse($constraint->evaluate('oryginał', '', true));
1144
        $this->assertEquals('does not contain "oryginał"', $constraint->toString());
1145
        $this->assertCount(1, $constraint);
1146
1147
        $this->expectException(ExpectationFailedException::class);
1148
1149
        $constraint->evaluate('ORYGINAŁ');
1150
    }
1151
1152
    public function testConstraintStringNotContainsForUtf8StringWhenNotIgnoreCase(): void
1153
    {
1154
        $constraint = Assert::logicalNot(
1155
            Assert::stringContains('oryginał', false)
1156
        );
1157
1158
        $this->assertTrue($constraint->evaluate('original', '', true));
1159
        $this->assertTrue($constraint->evaluate('ORYGINAŁ', '', true));
1160
        $this->assertFalse($constraint->evaluate('oryginał', '', true));
1161
        $this->assertEquals('does not contain "oryginał"', $constraint->toString());
1162
        $this->assertCount(1, $constraint);
1163
1164
        $this->expectException(ExpectationFailedException::class);
1165
1166
        $constraint->evaluate('oryginał');
1167
    }
1168
1169
    public function testConstraintStringNotContains2(): void
1170
    {
1171
        $constraint = Assert::logicalNot(
1172
            Assert::stringContains('foo')
1173
        );
1174
1175
        try {
1176
            $constraint->evaluate('barfoobar', 'custom message');
1177
        } catch (ExpectationFailedException $e) {
1178
            $this->assertEquals(
1179
                <<<EOF
1180
custom message
1181
Failed asserting that 'barfoobar' does not contain "foo".
1182
1183
EOF
1184
                ,
1185
                TestFailure::exceptionToString($e)
1186
            );
1187
1188
            return;
1189
        }
1190
1191
        $this->fail();
1192
    }
1193
1194
    public function testConstraintStringEndsNotWith(): void
1195
    {
1196
        $constraint = Assert::logicalNot(
1197
            Assert::stringEndsWith('suffix')
1198
        );
1199
1200
        $this->assertTrue($constraint->evaluate('foo', '', true));
1201
        $this->assertFalse($constraint->evaluate('foosuffix', '', true));
1202
        $this->assertEquals('ends not with "suffix"', $constraint->toString());
1203
        $this->assertCount(1, $constraint);
1204
1205
        try {
1206
            $constraint->evaluate('foosuffix');
1207
        } catch (ExpectationFailedException $e) {
1208
            $this->assertEquals(
1209
                <<<EOF
1210
Failed asserting that 'foosuffix' ends not with "suffix".
1211
1212
EOF
1213
                ,
1214
                TestFailure::exceptionToString($e)
1215
            );
1216
1217
            return;
1218
        }
1219
1220
        $this->fail();
1221
    }
1222
1223
    public function testConstraintStringEndsNotWith2(): void
1224
    {
1225
        $constraint = Assert::logicalNot(
1226
            Assert::stringEndsWith('suffix')
1227
        );
1228
1229
        try {
1230
            $constraint->evaluate('foosuffix', 'custom message');
1231
        } catch (ExpectationFailedException $e) {
1232
            $this->assertEquals(
1233
                <<<EOF
1234
custom message
1235
Failed asserting that 'foosuffix' ends not with "suffix".
1236
1237
EOF
1238
                ,
1239
                TestFailure::exceptionToString($e)
1240
            );
1241
1242
            return;
1243
        }
1244
1245
        $this->fail();
1246
    }
1247
1248
    public function testConstraintArrayNotContains(): void
1249
    {
1250
        $constraint = Assert::logicalNot(
1251
            new TraversableContains('foo')
1252
        );
1253
1254
        $this->assertTrue($constraint->evaluate(['bar'], '', true));
1255
        $this->assertFalse($constraint->evaluate(['foo'], '', true));
1256
        $this->assertEquals("does not contain 'foo'", $constraint->toString());
1257
        $this->assertCount(1, $constraint);
1258
1259
        try {
1260
            $constraint->evaluate(['foo']);
1261
        } catch (ExpectationFailedException $e) {
1262
            $this->assertEquals(
1263
                <<<EOF
1264
Failed asserting that an array does not contain 'foo'.
1265
1266
EOF
1267
                ,
1268
                TestFailure::exceptionToString($e)
1269
            );
1270
1271
            return;
1272
        }
1273
1274
        $this->fail();
1275
    }
1276
1277
    public function testConstraintArrayNotContains2(): void
1278
    {
1279
        $constraint = Assert::logicalNot(
1280
            new TraversableContains('foo')
1281
        );
1282
1283
        try {
1284
            $constraint->evaluate(['foo'], 'custom message');
1285
        } catch (ExpectationFailedException $e) {
1286
            $this->assertEquals(
1287
                <<<EOF
1288
custom message
1289
Failed asserting that an array does not contain 'foo'.
1290
1291
EOF
1292
                ,
1293
                TestFailure::exceptionToString($e)
1294
            );
1295
1296
            return;
1297
        }
1298
1299
        $this->fail();
1300
    }
1301
1302
    public function testAttributeNotEqualTo(): void
1303
    {
1304
        $object     = new \ClassWithNonPublicAttributes;
1305
        $constraint = Assert::logicalNot(
1306
            Assert::attributeEqualTo('foo', 2)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::attributeEqualTo() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3338 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1306
            /** @scrutinizer ignore-deprecated */ Assert::attributeEqualTo('foo', 2)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1307
        );
1308
1309
        $this->assertTrue($constraint->evaluate($object, '', true));
1310
        $this->assertEquals('attribute "foo" is not equal to 2', $constraint->toString());
1311
        $this->assertCount(1, $constraint);
1312
1313
        $constraint = Assert::logicalNot(
1314
            Assert::attributeEqualTo('foo', 1)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::attributeEqualTo() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3338 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1314
            /** @scrutinizer ignore-deprecated */ Assert::attributeEqualTo('foo', 1)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1315
        );
1316
1317
        $this->assertFalse($constraint->evaluate($object, '', true));
1318
1319
        try {
1320
            $constraint->evaluate($object);
1321
        } catch (ExpectationFailedException $e) {
1322
            $this->assertEquals(
1323
                <<<EOF
1324
Failed asserting that attribute "foo" is not equal to 1.
1325
1326
EOF
1327
                ,
1328
                TestFailure::exceptionToString($e)
1329
            );
1330
1331
            return;
1332
        }
1333
1334
        $this->fail();
1335
    }
1336
1337
    public function testAttributeNotEqualTo2(): void
1338
    {
1339
        $object     = new \ClassWithNonPublicAttributes;
1340
        $constraint = Assert::logicalNot(
1341
            Assert::attributeEqualTo('foo', 1)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::attributeEqualTo() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3338 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1341
            /** @scrutinizer ignore-deprecated */ Assert::attributeEqualTo('foo', 1)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1342
        );
1343
1344
        try {
1345
            $constraint->evaluate($object, 'custom message');
1346
        } catch (ExpectationFailedException $e) {
1347
            $this->assertEquals(
1348
                <<<EOF
1349
custom message\nFailed asserting that attribute "foo" is not equal to 1.
1350
1351
EOF
1352
                ,
1353
                TestFailure::exceptionToString($e)
1354
            );
1355
1356
            return;
1357
        }
1358
1359
        $this->fail();
1360
    }
1361
1362
    public function testConstraintCountWithAnArray(): void
1363
    {
1364
        $constraint = new Count(5);
1365
1366
        $this->assertTrue($constraint->evaluate([1, 2, 3, 4, 5], '', true));
1367
        $this->assertFalse($constraint->evaluate([1, 2, 3, 4], '', true));
1368
    }
1369
1370
    public function testConstraintCountWithAnIteratorWhichDoesNotImplementCountable(): void
1371
    {
1372
        $constraint = new Count(5);
1373
1374
        $this->assertTrue($constraint->evaluate(new \TestIterator([1, 2, 3, 4, 5]), '', true));
1375
        $this->assertFalse($constraint->evaluate(new \TestIterator([1, 2, 3, 4]), '', true));
1376
    }
1377
1378
    public function testConstraintCountWithAnObjectImplementingCountable(): void
1379
    {
1380
        $constraint = new Count(5);
1381
1382
        $this->assertTrue($constraint->evaluate(new \ArrayObject([1, 2, 3, 4, 5]), '', true));
1383
        $this->assertFalse($constraint->evaluate(new \ArrayObject([1, 2, 3, 4]), '', true));
1384
    }
1385
1386
    public function testConstraintCountFailing(): void
1387
    {
1388
        $constraint = new Count(5);
1389
1390
        try {
1391
            $constraint->evaluate([1, 2]);
1392
        } catch (ExpectationFailedException $e) {
1393
            $this->assertEquals(
1394
                <<<EOF
1395
Failed asserting that actual size 2 matches expected size 5.
1396
1397
EOF
1398
                ,
1399
                TestFailure::exceptionToString($e)
1400
            );
1401
1402
            return;
1403
        }
1404
1405
        $this->fail();
1406
    }
1407
1408
    public function testConstraintNotCountFailing(): void
1409
    {
1410
        $constraint = Assert::logicalNot(
1411
            new Count(2)
1412
        );
1413
1414
        try {
1415
            $constraint->evaluate([1, 2]);
1416
        } catch (ExpectationFailedException $e) {
1417
            $this->assertEquals(
1418
                <<<EOF
1419
Failed asserting that actual size 2 does not match expected size 2.
1420
1421
EOF
1422
                ,
1423
                TestFailure::exceptionToString($e)
1424
            );
1425
1426
            return;
1427
        }
1428
1429
        $this->fail();
1430
    }
1431
1432
    public function testConstraintNotSameSizeFailing(): void
1433
    {
1434
        $constraint = Assert::logicalNot(
1435
            new SameSize([1, 2])
1436
        );
1437
1438
        try {
1439
            $constraint->evaluate([3, 4]);
1440
        } catch (ExpectationFailedException $e) {
1441
            $this->assertEquals(
1442
                <<<EOF
1443
Failed asserting that actual size 2 does not match expected size 2.
1444
1445
EOF
1446
                ,
1447
                TestFailure::exceptionToString($e)
1448
            );
1449
1450
            return;
1451
        }
1452
1453
        $this->fail();
1454
    }
1455
1456
    public function testConstraintException(): void
1457
    {
1458
        $constraint = new Constraint\Exception('FoobarException');
1459
        $exception  = new \DummyException('Test');
1460
        $stackTrace = Filter::getFilteredStacktrace($exception);
1461
1462
        try {
1463
            $constraint->evaluate($exception);
1464
        } catch (ExpectationFailedException $e) {
1465
            $this->assertEquals(
1466
                <<<EOF
1467
Failed asserting that exception of type "DummyException" matches expected exception "FoobarException". Message was: "Test" at
1468
$stackTrace.
1469
1470
EOF
1471
                ,
1472
                TestFailure::exceptionToString($e)
1473
            );
1474
1475
            return;
1476
        }
1477
1478
        $this->fail();
1479
    }
1480
1481
    /**
1482
     * Removes spaces in front of newlines
1483
     *
1484
     * @param string $string
1485
     *
1486
     * @return string
1487
     */
1488
    private function trimnl($string)
1489
    {
1490
        return \preg_replace('/[ ]*\n/', "\n", $string);
1491
    }
1492
}
1493