5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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
43 * @link http://www.phpunit.de/
44 * @since File available since Release 2.0.0
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49 require_once 'PHPUnit/Util/Type.php';
50 require_once 'PHPUnit/Util/XML.php';
52 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54 if (!class_exists('PHPUnit_Framework_Assert', FALSE)) {
57 * A set of assert methods.
61 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
62 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
63 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
64 * @version Release: 3.3.17
65 * @link http://www.phpunit.de/
66 * @since Class available since Release 2.0.0
69 abstract class PHPUnit_Framework_Assert
74 private static $count = 0;
77 * Asserts that an array has a specified key.
81 * @param string $message
82 * @since Method available since Release 3.0.0
84 public static function assertArrayHasKey($key, array $array, $message = '')
86 if (!(is_integer($key) || is_string($key))) {
87 throw new InvalidArgumentException;
90 $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey($key);
92 self::assertThat($array, $constraint, $message);
96 * Asserts that an array does not have a specified key.
100 * @param string $message
101 * @since Method available since Release 3.0.0
103 public static function assertArrayNotHasKey($key, array $array, $message = '')
105 if (!(is_integer($key) || is_string($key))) {
106 throw new InvalidArgumentException;
109 $constraint = new PHPUnit_Framework_Constraint_Not(
110 new PHPUnit_Framework_Constraint_ArrayHasKey($key)
113 self::assertThat($array, $constraint, $message);
117 * Asserts that a haystack contains a needle.
119 * @param mixed $needle
120 * @param mixed $haystack
121 * @param string $message
122 * @since Method available since Release 2.1.0
124 public static function assertContains($needle, $haystack, $message = '')
126 if (is_array($haystack) ||
127 is_object($haystack) && $haystack instanceof Iterator) {
128 $constraint = new PHPUnit_Framework_Constraint_TraversableContains($needle);
131 else if (is_string($haystack)) {
132 $constraint = new PHPUnit_Framework_Constraint_StringContains($needle);
136 throw new InvalidArgumentException;
139 self::assertThat($haystack, $constraint, $message);
143 * Asserts that a haystack that is stored in a static attribute of a class
144 * or an attribute of an object contains a needle.
146 * @param mixed $needle
147 * @param string $haystackAttributeName
148 * @param mixed $haystackClassOrObject
149 * @param string $message
150 * @since Method available since Release 3.0.0
152 public static function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '')
154 self::assertContains(
156 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
162 * Asserts that a haystack does not contain a needle.
164 * @param mixed $needle
165 * @param mixed $haystack
166 * @param string $message
167 * @since Method available since Release 2.1.0
169 public static function assertNotContains($needle, $haystack, $message = '')
171 if (is_array($haystack) ||
172 is_object($haystack) && $haystack instanceof Iterator) {
173 $constraint = new PHPUnit_Framework_Constraint_Not(
174 new PHPUnit_Framework_Constraint_TraversableContains($needle)
178 else if (is_string($haystack)) {
179 $constraint = new PHPUnit_Framework_Constraint_Not(
180 new PHPUnit_Framework_Constraint_StringContains($needle)
185 throw new InvalidArgumentException;
188 self::assertThat($haystack, $constraint, $message);
192 * Asserts that a haystack that is stored in a static attribute of a class
193 * or an attribute of an object does not contain a needle.
195 * @param mixed $needle
196 * @param string $haystackAttributeName
197 * @param mixed $haystackClassOrObject
198 * @param string $message
199 * @since Method available since Release 3.0.0
201 public static function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '')
203 self::assertNotContains(
205 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
211 * Asserts that a haystack contains only values of a given type.
213 * @param string $type
214 * @param mixed $haystack
215 * @param boolean $isNativeType
216 * @param string $message
217 * @since Method available since Release 3.1.4
219 public static function assertContainsOnly($type, $haystack, $isNativeType = NULL, $message = '')
221 if (!(is_array($haystack) ||
222 is_object($haystack) && $haystack instanceof Iterator)) {
223 throw new InvalidArgumentException;
226 if ($isNativeType == NULL) {
227 $isNativeType = PHPUnit_Util_Type::isType($type);
232 new PHPUnit_Framework_Constraint_TraversableContainsOnly(
240 * Asserts that a haystack that is stored in a static attribute of a class
241 * or an attribute of an object contains only values of a given type.
243 * @param string $type
244 * @param string $haystackAttributeName
245 * @param mixed $haystackClassOrObject
246 * @param boolean $isNativeType
247 * @param string $message
248 * @since Method available since Release 3.1.4
250 public static function assertAttributeContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = NULL, $message = '')
252 self::assertContainsOnly(
254 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
261 * Asserts that a haystack does not contain only values of a given type.
263 * @param string $type
264 * @param mixed $haystack
265 * @param boolean $isNativeType
266 * @param string $message
267 * @since Method available since Release 3.1.4
269 public static function assertNotContainsOnly($type, $haystack, $isNativeType = NULL, $message = '')
271 if (!(is_array($haystack) ||
272 is_object($haystack) && $haystack instanceof Iterator)) {
273 throw new InvalidArgumentException;
276 if ($isNativeType == NULL) {
277 $isNativeType = PHPUnit_Util_Type::isType($type);
282 new PHPUnit_Framework_Constraint_Not(
283 new PHPUnit_Framework_Constraint_TraversableContainsOnly(
292 * Asserts that a haystack that is stored in a static attribute of a class
293 * or an attribute of an object does not contain only values of a given type.
295 * @param string $type
296 * @param string $haystackAttributeName
297 * @param mixed $haystackClassOrObject
298 * @param boolean $isNativeType
299 * @param string $message
300 * @since Method available since Release 3.1.4
302 public static function assertAttributeNotContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = NULL, $message = '')
304 self::assertNotContainsOnly(
306 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
313 * Asserts that two variables are equal.
315 * @param mixed $expected
316 * @param mixed $actual
317 * @param string $message
318 * @param float $delta
319 * @param integer $maxDepth
320 * @param boolean $canonicalizeEol
322 public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE)
324 $constraint = new PHPUnit_Framework_Constraint_IsEqual(
325 $expected, $delta, $maxDepth, $canonicalizeEol
328 self::assertThat($actual, $constraint, $message);
332 * Asserts that a variable is equal to an attribute of an object.
334 * @param mixed $expected
335 * @param string $actualAttributeName
336 * @param string $actualClassOrObject
337 * @param string $message
338 * @param float $delta
339 * @param integer $maxDepth
340 * @param boolean $canonicalizeEol
342 public static function assertAttributeEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE)
346 self::readAttribute($actualClassOrObject, $actualAttributeName),
355 * Asserts that two variables are not equal.
357 * @param mixed $expected
358 * @param mixed $actual
359 * @param string $message
360 * @param float $delta
361 * @param integer $maxDepth
362 * @param boolean $canonicalizeEol
363 * @since Method available since Release 2.3.0
365 public static function assertNotEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE)
367 $constraint = new PHPUnit_Framework_Constraint_Not(
368 new PHPUnit_Framework_Constraint_IsEqual(
369 $expected, $delta, $maxDepth, $canonicalizeEol
373 self::assertThat($actual, $constraint, $message);
377 * Asserts that a variable is not equal to an attribute of an object.
379 * @param mixed $expected
380 * @param string $actualAttributeName
381 * @param string $actualClassOrObject
382 * @param string $message
383 * @param float $delta
384 * @param integer $maxDepth
385 * @param boolean $canonicalizeEol
387 public static function assertAttributeNotEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE)
389 self::assertNotEquals(
391 self::readAttribute($actualClassOrObject, $actualAttributeName),
400 * Asserts that a value is greater than another value.
402 * @param mixed $expected
403 * @param mixed $actual
404 * @param string $message
405 * @since Method available since Release 3.1.0
407 public static function assertGreaterThan($expected, $actual, $message = '')
409 self::assertThat($actual, self::greaterThan($expected), $message);
413 * Asserts that an attribute is greater than another value.
415 * @param mixed $expected
416 * @param string $actualAttributeName
417 * @param string $actualClassOrObject
418 * @param string $message
419 * @since Method available since Release 3.1.0
421 public static function assertAttributeGreaterThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
423 self::assertGreaterThan(
425 self::readAttribute($actualClassOrObject, $actualAttributeName),
431 * Asserts that a value is greater than or equal to another value.
433 * @param mixed $expected
434 * @param mixed $actual
435 * @param string $message
436 * @since Method available since Release 3.1.0
438 public static function assertGreaterThanOrEqual($expected, $actual, $message = '')
440 self::assertThat($actual, self::greaterThanOrEqual($expected), $message);
444 * Asserts that an attribute is greater than or equal to another value.
446 * @param mixed $expected
447 * @param string $actualAttributeName
448 * @param string $actualClassOrObject
449 * @param string $message
450 * @since Method available since Release 3.1.0
452 public static function assertAttributeGreaterThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
454 self::assertGreaterThanOrEqual(
456 self::readAttribute($actualClassOrObject, $actualAttributeName),
462 * Asserts that a value is smaller than another value.
464 * @param mixed $expected
465 * @param mixed $actual
466 * @param string $message
467 * @since Method available since Release 3.1.0
469 public static function assertLessThan($expected, $actual, $message = '')
471 self::assertThat($actual, self::lessThan($expected), $message);
475 * Asserts that an attribute is smaller than another value.
477 * @param mixed $expected
478 * @param string $actualAttributeName
479 * @param string $actualClassOrObject
480 * @param string $message
481 * @since Method available since Release 3.1.0
483 public static function assertAttributeLessThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
485 self::assertLessThan(
487 self::readAttribute($actualClassOrObject, $actualAttributeName),
493 * Asserts that a value is smaller than or equal to another value.
495 * @param mixed $expected
496 * @param mixed $actual
497 * @param string $message
498 * @since Method available since Release 3.1.0
500 public static function assertLessThanOrEqual($expected, $actual, $message = '')
502 self::assertThat($actual, self::lessThanOrEqual($expected), $message);
506 * Asserts that an attribute is smaller than or equal to another value.
508 * @param mixed $expected
509 * @param string $actualAttributeName
510 * @param string $actualClassOrObject
511 * @param string $message
512 * @since Method available since Release 3.1.0
514 public static function assertAttributeLessThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
516 self::assertLessThanOrEqual(
518 self::readAttribute($actualClassOrObject, $actualAttributeName),
524 * Asserts that the contents of one file is equal to the contents of another
527 * @param string $expected
528 * @param string $actual
529 * @param string $message
530 * @param boolean $canonicalizeEol
531 * @since Method available since Release 3.2.14
533 public static function assertFileEquals($expected, $actual, $message = '', $canonicalizeEol = FALSE)
535 self::assertFileExists($expected, $message);
536 self::assertFileExists($actual, $message);
539 file_get_contents($expected),
540 file_get_contents($actual),
549 * Asserts that the contents of one file is not equal to the contents of
552 * @param string $expected
553 * @param string $actual
554 * @param string $message
555 * @param boolean $canonicalizeEol
556 * @since Method available since Release 3.2.14
558 public static function assertFileNotEquals($expected, $actual, $message = '', $canonicalizeEol = FALSE)
560 self::assertFileExists($expected, $message);
561 self::assertFileExists($actual, $message);
563 self::assertNotEquals(
564 file_get_contents($expected),
565 file_get_contents($actual),
574 * Asserts that the contents of a string is equal
575 * to the contents of a file.
577 * @param string $expectedFile
578 * @param string $actualString
579 * @param string $message
580 * @param boolean $canonicalizeEol
581 * @since Method available since Release 3.3.0
583 public static function assertStringEqualsFile($expectedFile, $actualString, $message = '', $canonicalizeEol = FALSE)
585 self::assertFileExists($expectedFile, $message);
588 file_get_contents($expectedFile),
598 * Asserts that the contents of a string is not equal
599 * to the contents of a file.
601 * @param string $expectedFile
602 * @param string $actualString
603 * @param string $message
604 * @param boolean $canonicalizeEol
605 * @since Method available since Release 3.3.0
607 public static function assertStringNotEqualsFile($expectedFile, $actualString, $message = '', $canonicalizeEol = FALSE)
609 self::assertFileExists($expectedFile, $message);
611 self::assertNotEquals(
612 file_get_contents($expectedFile),
622 * Asserts that a file exists.
624 * @param string $filename
625 * @param string $message
626 * @since Method available since Release 3.0.0
628 public static function assertFileExists($filename, $message = '')
630 if (!is_string($filename)) {
631 throw new InvalidArgumentException;
634 $constraint = new PHPUnit_Framework_Constraint_FileExists;
636 self::assertThat($filename, $constraint, $message);
640 * Asserts that a file does not exist.
642 * @param string $filename
643 * @param string $message
644 * @since Method available since Release 3.0.0
646 public static function assertFileNotExists($filename, $message = '')
648 if (!is_string($filename)) {
649 throw new InvalidArgumentException;
652 $constraint = new PHPUnit_Framework_Constraint_Not(
653 new PHPUnit_Framework_Constraint_FileExists
656 self::assertThat($filename, $constraint, $message);
660 * Asserts that a condition is true.
662 * @param boolean $condition
663 * @param string $message
664 * @throws PHPUnit_Framework_AssertionFailedError
666 public static function assertTrue($condition, $message = '')
668 self::assertThat($condition, self::isTrue(), $message);
672 * Asserts that a condition is false.
674 * @param boolean $condition
675 * @param string $message
676 * @throws PHPUnit_Framework_AssertionFailedError
678 public static function assertFalse($condition, $message = '')
680 self::assertThat($condition, self::isFalse(), $message);
684 * Asserts that a variable is not NULL.
686 * @param mixed $actual
687 * @param string $message
689 public static function assertNotNull($actual, $message = '')
691 self::assertThat($actual, self::logicalNot(self::isNull()), $message);
695 * Asserts that a variable is NULL.
697 * @param mixed $actual
698 * @param string $message
700 public static function assertNull($actual, $message = '')
702 self::assertThat($actual, self::isNull(), $message);
706 * Asserts that a class has a specified attribute.
708 * @param string $attributeName
709 * @param string $className
710 * @param string $message
711 * @since Method available since Release 3.1.0
713 public static function assertClassHasAttribute($attributeName, $className, $message = '')
715 if (!is_string($attributeName) || !is_string($className) || !class_exists($className, FALSE)) {
716 throw new InvalidArgumentException;
719 $constraint = new PHPUnit_Framework_Constraint_ClassHasAttribute($attributeName);
721 self::assertThat($className, $constraint, $message);
725 * Asserts that a class does not have a specified attribute.
727 * @param string $attributeName
728 * @param string $className
729 * @param string $message
730 * @since Method available since Release 3.1.0
732 public static function assertClassNotHasAttribute($attributeName, $className, $message = '')
734 if (!is_string($attributeName) || !is_string($className) || !class_exists($className)) {
735 throw new InvalidArgumentException;
738 $constraint = new PHPUnit_Framework_Constraint_Not(
739 new PHPUnit_Framework_Constraint_ClassHasAttribute($attributeName)
742 self::assertThat($className, $constraint, $message);
746 * Asserts that a class has a specified static attribute.
748 * @param string $attributeName
749 * @param string $className
750 * @param string $message
751 * @since Method available since Release 3.1.0
753 public static function assertClassHasStaticAttribute($attributeName, $className, $message = '')
755 if (!is_string($attributeName) || !is_string($className) || !class_exists($className)) {
756 throw new InvalidArgumentException;
759 $constraint = new PHPUnit_Framework_Constraint_ClassHasStaticAttribute($attributeName);
761 self::assertThat($className, $constraint, $message);
765 * Asserts that a class does not have a specified static attribute.
767 * @param string $attributeName
768 * @param string $className
769 * @param string $message
770 * @since Method available since Release 3.1.0
772 public static function assertClassNotHasStaticAttribute($attributeName, $className, $message = '')
774 if (!is_string($attributeName) || !is_string($className) || !class_exists($className)) {
775 throw new InvalidArgumentException;
778 $constraint = new PHPUnit_Framework_Constraint_Not(
779 new PHPUnit_Framework_Constraint_ClassHasStaticAttribute($attributeName)
782 self::assertThat($className, $constraint, $message);
786 * Asserts that an object has a specified attribute.
788 * @param string $attributeName
789 * @param object $object
790 * @param string $message
791 * @since Method available since Release 3.0.0
793 public static function assertObjectHasAttribute($attributeName, $object, $message = '')
795 if (!is_string($attributeName) || !is_object($object)) {
796 throw new InvalidArgumentException;
799 $constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName);
801 self::assertThat($object, $constraint, $message);
805 * Asserts that an object does not have a specified attribute.
807 * @param string $attributeName
808 * @param object $object
809 * @param string $message
810 * @since Method available since Release 3.0.0
812 public static function assertObjectNotHasAttribute($attributeName, $object, $message = '')
814 if (!is_string($attributeName) || !is_object($object)) {
815 throw new InvalidArgumentException;
818 $constraint = new PHPUnit_Framework_Constraint_Not(
819 new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName)
822 self::assertThat($object, $constraint, $message);
826 * Asserts that two variables have the same type and value.
827 * Used on objects, it asserts that two variables reference
830 * @param mixed $expected
831 * @param mixed $actual
832 * @param string $message
834 public static function assertSame($expected, $actual, $message = '')
836 if (is_bool($expected) && is_bool($actual)) {
837 self::assertEquals($expected, $actual, $message);
839 $constraint = new PHPUnit_Framework_Constraint_IsIdentical($expected);
841 self::assertThat($actual, $constraint, $message);
846 * Asserts that a variable and an attribute of an object have the same type
849 * @param mixed $expected
850 * @param string $actualAttributeName
851 * @param object $actualClassOrObject
852 * @param string $message
854 public static function assertAttributeSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
858 self::readAttribute($actualClassOrObject, $actualAttributeName),
864 * Asserts that two variables do not have the same type and value.
865 * Used on objects, it asserts that two variables do not reference
868 * @param mixed $expected
869 * @param mixed $actual
870 * @param string $message
872 public static function assertNotSame($expected, $actual, $message = '')
874 if (is_bool($expected) && is_bool($actual)) {
875 self::assertNotEquals($expected, $actual, $message);
877 $constraint = new PHPUnit_Framework_Constraint_Not(
878 new PHPUnit_Framework_Constraint_IsIdentical($expected)
881 self::assertThat($actual, $constraint, $message);
886 * Asserts that a variable and an attribute of an object do not have the
887 * same type and value.
889 * @param mixed $expected
890 * @param string $actualAttributeName
891 * @param object $actualClassOrObject
892 * @param string $message
894 public static function assertAttributeNotSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
898 self::readAttribute($actualClassOrObject, $actualAttributeName),
904 * Asserts that a variable is of a given type.
906 * @param string $expected
907 * @param mixed $actual
908 * @param string $message
910 public static function assertType($expected, $actual, $message = '')
912 if (is_string($expected)) {
913 if (PHPUnit_Util_Type::isType($expected)) {
914 $constraint = new PHPUnit_Framework_Constraint_IsType($expected);
917 else if (class_exists($expected) || interface_exists($expected)) {
918 $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
924 throw new InvalidArgumentException;
927 throw new InvalidArgumentException;
930 self::assertThat($actual, $constraint, $message);
934 * Asserts that a variable is not of a given type.
936 * @param string $expected
937 * @param mixed $actual
938 * @param string $message
939 * @since Method available since Release 2.2.0
941 public static function assertNotType($expected, $actual, $message = '')
943 if (is_string($expected)) {
944 if (PHPUnit_Util_Type::isType($expected)) {
945 $constraint = new PHPUnit_Framework_Constraint_Not(
946 new PHPUnit_Framework_Constraint_IsType($expected)
950 else if (class_exists($expected) || interface_exists($expected)) {
951 $constraint = new PHPUnit_Framework_Constraint_Not(
952 new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
957 throw new InvalidArgumentException;
960 throw new InvalidArgumentException;
963 self::assertThat($actual, $constraint, $message);
967 * Asserts that a string matches a given regular expression.
969 * @param string $pattern
970 * @param string $string
971 * @param string $message
973 public static function assertRegExp($pattern, $string, $message = '')
975 if (!is_string($pattern) || !is_string($string)) {
976 throw new InvalidArgumentException;
979 $constraint = new PHPUnit_Framework_Constraint_PCREMatch($pattern);
981 self::assertThat($string, $constraint, $message);
985 * Asserts that a string does not match a given regular expression.
987 * @param string $pattern
988 * @param string $string
989 * @param string $message
990 * @since Method available since Release 2.1.0
992 public static function assertNotRegExp($pattern, $string, $message = '')
994 if (!is_string($pattern) || !is_string($string)) {
995 throw new InvalidArgumentException;
998 $constraint = new PHPUnit_Framework_Constraint_Not(
999 new PHPUnit_Framework_Constraint_PCREMatch($pattern)
1002 self::assertThat($string, $constraint, $message);
1006 * Asserts that two XML files are equal.
1008 * @param string $expectedFile
1009 * @param string $actualFile
1010 * @param string $message
1011 * @since Method available since Release 3.1.0
1013 public static function assertXmlFileEqualsXmlFile($expectedFile, $actualFile, $message = '')
1015 self::assertFileExists($expectedFile);
1016 self::assertFileExists($actualFile);
1018 $expected = new DOMDocument;
1019 $expected->preserveWhiteSpace = FALSE;
1020 $expected->load($expectedFile);
1022 $actual = new DOMDocument;
1023 $actual->preserveWhiteSpace = FALSE;
1024 $actual->load($actualFile);
1026 self::assertEquals($expected, $actual, $message);
1030 * Asserts that two XML files are not equal.
1032 * @param string $expectedFile
1033 * @param string $actualFile
1034 * @param string $message
1035 * @since Method available since Release 3.1.0
1037 public static function assertXmlFileNotEqualsXmlFile($expectedFile, $actualFile, $message = '')
1039 self::assertFileExists($expectedFile);
1040 self::assertFileExists($actualFile);
1042 $expected = new DOMDocument;
1043 $expected->preserveWhiteSpace = FALSE;
1044 $expected->load($expectedFile);
1046 $actual = new DOMDocument;
1047 $actual->preserveWhiteSpace = FALSE;
1048 $actual->load($actualFile);
1050 self::assertNotEquals($expected, $actual, $message);
1054 * Asserts that two XML documents are equal.
1056 * @param string $expectedFile
1057 * @param string $actualXml
1058 * @param string $message
1059 * @since Method available since Release 3.3.0
1061 public static function assertXmlStringEqualsXmlFile($expectedFile, $actualXml, $message = '')
1063 self::assertFileExists($expectedFile);
1065 $expected = new DOMDocument;
1066 $expected->preserveWhiteSpace = FALSE;
1067 $expected->load($expectedFile);
1069 $actual = new DOMDocument;
1070 $actual->preserveWhiteSpace = FALSE;
1071 $actual->loadXML($actualXml);
1073 self::assertEquals($expected, $actual, $message);
1077 * Asserts that two XML documents are not equal.
1079 * @param string $expectedFile
1080 * @param string $actualXml
1081 * @param string $message
1082 * @since Method available since Release 3.3.0
1084 public static function assertXmlStringNotEqualsXmlFile($expectedFile, $actualXml, $message = '')
1086 self::assertFileExists($expectedFile);
1088 $expected = new DOMDocument;
1089 $expected->preserveWhiteSpace = FALSE;
1090 $expected->load($expectedFile);
1092 $actual = new DOMDocument;
1093 $actual->preserveWhiteSpace = FALSE;
1094 $actual->loadXML($actualXml);
1096 self::assertNotEquals($expected, $actual, $message);
1100 * Asserts that two XML documents are equal.
1102 * @param string $expectedXml
1103 * @param string $actualXml
1104 * @param string $message
1105 * @since Method available since Release 3.1.0
1107 public static function assertXmlStringEqualsXmlString($expectedXml, $actualXml, $message = '')
1109 $expected = new DOMDocument;
1110 $expected->preserveWhiteSpace = FALSE;
1111 $expected->loadXML($expectedXml);
1113 $actual = new DOMDocument;
1114 $actual->preserveWhiteSpace = FALSE;
1115 $actual->loadXML($actualXml);
1117 self::assertEquals($expected, $actual, $message);
1121 * Asserts that two XML documents are not equal.
1123 * @param string $expectedXml
1124 * @param string $actualXml
1125 * @param string $message
1126 * @since Method available since Release 3.1.0
1128 public static function assertXmlStringNotEqualsXmlString($expectedXml, $actualXml, $message = '')
1130 $expected = new DOMDocument;
1131 $expected->preserveWhiteSpace = FALSE;
1132 $expected->loadXML($expectedXml);
1134 $actual = new DOMDocument;
1135 $actual->preserveWhiteSpace = FALSE;
1136 $actual->loadXML($actualXml);
1138 self::assertNotEquals($expected, $actual, $message);
1142 * Asserts that a hierarchy of DOMNodes matches.
1144 * @param DOMNode $expectedNode
1145 * @param DOMNode $actualNode
1146 * @param boolean $checkAttributes
1147 * @param string $message
1148 * @author Mattis Stordalen Flister <mattis@xait.no>
1149 * @since Method available since Release 3.3.0
1151 public static function assertEqualXMLStructure(DOMNode $expectedNode, DOMNode $actualNode, $checkAttributes = FALSE, $message = '')
1154 $expectedNode->tagName,
1155 $actualNode->tagName,
1159 if ($checkAttributes) {
1161 $expectedNode->attributes->length,
1162 $actualNode->attributes->length,
1164 '%s%sNumber of attributes on node "%s" does not match',
1166 !empty($message) ? "\n" : '',
1167 $expectedNode->tagName
1171 for ($i = 0 ; $i < $expectedNode->attributes->length; $i++) {
1172 $expectedAttribute = $expectedNode->attributes->item($i);
1173 $actualAttribute = $actualNode->attributes->getNamedItem($expectedAttribute->name);
1175 if (!$actualAttribute) {
1178 '%s%sCould not find attribute "%s" on node "%s"',
1180 !empty($message) ? "\n" : '',
1181 $expectedAttribute->name,
1182 $expectedNode->tagName
1189 PHPUnit_Util_XML::removeCharacterDataNodes($expectedNode);
1190 PHPUnit_Util_XML::removeCharacterDataNodes($actualNode);
1193 $expectedNode->childNodes->length,
1194 $actualNode->childNodes->length,
1196 '%s%sNumber of child nodes of "%s" differs',
1198 !empty($message) ? "\n" : '',
1199 $expectedNode->tagName
1203 for ($i = 0; $i < $expectedNode->childNodes->length; $i++) {
1204 self::assertEqualXMLStructure(
1205 $expectedNode->childNodes->item($i),
1206 $actualNode->childNodes->item($i),
1214 * Assert the presence, absence, or count of elements in a document matching
1215 * the CSS $selector, regardless of the contents of those elements.
1217 * The first argument, $selector, is the CSS selector used to match
1218 * the elements in the $actual document.
1220 * The second argument, $count, can be either boolean or numeric. When boolean,
1221 * it asserts for presence of elements matching the selector (TRUE) or absence
1222 * of elements (FALSE). When numeric, it asserts the count of elements
1224 * assertSelectCount("#binder", true, $xml); // any?
1225 * assertSelectCount(".binder", 3, $xml); // exactly 3?
1227 * @param array $selector
1228 * @param integer $count
1229 * @param mixed $actual
1230 * @param string $message
1231 * @param boolean $isHtml
1232 * @since Method available since Release 3.3.0
1233 * @author Mike Naberezny <mike@maintainable.com>
1234 * @author Derek DeVries <derek@maintainable.com>
1236 public static function assertSelectCount($selector, $count, $actual, $message = '', $isHtml = TRUE)
1238 self::assertSelectEquals($selector, TRUE, $count, $actual, $message, $isHtml);
1242 * assertSelectRegExp("#binder .name", "/Mike|Derek/", true, $xml); // any?
1243 * assertSelectRegExp("#binder .name", "/Mike|Derek/", 3, $xml); // exactly 3?
1245 * @param array $selector
1246 * @param string $pattern
1247 * @param integer $count
1248 * @param mixed $actual
1249 * @param string $message
1250 * @param boolean $isHtml
1251 * @since Method available since Release 3.3.0
1252 * @author Mike Naberezny <mike@maintainable.com>
1253 * @author Derek DeVries <derek@maintainable.com>
1255 public static function assertSelectRegExp($selector, $pattern, $count, $actual, $message = '', $isHtml = TRUE)
1257 self::assertSelectEquals($selector, "regexp:$pattern", $count, $actual, $message, $isHtml);
1261 * assertSelectEquals("#binder .name", "Chuck", true, $xml); // any?
1262 * assertSelectEquals("#binder .name", "Chuck", false, $xml); // none?
1264 * @param array $selector
1265 * @param string $content
1266 * @param integer $count
1267 * @param mixed $actual
1268 * @param string $message
1269 * @param boolean $isHtml
1270 * @since Method available since Release 3.3.0
1271 * @author Mike Naberezny <mike@maintainable.com>
1272 * @author Derek DeVries <derek@maintainable.com>
1274 public static function assertSelectEquals($selector, $content, $count, $actual, $message = '', $isHtml = TRUE)
1276 $tags = PHPUnit_Util_XML::cssSelect($selector, $content, $actual, $isHtml);
1278 // assert specific number of elements
1279 if (is_numeric($count)) {
1280 $counted = $tags ? count($tags) : 0;
1281 self::assertEquals($count, $counted);
1283 // assert any elements exist if true, assert no elements exist if false
1284 } else if (is_bool($count)) {
1285 $any = count($tags) > 0 && $tags[0] instanceof DOMNode;
1288 self::assertTrue($any, $message);
1290 self::assertFalse($any, $message);
1293 // check for range number of elements
1294 } else if (is_array($count) && (isset($count['>']) || isset($count['<']) ||
1295 isset($count['>=']) || isset($count['<=']))) {
1296 $counted = $tags ? count($tags) : 0;
1298 if (isset($count['>'])) {
1299 self::assertTrue($counted > $count['>'], $message);
1302 if (isset($count['>='])) {
1303 self::assertTrue($counted >= $count['>='], $message);
1306 if (isset($count['<'])) {
1307 self::assertTrue($counted < $count['<'], $message);
1310 if (isset($count['<='])) {
1311 self::assertTrue($counted <= $count['<='], $message);
1315 throw new InvalidArgumentException();
1320 * Evaluate an HTML or XML string and assert its structure and/or contents.
1322 * The first argument ($matcher) is an associative array that specifies the
1323 * match criteria for the assertion:
1325 * - `id` : the node with the given id attribute must match the corresponsing value.
1326 * - `tag` : the node type must match the corresponding value.
1327 * - `attributes` : a hash. The node's attributres must match the corresponsing values in the hash.
1328 * - `content` : The text content must match the given value.
1329 * - `parent` : a hash. The node's parent must match the corresponsing hash.
1330 * - `child` : a hash. At least one of the node's immediate children must meet the criteria described by the hash.
1331 * - `ancestor` : a hash. At least one of the node's ancestors must meet the criteria described by the hash.
1332 * - `descendant` : a hash. At least one of the node's descendants must meet the criteria described by the hash.
1333 * - `children` : a hash, for counting children of a node. Accepts the keys:
1334 * - `count` : a number which must equal the number of children that match
1335 * - `less_than` : the number of matching children must be greater than this number
1336 * - `greater_than` : the number of matching children must be less than this number
1337 * - `only` : another hash consisting of the keys to use to match on the children, and only matching children will be counted
1340 * // Matcher that asserts that there is an element with an id="my_id".
1341 * $matcher = array('id' => 'my_id');
1343 * // Matcher that asserts that there is a "span" tag.
1344 * $matcher = array('tag' => 'span');
1346 * // Matcher that asserts that there is a "span" tag with the content
1348 * $matcher = array('tag' => 'span', 'content' => 'Hello World');
1350 * // Matcher that asserts that there is a "span" tag with content matching the
1351 * // regular expression pattern.
1352 * $matcher = array('tag' => 'span', 'content' => '/Try P(HP|ython)/');
1354 * // Matcher that asserts that there is a "span" with an "list" class attribute.
1357 * 'attributes' => array('class' => 'list')
1360 * // Matcher that asserts that there is a "span" inside of a "div".
1363 * 'parent' => array('tag' => 'div')
1366 * // Matcher that asserts that there is a "span" somewhere inside a "table".
1369 * 'ancestor' => array('tag' => 'table')
1372 * // Matcher that asserts that there is a "span" with at least one "em" child.
1375 * 'child' => array('tag' => 'em')
1378 * // Matcher that asserts that there is a "span" containing a (possibly nested)
1382 * 'descendant' => array('tag' => 'strong')
1385 * // Matcher that asserts that there is a "span" containing 5-10 "em" tags as
1386 * // immediate children.
1389 * 'children' => array(
1390 * 'less_than' => 11,
1391 * 'greater_than' => 4,
1392 * 'only' => array('tag' => 'em')
1396 * // Matcher that asserts that there is a "div", with an "ul" ancestor and a "li"
1397 * // parent (with class="enum"), and containing a "span" descendant that contains
1398 * // an element with id="my_test" and the text "Hello World".
1401 * 'ancestor' => array('tag' => 'ul'),
1402 * 'parent' => array(
1404 * 'attributes' => array('class' => 'enum')
1406 * 'descendant' => array(
1409 * 'id' => 'my_test',
1410 * 'content' => 'Hello World'
1415 * // Use assertTag() to apply a $matcher to a piece of $html.
1416 * $this->assertTag($matcher, $html);
1418 * // Use assertTag() to apply a $matcher to a piece of $xml.
1419 * $this->assertTag($matcher, $xml, '', FALSE);
1422 * The second argument ($actual) is a string containing either HTML or
1423 * XML text to be tested.
1425 * The third argument ($message) is an optional message that will be
1426 * used if the assertion fails.
1428 * The fourth argument ($html) is an optional flag specifying whether
1429 * to load the $actual string into a DOMDocument using the HTML or
1430 * XML load strategy. It is TRUE by default, which assumes the HTML
1431 * load strategy. In many cases, this will be acceptable for XML as well.
1433 * @param array $matcher
1434 * @param string $actual
1435 * @param string $message
1436 * @param boolean $isHtml
1437 * @since Method available since Release 3.3.0
1438 * @author Mike Naberezny <mike@maintainable.com>
1439 * @author Derek DeVries <derek@maintainable.com>
1441 public static function assertTag($matcher, $actual, $message = '', $isHtml = TRUE)
1443 $dom = PHPUnit_Util_XML::load($actual, $isHtml);
1444 $tags = PHPUnit_Util_XML::findNodes($dom, $matcher);
1445 $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
1447 self::assertTrue($matched, $message);
1451 * This assertion is the exact opposite of assertTag(). Rather than asserting
1452 * that $matcher results in a match, it asserts that $matcher does not match.
1454 * @param array $matcher
1455 * @param string $actual
1456 * @param string $message
1457 * @param boolean $isHtml
1458 * @since Method available since Release 3.3.0
1459 * @author Mike Naberezny <mike@maintainable.com>
1460 * @author Derek DeVries <derek@maintainable.com>
1462 public static function assertNotTag($matcher, $actual, $message = '', $isHtml = TRUE)
1464 $dom = PHPUnit_Util_XML::load($actual, $isHtml);
1465 $tags = PHPUnit_Util_XML::findNodes($dom, $matcher);
1466 $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
1468 self::assertFalse($matched, $message);
1472 * Evaluates a PHPUnit_Framework_Constraint matcher object.
1474 * @param mixed $value
1475 * @param PHPUnit_Framework_Constraint $constraint
1476 * @param string $message
1477 * @since Method available since Release 3.0.0
1479 public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '')
1483 if (!$constraint->evaluate($value)) {
1484 $constraint->fail($value, $message);
1489 * Returns a PHPUnit_Framework_Constraint_And matcher object.
1491 * @return PHPUnit_Framework_Constraint_And
1492 * @since Method available since Release 3.0.0
1494 public static function logicalAnd()
1496 $constraints = func_get_args();
1498 $constraint = new PHPUnit_Framework_Constraint_And;
1499 $constraint->setConstraints($constraints);
1505 * Returns a PHPUnit_Framework_Constraint_Or matcher object.
1507 * @return PHPUnit_Framework_Constraint_Or
1508 * @since Method available since Release 3.0.0
1510 public static function logicalOr()
1512 $constraints = func_get_args();
1514 $constraint = new PHPUnit_Framework_Constraint_Or;
1515 $constraint->setConstraints($constraints);
1521 * Returns a PHPUnit_Framework_Constraint_Not matcher object.
1523 * @param PHPUnit_Framework_Constraint $constraint
1524 * @return PHPUnit_Framework_Constraint_Not
1525 * @since Method available since Release 3.0.0
1527 public static function logicalNot(PHPUnit_Framework_Constraint $constraint)
1529 return new PHPUnit_Framework_Constraint_Not($constraint);
1533 * Returns a PHPUnit_Framework_Constraint_Xor matcher object.
1535 * @return PHPUnit_Framework_Constraint_Xor
1536 * @since Method available since Release 3.0.0
1538 public static function logicalXor()
1540 $constraints = func_get_args();
1542 $constraint = new PHPUnit_Framework_Constraint_Xor;
1543 $constraint->setConstraints($constraints);
1549 * Returns a PHPUnit_Framework_Constraint_IsAnything matcher object.
1551 * @return PHPUnit_Framework_Constraint_IsAnything
1552 * @since Method available since Release 3.0.0
1554 public static function anything()
1556 return new PHPUnit_Framework_Constraint_IsAnything;
1560 * Returns a PHPUnit_Framework_Constraint_IsTrue matcher object.
1562 * @return PHPUnit_Framework_Constraint_IsTrue
1563 * @since Method available since Release 3.3.0
1565 public static function isTrue()
1567 return new PHPUnit_Framework_Constraint_IsTrue;
1571 * Returns a PHPUnit_Framework_Constraint_IsFalse matcher object.
1573 * @return PHPUnit_Framework_Constraint_IsFalse
1574 * @since Method available since Release 3.3.0
1576 public static function isFalse()
1578 return new PHPUnit_Framework_Constraint_IsFalse;
1582 * Returns a PHPUnit_Framework_Constraint_IsNull matcher object.
1584 * @return PHPUnit_Framework_Constraint_IsNull
1585 * @since Method available since Release 3.3.0
1587 public static function isNull()
1589 return new PHPUnit_Framework_Constraint_IsNull;
1593 * Returns a PHPUnit_Framework_Constraint_Attribute matcher object.
1595 * @param PHPUnit_Framework_Constraint $constraint
1596 * @param string $attributeName
1597 * @return PHPUnit_Framework_Constraint_Attribute
1598 * @since Method available since Release 3.1.0
1600 public static function attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)
1602 return new PHPUnit_Framework_Constraint_Attribute(
1603 $constraint, $attributeName
1608 * Returns a PHPUnit_Framework_Constraint_TraversableContains matcher
1611 * @param mixed $value
1612 * @return PHPUnit_Framework_Constraint_TraversableContains
1613 * @since Method available since Release 3.0.0
1615 public static function contains($value)
1617 return new PHPUnit_Framework_Constraint_TraversableContains($value);
1621 * Returns a PHPUnit_Framework_Constraint_TraversableContainsOnly matcher
1624 * @param string $type
1625 * @return PHPUnit_Framework_Constraint_TraversableContainsOnly
1626 * @since Method available since Release 3.1.4
1628 public static function containsOnly($type)
1630 return new PHPUnit_Framework_Constraint_TraversableContainsOnly($type);
1634 * Returns a PHPUnit_Framework_Constraint_ArrayHasKey matcher object.
1637 * @return PHPUnit_Framework_Constraint_ArrayHasKey
1638 * @since Method available since Release 3.0.0
1640 public static function arrayHasKey($key)
1642 return new PHPUnit_Framework_Constraint_ArrayHasKey($key);
1646 * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object.
1648 * @param mixed $value
1649 * @param float $delta
1650 * @param integer $maxDepth
1651 * @return PHPUnit_Framework_Constraint_IsEqual
1652 * @since Method available since Release 3.0.0
1654 public static function equalTo($value, $delta = 0, $maxDepth = 10)
1656 return new PHPUnit_Framework_Constraint_IsEqual($value, $delta, $maxDepth);
1660 * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object
1661 * that is wrapped in a PHPUnit_Framework_Constraint_Attribute matcher
1664 * @param string $attributeName
1665 * @param mixed $value
1666 * @param float $delta
1667 * @param integer $maxDepth
1668 * @return PHPUnit_Framework_Constraint_Attribute
1669 * @since Method available since Release 3.1.0
1671 public static function attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10)
1673 return new PHPUnit_Framework_Constraint_Attribute(
1674 new PHPUnit_Framework_Constraint_IsEqual($value, $delta, $maxDepth),
1680 * Returns a PHPUnit_Framework_Constraint_FileExists matcher object.
1682 * @return PHPUnit_Framework_Constraint_FileExists
1683 * @since Method available since Release 3.0.0
1685 public static function fileExists()
1687 return new PHPUnit_Framework_Constraint_FileExists;
1691 * Returns a PHPUnit_Framework_Constraint_GreaterThan matcher object.
1693 * @param mixed $value
1694 * @return PHPUnit_Framework_Constraint_GreaterThan
1695 * @since Method available since Release 3.0.0
1697 public static function greaterThan($value)
1699 return new PHPUnit_Framework_Constraint_GreaterThan($value);
1703 * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
1704 * a PHPUnit_Framework_Constraint_IsEqual and a
1705 * PHPUnit_Framework_Constraint_GreaterThan matcher object.
1707 * @param mixed $value
1708 * @return PHPUnit_Framework_Constraint_Or
1709 * @since Method available since Release 3.1.0
1711 public static function greaterThanOrEqual($value)
1713 return self::logicalOr(
1714 new PHPUnit_Framework_Constraint_IsEqual($value),
1715 new PHPUnit_Framework_Constraint_GreaterThan($value)
1720 * Returns a PHPUnit_Framework_Constraint_ClassHasAttribute matcher object.
1722 * @param string $attributeName
1723 * @return PHPUnit_Framework_Constraint_ClassHasAttribute
1724 * @since Method available since Release 3.1.0
1726 public static function classHasAttribute($attributeName)
1728 return new PHPUnit_Framework_Constraint_ClassHasAttribute($attributeName);
1732 * Returns a PHPUnit_Framework_Constraint_ClassHasStaticAttribute matcher
1735 * @param string $attributeName
1736 * @return PHPUnit_Framework_Constraint_ClassHasStaticAttribute
1737 * @since Method available since Release 3.1.0
1739 public static function classHasStaticAttribute($attributeName)
1741 return new PHPUnit_Framework_Constraint_ClassHasStaticAttribute($attributeName);
1745 * Returns a PHPUnit_Framework_Constraint_ObjectHasAttribute matcher object.
1747 * @param string $attributeName
1748 * @return PHPUnit_Framework_Constraint_ObjectHasAttribute
1749 * @since Method available since Release 3.0.0
1751 public static function objectHasAttribute($attributeName)
1753 return new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName);
1757 * Returns a PHPUnit_Framework_Constraint_IsIdentical matcher object.
1759 * @param mixed $value
1760 * @return PHPUnit_Framework_Constraint_IsIdentical
1761 * @since Method available since Release 3.0.0
1763 public static function identicalTo($value)
1765 return new PHPUnit_Framework_Constraint_IsIdentical($value);
1769 * Returns a PHPUnit_Framework_Constraint_IsInstanceOf matcher object.
1771 * @param string $className
1772 * @return PHPUnit_Framework_Constraint_IsInstanceOf
1773 * @since Method available since Release 3.0.0
1775 public static function isInstanceOf($className)
1777 return new PHPUnit_Framework_Constraint_IsInstanceOf($className);
1781 * Returns a PHPUnit_Framework_Constraint_IsType matcher object.
1783 * @param string $type
1784 * @return PHPUnit_Framework_Constraint_IsType
1785 * @since Method available since Release 3.0.0
1787 public static function isType($type)
1789 return new PHPUnit_Framework_Constraint_IsType($type);
1793 * Returns a PHPUnit_Framework_Constraint_LessThan matcher object.
1795 * @param mixed $value
1796 * @return PHPUnit_Framework_Constraint_LessThan
1797 * @since Method available since Release 3.0.0
1799 public static function lessThan($value)
1801 return new PHPUnit_Framework_Constraint_LessThan($value);
1805 * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
1806 * a PHPUnit_Framework_Constraint_IsEqual and a
1807 * PHPUnit_Framework_Constraint_LessThan matcher object.
1809 * @param mixed $value
1810 * @return PHPUnit_Framework_Constraint_Or
1811 * @since Method available since Release 3.1.0
1813 public static function lessThanOrEqual($value)
1815 return self::logicalOr(
1816 new PHPUnit_Framework_Constraint_IsEqual($value),
1817 new PHPUnit_Framework_Constraint_LessThan($value)
1822 * Returns a PHPUnit_Framework_Constraint_PCREMatch matcher object.
1824 * @param string $pattern
1825 * @return PHPUnit_Framework_Constraint_PCREMatch
1826 * @since Method available since Release 3.0.0
1828 public static function matchesRegularExpression($pattern)
1830 return new PHPUnit_Framework_Constraint_PCREMatch($pattern);
1834 * Returns a PHPUnit_Framework_Constraint_StringContains matcher object.
1836 * @param string $string
1837 * @param boolean $case
1838 * @return PHPUnit_Framework_Constraint_StringContains
1839 * @since Method available since Release 3.0.0
1841 public static function stringContains($string, $case = TRUE)
1843 return new PHPUnit_Framework_Constraint_StringContains($string, $case);
1848 * Fails a test with the given message.
1850 * @param string $message
1851 * @throws PHPUnit_Framework_AssertionFailedError
1853 public static function fail($message = '')
1855 throw new PHPUnit_Framework_AssertionFailedError($message);
1859 * Returns the value of an attribute of a class or an object.
1860 * This also works for attributes that are declared protected or private.
1862 * @param mixed $classOrObject
1863 * @param string $attributeName
1865 * @throws InvalidArgumentException
1867 public static function readAttribute($classOrObject, $attributeName)
1869 if (!is_string($attributeName)) {
1870 throw new InvalidArgumentException;
1873 if (is_string($classOrObject)) {
1874 if (!class_exists($classOrObject)) {
1875 throw new InvalidArgumentException;
1878 return self::getStaticAttribute(
1884 else if (is_object($classOrObject)) {
1885 return self::getObjectAttribute(
1892 throw new InvalidArgumentException;
1897 * Returns the value of a static attribute.
1898 * This also works for attributes that are declared protected or private.
1900 * @param string $className
1901 * @param string $attributeName
1903 * @throws InvalidArgumentException
1904 * @since Method available since Release 3.1.0
1906 public static function getStaticAttribute($className, $attributeName)
1908 if (!is_string($className) || !class_exists($className) || !is_string($attributeName)) {
1909 throw new InvalidArgumentException;
1912 $class = new ReflectionClass($className);
1913 $attributes = $class->getStaticProperties();
1915 if (array_key_exists($attributeName, $attributes)) {
1916 return $attributes[$attributeName];
1919 if (version_compare(PHP_VERSION, '5.2', '<')) {
1920 $protectedName = "\0*\0" . $attributeName;
1922 $protectedName = '*' . $attributeName;
1925 if (array_key_exists($protectedName, $attributes)) {
1926 return $attributes[$protectedName];
1929 $classes = PHPUnit_Util_Class::getHierarchy($className);
1931 foreach ($classes as $class) {
1932 $privateName = sprintf(
1939 if (array_key_exists($privateName, $attributes)) {
1940 return $attributes[$privateName];
1944 throw new RuntimeException(
1946 'Attribute "%s" not found in class.',
1954 * Returns the value of an object's attribute.
1955 * This also works for attributes that are declared protected or private.
1957 * @param object $object
1958 * @param string $attributeName
1960 * @throws InvalidArgumentException
1961 * @since Method available since Release 3.1.0
1963 public static function getObjectAttribute($object, $attributeName)
1965 if (!is_object($object) || !is_string($attributeName)) {
1966 throw new InvalidArgumentException;
1969 self::assertObjectHasAttribute($attributeName, $object);
1972 $attribute = new ReflectionProperty($object, $attributeName);
1975 catch (ReflectionException $e) {
1976 // Workaround for http://bugs.php.net/46064
1977 if (version_compare(PHP_VERSION, '5.2.7', '<')) {
1978 $reflector = new ReflectionObject($object);
1979 $attributes = $reflector->getProperties();
1981 foreach ($attributes as $_attribute) {
1982 if ($_attribute->getName() == $attributeName) {
1983 $attribute = $_attribute;
1989 $reflector = new ReflectionObject($object);
1991 while ($reflector = $reflector->getParentClass()) {
1993 $attribute = $reflector->getProperty($attributeName);
1997 catch(ReflectionException $e) {
2002 if ($attribute->isPublic()) {
2003 return $object->$attributeName;
2005 $array = (array)$object;
2006 $protectedName = "\0*\0" . $attributeName;
2008 if (array_key_exists($protectedName, $array)) {
2009 return $array[$protectedName];
2011 $classes = PHPUnit_Util_Class::getHierarchy(get_class($object));
2013 foreach ($classes as $class) {
2014 $privateName = sprintf(
2021 if (array_key_exists($privateName, $array)) {
2022 return $array[$privateName];
2028 throw new RuntimeException(
2030 'Attribute "%s" not found in object.',
2038 * Mark the test as incomplete.
2040 * @param string $message
2041 * @throws PHPUnit_Framework_IncompleteTestError
2042 * @since Method available since Release 3.0.0
2044 public static function markTestIncomplete($message = '')
2046 throw new PHPUnit_Framework_IncompleteTestError($message);
2050 * Mark the test as skipped.
2052 * @param string $message
2053 * @throws PHPUnit_Framework_SkippedTestError
2054 * @since Method available since Release 3.0.0
2056 public static function markTestSkipped($message = '')
2058 throw new PHPUnit_Framework_SkippedTestError($message);
2062 * Return the current assertion count.
2065 * @since Method available since Release 3.3.3
2067 public static function getCount()
2069 return self::$count;
2073 * Reset the assertion counter.
2075 * @since Method available since Release 3.3.3
2077 public static function resetCount()