3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * A set of assertion methods.
14 * @since Class available since Release 2.0.0
16 abstract class PHPUnit_Framework_Assert
21 private static $count = 0;
24 * Asserts that an array has a specified key.
27 * @param array|ArrayAccess $array
28 * @param string $message
30 * @since Method available since Release 3.0.0
32 public static function assertArrayHasKey($key, $array, $message = '')
34 if (!(is_integer($key) || is_string($key))) {
35 throw PHPUnit_Util_InvalidArgumentHelper::factory(
41 if (!(is_array($array) || $array instanceof ArrayAccess)) {
42 throw PHPUnit_Util_InvalidArgumentHelper::factory(
44 'array or ArrayAccess'
48 $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey($key);
50 self::assertThat($array, $constraint, $message);
54 * Asserts that an array has a specified subset.
56 * @param array|ArrayAccess $subset
57 * @param array|ArrayAccess $array
58 * @param bool $strict Check for object identity
59 * @param string $message
61 * @since Method available since Release 4.4.0
63 public static function assertArraySubset($subset, $array, $strict = false, $message = '')
65 if (!(is_array($subset) || $subset instanceof ArrayAccess)) {
66 throw PHPUnit_Util_InvalidArgumentHelper::factory(
68 'array or ArrayAccess'
72 if (!(is_array($array) || $array instanceof ArrayAccess)) {
73 throw PHPUnit_Util_InvalidArgumentHelper::factory(
75 'array or ArrayAccess'
79 $constraint = new PHPUnit_Framework_Constraint_ArraySubset($subset, $strict);
81 self::assertThat($array, $constraint, $message);
85 * Asserts that an array does not have a specified key.
88 * @param array|ArrayAccess $array
89 * @param string $message
91 * @since Method available since Release 3.0.0
93 public static function assertArrayNotHasKey($key, $array, $message = '')
95 if (!(is_integer($key) || is_string($key))) {
96 throw PHPUnit_Util_InvalidArgumentHelper::factory(
102 if (!(is_array($array) || $array instanceof ArrayAccess)) {
103 throw PHPUnit_Util_InvalidArgumentHelper::factory(
105 'array or ArrayAccess'
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 * @param bool $ignoreCase
123 * @param bool $checkForObjectIdentity
124 * @param bool $checkForNonObjectIdentity
126 * @since Method available since Release 2.1.0
128 public static function assertContains($needle, $haystack, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
130 if (is_array($haystack) ||
131 is_object($haystack) && $haystack instanceof Traversable) {
132 $constraint = new PHPUnit_Framework_Constraint_TraversableContains(
134 $checkForObjectIdentity,
135 $checkForNonObjectIdentity
137 } elseif (is_string($haystack)) {
138 if (!is_string($needle)) {
139 throw PHPUnit_Util_InvalidArgumentHelper::factory(
145 $constraint = new PHPUnit_Framework_Constraint_StringContains(
150 throw PHPUnit_Util_InvalidArgumentHelper::factory(
152 'array, traversable or string'
156 self::assertThat($haystack, $constraint, $message);
160 * Asserts that a haystack that is stored in a static attribute of a class
161 * or an attribute of an object contains a needle.
163 * @param mixed $needle
164 * @param string $haystackAttributeName
165 * @param mixed $haystackClassOrObject
166 * @param string $message
167 * @param bool $ignoreCase
168 * @param bool $checkForObjectIdentity
169 * @param bool $checkForNonObjectIdentity
171 * @since Method available since Release 3.0.0
173 public static function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
175 self::assertContains(
177 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
180 $checkForObjectIdentity,
181 $checkForNonObjectIdentity
186 * Asserts that a haystack does not contain a needle.
188 * @param mixed $needle
189 * @param mixed $haystack
190 * @param string $message
191 * @param bool $ignoreCase
192 * @param bool $checkForObjectIdentity
193 * @param bool $checkForNonObjectIdentity
195 * @since Method available since Release 2.1.0
197 public static function assertNotContains($needle, $haystack, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
199 if (is_array($haystack) ||
200 is_object($haystack) && $haystack instanceof Traversable) {
201 $constraint = new PHPUnit_Framework_Constraint_Not(
202 new PHPUnit_Framework_Constraint_TraversableContains(
204 $checkForObjectIdentity,
205 $checkForNonObjectIdentity
208 } elseif (is_string($haystack)) {
209 if (!is_string($needle)) {
210 throw PHPUnit_Util_InvalidArgumentHelper::factory(
216 $constraint = new PHPUnit_Framework_Constraint_Not(
217 new PHPUnit_Framework_Constraint_StringContains(
223 throw PHPUnit_Util_InvalidArgumentHelper::factory(
225 'array, traversable or string'
229 self::assertThat($haystack, $constraint, $message);
233 * Asserts that a haystack that is stored in a static attribute of a class
234 * or an attribute of an object does not contain a needle.
236 * @param mixed $needle
237 * @param string $haystackAttributeName
238 * @param mixed $haystackClassOrObject
239 * @param string $message
240 * @param bool $ignoreCase
241 * @param bool $checkForObjectIdentity
242 * @param bool $checkForNonObjectIdentity
244 * @since Method available since Release 3.0.0
246 public static function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
248 self::assertNotContains(
250 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
253 $checkForObjectIdentity,
254 $checkForNonObjectIdentity
259 * Asserts that a haystack contains only values of a given type.
261 * @param string $type
262 * @param mixed $haystack
263 * @param bool $isNativeType
264 * @param string $message
266 * @since Method available since Release 3.1.4
268 public static function assertContainsOnly($type, $haystack, $isNativeType = null, $message = '')
270 if (!(is_array($haystack) ||
271 is_object($haystack) && $haystack instanceof Traversable)) {
272 throw PHPUnit_Util_InvalidArgumentHelper::factory(
274 'array or traversable'
278 if ($isNativeType == null) {
279 $isNativeType = PHPUnit_Util_Type::isType($type);
284 new PHPUnit_Framework_Constraint_TraversableContainsOnly(
293 * Asserts that a haystack contains only instances of a given classname
295 * @param string $classname
296 * @param array|Traversable $haystack
297 * @param string $message
299 public static function assertContainsOnlyInstancesOf($classname, $haystack, $message = '')
301 if (!(is_array($haystack) ||
302 is_object($haystack) && $haystack instanceof Traversable)) {
303 throw PHPUnit_Util_InvalidArgumentHelper::factory(
305 'array or traversable'
311 new PHPUnit_Framework_Constraint_TraversableContainsOnly(
320 * Asserts that a haystack that is stored in a static attribute of a class
321 * or an attribute of an object contains only values of a given type.
323 * @param string $type
324 * @param string $haystackAttributeName
325 * @param mixed $haystackClassOrObject
326 * @param bool $isNativeType
327 * @param string $message
329 * @since Method available since Release 3.1.4
331 public static function assertAttributeContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = null, $message = '')
333 self::assertContainsOnly(
335 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
342 * Asserts that a haystack does not contain only values of a given type.
344 * @param string $type
345 * @param mixed $haystack
346 * @param bool $isNativeType
347 * @param string $message
349 * @since Method available since Release 3.1.4
351 public static function assertNotContainsOnly($type, $haystack, $isNativeType = null, $message = '')
353 if (!(is_array($haystack) ||
354 is_object($haystack) && $haystack instanceof Traversable)) {
355 throw PHPUnit_Util_InvalidArgumentHelper::factory(
357 'array or traversable'
361 if ($isNativeType == null) {
362 $isNativeType = PHPUnit_Util_Type::isType($type);
367 new PHPUnit_Framework_Constraint_Not(
368 new PHPUnit_Framework_Constraint_TraversableContainsOnly(
378 * Asserts that a haystack that is stored in a static attribute of a class
379 * or an attribute of an object does not contain only values of a given
382 * @param string $type
383 * @param string $haystackAttributeName
384 * @param mixed $haystackClassOrObject
385 * @param bool $isNativeType
386 * @param string $message
388 * @since Method available since Release 3.1.4
390 public static function assertAttributeNotContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = null, $message = '')
392 self::assertNotContainsOnly(
394 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
401 * Asserts the number of elements of an array, Countable or Traversable.
403 * @param int $expectedCount
404 * @param mixed $haystack
405 * @param string $message
407 public static function assertCount($expectedCount, $haystack, $message = '')
409 if (!is_int($expectedCount)) {
410 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer');
413 if (!$haystack instanceof Countable &&
414 !$haystack instanceof Traversable &&
415 !is_array($haystack)) {
416 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
421 new PHPUnit_Framework_Constraint_Count($expectedCount),
427 * Asserts the number of elements of an array, Countable or Traversable
428 * that is stored in an attribute.
430 * @param int $expectedCount
431 * @param string $haystackAttributeName
432 * @param mixed $haystackClassOrObject
433 * @param string $message
435 * @since Method available since Release 3.6.0
437 public static function assertAttributeCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '')
441 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
447 * Asserts the number of elements of an array, Countable or Traversable.
449 * @param int $expectedCount
450 * @param mixed $haystack
451 * @param string $message
453 public static function assertNotCount($expectedCount, $haystack, $message = '')
455 if (!is_int($expectedCount)) {
456 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer');
459 if (!$haystack instanceof Countable &&
460 !$haystack instanceof Traversable &&
461 !is_array($haystack)) {
462 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
465 $constraint = new PHPUnit_Framework_Constraint_Not(
466 new PHPUnit_Framework_Constraint_Count($expectedCount)
469 self::assertThat($haystack, $constraint, $message);
473 * Asserts the number of elements of an array, Countable or Traversable
474 * that is stored in an attribute.
476 * @param int $expectedCount
477 * @param string $haystackAttributeName
478 * @param mixed $haystackClassOrObject
479 * @param string $message
481 * @since Method available since Release 3.6.0
483 public static function assertAttributeNotCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '')
485 self::assertNotCount(
487 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
493 * Asserts that two variables are equal.
495 * @param mixed $expected
496 * @param mixed $actual
497 * @param string $message
498 * @param float $delta
499 * @param int $maxDepth
500 * @param bool $canonicalize
501 * @param bool $ignoreCase
503 public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
505 $constraint = new PHPUnit_Framework_Constraint_IsEqual(
513 self::assertThat($actual, $constraint, $message);
517 * Asserts that a variable is equal to an attribute of an object.
519 * @param mixed $expected
520 * @param string $actualAttributeName
521 * @param string $actualClassOrObject
522 * @param string $message
523 * @param float $delta
524 * @param int $maxDepth
525 * @param bool $canonicalize
526 * @param bool $ignoreCase
528 public static function assertAttributeEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
532 self::readAttribute($actualClassOrObject, $actualAttributeName),
542 * Asserts that two variables are not equal.
544 * @param mixed $expected
545 * @param mixed $actual
546 * @param string $message
547 * @param float $delta
548 * @param int $maxDepth
549 * @param bool $canonicalize
550 * @param bool $ignoreCase
552 * @since Method available since Release 2.3.0
554 public static function assertNotEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
556 $constraint = new PHPUnit_Framework_Constraint_Not(
557 new PHPUnit_Framework_Constraint_IsEqual(
566 self::assertThat($actual, $constraint, $message);
570 * Asserts that a variable is not equal to an attribute of an object.
572 * @param mixed $expected
573 * @param string $actualAttributeName
574 * @param string $actualClassOrObject
575 * @param string $message
576 * @param float $delta
577 * @param int $maxDepth
578 * @param bool $canonicalize
579 * @param bool $ignoreCase
581 public static function assertAttributeNotEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
583 self::assertNotEquals(
585 self::readAttribute($actualClassOrObject, $actualAttributeName),
595 * Asserts that a variable is empty.
597 * @param mixed $actual
598 * @param string $message
600 * @throws PHPUnit_Framework_AssertionFailedError
602 public static function assertEmpty($actual, $message = '')
604 self::assertThat($actual, self::isEmpty(), $message);
608 * Asserts that a static attribute of a class or an attribute of an object
611 * @param string $haystackAttributeName
612 * @param mixed $haystackClassOrObject
613 * @param string $message
615 * @since Method available since Release 3.5.0
617 public static function assertAttributeEmpty($haystackAttributeName, $haystackClassOrObject, $message = '')
620 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
626 * Asserts that a variable is not empty.
628 * @param mixed $actual
629 * @param string $message
631 * @throws PHPUnit_Framework_AssertionFailedError
633 public static function assertNotEmpty($actual, $message = '')
635 self::assertThat($actual, self::logicalNot(self::isEmpty()), $message);
639 * Asserts that a static attribute of a class or an attribute of an object
642 * @param string $haystackAttributeName
643 * @param mixed $haystackClassOrObject
644 * @param string $message
646 * @since Method available since Release 3.5.0
648 public static function assertAttributeNotEmpty($haystackAttributeName, $haystackClassOrObject, $message = '')
650 self::assertNotEmpty(
651 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
657 * Asserts that a value is greater than another value.
659 * @param mixed $expected
660 * @param mixed $actual
661 * @param string $message
663 * @since Method available since Release 3.1.0
665 public static function assertGreaterThan($expected, $actual, $message = '')
667 self::assertThat($actual, self::greaterThan($expected), $message);
671 * Asserts that an attribute is greater than another value.
673 * @param mixed $expected
674 * @param string $actualAttributeName
675 * @param string $actualClassOrObject
676 * @param string $message
678 * @since Method available since Release 3.1.0
680 public static function assertAttributeGreaterThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
682 self::assertGreaterThan(
684 self::readAttribute($actualClassOrObject, $actualAttributeName),
690 * Asserts that a value is greater than or equal to another value.
692 * @param mixed $expected
693 * @param mixed $actual
694 * @param string $message
696 * @since Method available since Release 3.1.0
698 public static function assertGreaterThanOrEqual($expected, $actual, $message = '')
702 self::greaterThanOrEqual($expected),
708 * Asserts that an attribute is greater than or equal to another value.
710 * @param mixed $expected
711 * @param string $actualAttributeName
712 * @param string $actualClassOrObject
713 * @param string $message
715 * @since Method available since Release 3.1.0
717 public static function assertAttributeGreaterThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
719 self::assertGreaterThanOrEqual(
721 self::readAttribute($actualClassOrObject, $actualAttributeName),
727 * Asserts that a value is smaller than another value.
729 * @param mixed $expected
730 * @param mixed $actual
731 * @param string $message
733 * @since Method available since Release 3.1.0
735 public static function assertLessThan($expected, $actual, $message = '')
737 self::assertThat($actual, self::lessThan($expected), $message);
741 * Asserts that an attribute is smaller than another value.
743 * @param mixed $expected
744 * @param string $actualAttributeName
745 * @param string $actualClassOrObject
746 * @param string $message
748 * @since Method available since Release 3.1.0
750 public static function assertAttributeLessThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
752 self::assertLessThan(
754 self::readAttribute($actualClassOrObject, $actualAttributeName),
760 * Asserts that a value is smaller than or equal to another value.
762 * @param mixed $expected
763 * @param mixed $actual
764 * @param string $message
766 * @since Method available since Release 3.1.0
768 public static function assertLessThanOrEqual($expected, $actual, $message = '')
770 self::assertThat($actual, self::lessThanOrEqual($expected), $message);
774 * Asserts that an attribute is smaller than or equal to another value.
776 * @param mixed $expected
777 * @param string $actualAttributeName
778 * @param string $actualClassOrObject
779 * @param string $message
781 * @since Method available since Release 3.1.0
783 public static function assertAttributeLessThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
785 self::assertLessThanOrEqual(
787 self::readAttribute($actualClassOrObject, $actualAttributeName),
793 * Asserts that the contents of one file is equal to the contents of another
796 * @param string $expected
797 * @param string $actual
798 * @param string $message
799 * @param bool $canonicalize
800 * @param bool $ignoreCase
802 * @since Method available since Release 3.2.14
804 public static function assertFileEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
806 self::assertFileExists($expected, $message);
807 self::assertFileExists($actual, $message);
810 file_get_contents($expected),
811 file_get_contents($actual),
821 * Asserts that the contents of one file is not equal to the contents of
824 * @param string $expected
825 * @param string $actual
826 * @param string $message
827 * @param bool $canonicalize
828 * @param bool $ignoreCase
830 * @since Method available since Release 3.2.14
832 public static function assertFileNotEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
834 self::assertFileExists($expected, $message);
835 self::assertFileExists($actual, $message);
837 self::assertNotEquals(
838 file_get_contents($expected),
839 file_get_contents($actual),
849 * Asserts that the contents of a string is equal
850 * to the contents of a file.
852 * @param string $expectedFile
853 * @param string $actualString
854 * @param string $message
855 * @param bool $canonicalize
856 * @param bool $ignoreCase
858 * @since Method available since Release 3.3.0
860 public static function assertStringEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = false, $ignoreCase = false)
862 self::assertFileExists($expectedFile, $message);
865 file_get_contents($expectedFile),
876 * Asserts that the contents of a string is not equal
877 * to the contents of a file.
879 * @param string $expectedFile
880 * @param string $actualString
881 * @param string $message
882 * @param bool $canonicalize
883 * @param bool $ignoreCase
885 * @since Method available since Release 3.3.0
887 public static function assertStringNotEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = false, $ignoreCase = false)
889 self::assertFileExists($expectedFile, $message);
891 self::assertNotEquals(
892 file_get_contents($expectedFile),
903 * Asserts that a file exists.
905 * @param string $filename
906 * @param string $message
908 * @since Method available since Release 3.0.0
910 public static function assertFileExists($filename, $message = '')
912 if (!is_string($filename)) {
913 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
916 $constraint = new PHPUnit_Framework_Constraint_FileExists;
918 self::assertThat($filename, $constraint, $message);
922 * Asserts that a file does not exist.
924 * @param string $filename
925 * @param string $message
927 * @since Method available since Release 3.0.0
929 public static function assertFileNotExists($filename, $message = '')
931 if (!is_string($filename)) {
932 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
935 $constraint = new PHPUnit_Framework_Constraint_Not(
936 new PHPUnit_Framework_Constraint_FileExists
939 self::assertThat($filename, $constraint, $message);
943 * Asserts that a condition is true.
945 * @param bool $condition
946 * @param string $message
948 * @throws PHPUnit_Framework_AssertionFailedError
950 public static function assertTrue($condition, $message = '')
952 self::assertThat($condition, self::isTrue(), $message);
956 * Asserts that a condition is not true.
958 * @param bool $condition
959 * @param string $message
961 * @throws PHPUnit_Framework_AssertionFailedError
963 public static function assertNotTrue($condition, $message = '')
965 self::assertThat($condition, self::logicalNot(self::isTrue()), $message);
969 * Asserts that a condition is false.
971 * @param bool $condition
972 * @param string $message
974 * @throws PHPUnit_Framework_AssertionFailedError
976 public static function assertFalse($condition, $message = '')
978 self::assertThat($condition, self::isFalse(), $message);
982 * Asserts that a condition is not false.
984 * @param bool $condition
985 * @param string $message
987 * @throws PHPUnit_Framework_AssertionFailedError
989 public static function assertNotFalse($condition, $message = '')
991 self::assertThat($condition, self::logicalNot(self::isFalse()), $message);
995 * Asserts that a variable is not null.
997 * @param mixed $actual
998 * @param string $message
1000 public static function assertNotNull($actual, $message = '')
1002 self::assertThat($actual, self::logicalNot(self::isNull()), $message);
1006 * Asserts that a variable is null.
1008 * @param mixed $actual
1009 * @param string $message
1011 public static function assertNull($actual, $message = '')
1013 self::assertThat($actual, self::isNull(), $message);
1017 * Asserts that a class has a specified attribute.
1019 * @param string $attributeName
1020 * @param string $className
1021 * @param string $message
1023 * @since Method available since Release 3.1.0
1025 public static function assertClassHasAttribute($attributeName, $className, $message = '')
1027 if (!is_string($attributeName)) {
1028 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1031 if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
1032 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
1035 if (!is_string($className) || !class_exists($className)) {
1036 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className);
1039 $constraint = new PHPUnit_Framework_Constraint_ClassHasAttribute(
1043 self::assertThat($className, $constraint, $message);
1047 * Asserts that a class does not have a specified attribute.
1049 * @param string $attributeName
1050 * @param string $className
1051 * @param string $message
1053 * @since Method available since Release 3.1.0
1055 public static function assertClassNotHasAttribute($attributeName, $className, $message = '')
1057 if (!is_string($attributeName)) {
1058 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1061 if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
1062 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
1065 if (!is_string($className) || !class_exists($className)) {
1066 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className);
1069 $constraint = new PHPUnit_Framework_Constraint_Not(
1070 new PHPUnit_Framework_Constraint_ClassHasAttribute($attributeName)
1073 self::assertThat($className, $constraint, $message);
1077 * Asserts that a class has a specified static attribute.
1079 * @param string $attributeName
1080 * @param string $className
1081 * @param string $message
1083 * @since Method available since Release 3.1.0
1085 public static function assertClassHasStaticAttribute($attributeName, $className, $message = '')
1087 if (!is_string($attributeName)) {
1088 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1091 if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
1092 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
1095 if (!is_string($className) || !class_exists($className)) {
1096 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className);
1099 $constraint = new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
1103 self::assertThat($className, $constraint, $message);
1107 * Asserts that a class does not have a specified static attribute.
1109 * @param string $attributeName
1110 * @param string $className
1111 * @param string $message
1113 * @since Method available since Release 3.1.0
1115 public static function assertClassNotHasStaticAttribute($attributeName, $className, $message = '')
1117 if (!is_string($attributeName)) {
1118 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1121 if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
1122 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
1125 if (!is_string($className) || !class_exists($className)) {
1126 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className);
1129 $constraint = new PHPUnit_Framework_Constraint_Not(
1130 new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
1135 self::assertThat($className, $constraint, $message);
1139 * Asserts that an object has a specified attribute.
1141 * @param string $attributeName
1142 * @param object $object
1143 * @param string $message
1145 * @since Method available since Release 3.0.0
1147 public static function assertObjectHasAttribute($attributeName, $object, $message = '')
1149 if (!is_string($attributeName)) {
1150 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1153 if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
1154 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
1157 if (!is_object($object)) {
1158 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
1161 $constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute(
1165 self::assertThat($object, $constraint, $message);
1169 * Asserts that an object does not have a specified attribute.
1171 * @param string $attributeName
1172 * @param object $object
1173 * @param string $message
1175 * @since Method available since Release 3.0.0
1177 public static function assertObjectNotHasAttribute($attributeName, $object, $message = '')
1179 if (!is_string($attributeName)) {
1180 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1183 if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
1184 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
1187 if (!is_object($object)) {
1188 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
1191 $constraint = new PHPUnit_Framework_Constraint_Not(
1192 new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName)
1195 self::assertThat($object, $constraint, $message);
1199 * Asserts that two variables have the same type and value.
1200 * Used on objects, it asserts that two variables reference
1203 * @param mixed $expected
1204 * @param mixed $actual
1205 * @param string $message
1207 public static function assertSame($expected, $actual, $message = '')
1209 if (is_bool($expected) && is_bool($actual)) {
1210 self::assertEquals($expected, $actual, $message);
1212 $constraint = new PHPUnit_Framework_Constraint_IsIdentical(
1216 self::assertThat($actual, $constraint, $message);
1221 * Asserts that a variable and an attribute of an object have the same type
1224 * @param mixed $expected
1225 * @param string $actualAttributeName
1226 * @param object $actualClassOrObject
1227 * @param string $message
1229 public static function assertAttributeSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
1233 self::readAttribute($actualClassOrObject, $actualAttributeName),
1239 * Asserts that two variables do not have the same type and value.
1240 * Used on objects, it asserts that two variables do not reference
1243 * @param mixed $expected
1244 * @param mixed $actual
1245 * @param string $message
1247 public static function assertNotSame($expected, $actual, $message = '')
1249 if (is_bool($expected) && is_bool($actual)) {
1250 self::assertNotEquals($expected, $actual, $message);
1252 $constraint = new PHPUnit_Framework_Constraint_Not(
1253 new PHPUnit_Framework_Constraint_IsIdentical($expected)
1256 self::assertThat($actual, $constraint, $message);
1261 * Asserts that a variable and an attribute of an object do not have the
1262 * same type and value.
1264 * @param mixed $expected
1265 * @param string $actualAttributeName
1266 * @param object $actualClassOrObject
1267 * @param string $message
1269 public static function assertAttributeNotSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
1271 self::assertNotSame(
1273 self::readAttribute($actualClassOrObject, $actualAttributeName),
1279 * Asserts that a variable is of a given type.
1281 * @param string $expected
1282 * @param mixed $actual
1283 * @param string $message
1285 * @since Method available since Release 3.5.0
1287 public static function assertInstanceOf($expected, $actual, $message = '')
1289 if (!(is_string($expected) && (class_exists($expected) || interface_exists($expected)))) {
1290 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class or interface name');
1293 $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
1297 self::assertThat($actual, $constraint, $message);
1301 * Asserts that an attribute is of a given type.
1303 * @param string $expected
1304 * @param string $attributeName
1305 * @param mixed $classOrObject
1306 * @param string $message
1308 * @since Method available since Release 3.5.0
1310 public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '')
1312 self::assertInstanceOf(
1314 self::readAttribute($classOrObject, $attributeName),
1320 * Asserts that a variable is not of a given type.
1322 * @param string $expected
1323 * @param mixed $actual
1324 * @param string $message
1326 * @since Method available since Release 3.5.0
1328 public static function assertNotInstanceOf($expected, $actual, $message = '')
1330 if (!(is_string($expected) && (class_exists($expected) || interface_exists($expected)))) {
1331 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class or interface name');
1334 $constraint = new PHPUnit_Framework_Constraint_Not(
1335 new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
1338 self::assertThat($actual, $constraint, $message);
1342 * Asserts that an attribute is of a given type.
1344 * @param string $expected
1345 * @param string $attributeName
1346 * @param mixed $classOrObject
1347 * @param string $message
1349 * @since Method available since Release 3.5.0
1351 public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '')
1353 self::assertNotInstanceOf(
1355 self::readAttribute($classOrObject, $attributeName),
1361 * Asserts that a variable is of a given type.
1363 * @param string $expected
1364 * @param mixed $actual
1365 * @param string $message
1367 * @since Method available since Release 3.5.0
1369 public static function assertInternalType($expected, $actual, $message = '')
1371 if (!is_string($expected)) {
1372 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1375 $constraint = new PHPUnit_Framework_Constraint_IsType(
1379 self::assertThat($actual, $constraint, $message);
1383 * Asserts that an attribute is of a given type.
1385 * @param string $expected
1386 * @param string $attributeName
1387 * @param mixed $classOrObject
1388 * @param string $message
1390 * @since Method available since Release 3.5.0
1392 public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '')
1394 self::assertInternalType(
1396 self::readAttribute($classOrObject, $attributeName),
1402 * Asserts that a variable is not of a given type.
1404 * @param string $expected
1405 * @param mixed $actual
1406 * @param string $message
1408 * @since Method available since Release 3.5.0
1410 public static function assertNotInternalType($expected, $actual, $message = '')
1412 if (!is_string($expected)) {
1413 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1416 $constraint = new PHPUnit_Framework_Constraint_Not(
1417 new PHPUnit_Framework_Constraint_IsType($expected)
1420 self::assertThat($actual, $constraint, $message);
1424 * Asserts that an attribute is of a given type.
1426 * @param string $expected
1427 * @param string $attributeName
1428 * @param mixed $classOrObject
1429 * @param string $message
1431 * @since Method available since Release 3.5.0
1433 public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '')
1435 self::assertNotInternalType(
1437 self::readAttribute($classOrObject, $attributeName),
1443 * Asserts that a string matches a given regular expression.
1445 * @param string $pattern
1446 * @param string $string
1447 * @param string $message
1449 public static function assertRegExp($pattern, $string, $message = '')
1451 if (!is_string($pattern)) {
1452 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1455 if (!is_string($string)) {
1456 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1459 $constraint = new PHPUnit_Framework_Constraint_PCREMatch($pattern);
1461 self::assertThat($string, $constraint, $message);
1465 * Asserts that a string does not match a given regular expression.
1467 * @param string $pattern
1468 * @param string $string
1469 * @param string $message
1471 * @since Method available since Release 2.1.0
1473 public static function assertNotRegExp($pattern, $string, $message = '')
1475 if (!is_string($pattern)) {
1476 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1479 if (!is_string($string)) {
1480 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1483 $constraint = new PHPUnit_Framework_Constraint_Not(
1484 new PHPUnit_Framework_Constraint_PCREMatch($pattern)
1487 self::assertThat($string, $constraint, $message);
1491 * Assert that the size of two arrays (or `Countable` or `Traversable` objects)
1494 * @param array|Countable|Traversable $expected
1495 * @param array|Countable|Traversable $actual
1496 * @param string $message
1498 public static function assertSameSize($expected, $actual, $message = '')
1500 if (!$expected instanceof Countable &&
1501 !$expected instanceof Traversable &&
1502 !is_array($expected)) {
1503 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable or traversable');
1506 if (!$actual instanceof Countable &&
1507 !$actual instanceof Traversable &&
1508 !is_array($actual)) {
1509 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
1514 new PHPUnit_Framework_Constraint_SameSize($expected),
1520 * Assert that the size of two arrays (or `Countable` or `Traversable` objects)
1523 * @param array|Countable|Traversable $expected
1524 * @param array|Countable|Traversable $actual
1525 * @param string $message
1527 public static function assertNotSameSize($expected, $actual, $message = '')
1529 if (!$expected instanceof Countable &&
1530 !$expected instanceof Traversable &&
1531 !is_array($expected)) {
1532 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable or traversable');
1535 if (!$actual instanceof Countable &&
1536 !$actual instanceof Traversable &&
1537 !is_array($actual)) {
1538 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
1541 $constraint = new PHPUnit_Framework_Constraint_Not(
1542 new PHPUnit_Framework_Constraint_SameSize($expected)
1545 self::assertThat($actual, $constraint, $message);
1549 * Asserts that a string matches a given format string.
1551 * @param string $format
1552 * @param string $string
1553 * @param string $message
1555 * @since Method available since Release 3.5.0
1557 public static function assertStringMatchesFormat($format, $string, $message = '')
1559 if (!is_string($format)) {
1560 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1563 if (!is_string($string)) {
1564 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1567 $constraint = new PHPUnit_Framework_Constraint_StringMatches($format);
1569 self::assertThat($string, $constraint, $message);
1573 * Asserts that a string does not match a given format string.
1575 * @param string $format
1576 * @param string $string
1577 * @param string $message
1579 * @since Method available since Release 3.5.0
1581 public static function assertStringNotMatchesFormat($format, $string, $message = '')
1583 if (!is_string($format)) {
1584 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1587 if (!is_string($string)) {
1588 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1591 $constraint = new PHPUnit_Framework_Constraint_Not(
1592 new PHPUnit_Framework_Constraint_StringMatches($format)
1595 self::assertThat($string, $constraint, $message);
1599 * Asserts that a string matches a given format file.
1601 * @param string $formatFile
1602 * @param string $string
1603 * @param string $message
1605 * @since Method available since Release 3.5.0
1607 public static function assertStringMatchesFormatFile($formatFile, $string, $message = '')
1609 self::assertFileExists($formatFile, $message);
1611 if (!is_string($string)) {
1612 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1615 $constraint = new PHPUnit_Framework_Constraint_StringMatches(
1616 file_get_contents($formatFile)
1619 self::assertThat($string, $constraint, $message);
1623 * Asserts that a string does not match a given format string.
1625 * @param string $formatFile
1626 * @param string $string
1627 * @param string $message
1629 * @since Method available since Release 3.5.0
1631 public static function assertStringNotMatchesFormatFile($formatFile, $string, $message = '')
1633 self::assertFileExists($formatFile, $message);
1635 if (!is_string($string)) {
1636 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1639 $constraint = new PHPUnit_Framework_Constraint_Not(
1640 new PHPUnit_Framework_Constraint_StringMatches(
1641 file_get_contents($formatFile)
1645 self::assertThat($string, $constraint, $message);
1649 * Asserts that a string starts with a given prefix.
1651 * @param string $prefix
1652 * @param string $string
1653 * @param string $message
1655 * @since Method available since Release 3.4.0
1657 public static function assertStringStartsWith($prefix, $string, $message = '')
1659 if (!is_string($prefix)) {
1660 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1663 if (!is_string($string)) {
1664 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1667 $constraint = new PHPUnit_Framework_Constraint_StringStartsWith(
1671 self::assertThat($string, $constraint, $message);
1675 * Asserts that a string starts not with a given prefix.
1677 * @param string $prefix
1678 * @param string $string
1679 * @param string $message
1681 * @since Method available since Release 3.4.0
1683 public static function assertStringStartsNotWith($prefix, $string, $message = '')
1685 if (!is_string($prefix)) {
1686 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1689 if (!is_string($string)) {
1690 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1693 $constraint = new PHPUnit_Framework_Constraint_Not(
1694 new PHPUnit_Framework_Constraint_StringStartsWith($prefix)
1697 self::assertThat($string, $constraint, $message);
1701 * Asserts that a string ends with a given suffix.
1703 * @param string $suffix
1704 * @param string $string
1705 * @param string $message
1707 * @since Method available since Release 3.4.0
1709 public static function assertStringEndsWith($suffix, $string, $message = '')
1711 if (!is_string($suffix)) {
1712 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1715 if (!is_string($string)) {
1716 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1719 $constraint = new PHPUnit_Framework_Constraint_StringEndsWith($suffix);
1721 self::assertThat($string, $constraint, $message);
1725 * Asserts that a string ends not with a given suffix.
1727 * @param string $suffix
1728 * @param string $string
1729 * @param string $message
1731 * @since Method available since Release 3.4.0
1733 public static function assertStringEndsNotWith($suffix, $string, $message = '')
1735 if (!is_string($suffix)) {
1736 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1739 if (!is_string($string)) {
1740 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1743 $constraint = new PHPUnit_Framework_Constraint_Not(
1744 new PHPUnit_Framework_Constraint_StringEndsWith($suffix)
1747 self::assertThat($string, $constraint, $message);
1751 * Asserts that two XML files are equal.
1753 * @param string $expectedFile
1754 * @param string $actualFile
1755 * @param string $message
1757 * @since Method available since Release 3.1.0
1759 public static function assertXmlFileEqualsXmlFile($expectedFile, $actualFile, $message = '')
1761 $expected = PHPUnit_Util_XML::loadFile($expectedFile);
1762 $actual = PHPUnit_Util_XML::loadFile($actualFile);
1764 self::assertEquals($expected, $actual, $message);
1768 * Asserts that two XML files are not equal.
1770 * @param string $expectedFile
1771 * @param string $actualFile
1772 * @param string $message
1774 * @since Method available since Release 3.1.0
1776 public static function assertXmlFileNotEqualsXmlFile($expectedFile, $actualFile, $message = '')
1778 $expected = PHPUnit_Util_XML::loadFile($expectedFile);
1779 $actual = PHPUnit_Util_XML::loadFile($actualFile);
1781 self::assertNotEquals($expected, $actual, $message);
1785 * Asserts that two XML documents are equal.
1787 * @param string $expectedFile
1788 * @param string $actualXml
1789 * @param string $message
1791 * @since Method available since Release 3.3.0
1793 public static function assertXmlStringEqualsXmlFile($expectedFile, $actualXml, $message = '')
1795 $expected = PHPUnit_Util_XML::loadFile($expectedFile);
1796 $actual = PHPUnit_Util_XML::load($actualXml);
1798 self::assertEquals($expected, $actual, $message);
1802 * Asserts that two XML documents are not equal.
1804 * @param string $expectedFile
1805 * @param string $actualXml
1806 * @param string $message
1808 * @since Method available since Release 3.3.0
1810 public static function assertXmlStringNotEqualsXmlFile($expectedFile, $actualXml, $message = '')
1812 $expected = PHPUnit_Util_XML::loadFile($expectedFile);
1813 $actual = PHPUnit_Util_XML::load($actualXml);
1815 self::assertNotEquals($expected, $actual, $message);
1819 * Asserts that two XML documents are equal.
1821 * @param string $expectedXml
1822 * @param string $actualXml
1823 * @param string $message
1825 * @since Method available since Release 3.1.0
1827 public static function assertXmlStringEqualsXmlString($expectedXml, $actualXml, $message = '')
1829 $expected = PHPUnit_Util_XML::load($expectedXml);
1830 $actual = PHPUnit_Util_XML::load($actualXml);
1832 self::assertEquals($expected, $actual, $message);
1836 * Asserts that two XML documents are not equal.
1838 * @param string $expectedXml
1839 * @param string $actualXml
1840 * @param string $message
1842 * @since Method available since Release 3.1.0
1844 public static function assertXmlStringNotEqualsXmlString($expectedXml, $actualXml, $message = '')
1846 $expected = PHPUnit_Util_XML::load($expectedXml);
1847 $actual = PHPUnit_Util_XML::load($actualXml);
1849 self::assertNotEquals($expected, $actual, $message);
1853 * Asserts that a hierarchy of DOMElements matches.
1855 * @param DOMElement $expectedElement
1856 * @param DOMElement $actualElement
1857 * @param bool $checkAttributes
1858 * @param string $message
1860 * @since Method available since Release 3.3.0
1862 public static function assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement, $checkAttributes = false, $message = '')
1864 $tmp = new DOMDocument;
1865 $expectedElement = $tmp->importNode($expectedElement, true);
1867 $tmp = new DOMDocument;
1868 $actualElement = $tmp->importNode($actualElement, true);
1873 $expectedElement->tagName,
1874 $actualElement->tagName,
1878 if ($checkAttributes) {
1880 $expectedElement->attributes->length,
1881 $actualElement->attributes->length,
1883 '%s%sNumber of attributes on node "%s" does not match',
1885 !empty($message) ? "\n" : '',
1886 $expectedElement->tagName
1890 for ($i = 0; $i < $expectedElement->attributes->length; $i++) {
1891 $expectedAttribute = $expectedElement->attributes->item($i);
1892 $actualAttribute = $actualElement->attributes->getNamedItem(
1893 $expectedAttribute->name
1896 if (!$actualAttribute) {
1899 '%s%sCould not find attribute "%s" on node "%s"',
1901 !empty($message) ? "\n" : '',
1902 $expectedAttribute->name,
1903 $expectedElement->tagName
1910 PHPUnit_Util_XML::removeCharacterDataNodes($expectedElement);
1911 PHPUnit_Util_XML::removeCharacterDataNodes($actualElement);
1914 $expectedElement->childNodes->length,
1915 $actualElement->childNodes->length,
1917 '%s%sNumber of child nodes of "%s" differs',
1919 !empty($message) ? "\n" : '',
1920 $expectedElement->tagName
1924 for ($i = 0; $i < $expectedElement->childNodes->length; $i++) {
1925 self::assertEqualXMLStructure(
1926 $expectedElement->childNodes->item($i),
1927 $actualElement->childNodes->item($i),
1935 * Assert the presence, absence, or count of elements in a document matching
1936 * the CSS $selector, regardless of the contents of those elements.
1938 * The first argument, $selector, is the CSS selector used to match
1939 * the elements in the $actual document.
1941 * The second argument, $count, can be either boolean or numeric.
1942 * When boolean, it asserts for presence of elements matching the selector
1943 * (true) or absence of elements (false).
1944 * When numeric, it asserts the count of elements.
1946 * assertSelectCount("#binder", true, $xml); // any?
1947 * assertSelectCount(".binder", 3, $xml); // exactly 3?
1949 * @param array $selector
1950 * @param int|bool|array $count
1951 * @param mixed $actual
1952 * @param string $message
1953 * @param bool $isHtml
1955 * @since Method available since Release 3.3.0
1957 * @codeCoverageIgnore
1959 public static function assertSelectCount($selector, $count, $actual, $message = '', $isHtml = true)
1961 trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
1963 self::assertSelectEquals(
1974 * assertSelectRegExp("#binder .name", "/Mike|Derek/", true, $xml); // any?
1975 * assertSelectRegExp("#binder .name", "/Mike|Derek/", 3, $xml); // 3?
1977 * @param array $selector
1978 * @param string $pattern
1979 * @param int|bool|array $count
1980 * @param mixed $actual
1981 * @param string $message
1982 * @param bool $isHtml
1984 * @since Method available since Release 3.3.0
1986 * @codeCoverageIgnore
1988 public static function assertSelectRegExp($selector, $pattern, $count, $actual, $message = '', $isHtml = true)
1990 trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
1992 self::assertSelectEquals(
2003 * assertSelectEquals("#binder .name", "Chuck", true, $xml); // any?
2004 * assertSelectEquals("#binder .name", "Chuck", false, $xml); // none?
2006 * @param array $selector
2007 * @param string $content
2008 * @param int|bool|array $count
2009 * @param mixed $actual
2010 * @param string $message
2011 * @param bool $isHtml
2013 * @since Method available since Release 3.3.0
2015 * @codeCoverageIgnore
2017 public static function assertSelectEquals($selector, $content, $count, $actual, $message = '', $isHtml = true)
2019 trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
2021 $tags = PHPUnit_Util_XML::cssSelect(
2028 // assert specific number of elements
2029 if (is_numeric($count)) {
2030 $counted = $tags ? count($tags) : 0;
2031 self::assertEquals($count, $counted, $message);
2032 } // assert any elements exist if true, assert no elements exist if false
2033 elseif (is_bool($count)) {
2034 $any = count($tags) > 0 && $tags[0] instanceof DOMNode;
2037 self::assertTrue($any, $message);
2039 self::assertFalse($any, $message);
2041 } // check for range number of elements
2042 elseif (is_array($count) &&
2043 (isset($count['>']) || isset($count['<']) ||
2044 isset($count['>=']) || isset($count['<=']))) {
2045 $counted = $tags ? count($tags) : 0;
2047 if (isset($count['>'])) {
2048 self::assertTrue($counted > $count['>'], $message);
2051 if (isset($count['>='])) {
2052 self::assertTrue($counted >= $count['>='], $message);
2055 if (isset($count['<'])) {
2056 self::assertTrue($counted < $count['<'], $message);
2059 if (isset($count['<='])) {
2060 self::assertTrue($counted <= $count['<='], $message);
2063 throw new PHPUnit_Framework_Exception;
2068 * Evaluate an HTML or XML string and assert its structure and/or contents.
2070 * The first argument ($matcher) is an associative array that specifies the
2071 * match criteria for the assertion:
2073 * - `id` : the node with the given id attribute must match the
2074 * corresponding value.
2075 * - `tag` : the node type must match the corresponding value.
2076 * - `attributes` : a hash. The node's attributes must match the
2077 * corresponding values in the hash.
2078 * - `content` : The text content must match the given value.
2079 * - `parent` : a hash. The node's parent must match the
2080 * corresponding hash.
2081 * - `child` : a hash. At least one of the node's immediate children
2082 * must meet the criteria described by the hash.
2083 * - `ancestor` : a hash. At least one of the node's ancestors must
2084 * meet the criteria described by the hash.
2085 * - `descendant` : a hash. At least one of the node's descendants must
2086 * meet the criteria described by the hash.
2087 * - `children` : a hash, for counting children of a node.
2089 * - `count` : a number which must equal the number of children
2091 * - `less_than` : the number of matching children must be greater
2093 * - `greater_than` : the number of matching children must be less than
2095 * - `only` : another hash consisting of the keys to use to match
2096 * on the children, and only matching children will be
2100 * // Matcher that asserts that there is an element with an id="my_id".
2101 * $matcher = array('id' => 'my_id');
2103 * // Matcher that asserts that there is a "span" tag.
2104 * $matcher = array('tag' => 'span');
2106 * // Matcher that asserts that there is a "span" tag with the content
2108 * $matcher = array('tag' => 'span', 'content' => 'Hello World');
2110 * // Matcher that asserts that there is a "span" tag with content matching
2111 * // the regular expression pattern.
2112 * $matcher = array('tag' => 'span', 'content' => 'regexp:/Try P(HP|ython)/');
2114 * // Matcher that asserts that there is a "span" with an "list" class
2118 * 'attributes' => array('class' => 'list')
2121 * // Matcher that asserts that there is a "span" inside of a "div".
2124 * 'parent' => array('tag' => 'div')
2127 * // Matcher that asserts that there is a "span" somewhere inside a
2131 * 'ancestor' => array('tag' => 'table')
2134 * // Matcher that asserts that there is a "span" with at least one "em"
2138 * 'child' => array('tag' => 'em')
2141 * // Matcher that asserts that there is a "span" containing a (possibly
2142 * // nested) "strong" tag.
2145 * 'descendant' => array('tag' => 'strong')
2148 * // Matcher that asserts that there is a "span" containing 5-10 "em" tags
2149 * // as immediate children.
2152 * 'children' => array(
2153 * 'less_than' => 11,
2154 * 'greater_than' => 4,
2155 * 'only' => array('tag' => 'em')
2159 * // Matcher that asserts that there is a "div", with an "ul" ancestor and
2160 * // a "li" parent (with class="enum"), and containing a "span" descendant
2161 * // that contains an element with id="my_test" and the text "Hello World".
2164 * 'ancestor' => array('tag' => 'ul'),
2165 * 'parent' => array(
2167 * 'attributes' => array('class' => 'enum')
2169 * 'descendant' => array(
2172 * 'id' => 'my_test',
2173 * 'content' => 'Hello World'
2178 * // Use assertTag() to apply a $matcher to a piece of $html.
2179 * $this->assertTag($matcher, $html);
2181 * // Use assertTag() to apply a $matcher to a piece of $xml.
2182 * $this->assertTag($matcher, $xml, '', false);
2185 * The second argument ($actual) is a string containing either HTML or
2186 * XML text to be tested.
2188 * The third argument ($message) is an optional message that will be
2189 * used if the assertion fails.
2191 * The fourth argument ($html) is an optional flag specifying whether
2192 * to load the $actual string into a DOMDocument using the HTML or
2193 * XML load strategy. It is true by default, which assumes the HTML
2194 * load strategy. In many cases, this will be acceptable for XML as well.
2196 * @param array $matcher
2197 * @param string $actual
2198 * @param string $message
2199 * @param bool $isHtml
2201 * @since Method available since Release 3.3.0
2203 * @codeCoverageIgnore
2205 public static function assertTag($matcher, $actual, $message = '', $isHtml = true)
2207 trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
2209 $dom = PHPUnit_Util_XML::load($actual, $isHtml);
2210 $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
2211 $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
2213 self::assertTrue($matched, $message);
2217 * This assertion is the exact opposite of assertTag().
2219 * Rather than asserting that $matcher results in a match, it asserts that
2220 * $matcher does not match.
2222 * @param array $matcher
2223 * @param string $actual
2224 * @param string $message
2225 * @param bool $isHtml
2227 * @since Method available since Release 3.3.0
2229 * @codeCoverageIgnore
2231 public static function assertNotTag($matcher, $actual, $message = '', $isHtml = true)
2233 trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
2235 $dom = PHPUnit_Util_XML::load($actual, $isHtml);
2236 $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
2237 $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
2239 self::assertFalse($matched, $message);
2243 * Evaluates a PHPUnit_Framework_Constraint matcher object.
2245 * @param mixed $value
2246 * @param PHPUnit_Framework_Constraint $constraint
2247 * @param string $message
2249 * @since Method available since Release 3.0.0
2251 public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '')
2253 self::$count += count($constraint);
2255 $constraint->evaluate($value, $message);
2259 * Asserts that a string is a valid JSON string.
2261 * @param string $actualJson
2262 * @param string $message
2264 * @since Method available since Release 3.7.20
2266 public static function assertJson($actualJson, $message = '')
2268 if (!is_string($actualJson)) {
2269 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
2272 self::assertThat($actualJson, self::isJson(), $message);
2276 * Asserts that two given JSON encoded objects or arrays are equal.
2278 * @param string $expectedJson
2279 * @param string $actualJson
2280 * @param string $message
2282 public static function assertJsonStringEqualsJsonString($expectedJson, $actualJson, $message = '')
2284 self::assertJson($expectedJson, $message);
2285 self::assertJson($actualJson, $message);
2287 $expected = json_decode($expectedJson);
2288 $actual = json_decode($actualJson);
2290 self::assertEquals($expected, $actual, $message);
2294 * Asserts that two given JSON encoded objects or arrays are not equal.
2296 * @param string $expectedJson
2297 * @param string $actualJson
2298 * @param string $message
2300 public static function assertJsonStringNotEqualsJsonString($expectedJson, $actualJson, $message = '')
2302 self::assertJson($expectedJson, $message);
2303 self::assertJson($actualJson, $message);
2305 $expected = json_decode($expectedJson);
2306 $actual = json_decode($actualJson);
2308 self::assertNotEquals($expected, $actual, $message);
2312 * Asserts that the generated JSON encoded object and the content of the given file are equal.
2314 * @param string $expectedFile
2315 * @param string $actualJson
2316 * @param string $message
2318 public static function assertJsonStringEqualsJsonFile($expectedFile, $actualJson, $message = '')
2320 self::assertFileExists($expectedFile, $message);
2321 $expectedJson = file_get_contents($expectedFile);
2323 self::assertJson($expectedJson, $message);
2324 self::assertJson($actualJson, $message);
2327 $constraint = new PHPUnit_Framework_Constraint_JsonMatches(
2331 self::assertThat($actualJson, $constraint, $message);
2335 * Asserts that the generated JSON encoded object and the content of the given file are not equal.
2337 * @param string $expectedFile
2338 * @param string $actualJson
2339 * @param string $message
2341 public static function assertJsonStringNotEqualsJsonFile($expectedFile, $actualJson, $message = '')
2343 self::assertFileExists($expectedFile, $message);
2344 $expectedJson = file_get_contents($expectedFile);
2346 self::assertJson($expectedJson, $message);
2347 self::assertJson($actualJson, $message);
2350 $constraint = new PHPUnit_Framework_Constraint_JsonMatches(
2354 self::assertThat($actualJson, new PHPUnit_Framework_Constraint_Not($constraint), $message);
2358 * Asserts that two JSON files are not equal.
2360 * @param string $expectedFile
2361 * @param string $actualFile
2362 * @param string $message
2364 public static function assertJsonFileNotEqualsJsonFile($expectedFile, $actualFile, $message = '')
2366 self::assertFileExists($expectedFile, $message);
2367 self::assertFileExists($actualFile, $message);
2369 $actualJson = file_get_contents($actualFile);
2370 $expectedJson = file_get_contents($expectedFile);
2372 self::assertJson($expectedJson, $message);
2373 self::assertJson($actualJson, $message);
2376 $constraintExpected = new PHPUnit_Framework_Constraint_JsonMatches(
2380 $constraintActual = new PHPUnit_Framework_Constraint_JsonMatches($actualJson);
2382 self::assertThat($expectedJson, new PHPUnit_Framework_Constraint_Not($constraintActual), $message);
2383 self::assertThat($actualJson, new PHPUnit_Framework_Constraint_Not($constraintExpected), $message);
2387 * Asserts that two JSON files are equal.
2389 * @param string $expectedFile
2390 * @param string $actualFile
2391 * @param string $message
2393 public static function assertJsonFileEqualsJsonFile($expectedFile, $actualFile, $message = '')
2395 self::assertFileExists($expectedFile, $message);
2396 self::assertFileExists($actualFile, $message);
2398 $actualJson = file_get_contents($actualFile);
2399 $expectedJson = file_get_contents($expectedFile);
2401 self::assertJson($expectedJson, $message);
2402 self::assertJson($actualJson, $message);
2405 $constraintExpected = new PHPUnit_Framework_Constraint_JsonMatches(
2409 $constraintActual = new PHPUnit_Framework_Constraint_JsonMatches($actualJson);
2411 self::assertThat($expectedJson, $constraintActual, $message);
2412 self::assertThat($actualJson, $constraintExpected, $message);
2416 * Returns a PHPUnit_Framework_Constraint_And matcher object.
2418 * @return PHPUnit_Framework_Constraint_And
2420 * @since Method available since Release 3.0.0
2422 public static function logicalAnd()
2424 $constraints = func_get_args();
2426 $constraint = new PHPUnit_Framework_Constraint_And;
2427 $constraint->setConstraints($constraints);
2433 * Returns a PHPUnit_Framework_Constraint_Or matcher object.
2435 * @return PHPUnit_Framework_Constraint_Or
2437 * @since Method available since Release 3.0.0
2439 public static function logicalOr()
2441 $constraints = func_get_args();
2443 $constraint = new PHPUnit_Framework_Constraint_Or;
2444 $constraint->setConstraints($constraints);
2450 * Returns a PHPUnit_Framework_Constraint_Not matcher object.
2452 * @param PHPUnit_Framework_Constraint $constraint
2454 * @return PHPUnit_Framework_Constraint_Not
2456 * @since Method available since Release 3.0.0
2458 public static function logicalNot(PHPUnit_Framework_Constraint $constraint)
2460 return new PHPUnit_Framework_Constraint_Not($constraint);
2464 * Returns a PHPUnit_Framework_Constraint_Xor matcher object.
2466 * @return PHPUnit_Framework_Constraint_Xor
2468 * @since Method available since Release 3.0.0
2470 public static function logicalXor()
2472 $constraints = func_get_args();
2474 $constraint = new PHPUnit_Framework_Constraint_Xor;
2475 $constraint->setConstraints($constraints);
2481 * Returns a PHPUnit_Framework_Constraint_IsAnything matcher object.
2483 * @return PHPUnit_Framework_Constraint_IsAnything
2485 * @since Method available since Release 3.0.0
2487 public static function anything()
2489 return new PHPUnit_Framework_Constraint_IsAnything;
2493 * Returns a PHPUnit_Framework_Constraint_IsTrue matcher object.
2495 * @return PHPUnit_Framework_Constraint_IsTrue
2497 * @since Method available since Release 3.3.0
2499 public static function isTrue()
2501 return new PHPUnit_Framework_Constraint_IsTrue;
2505 * Returns a PHPUnit_Framework_Constraint_Callback matcher object.
2507 * @param callable $callback
2509 * @return PHPUnit_Framework_Constraint_Callback
2511 public static function callback($callback)
2513 return new PHPUnit_Framework_Constraint_Callback($callback);
2517 * Returns a PHPUnit_Framework_Constraint_IsFalse matcher object.
2519 * @return PHPUnit_Framework_Constraint_IsFalse
2521 * @since Method available since Release 3.3.0
2523 public static function isFalse()
2525 return new PHPUnit_Framework_Constraint_IsFalse;
2529 * Returns a PHPUnit_Framework_Constraint_IsJson matcher object.
2531 * @return PHPUnit_Framework_Constraint_IsJson
2533 * @since Method available since Release 3.7.20
2535 public static function isJson()
2537 return new PHPUnit_Framework_Constraint_IsJson;
2541 * Returns a PHPUnit_Framework_Constraint_IsNull matcher object.
2543 * @return PHPUnit_Framework_Constraint_IsNull
2545 * @since Method available since Release 3.3.0
2547 public static function isNull()
2549 return new PHPUnit_Framework_Constraint_IsNull;
2553 * Returns a PHPUnit_Framework_Constraint_Attribute matcher object.
2555 * @param PHPUnit_Framework_Constraint $constraint
2556 * @param string $attributeName
2558 * @return PHPUnit_Framework_Constraint_Attribute
2560 * @since Method available since Release 3.1.0
2562 public static function attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)
2564 return new PHPUnit_Framework_Constraint_Attribute(
2571 * Returns a PHPUnit_Framework_Constraint_TraversableContains matcher
2574 * @param mixed $value
2575 * @param bool $checkForObjectIdentity
2576 * @param bool $checkForNonObjectIdentity
2578 * @return PHPUnit_Framework_Constraint_TraversableContains
2580 * @since Method available since Release 3.0.0
2582 public static function contains($value, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
2584 return new PHPUnit_Framework_Constraint_TraversableContains($value, $checkForObjectIdentity, $checkForNonObjectIdentity);
2588 * Returns a PHPUnit_Framework_Constraint_TraversableContainsOnly matcher
2591 * @param string $type
2593 * @return PHPUnit_Framework_Constraint_TraversableContainsOnly
2595 * @since Method available since Release 3.1.4
2597 public static function containsOnly($type)
2599 return new PHPUnit_Framework_Constraint_TraversableContainsOnly($type);
2603 * Returns a PHPUnit_Framework_Constraint_TraversableContainsOnly matcher
2606 * @param string $classname
2608 * @return PHPUnit_Framework_Constraint_TraversableContainsOnly
2610 public static function containsOnlyInstancesOf($classname)
2612 return new PHPUnit_Framework_Constraint_TraversableContainsOnly($classname, false);
2616 * Returns a PHPUnit_Framework_Constraint_ArrayHasKey matcher object.
2620 * @return PHPUnit_Framework_Constraint_ArrayHasKey
2622 * @since Method available since Release 3.0.0
2624 public static function arrayHasKey($key)
2626 return new PHPUnit_Framework_Constraint_ArrayHasKey($key);
2630 * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object.
2632 * @param mixed $value
2633 * @param float $delta
2634 * @param int $maxDepth
2635 * @param bool $canonicalize
2636 * @param bool $ignoreCase
2638 * @return PHPUnit_Framework_Constraint_IsEqual
2640 * @since Method available since Release 3.0.0
2642 public static function equalTo($value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
2644 return new PHPUnit_Framework_Constraint_IsEqual(
2654 * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object
2655 * that is wrapped in a PHPUnit_Framework_Constraint_Attribute matcher
2658 * @param string $attributeName
2659 * @param mixed $value
2660 * @param float $delta
2661 * @param int $maxDepth
2662 * @param bool $canonicalize
2663 * @param bool $ignoreCase
2665 * @return PHPUnit_Framework_Constraint_Attribute
2667 * @since Method available since Release 3.1.0
2669 public static function attributeEqualTo($attributeName, $value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
2671 return self::attribute(
2684 * Returns a PHPUnit_Framework_Constraint_IsEmpty matcher object.
2686 * @return PHPUnit_Framework_Constraint_IsEmpty
2688 * @since Method available since Release 3.5.0
2690 public static function isEmpty()
2692 return new PHPUnit_Framework_Constraint_IsEmpty;
2696 * Returns a PHPUnit_Framework_Constraint_FileExists matcher object.
2698 * @return PHPUnit_Framework_Constraint_FileExists
2700 * @since Method available since Release 3.0.0
2702 public static function fileExists()
2704 return new PHPUnit_Framework_Constraint_FileExists;
2708 * Returns a PHPUnit_Framework_Constraint_GreaterThan matcher object.
2710 * @param mixed $value
2712 * @return PHPUnit_Framework_Constraint_GreaterThan
2714 * @since Method available since Release 3.0.0
2716 public static function greaterThan($value)
2718 return new PHPUnit_Framework_Constraint_GreaterThan($value);
2722 * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
2723 * a PHPUnit_Framework_Constraint_IsEqual and a
2724 * PHPUnit_Framework_Constraint_GreaterThan matcher object.
2726 * @param mixed $value
2728 * @return PHPUnit_Framework_Constraint_Or
2730 * @since Method available since Release 3.1.0
2732 public static function greaterThanOrEqual($value)
2734 return self::logicalOr(
2735 new PHPUnit_Framework_Constraint_IsEqual($value),
2736 new PHPUnit_Framework_Constraint_GreaterThan($value)
2741 * Returns a PHPUnit_Framework_Constraint_ClassHasAttribute matcher object.
2743 * @param string $attributeName
2745 * @return PHPUnit_Framework_Constraint_ClassHasAttribute
2747 * @since Method available since Release 3.1.0
2749 public static function classHasAttribute($attributeName)
2751 return new PHPUnit_Framework_Constraint_ClassHasAttribute(
2757 * Returns a PHPUnit_Framework_Constraint_ClassHasStaticAttribute matcher
2760 * @param string $attributeName
2762 * @return PHPUnit_Framework_Constraint_ClassHasStaticAttribute
2764 * @since Method available since Release 3.1.0
2766 public static function classHasStaticAttribute($attributeName)
2768 return new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
2774 * Returns a PHPUnit_Framework_Constraint_ObjectHasAttribute matcher object.
2776 * @param string $attributeName
2778 * @return PHPUnit_Framework_Constraint_ObjectHasAttribute
2780 * @since Method available since Release 3.0.0
2782 public static function objectHasAttribute($attributeName)
2784 return new PHPUnit_Framework_Constraint_ObjectHasAttribute(
2790 * Returns a PHPUnit_Framework_Constraint_IsIdentical matcher object.
2792 * @param mixed $value
2794 * @return PHPUnit_Framework_Constraint_IsIdentical
2796 * @since Method available since Release 3.0.0
2798 public static function identicalTo($value)
2800 return new PHPUnit_Framework_Constraint_IsIdentical($value);
2804 * Returns a PHPUnit_Framework_Constraint_IsInstanceOf matcher object.
2806 * @param string $className
2808 * @return PHPUnit_Framework_Constraint_IsInstanceOf
2810 * @since Method available since Release 3.0.0
2812 public static function isInstanceOf($className)
2814 return new PHPUnit_Framework_Constraint_IsInstanceOf($className);
2818 * Returns a PHPUnit_Framework_Constraint_IsType matcher object.
2820 * @param string $type
2822 * @return PHPUnit_Framework_Constraint_IsType
2824 * @since Method available since Release 3.0.0
2826 public static function isType($type)
2828 return new PHPUnit_Framework_Constraint_IsType($type);
2832 * Returns a PHPUnit_Framework_Constraint_LessThan matcher object.
2834 * @param mixed $value
2836 * @return PHPUnit_Framework_Constraint_LessThan
2838 * @since Method available since Release 3.0.0
2840 public static function lessThan($value)
2842 return new PHPUnit_Framework_Constraint_LessThan($value);
2846 * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
2847 * a PHPUnit_Framework_Constraint_IsEqual and a
2848 * PHPUnit_Framework_Constraint_LessThan matcher object.
2850 * @param mixed $value
2852 * @return PHPUnit_Framework_Constraint_Or
2854 * @since Method available since Release 3.1.0
2856 public static function lessThanOrEqual($value)
2858 return self::logicalOr(
2859 new PHPUnit_Framework_Constraint_IsEqual($value),
2860 new PHPUnit_Framework_Constraint_LessThan($value)
2865 * Returns a PHPUnit_Framework_Constraint_PCREMatch matcher object.
2867 * @param string $pattern
2869 * @return PHPUnit_Framework_Constraint_PCREMatch
2871 * @since Method available since Release 3.0.0
2873 public static function matchesRegularExpression($pattern)
2875 return new PHPUnit_Framework_Constraint_PCREMatch($pattern);
2879 * Returns a PHPUnit_Framework_Constraint_StringMatches matcher object.
2881 * @param string $string
2883 * @return PHPUnit_Framework_Constraint_StringMatches
2885 * @since Method available since Release 3.5.0
2887 public static function matches($string)
2889 return new PHPUnit_Framework_Constraint_StringMatches($string);
2893 * Returns a PHPUnit_Framework_Constraint_StringStartsWith matcher object.
2895 * @param mixed $prefix
2897 * @return PHPUnit_Framework_Constraint_StringStartsWith
2899 * @since Method available since Release 3.4.0
2901 public static function stringStartsWith($prefix)
2903 return new PHPUnit_Framework_Constraint_StringStartsWith($prefix);
2907 * Returns a PHPUnit_Framework_Constraint_StringContains matcher object.
2909 * @param string $string
2912 * @return PHPUnit_Framework_Constraint_StringContains
2914 * @since Method available since Release 3.0.0
2916 public static function stringContains($string, $case = true)
2918 return new PHPUnit_Framework_Constraint_StringContains($string, $case);
2922 * Returns a PHPUnit_Framework_Constraint_StringEndsWith matcher object.
2924 * @param mixed $suffix
2926 * @return PHPUnit_Framework_Constraint_StringEndsWith
2928 * @since Method available since Release 3.4.0
2930 public static function stringEndsWith($suffix)
2932 return new PHPUnit_Framework_Constraint_StringEndsWith($suffix);
2936 * Returns a PHPUnit_Framework_Constraint_Count matcher object.
2940 * @return PHPUnit_Framework_Constraint_Count
2942 public static function countOf($count)
2944 return new PHPUnit_Framework_Constraint_Count($count);
2947 * Fails a test with the given message.
2949 * @param string $message
2951 * @throws PHPUnit_Framework_AssertionFailedError
2953 public static function fail($message = '')
2955 throw new PHPUnit_Framework_AssertionFailedError($message);
2959 * Returns the value of an attribute of a class or an object.
2960 * This also works for attributes that are declared protected or private.
2962 * @param mixed $classOrObject
2963 * @param string $attributeName
2967 * @throws PHPUnit_Framework_Exception
2969 public static function readAttribute($classOrObject, $attributeName)
2971 if (!is_string($attributeName)) {
2972 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
2975 if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
2976 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
2979 if (is_string($classOrObject)) {
2980 if (!class_exists($classOrObject)) {
2981 throw PHPUnit_Util_InvalidArgumentHelper::factory(
2987 return self::getStaticAttribute(
2991 } elseif (is_object($classOrObject)) {
2992 return self::getObjectAttribute(
2997 throw PHPUnit_Util_InvalidArgumentHelper::factory(
2999 'class name or object'
3005 * Returns the value of a static attribute.
3006 * This also works for attributes that are declared protected or private.
3008 * @param string $className
3009 * @param string $attributeName
3013 * @throws PHPUnit_Framework_Exception
3015 * @since Method available since Release 4.0.0
3017 public static function getStaticAttribute($className, $attributeName)
3019 if (!is_string($className)) {
3020 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
3023 if (!class_exists($className)) {
3024 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class name');
3027 if (!is_string($attributeName)) {
3028 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
3031 if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
3032 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
3035 $class = new ReflectionClass($className);
3038 $attributes = $class->getStaticProperties();
3040 if (array_key_exists($attributeName, $attributes)) {
3041 return $attributes[$attributeName];
3044 $class = $class->getParentClass();
3047 throw new PHPUnit_Framework_Exception(
3049 'Attribute "%s" not found in class.',
3056 * Returns the value of an object's attribute.
3057 * This also works for attributes that are declared protected or private.
3059 * @param object $object
3060 * @param string $attributeName
3064 * @throws PHPUnit_Framework_Exception
3066 * @since Method available since Release 4.0.0
3068 public static function getObjectAttribute($object, $attributeName)
3070 if (!is_object($object)) {
3071 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'object');
3074 if (!is_string($attributeName)) {
3075 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
3078 if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
3079 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
3083 $attribute = new ReflectionProperty($object, $attributeName);
3084 } catch (ReflectionException $e) {
3085 $reflector = new ReflectionObject($object);
3087 while ($reflector = $reflector->getParentClass()) {
3089 $attribute = $reflector->getProperty($attributeName);
3091 } catch (ReflectionException $e) {
3096 if (isset($attribute)) {
3097 if (!$attribute || $attribute->isPublic()) {
3098 return $object->$attributeName;
3101 $attribute->setAccessible(true);
3102 $value = $attribute->getValue($object);
3103 $attribute->setAccessible(false);
3108 throw new PHPUnit_Framework_Exception(
3110 'Attribute "%s" not found in object.',
3117 * Mark the test as incomplete.
3119 * @param string $message
3121 * @throws PHPUnit_Framework_IncompleteTestError
3123 * @since Method available since Release 3.0.0
3125 public static function markTestIncomplete($message = '')
3127 throw new PHPUnit_Framework_IncompleteTestError($message);
3131 * Mark the test as skipped.
3133 * @param string $message
3135 * @throws PHPUnit_Framework_SkippedTestError
3137 * @since Method available since Release 3.0.0
3139 public static function markTestSkipped($message = '')
3141 throw new PHPUnit_Framework_SkippedTestError($message);
3145 * Return the current assertion count.
3149 * @since Method available since Release 3.3.3
3151 public static function getCount()
3153 return self::$count;
3157 * Reset the assertion counter.
3159 * @since Method available since Release 3.3.3
3161 public static function resetCount()