]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Tests/Framework/ConstraintTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Tests / Framework / ConstraintTest.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category   Testing
38  * @package    PHPUnit
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 3.0.0
45  */
46
47 require_once 'PHPUnit/Framework/TestCase.php';
48
49 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ClassWithNonPublicAttributes.php';
50
51 /**
52  *
53  *
54  * @category   Testing
55  * @package    PHPUnit
56  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
57  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
58  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
59  * @version    Release: 3.3.17
60  * @link       http://www.phpunit.de/
61  * @since      Class available since Release 3.0.0
62  */
63 class Framework_ConstraintTest extends PHPUnit_Framework_TestCase
64 {
65     public function testConstraintArrayHasKey()
66     {
67         $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey(0);
68
69         $this->assertFalse($constraint->evaluate(array()));
70         $this->assertEquals('has the key <integer:0>', $constraint->toString());
71
72         try {
73             $constraint->fail(array(), '');
74         }
75
76         catch (PHPUnit_Framework_ExpectationFailedException $e) {
77             $this->assertEquals(
78               "Failed asserting that an array has the key <integer:0>.",
79               $e->getDescription()
80             );
81
82             return;
83         }
84
85         $this->fail();
86     }
87
88     public function testConstraintArrayHasKey2()
89     {
90         $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey(0);
91
92         try {
93             $constraint->fail(array(), 'custom message');
94         }
95
96         catch (PHPUnit_Framework_ExpectationFailedException $e) {
97             $this->assertEquals(
98               "custom message\nFailed asserting that an array has the key <integer:0>.",
99               $e->getDescription()
100             );
101
102             return;
103         }
104
105         $this->fail();
106     }
107
108     public function testConstraintArrayNotHasKey()
109     {
110         $constraint = new PHPUnit_Framework_Constraint_Not(
111           new PHPUnit_Framework_Constraint_ArrayHasKey(0)
112         );
113
114         $this->assertTrue($constraint->evaluate(array()));
115         $this->assertEquals('does not have the key <integer:0>', $constraint->toString());
116
117         try {
118             $constraint->fail(array(0), '', TRUE);
119         }
120
121         catch (PHPUnit_Framework_ExpectationFailedException $e) {
122             $this->assertEquals(
123               "Failed asserting that an array does not have the key <integer:0>.",
124               $e->getDescription()
125             );
126
127             return;
128         }
129
130         $this->fail();
131     }
132
133     public function testConstraintArrayNotHasKey2()
134     {
135         $constraint = new PHPUnit_Framework_Constraint_Not(
136           new PHPUnit_Framework_Constraint_ArrayHasKey(0)
137         );
138
139         try {
140             $constraint->fail(array(0), 'custom message', TRUE);
141         }
142
143         catch (PHPUnit_Framework_ExpectationFailedException $e) {
144             $this->assertEquals(
145               "custom message\nFailed asserting that an array does not have the key <integer:0>.",
146               $e->getDescription()
147             );
148
149             return;
150         }
151
152         $this->fail();
153     }
154
155     public function testConstraintFileExists()
156     {
157         $constraint = new PHPUnit_Framework_Constraint_FileExists;
158
159         $this->assertFalse($constraint->evaluate('foo'));
160         $this->assertEquals('file exists', $constraint->toString());
161
162         try {
163             $constraint->fail('foo', '');
164         }
165
166         catch (PHPUnit_Framework_ExpectationFailedException $e) {
167             $this->assertEquals(
168               'Failed asserting that file "foo" exists.',
169               $e->getDescription()
170             );
171
172             return;
173         }
174
175         $this->fail();
176     }
177
178     public function testConstraintFileExists2()
179     {
180         $constraint = new PHPUnit_Framework_Constraint_FileExists;
181
182         try {
183             $constraint->fail('foo', 'custom message');
184         }
185
186         catch (PHPUnit_Framework_ExpectationFailedException $e) {
187             $this->assertEquals(
188               "custom message\nFailed asserting that file \"foo\" exists.",
189               $e->getDescription()
190             );
191
192             return;
193         }
194
195         $this->fail();
196     }
197
198     public function testConstraintFileNotExists()
199     {
200         $constraint = new PHPUnit_Framework_Constraint_Not(
201           new PHPUnit_Framework_Constraint_FileExists
202         );
203
204         $this->assertTrue($constraint->evaluate('foo'));
205         $this->assertEquals('file does not exist', $constraint->toString());
206
207         try {
208             $constraint->fail('foo', '', TRUE);
209         }
210
211         catch (PHPUnit_Framework_ExpectationFailedException $e) {
212             $this->assertEquals(
213               'Failed asserting that file "foo" does not exist.',
214               $e->getDescription()
215             );
216
217             return;
218         }
219
220         $this->fail();
221     }
222
223     public function testConstraintFileNotExists2()
224     {
225         $constraint = new PHPUnit_Framework_Constraint_Not(
226           new PHPUnit_Framework_Constraint_FileExists
227         );
228
229         try {
230             $constraint->fail('foo', 'custom message', TRUE);
231         }
232
233         catch (PHPUnit_Framework_ExpectationFailedException $e) {
234             $this->assertEquals(
235               "custom message\nFailed asserting that file \"foo\" does not exist.",
236               $e->getDescription()
237             );
238
239             return;
240         }
241
242         $this->fail();
243     }
244
245     public function testConstraintGreaterThan()
246     {
247         $constraint = new PHPUnit_Framework_Constraint_GreaterThan(1);
248
249         $this->assertFalse($constraint->evaluate(0));
250         $this->assertTrue($constraint->evaluate(2));
251         $this->assertEquals('is greater than <integer:1>', $constraint->toString());
252
253         try {
254             $constraint->fail(0, '');
255         }
256
257         catch (PHPUnit_Framework_ExpectationFailedException $e) {
258             $this->assertEquals(
259               "Failed asserting that <integer:0> is greater than <integer:1>.",
260               $e->getDescription()
261             );
262
263             return;
264         }
265
266         $this->fail();
267     }
268
269     public function testConstraintGreaterThan2()
270     {
271         $constraint = new PHPUnit_Framework_Constraint_GreaterThan(1);
272
273         try {
274             $constraint->fail(0, 'custom message');
275         }
276
277         catch (PHPUnit_Framework_ExpectationFailedException $e) {
278             $this->assertEquals(
279               "custom message\nFailed asserting that <integer:0> is greater than <integer:1>.",
280               $e->getDescription()
281             );
282
283             return;
284         }
285
286         $this->fail();
287     }
288
289     public function testConstraintNotGreaterThan()
290     {
291         $constraint = new PHPUnit_Framework_Constraint_Not(
292           new PHPUnit_Framework_Constraint_GreaterThan(1)
293         );
294
295         $this->assertTrue($constraint->evaluate(1));
296         $this->assertEquals('is not greater than <integer:1>', $constraint->toString());
297
298         try {
299             $constraint->fail(1, '', TRUE);
300         }
301
302         catch (PHPUnit_Framework_ExpectationFailedException $e) {
303             $this->assertEquals(
304               "Failed asserting that <integer:1> is not greater than <integer:1>.",
305               $e->getDescription()
306             );
307
308             return;
309         }
310
311         $this->fail();
312     }
313
314     public function testConstraintNotGreaterThan2()
315     {
316         $constraint = new PHPUnit_Framework_Constraint_Not(
317           new PHPUnit_Framework_Constraint_GreaterThan(1)
318         );
319
320         try {
321             $constraint->fail(1, 'custom message', TRUE);
322         }
323
324         catch (PHPUnit_Framework_ExpectationFailedException $e) {
325             $this->assertEquals(
326               "custom message\nFailed asserting that <integer:1> is not greater than <integer:1>.",
327               $e->getDescription()
328             );
329
330             return;
331         }
332
333         $this->fail();
334     }
335
336     public function testConstraintIsAnything()
337     {
338         $constraint = new PHPUnit_Framework_Constraint_IsAnything;
339
340         $this->assertTrue($constraint->evaluate(NULL));
341         $this->assertNull($constraint->fail(NULL, ''));
342         $this->assertEquals('is anything', $constraint->toString());
343     }
344
345     public function testConstraintNotIsAnything()
346     {
347         $constraint = new PHPUnit_Framework_Constraint_Not(
348           new PHPUnit_Framework_Constraint_IsAnything
349         );
350
351         $this->assertFalse($constraint->evaluate(NULL));
352         $this->assertNull($constraint->fail(NULL, ''));
353         $this->assertEquals('is not anything', $constraint->toString());
354     }
355
356     public function testConstraintIsEqual()
357     {
358         $constraint = new PHPUnit_Framework_Constraint_IsEqual(1);
359
360         $this->assertFalse($constraint->evaluate(0));
361         $this->assertTrue($constraint->evaluate(1));
362         $this->assertEquals('is equal to <integer:1>', $constraint->toString());
363
364         try {
365             $constraint->fail(0, '');
366         }
367
368         catch (PHPUnit_Framework_ExpectationFailedException $e) {
369             $this->assertEquals(
370               "Failed asserting that <integer:0> is equal to <integer:1>.",
371               $e->getDescription()
372             );
373
374             return;
375         }
376
377         $this->fail();
378     }
379
380     public function testConstraintIsEqual2()
381     {
382         $constraint = new PHPUnit_Framework_Constraint_IsEqual(1);
383
384         try {
385             $constraint->fail(0, 'custom message');
386         }
387
388         catch (PHPUnit_Framework_ExpectationFailedException $e) {
389             $this->assertEquals(
390               "custom message\nFailed asserting that <integer:0> is equal to <integer:1>.",
391               $e->getDescription()
392             );
393
394             return;
395         }
396
397         $this->fail();
398     }
399
400     public function testConstraintIsNotEqual()
401     {
402         $constraint = new PHPUnit_Framework_Constraint_Not(
403           new PHPUnit_Framework_Constraint_IsEqual(1)
404         );
405
406         $this->assertTrue($constraint->evaluate(0));
407         $this->assertFalse($constraint->evaluate(1));
408         $this->assertEquals('is not equal to <integer:1>', $constraint->toString());
409
410         try {
411             $constraint->fail(1, '', TRUE);
412         }
413
414         catch (PHPUnit_Framework_ExpectationFailedException $e) {
415             $this->assertEquals(
416               "Failed asserting that <integer:1> is not equal to <integer:1>.",
417               $e->getDescription()
418             );
419
420             return;
421         }
422
423         $this->fail();
424     }
425
426     public function testConstraintIsNotEqual2()
427     {
428         $constraint = new PHPUnit_Framework_Constraint_Not(
429           new PHPUnit_Framework_Constraint_IsEqual(1)
430         );
431
432         try {
433             $constraint->fail(1, 'custom message', TRUE);
434         }
435
436         catch (PHPUnit_Framework_ExpectationFailedException $e) {
437             $this->assertEquals(
438               "custom message\nFailed asserting that <integer:1> is not equal to <integer:1>.",
439               $e->getDescription()
440             );
441
442             return;
443         }
444
445         $this->fail();
446     }
447
448     public function testConstraintIsIdentical()
449     {
450         $a = new stdClass;
451         $b = new stdClass;
452
453         $constraint = new PHPUnit_Framework_Constraint_IsIdentical($a);
454
455         $this->assertFalse($constraint->evaluate($b));
456         $this->assertTrue($constraint->evaluate($a));
457         $this->assertEquals('is identical to an object of class "stdClass"', $constraint->toString());
458
459         try {
460             $constraint->fail($b, '');
461         }
462
463         catch (PHPUnit_Framework_ExpectationFailedException $e) {
464             $this->assertEquals(
465               "Failed asserting that \nstdClass Object\n(\n)\n is identical to an object of class \"stdClass\".",
466               $e->getDescription()
467             );
468
469             return;
470         }
471
472         $this->fail();
473     }
474
475     public function testConstraintIsIdentical2()
476     {
477         $a = new stdClass;
478         $b = new stdClass;
479
480         $constraint = new PHPUnit_Framework_Constraint_IsIdentical($a);
481
482         try {
483             $constraint->fail($b, 'custom message');
484         }
485
486         catch (PHPUnit_Framework_ExpectationFailedException $e) {
487             $this->assertEquals(
488               "custom message\nFailed asserting that \nstdClass Object\n(\n)\n is identical to an object of class \"stdClass\".",
489               $e->getDescription()
490             );
491
492             return;
493         }
494
495         $this->fail();
496     }
497
498     public function testConstraintIsNotIdentical()
499     {
500         $a = new stdClass;
501         $b = new stdClass;
502
503         $constraint = new PHPUnit_Framework_Constraint_Not(
504           new PHPUnit_Framework_Constraint_IsIdentical($a)
505         );
506
507         $this->assertTrue($constraint->evaluate($b));
508         $this->assertFalse($constraint->evaluate($a));
509         $this->assertEquals("is not identical to an object of class \"stdClass\"", $constraint->toString());
510
511         try {
512             $constraint->fail($a, '', TRUE);
513         }
514
515         catch (PHPUnit_Framework_ExpectationFailedException $e) {
516             $this->assertEquals(
517               "Failed asserting that \nstdClass Object\n(\n)\n is not identical to an object of class \"stdClass\".",
518               $e->getDescription()
519             );
520
521             return;
522         }
523
524         $this->fail();
525     }
526
527     public function testConstraintIsNotIdentical2()
528     {
529         $a = new stdClass;
530
531         $constraint = new PHPUnit_Framework_Constraint_Not(
532           new PHPUnit_Framework_Constraint_IsIdentical($a)
533         );
534
535         try {
536             $constraint->fail($a, 'custom message', TRUE);
537         }
538
539         catch (PHPUnit_Framework_ExpectationFailedException $e) {
540             $this->assertEquals(
541               "custom message\nFailed asserting that \nstdClass Object\n(\n)\n is not identical to an object of class \"stdClass\".",
542               $e->getDescription()
543             );
544
545             return;
546         }
547
548         $this->fail();
549     }
550
551     public function testConstraintIsInstanceOf()
552     {
553         $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf('Exception');
554
555         $this->assertFalse($constraint->evaluate(new stdClass));
556         $this->assertTrue($constraint->evaluate(new Exception));
557         $this->assertEquals('is instance of class "Exception"', $constraint->toString());
558
559         try {
560             $constraint->fail(new stdClass, '');
561         }
562
563         catch (PHPUnit_Framework_ExpectationFailedException $e) {
564             $this->assertEquals(
565               'Failed asserting that <stdClass> is an instance of class "Exception".',
566               $e->getDescription()
567             );
568
569             return;
570         }
571
572         $this->fail();
573     }
574
575     public function testConstraintIsInstanceOf2()
576     {
577         $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf('Exception');
578
579         try {
580             $constraint->fail(new stdClass, 'custom message');
581         }
582
583         catch (PHPUnit_Framework_ExpectationFailedException $e) {
584             $this->assertEquals(
585               "custom message\nFailed asserting that <stdClass> is an instance of class \"Exception\".",
586               $e->getDescription()
587             );
588
589             return;
590         }
591
592         $this->fail();
593     }
594
595     public function testConstraintIsNotInstanceOf()
596     {
597         $constraint = new PHPUnit_Framework_Constraint_Not(
598           new PHPUnit_Framework_Constraint_IsInstanceOf('stdClass')
599         );
600
601         $this->assertFalse($constraint->evaluate(new stdClass));
602         $this->assertTrue($constraint->evaluate(new Exception));
603         $this->assertEquals('is not instance of class "stdClass"', $constraint->toString());
604
605         try {
606             $constraint->fail(new stdClass, '', TRUE);
607         }
608
609         catch (PHPUnit_Framework_ExpectationFailedException $e) {
610             $this->assertEquals(
611               'Failed asserting that <stdClass> is not an instance of class "stdClass".',
612               $e->getDescription()
613             );
614
615             return;
616         }
617
618         $this->fail();
619     }
620
621     public function testConstraintIsNotInstanceOf2()
622     {
623         $constraint = new PHPUnit_Framework_Constraint_Not(
624           new PHPUnit_Framework_Constraint_IsInstanceOf('stdClass')
625         );
626
627         try {
628             $constraint->fail(new stdClass, 'custom message', TRUE);
629         }
630
631         catch (PHPUnit_Framework_ExpectationFailedException $e) {
632             $this->assertEquals(
633               "custom message\nFailed asserting that <stdClass> is not an instance of class \"stdClass\".",
634               $e->getDescription()
635             );
636
637             return;
638         }
639
640         $this->fail();
641     }
642
643     public function testConstraintIsType()
644     {
645         $constraint = new PHPUnit_Framework_Constraint_IsType('string');
646
647         $this->assertFalse($constraint->evaluate(0));
648         $this->assertTrue($constraint->evaluate(''));
649         $this->assertEquals('is of type "string"', $constraint->toString());
650
651         try {
652             $constraint->fail(new stdClass, '');
653         }
654
655         catch (PHPUnit_Framework_ExpectationFailedException $e) {
656             $this->assertEquals(
657               "Failed asserting that \nstdClass Object\n(\n)\n is of type \"string\".",
658               $e->getDescription()
659             );
660
661             return;
662         }
663
664         $this->fail();
665     }
666
667     public function testConstraintIsType2()
668     {
669         $constraint = new PHPUnit_Framework_Constraint_IsType('string');
670
671         try {
672             $constraint->fail(new stdClass, 'custom message');
673         }
674
675         catch (PHPUnit_Framework_ExpectationFailedException $e) {
676             $this->assertEquals(
677               "custom message\nFailed asserting that \nstdClass Object\n(\n)\n is of type \"string\".",
678               $e->getDescription()
679             );
680
681             return;
682         }
683
684         $this->fail();
685     }
686
687     public function testConstraintIsNotType()
688     {
689         $constraint = new PHPUnit_Framework_Constraint_Not(
690           new PHPUnit_Framework_Constraint_IsType('string')
691         );
692
693         $this->assertTrue($constraint->evaluate(0));
694         $this->assertFalse($constraint->evaluate(''));
695         $this->assertEquals('is not of type "string"', $constraint->toString());
696
697         try {
698             $constraint->fail('', '', TRUE);
699         }
700
701         catch (PHPUnit_Framework_ExpectationFailedException $e) {
702             $this->assertEquals(
703               'Failed asserting that <string:> is not of type "string".',
704               $e->getDescription()
705             );
706
707             return;
708         }
709
710         $this->fail();
711     }
712
713     public function testConstraintIsNotType2()
714     {
715         $constraint = new PHPUnit_Framework_Constraint_Not(
716           new PHPUnit_Framework_Constraint_IsType('string')
717         );
718
719         try {
720             $constraint->fail('', 'custom message', TRUE);
721         }
722
723         catch (PHPUnit_Framework_ExpectationFailedException $e) {
724             $this->assertEquals(
725               "custom message\nFailed asserting that <string:> is not of type \"string\".",
726               $e->getDescription()
727             );
728
729             return;
730         }
731
732         $this->fail();
733     }
734
735     public function testConstraintLessThan()
736     {
737         $constraint = new PHPUnit_Framework_Constraint_LessThan(1);
738
739         $this->assertTrue($constraint->evaluate(0));
740         $this->assertFalse($constraint->evaluate(2));
741         $this->assertEquals('is less than <integer:1>', $constraint->toString());
742
743         try {
744             $constraint->fail(0, '');
745         }
746
747         catch (PHPUnit_Framework_ExpectationFailedException $e) {
748             $this->assertEquals(
749               "Failed asserting that <integer:0> is less than <integer:1>.",
750               $e->getDescription()
751             );
752
753             return;
754         }
755
756         $this->fail();
757     }
758
759     public function testConstraintLessThan2()
760     {
761         $constraint = new PHPUnit_Framework_Constraint_LessThan(1);
762
763         try {
764             $constraint->fail(0, 'custom message');
765         }
766
767         catch (PHPUnit_Framework_ExpectationFailedException $e) {
768             $this->assertEquals(
769               "custom message\nFailed asserting that <integer:0> is less than <integer:1>.",
770               $e->getDescription()
771             );
772
773             return;
774         }
775
776         $this->fail();
777     }
778
779     public function testConstraintNotLessThan()
780     {
781         $constraint = new PHPUnit_Framework_Constraint_Not(
782           new PHPUnit_Framework_Constraint_LessThan(1)
783         );
784
785         $this->assertTrue($constraint->evaluate(1));
786         $this->assertEquals('is not less than <integer:1>', $constraint->toString());
787
788         try {
789             $constraint->fail(1, '', TRUE);
790         }
791
792         catch (PHPUnit_Framework_ExpectationFailedException $e) {
793             $this->assertEquals(
794               "Failed asserting that <integer:1> is not less than <integer:1>.",
795               $e->getDescription()
796             );
797
798             return;
799         }
800
801         $this->fail();
802     }
803
804     public function testConstraintNotLessThan2()
805     {
806         $constraint = new PHPUnit_Framework_Constraint_Not(
807           new PHPUnit_Framework_Constraint_LessThan(1)
808         );
809
810         try {
811             $constraint->fail(1, 'custom message', TRUE);
812         }
813
814         catch (PHPUnit_Framework_ExpectationFailedException $e) {
815             $this->assertEquals(
816               "custom message\nFailed asserting that <integer:1> is not less than <integer:1>.",
817               $e->getDescription()
818             );
819
820             return;
821         }
822
823         $this->fail();
824     }
825
826     public function testConstraintObjectHasAttribute()
827     {
828         $constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute('foo');
829
830         $this->assertFalse($constraint->evaluate(new stdClass));
831         $this->assertEquals('has attribute "foo"', $constraint->toString());
832
833         try {
834             $constraint->fail(new stdClass, '');
835         }
836
837         catch (PHPUnit_Framework_ExpectationFailedException $e) {
838             $this->assertEquals(
839               'Failed asserting that object of class "stdClass" has attribute "foo".',
840               $e->getDescription()
841             );
842
843             return;
844         }
845
846         $this->fail();
847     }
848
849     public function testConstraintObjectHasAttribute2()
850     {
851         $constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute('foo');
852
853         try {
854             $constraint->fail(new stdClass, 'custom message');
855         }
856
857         catch (PHPUnit_Framework_ExpectationFailedException $e) {
858             $this->assertEquals(
859               "custom message\nFailed asserting that object of class \"stdClass\" has attribute \"foo\".",
860               $e->getDescription()
861             );
862
863             return;
864         }
865
866         $this->fail();
867     }
868
869     public function testConstraintObjectNotHasAttribute()
870     {
871         $constraint = new PHPUnit_Framework_Constraint_Not(
872           new PHPUnit_Framework_Constraint_ObjectHasAttribute('foo')
873         );
874
875         $this->assertTrue($constraint->evaluate(new stdClass));
876         $this->assertEquals('does not have attribute "foo"', $constraint->toString());
877
878         $o = new stdClass;
879         $o->foo = 'bar';
880
881         try {
882             $constraint->fail($o, '', TRUE);
883         }
884
885         catch (PHPUnit_Framework_ExpectationFailedException $e) {
886             $this->assertEquals(
887               'Failed asserting that object of class "stdClass" does not have attribute "foo".',
888               $e->getDescription()
889             );
890
891             return;
892         }
893
894         $this->fail();
895     }
896
897     public function testConstraintObjectNotHasAttribute2()
898     {
899         $constraint = new PHPUnit_Framework_Constraint_Not(
900           new PHPUnit_Framework_Constraint_ObjectHasAttribute('foo')
901         );
902
903         $o = new stdClass;
904         $o->foo = 'bar';
905
906         try {
907             $constraint->fail($o, 'custom message', TRUE);
908         }
909
910         catch (PHPUnit_Framework_ExpectationFailedException $e) {
911             $this->assertEquals(
912               "custom message\nFailed asserting that object of class \"stdClass\" does not have attribute \"foo\".",
913               $e->getDescription()
914             );
915
916             return;
917         }
918
919         $this->fail();
920     }
921
922     public function testConstraintPCREMatch()
923     {
924         $constraint = new PHPUnit_Framework_Constraint_PCREMatch('/foo/');
925
926         $this->assertFalse($constraint->evaluate('barbazbar'));
927         $this->assertTrue($constraint->evaluate('barfoobar'));
928         $this->assertEquals('matches PCRE pattern "/foo/"', $constraint->toString());
929
930         try {
931             $constraint->fail('barbazbar', '');
932         }
933
934         catch (PHPUnit_Framework_ExpectationFailedException $e) {
935             $this->assertEquals(
936               'Failed asserting that <string:barbazbar> matches PCRE pattern "/foo/".',
937               $e->getDescription()
938             );
939
940             return;
941         }
942
943         $this->fail();
944     }
945
946     public function testConstraintPCREMatch2()
947     {
948         $constraint = new PHPUnit_Framework_Constraint_PCREMatch('/foo/');
949
950         try {
951             $constraint->fail('barbazbar', 'custom message');
952         }
953
954         catch (PHPUnit_Framework_ExpectationFailedException $e) {
955             $this->assertEquals(
956               "custom message\nFailed asserting that <string:barbazbar> matches PCRE pattern \"/foo/\".",
957               $e->getDescription()
958             );
959
960             return;
961         }
962
963         $this->fail();
964     }
965
966     public function testConstraintPCRENotMatch()
967     {
968         $constraint = new PHPUnit_Framework_Constraint_Not(
969           new PHPUnit_Framework_Constraint_PCREMatch('/foo/')
970         );
971
972         $this->assertTrue($constraint->evaluate('barbazbar'));
973         $this->assertFalse($constraint->evaluate('barfoobar'));
974         $this->assertEquals('does not match PCRE pattern "/foo/"', $constraint->toString());
975
976         try {
977             $constraint->fail('barfoobar', '', TRUE);
978         }
979
980         catch (PHPUnit_Framework_ExpectationFailedException $e) {
981             $this->assertEquals(
982               'Failed asserting that <string:barfoobar> does not match PCRE pattern "/foo/".',
983               $e->getDescription()
984             );
985
986             return;
987         }
988
989         $this->fail();
990     }
991
992     public function testConstraintPCRENotMatch2()
993     {
994         $constraint = new PHPUnit_Framework_Constraint_Not(
995           new PHPUnit_Framework_Constraint_PCREMatch('/foo/')
996         );
997
998         try {
999             $constraint->fail('barfoobar', 'custom message', TRUE);
1000         }
1001
1002         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1003             $this->assertEquals(
1004               "custom message\nFailed asserting that <string:barfoobar> does not match PCRE pattern \"/foo/\".",
1005               $e->getDescription()
1006             );
1007
1008             return;
1009         }
1010
1011         $this->fail();
1012     }
1013
1014     public function testConstraintStringContains()
1015     {
1016         $constraint = new PHPUnit_Framework_Constraint_StringContains('foo');
1017
1018         $this->assertFalse($constraint->evaluate('barbazbar'));
1019         $this->assertTrue($constraint->evaluate('barfoobar'));
1020         $this->assertEquals('contains "foo"', $constraint->toString());
1021
1022         try {
1023             $constraint->fail('barbazbar', '');
1024         }
1025
1026         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1027             $this->assertEquals(
1028               'Failed asserting that <string:barbazbar> contains "foo".',
1029               $e->getDescription()
1030             );
1031
1032             return;
1033         }
1034
1035         $this->fail();
1036     }
1037
1038     public function testConstraintStringContains2()
1039     {
1040         $constraint = new PHPUnit_Framework_Constraint_StringContains('foo');
1041
1042         try {
1043             $constraint->fail('barbazbar', 'custom message');
1044         }
1045
1046         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1047             $this->assertEquals(
1048               "custom message\nFailed asserting that <string:barbazbar> contains \"foo\".",
1049               $e->getDescription()
1050             );
1051
1052             return;
1053         }
1054
1055         $this->fail();
1056     }
1057
1058     public function testConstraintStringNotContains()
1059     {
1060         $constraint = new PHPUnit_Framework_Constraint_Not(
1061           new PHPUnit_Framework_Constraint_StringContains('foo')
1062         );
1063
1064         $this->assertTrue($constraint->evaluate('barbazbar'));
1065         $this->assertFalse($constraint->evaluate('barfoobar'));
1066         $this->assertEquals('does not contain "foo"', $constraint->toString());
1067
1068         try {
1069             $constraint->fail('barfoobar', '', TRUE);
1070         }
1071
1072         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1073             $this->assertEquals(
1074               'Failed asserting that <string:barfoobar> does not contain "foo".',
1075               $e->getDescription()
1076             );
1077
1078             return;
1079         }
1080
1081         $this->fail();
1082     }
1083
1084     public function testConstraintStringNotContains2()
1085     {
1086         $constraint = new PHPUnit_Framework_Constraint_Not(
1087           new PHPUnit_Framework_Constraint_StringContains('foo')
1088         );
1089
1090         try {
1091             $constraint->fail('barfoobar', 'custom message', TRUE);
1092         }
1093
1094         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1095             $this->assertEquals(
1096               "custom message\nFailed asserting that <string:barfoobar> does not contain \"foo\".",
1097               $e->getDescription()
1098             );
1099
1100             return;
1101         }
1102
1103         $this->fail();
1104     }
1105
1106     public function testConstraintArrayContains()
1107     {
1108         $constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo');
1109
1110         $this->assertFalse($constraint->evaluate(array('bar')));
1111         $this->assertTrue($constraint->evaluate(array('foo')));
1112         $this->assertEquals('contains <string:foo>', $constraint->toString());
1113
1114         try {
1115             $constraint->fail(array('bar'), '');
1116         }
1117
1118         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1119             $this->assertEquals(
1120               'Failed asserting that an array contains <string:foo>.',
1121               $e->getDescription()
1122             );
1123
1124             return;
1125         }
1126
1127         $this->fail();
1128     }
1129
1130     public function testConstraintArrayContains2()
1131     {
1132         $constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo');
1133
1134         try {
1135             $constraint->fail(array('bar'), 'custom message');
1136         }
1137
1138         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1139             $this->assertEquals(
1140               "custom message\nFailed asserting that an array contains <string:foo>.",
1141               $e->getDescription()
1142             );
1143
1144             return;
1145         }
1146
1147         $this->fail();
1148     }
1149
1150     public function testConstraintArrayNotContains()
1151     {
1152         $constraint = new PHPUnit_Framework_Constraint_Not(
1153           new PHPUnit_Framework_Constraint_TraversableContains('foo')
1154         );
1155
1156         $this->assertTrue($constraint->evaluate(array('bar')));
1157         $this->assertFalse($constraint->evaluate(array('foo')));
1158         $this->assertEquals('does not contain <string:foo>', $constraint->toString());
1159
1160         try {
1161             $constraint->fail(array('foo'), '', TRUE);
1162         }
1163
1164         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1165             $this->assertEquals(
1166               'Failed asserting that an array does not contain <string:foo>.',
1167               $e->getDescription()
1168             );
1169
1170             return;
1171         }
1172
1173         $this->fail();
1174     }
1175
1176     public function testConstraintArrayNotContains2()
1177     {
1178         $constraint = new PHPUnit_Framework_Constraint_Not(
1179           new PHPUnit_Framework_Constraint_TraversableContains('foo')
1180         );
1181
1182         try {
1183             $constraint->fail(array('foo'), 'custom message', TRUE);
1184         }
1185
1186         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1187             $this->assertEquals(
1188               "custom message\nFailed asserting that an array does not contain <string:foo>.",
1189               $e->getDescription()
1190             );
1191
1192             return;
1193         }
1194
1195         $this->fail();
1196     }
1197
1198     /**
1199      * @covers PHPUnit_Framework_Constraint_TraversableContains
1200      */
1201     public function testConstraintSplObjectStorageContains()
1202     {
1203         $object     = new StdClass;
1204         $constraint = new PHPUnit_Framework_Constraint_TraversableContains($object);
1205         $this->assertEquals("contains \nstdClass Object\n(\n)\n", $constraint->toString());
1206
1207         $storage = new SplObjectStorage;
1208         $this->assertFalse($constraint->evaluate($storage));
1209
1210         $storage->attach($object);
1211         $this->assertTrue($constraint->evaluate($storage));
1212
1213         try {
1214             $constraint->fail(new SplObjectStorage, '');
1215         }
1216
1217         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1218             $this->assertEquals(
1219               "Failed asserting that an iterator contains \nstdClass Object\n(\n)\n.",
1220               $e->getDescription()
1221             );
1222
1223             return;
1224         }
1225
1226         $this->fail();
1227     }
1228
1229     /**
1230      * @covers PHPUnit_Framework_Constraint_TraversableContains
1231      */
1232     public function testConstraintSplObjectStorageContains2()
1233     {
1234         $object     = new StdClass;
1235         $constraint = new PHPUnit_Framework_Constraint_TraversableContains($object);
1236
1237         try {
1238             $constraint->fail(new SplObjectStorage, 'custom message');
1239         }
1240
1241         catch (PHPUnit_Framework_ExpectationFailedException $e) {
1242             $this->assertEquals(
1243               "custom message\nFailed asserting that an iterator contains \nstdClass Object\n(\n)\n.",
1244               $e->getDescription()
1245             );
1246
1247             return;
1248         }
1249
1250         $this->fail();
1251     }
1252 }
1253 ?>