5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.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.
38 * @subpackage Framework
39 * @author Sebastian Bergmann <sebastian@phpunit.de>
40 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @link http://www.phpunit.de/
43 * @since File available since Release 2.0.0
47 * A set of assert methods.
50 * @subpackage Framework
51 * @author Sebastian Bergmann <sebastian@phpunit.de>
52 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
53 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
54 * @version Release: 3.5.14
55 * @link http://www.phpunit.de/
56 * @since Class available since Release 2.0.0
58 abstract class PHPUnit_Framework_Assert
63 private static $count = 0;
66 * Asserts that an array has a specified key.
70 * @param string $message
71 * @since Method available since Release 3.0.0
73 public static function assertArrayHasKey($key, array $array, $message = '')
75 if (!(is_integer($key) || is_string($key))) {
76 throw PHPUnit_Util_InvalidArgumentHelper::factory(
77 1, 'integer or string'
81 $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey($key);
83 self::assertThat($array, $constraint, $message);
87 * Asserts that an array does not have a specified key.
91 * @param string $message
92 * @since Method available since Release 3.0.0
94 public static function assertArrayNotHasKey($key, array $array, $message = '')
96 if (!(is_integer($key) || is_string($key))) {
97 throw PHPUnit_Util_InvalidArgumentHelper::factory(
98 1, 'integer or string'
102 $constraint = new PHPUnit_Framework_Constraint_Not(
103 new PHPUnit_Framework_Constraint_ArrayHasKey($key)
106 self::assertThat($array, $constraint, $message);
110 * Asserts that a haystack contains a needle.
112 * @param mixed $needle
113 * @param mixed $haystack
114 * @param string $message
115 * @param boolean $ignoreCase
116 * @since Method available since Release 2.1.0
118 public static function assertContains($needle, $haystack, $message = '', $ignoreCase = FALSE)
120 if (is_array($haystack) ||
121 is_object($haystack) && $haystack instanceof Traversable) {
122 $constraint = new PHPUnit_Framework_Constraint_TraversableContains(
127 else if (is_string($haystack)) {
128 $constraint = new PHPUnit_Framework_Constraint_StringContains(
134 throw PHPUnit_Util_InvalidArgumentHelper::factory(
135 2, 'array, iterator or string'
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 * @param boolean $ignoreCase
151 * @since Method available since Release 3.0.0
153 public static function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE)
155 self::assertContains(
157 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
164 * Asserts that a haystack does not contain a needle.
166 * @param mixed $needle
167 * @param mixed $haystack
168 * @param string $message
169 * @param boolean $ignoreCase
170 * @since Method available since Release 2.1.0
172 public static function assertNotContains($needle, $haystack, $message = '', $ignoreCase = FALSE)
174 if (is_array($haystack) ||
175 is_object($haystack) && $haystack instanceof Traversable) {
176 $constraint = new PHPUnit_Framework_Constraint_Not(
177 new PHPUnit_Framework_Constraint_TraversableContains($needle)
181 else if (is_string($haystack)) {
182 $constraint = new PHPUnit_Framework_Constraint_Not(
183 new PHPUnit_Framework_Constraint_StringContains(
190 throw PHPUnit_Util_InvalidArgumentHelper::factory(
191 2, 'array, iterator or string'
195 self::assertThat($haystack, $constraint, $message);
199 * Asserts that a haystack that is stored in a static attribute of a class
200 * or an attribute of an object does not contain a needle.
202 * @param mixed $needle
203 * @param string $haystackAttributeName
204 * @param mixed $haystackClassOrObject
205 * @param string $message
206 * @param boolean $ignoreCase
207 * @since Method available since Release 3.0.0
209 public static function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE)
211 self::assertNotContains(
213 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
220 * Asserts that a haystack contains only values of a given type.
222 * @param string $type
223 * @param mixed $haystack
224 * @param boolean $isNativeType
225 * @param string $message
226 * @since Method available since Release 3.1.4
228 public static function assertContainsOnly($type, $haystack, $isNativeType = NULL, $message = '')
230 if (!(is_array($haystack) ||
231 is_object($haystack) && $haystack instanceof Traversable)) {
232 throw PHPUnit_Util_InvalidArgumentHelper::factory(
233 2, 'array or iterator'
237 if ($isNativeType == NULL) {
238 $isNativeType = PHPUnit_Util_Type::isType($type);
243 new PHPUnit_Framework_Constraint_TraversableContainsOnly(
251 * Asserts that a haystack that is stored in a static attribute of a class
252 * or an attribute of an object contains only values of a given type.
254 * @param string $type
255 * @param string $haystackAttributeName
256 * @param mixed $haystackClassOrObject
257 * @param boolean $isNativeType
258 * @param string $message
259 * @since Method available since Release 3.1.4
261 public static function assertAttributeContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = NULL, $message = '')
263 self::assertContainsOnly(
265 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
272 * Asserts that a haystack does not contain only values of a given type.
274 * @param string $type
275 * @param mixed $haystack
276 * @param boolean $isNativeType
277 * @param string $message
278 * @since Method available since Release 3.1.4
280 public static function assertNotContainsOnly($type, $haystack, $isNativeType = NULL, $message = '')
282 if (!(is_array($haystack) ||
283 is_object($haystack) && $haystack instanceof Traversable)) {
284 throw PHPUnit_Util_InvalidArgumentHelper::factory(
285 2, 'array or iterator'
289 if ($isNativeType == NULL) {
290 $isNativeType = PHPUnit_Util_Type::isType($type);
295 new PHPUnit_Framework_Constraint_Not(
296 new PHPUnit_Framework_Constraint_TraversableContainsOnly(
305 * Asserts that a haystack that is stored in a static attribute of a class
306 * or an attribute of an object does not contain only values of a given
309 * @param string $type
310 * @param string $haystackAttributeName
311 * @param mixed $haystackClassOrObject
312 * @param boolean $isNativeType
313 * @param string $message
314 * @since Method available since Release 3.1.4
316 public static function assertAttributeNotContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = NULL, $message = '')
318 self::assertNotContainsOnly(
320 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
327 * Asserts that two variables are equal.
329 * @param mixed $expected
330 * @param mixed $actual
331 * @param string $message
332 * @param float $delta
333 * @param integer $maxDepth
334 * @param boolean $canonicalize
335 * @param boolean $ignoreCase
337 public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
339 $constraint = new PHPUnit_Framework_Constraint_IsEqual(
340 $expected, $delta, $maxDepth, $canonicalize, $ignoreCase
343 self::assertThat($actual, $constraint, $message);
347 * Asserts that a variable is equal to an attribute of an object.
349 * @param mixed $expected
350 * @param string $actualAttributeName
351 * @param string $actualClassOrObject
352 * @param string $message
353 * @param float $delta
354 * @param integer $maxDepth
355 * @param boolean $canonicalize
356 * @param boolean $ignoreCase
358 public static function assertAttributeEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
362 self::readAttribute($actualClassOrObject, $actualAttributeName),
372 * Asserts that two variables are not equal.
374 * @param mixed $expected
375 * @param mixed $actual
376 * @param string $message
377 * @param float $delta
378 * @param integer $maxDepth
379 * @param boolean $canonicalize
380 * @param boolean $ignoreCase
381 * @since Method available since Release 2.3.0
383 public static function assertNotEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
385 $constraint = new PHPUnit_Framework_Constraint_Not(
386 new PHPUnit_Framework_Constraint_IsEqual(
387 $expected, $delta, $maxDepth, $canonicalize, $ignoreCase
391 self::assertThat($actual, $constraint, $message);
395 * Asserts that a variable is not equal to an attribute of an object.
397 * @param mixed $expected
398 * @param string $actualAttributeName
399 * @param string $actualClassOrObject
400 * @param string $message
401 * @param float $delta
402 * @param integer $maxDepth
403 * @param boolean $canonicalize
404 * @param boolean $ignoreCase
406 public static function assertAttributeNotEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
408 self::assertNotEquals(
410 self::readAttribute($actualClassOrObject, $actualAttributeName),
420 * Asserts that a variable is empty.
422 * @param mixed $actual
423 * @param string $message
424 * @throws PHPUnit_Framework_AssertionFailedError
426 public static function assertEmpty($actual, $message = '')
428 self::assertThat($actual, self::isEmpty(), $message);
432 * Asserts that a static attribute of a class or an attribute of an object
435 * @param string $haystackAttributeName
436 * @param mixed $haystackClassOrObject
437 * @param string $message
438 * @since Method available since Release 3.5.0
440 public static function assertAttributeEmpty($haystackAttributeName, $haystackClassOrObject, $message = '')
443 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
449 * Asserts that a variable is not empty.
451 * @param mixed $actual
452 * @param string $message
453 * @throws PHPUnit_Framework_AssertionFailedError
455 public static function assertNotEmpty($actual, $message = '')
457 self::assertThat($actual, self::logicalNot(self::isEmpty()), $message);
461 * Asserts that a static attribute of a class or an attribute of an object
464 * @param string $haystackAttributeName
465 * @param mixed $haystackClassOrObject
466 * @param string $message
467 * @since Method available since Release 3.5.0
469 public static function assertAttributeNotEmpty($haystackAttributeName, $haystackClassOrObject, $message = '')
471 self::assertNotEmpty(
472 self::readAttribute($haystackClassOrObject, $haystackAttributeName),
478 * Asserts that a value is greater than another value.
480 * @param mixed $expected
481 * @param mixed $actual
482 * @param string $message
483 * @since Method available since Release 3.1.0
485 public static function assertGreaterThan($expected, $actual, $message = '')
487 self::assertThat($actual, self::greaterThan($expected), $message);
491 * Asserts that an attribute is greater than another value.
493 * @param mixed $expected
494 * @param string $actualAttributeName
495 * @param string $actualClassOrObject
496 * @param string $message
497 * @since Method available since Release 3.1.0
499 public static function assertAttributeGreaterThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
501 self::assertGreaterThan(
503 self::readAttribute($actualClassOrObject, $actualAttributeName),
509 * Asserts that a value is greater than or equal to another value.
511 * @param mixed $expected
512 * @param mixed $actual
513 * @param string $message
514 * @since Method available since Release 3.1.0
516 public static function assertGreaterThanOrEqual($expected, $actual, $message = '')
519 $actual, self::greaterThanOrEqual($expected), $message
524 * Asserts that an attribute is greater than or equal to another value.
526 * @param mixed $expected
527 * @param string $actualAttributeName
528 * @param string $actualClassOrObject
529 * @param string $message
530 * @since Method available since Release 3.1.0
532 public static function assertAttributeGreaterThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
534 self::assertGreaterThanOrEqual(
536 self::readAttribute($actualClassOrObject, $actualAttributeName),
542 * Asserts that a value is smaller than another value.
544 * @param mixed $expected
545 * @param mixed $actual
546 * @param string $message
547 * @since Method available since Release 3.1.0
549 public static function assertLessThan($expected, $actual, $message = '')
551 self::assertThat($actual, self::lessThan($expected), $message);
555 * Asserts that an attribute is smaller than another value.
557 * @param mixed $expected
558 * @param string $actualAttributeName
559 * @param string $actualClassOrObject
560 * @param string $message
561 * @since Method available since Release 3.1.0
563 public static function assertAttributeLessThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
565 self::assertLessThan(
567 self::readAttribute($actualClassOrObject, $actualAttributeName),
573 * Asserts that a value is smaller than or equal to another value.
575 * @param mixed $expected
576 * @param mixed $actual
577 * @param string $message
578 * @since Method available since Release 3.1.0
580 public static function assertLessThanOrEqual($expected, $actual, $message = '')
582 self::assertThat($actual, self::lessThanOrEqual($expected), $message);
586 * Asserts that an attribute is smaller than or equal to another value.
588 * @param mixed $expected
589 * @param string $actualAttributeName
590 * @param string $actualClassOrObject
591 * @param string $message
592 * @since Method available since Release 3.1.0
594 public static function assertAttributeLessThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
596 self::assertLessThanOrEqual(
598 self::readAttribute($actualClassOrObject, $actualAttributeName),
604 * Asserts that the contents of one file is equal to the contents of another
607 * @param string $expected
608 * @param string $actual
609 * @param string $message
610 * @param boolean $canonicalize
611 * @param boolean $ignoreCase
612 * @since Method available since Release 3.2.14
614 public static function assertFileEquals($expected, $actual, $message = '', $canonicalize = FALSE, $ignoreCase = FALSE)
616 self::assertFileExists($expected, $message);
617 self::assertFileExists($actual, $message);
620 file_get_contents($expected),
621 file_get_contents($actual),
631 * Asserts that the contents of one file is not equal to the contents of
634 * @param string $expected
635 * @param string $actual
636 * @param string $message
637 * @param boolean $canonicalize
638 * @param boolean $ignoreCase
639 * @since Method available since Release 3.2.14
641 public static function assertFileNotEquals($expected, $actual, $message = '', $canonicalize = FALSE, $ignoreCase = FALSE)
643 self::assertFileExists($expected, $message);
644 self::assertFileExists($actual, $message);
646 self::assertNotEquals(
647 file_get_contents($expected),
648 file_get_contents($actual),
658 * Asserts that the contents of a string is equal
659 * to the contents of a file.
661 * @param string $expectedFile
662 * @param string $actualString
663 * @param string $message
664 * @param boolean $canonicalize
665 * @param boolean $ignoreCase
666 * @since Method available since Release 3.3.0
668 public static function assertStringEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = FALSE, $ignoreCase = FALSE)
670 self::assertFileExists($expectedFile, $message);
673 file_get_contents($expectedFile),
684 * Asserts that the contents of a string is not equal
685 * to the contents of a file.
687 * @param string $expectedFile
688 * @param string $actualString
689 * @param string $message
690 * @param boolean $canonicalize
691 * @param boolean $ignoreCase
692 * @since Method available since Release 3.3.0
694 public static function assertStringNotEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = FALSE, $ignoreCase = FALSE)
696 self::assertFileExists($expectedFile, $message);
698 self::assertNotEquals(
699 file_get_contents($expectedFile),
710 * Asserts that a file exists.
712 * @param string $filename
713 * @param string $message
714 * @since Method available since Release 3.0.0
716 public static function assertFileExists($filename, $message = '')
718 if (!is_string($filename)) {
719 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
722 $constraint = new PHPUnit_Framework_Constraint_FileExists;
724 self::assertThat($filename, $constraint, $message);
728 * Asserts that a file does not exist.
730 * @param string $filename
731 * @param string $message
732 * @since Method available since Release 3.0.0
734 public static function assertFileNotExists($filename, $message = '')
736 if (!is_string($filename)) {
737 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
740 $constraint = new PHPUnit_Framework_Constraint_Not(
741 new PHPUnit_Framework_Constraint_FileExists
744 self::assertThat($filename, $constraint, $message);
748 * Asserts that a condition is true.
750 * @param boolean $condition
751 * @param string $message
752 * @throws PHPUnit_Framework_AssertionFailedError
754 public static function assertTrue($condition, $message = '')
756 self::assertThat($condition, self::isTrue(), $message);
760 * Asserts that a condition is false.
762 * @param boolean $condition
763 * @param string $message
764 * @throws PHPUnit_Framework_AssertionFailedError
766 public static function assertFalse($condition, $message = '')
768 self::assertThat($condition, self::isFalse(), $message);
772 * Asserts that a variable is not NULL.
774 * @param mixed $actual
775 * @param string $message
777 public static function assertNotNull($actual, $message = '')
779 self::assertThat($actual, self::logicalNot(self::isNull()), $message);
783 * Asserts that a variable is NULL.
785 * @param mixed $actual
786 * @param string $message
788 public static function assertNull($actual, $message = '')
790 self::assertThat($actual, self::isNull(), $message);
794 * Asserts that a class has a specified attribute.
796 * @param string $attributeName
797 * @param string $className
798 * @param string $message
799 * @since Method available since Release 3.1.0
801 public static function assertClassHasAttribute($attributeName, $className, $message = '')
803 if (!is_string($attributeName)) {
804 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
807 if (!is_string($className) || !class_exists($className, FALSE)) {
808 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
811 $constraint = new PHPUnit_Framework_Constraint_ClassHasAttribute(
815 self::assertThat($className, $constraint, $message);
819 * Asserts that a class does not have a specified attribute.
821 * @param string $attributeName
822 * @param string $className
823 * @param string $message
824 * @since Method available since Release 3.1.0
826 public static function assertClassNotHasAttribute($attributeName, $className, $message = '')
828 if (!is_string($attributeName)) {
829 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
832 if (!is_string($className) || !class_exists($className, FALSE)) {
833 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
836 $constraint = new PHPUnit_Framework_Constraint_Not(
837 new PHPUnit_Framework_Constraint_ClassHasAttribute($attributeName)
840 self::assertThat($className, $constraint, $message);
844 * Asserts that a class has a specified static attribute.
846 * @param string $attributeName
847 * @param string $className
848 * @param string $message
849 * @since Method available since Release 3.1.0
851 public static function assertClassHasStaticAttribute($attributeName, $className, $message = '')
853 if (!is_string($attributeName)) {
854 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
857 if (!is_string($className) || !class_exists($className, FALSE)) {
858 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
861 $constraint = new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
865 self::assertThat($className, $constraint, $message);
869 * Asserts that a class does not have a specified static attribute.
871 * @param string $attributeName
872 * @param string $className
873 * @param string $message
874 * @since Method available since Release 3.1.0
876 public static function assertClassNotHasStaticAttribute($attributeName, $className, $message = '')
878 if (!is_string($attributeName)) {
879 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
882 if (!is_string($className) || !class_exists($className, FALSE)) {
883 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
886 $constraint = new PHPUnit_Framework_Constraint_Not(
887 new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
892 self::assertThat($className, $constraint, $message);
896 * Asserts that an object has a specified attribute.
898 * @param string $attributeName
899 * @param object $object
900 * @param string $message
901 * @since Method available since Release 3.0.0
903 public static function assertObjectHasAttribute($attributeName, $object, $message = '')
905 if (!is_string($attributeName)) {
906 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
909 if (!is_object($object)) {
910 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
913 $constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute(
917 self::assertThat($object, $constraint, $message);
921 * Asserts that an object does not have a specified attribute.
923 * @param string $attributeName
924 * @param object $object
925 * @param string $message
926 * @since Method available since Release 3.0.0
928 public static function assertObjectNotHasAttribute($attributeName, $object, $message = '')
930 if (!is_string($attributeName)) {
931 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
934 if (!is_object($object)) {
935 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
938 $constraint = new PHPUnit_Framework_Constraint_Not(
939 new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName)
942 self::assertThat($object, $constraint, $message);
946 * Asserts that two variables have the same type and value.
947 * Used on objects, it asserts that two variables reference
950 * @param mixed $expected
951 * @param mixed $actual
952 * @param string $message
954 public static function assertSame($expected, $actual, $message = '')
956 if (is_bool($expected) && is_bool($actual)) {
957 self::assertEquals($expected, $actual, $message);
959 $constraint = new PHPUnit_Framework_Constraint_IsIdentical(
963 self::assertThat($actual, $constraint, $message);
968 * Asserts that a variable and an attribute of an object have the same type
971 * @param mixed $expected
972 * @param string $actualAttributeName
973 * @param object $actualClassOrObject
974 * @param string $message
976 public static function assertAttributeSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
980 self::readAttribute($actualClassOrObject, $actualAttributeName),
986 * Asserts that two variables do not have the same type and value.
987 * Used on objects, it asserts that two variables do not reference
990 * @param mixed $expected
991 * @param mixed $actual
992 * @param string $message
994 public static function assertNotSame($expected, $actual, $message = '')
996 if (is_bool($expected) && is_bool($actual)) {
997 self::assertNotEquals($expected, $actual, $message);
999 $constraint = new PHPUnit_Framework_Constraint_Not(
1000 new PHPUnit_Framework_Constraint_IsIdentical($expected)
1003 self::assertThat($actual, $constraint, $message);
1008 * Asserts that a variable and an attribute of an object do not have the
1009 * same type and value.
1011 * @param mixed $expected
1012 * @param string $actualAttributeName
1013 * @param object $actualClassOrObject
1014 * @param string $message
1016 public static function assertAttributeNotSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
1018 self::assertNotSame(
1020 self::readAttribute($actualClassOrObject, $actualAttributeName),
1026 * Asserts that a variable is of a given type.
1028 * @param string $expected
1029 * @param mixed $actual
1030 * @param string $message
1031 * @since Method available since Release 3.5.0
1033 public static function assertInstanceOf($expected, $actual, $message = '')
1035 if (is_string($expected)) {
1036 if (class_exists($expected) || interface_exists($expected)) {
1037 $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
1043 throw PHPUnit_Util_InvalidArgumentHelper::factory(
1044 1, 'class or interface name'
1048 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1051 self::assertThat($actual, $constraint, $message);
1055 * Asserts that an attribute is of a given type.
1057 * @param string $expected
1058 * @param string $attributeName
1059 * @param mixed $classOrObject
1060 * @param string $message
1061 * @since Method available since Release 3.5.0
1063 public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '')
1065 self::assertInstanceOf(
1067 self::readAttribute($classOrObject, $attributeName),
1073 * Asserts that a variable is not of a given type.
1075 * @param string $expected
1076 * @param mixed $actual
1077 * @param string $message
1078 * @since Method available since Release 3.5.0
1080 public static function assertNotInstanceOf($expected, $actual, $message = '')
1082 if (is_string($expected)) {
1083 if (class_exists($expected) || interface_exists($expected)) {
1084 $constraint = new PHPUnit_Framework_Constraint_Not(
1085 new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
1090 throw PHPUnit_Util_InvalidArgumentHelper::factory(
1091 1, 'class or interface name'
1095 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1098 self::assertThat($actual, $constraint, $message);
1102 * Asserts that an attribute is of a given type.
1104 * @param string $expected
1105 * @param string $attributeName
1106 * @param mixed $classOrObject
1107 * @param string $message
1108 * @since Method available since Release 3.5.0
1110 public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '')
1112 self::assertNotInstanceOf(
1114 self::readAttribute($classOrObject, $attributeName),
1120 * Asserts that a variable is of a given type.
1122 * @param string $expected
1123 * @param mixed $actual
1124 * @param string $message
1125 * @since Method available since Release 3.5.0
1127 public static function assertInternalType($expected, $actual, $message = '')
1129 if (is_string($expected)) {
1130 $constraint = new PHPUnit_Framework_Constraint_IsType(
1134 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1137 self::assertThat($actual, $constraint, $message);
1141 * Asserts that an attribute is of a given type.
1143 * @param string $expected
1144 * @param string $attributeName
1145 * @param mixed $classOrObject
1146 * @param string $message
1147 * @since Method available since Release 3.5.0
1149 public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '')
1151 self::assertInternalType(
1153 self::readAttribute($classOrObject, $attributeName),
1159 * Asserts that a variable is not of a given type.
1161 * @param string $expected
1162 * @param mixed $actual
1163 * @param string $message
1164 * @since Method available since Release 3.5.0
1166 public static function assertNotInternalType($expected, $actual, $message = '')
1168 if (is_string($expected)) {
1169 $constraint = new PHPUnit_Framework_Constraint_Not(
1170 new PHPUnit_Framework_Constraint_IsType($expected)
1173 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1176 self::assertThat($actual, $constraint, $message);
1180 * Asserts that an attribute is of a given type.
1182 * @param string $expected
1183 * @param string $attributeName
1184 * @param mixed $classOrObject
1185 * @param string $message
1186 * @since Method available since Release 3.5.0
1188 public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '')
1190 self::assertNotInternalType(
1192 self::readAttribute($classOrObject, $attributeName),
1198 * Asserts that a variable is of a given type.
1200 * @param string $expected
1201 * @param mixed $actual
1202 * @param string $message
1205 public static function assertType($expected, $actual, $message = '')
1207 PHPUnit_Util_DeprecatedFeature_Logger::log(
1208 'assertType() will be removed in PHPUnit 3.6 and should no longer ' .
1209 'be used. assertInternalType() should be used for asserting ' .
1210 'internal types such as "integer" or "string" whereas ' .
1211 'assertInstanceOf() should be used for asserting that an object is ' .
1212 'an instance of a specified class or interface.'
1215 if (is_string($expected)) {
1216 if (PHPUnit_Util_Type::isType($expected)) {
1217 $constraint = new PHPUnit_Framework_Constraint_IsType(
1222 else if (class_exists($expected) || interface_exists($expected)) {
1223 $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
1229 throw PHPUnit_Util_InvalidArgumentHelper::factory(
1230 1, 'class or interface name'
1234 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1237 self::assertThat($actual, $constraint, $message);
1241 * Asserts that an attribute is of a given type.
1243 * @param string $expected
1244 * @param string $attributeName
1245 * @param mixed $classOrObject
1246 * @param string $message
1247 * @since Method available since Release 3.4.0
1250 public static function assertAttributeType($expected, $attributeName, $classOrObject, $message = '')
1252 PHPUnit_Util_DeprecatedFeature_Logger::log(
1253 'assertAttributeType() will be removed in PHPUnit 3.6 and should ' .
1254 'no longer be used. assertAttributeInternalType() should be used ' .
1255 'for asserting internal types such as "integer" or "string" ' .
1256 'whereas assertAttributeInstanceOf() should be used for asserting ' .
1257 'that an object is an instance of a specified class or interface.'
1262 self::readAttribute($classOrObject, $attributeName),
1268 * Asserts that a variable is not of a given type.
1270 * @param string $expected
1271 * @param mixed $actual
1272 * @param string $message
1273 * @since Method available since Release 2.2.0
1276 public static function assertNotType($expected, $actual, $message = '')
1278 PHPUnit_Util_DeprecatedFeature_Logger::log(
1279 'assertNotType() will be removed in PHPUnit 3.6 and should no ' .
1280 'longer be used. assertNotInternalType() should be used for ' .
1281 'asserting internal types such as "integer" or "string" whereas ' .
1282 'assertNotInstanceOf() should be used for asserting that an object ' .
1283 'is not an instance of a specified class or interface.'
1286 if (is_string($expected)) {
1287 if (PHPUnit_Util_Type::isType($expected)) {
1288 $constraint = new PHPUnit_Framework_Constraint_Not(
1289 new PHPUnit_Framework_Constraint_IsType($expected)
1293 else if (class_exists($expected) || interface_exists($expected)) {
1294 $constraint = new PHPUnit_Framework_Constraint_Not(
1295 new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
1300 throw PHPUnit_Util_InvalidArgumentHelper::factory(
1301 1, 'class or interface name'
1305 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1308 self::assertThat($actual, $constraint, $message);
1312 * Asserts that an attribute is of a given type.
1314 * @param string $expected
1315 * @param string $attributeName
1316 * @param mixed $classOrObject
1317 * @param string $message
1318 * @since Method available since Release 3.4.0
1321 public static function assertAttributeNotType($expected, $attributeName, $classOrObject, $message = '')
1323 PHPUnit_Util_DeprecatedFeature_Logger::log(
1324 'assertAttributeNotType() will be removed in PHPUnit 3.6 and ' .
1325 'should no longer be used. assertAttributeNotInternalType() should ' .
1326 'be used for asserting internal types such as "integer" or ' .
1327 '"string" whereas assertAttributeNotInstanceOf() should be used ' .
1328 'for asserting that an object is an instance of a specified class ' .
1332 self::assertNotType(
1334 self::readAttribute($classOrObject, $attributeName),
1340 * Asserts that a string matches a given regular expression.
1342 * @param string $pattern
1343 * @param string $string
1344 * @param string $message
1346 public static function assertRegExp($pattern, $string, $message = '')
1348 if (!is_string($pattern)) {
1349 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1352 if (!is_string($string)) {
1353 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1356 $constraint = new PHPUnit_Framework_Constraint_PCREMatch($pattern);
1358 self::assertThat($string, $constraint, $message);
1362 * Asserts that a string does not match a given regular expression.
1364 * @param string $pattern
1365 * @param string $string
1366 * @param string $message
1367 * @since Method available since Release 2.1.0
1369 public static function assertNotRegExp($pattern, $string, $message = '')
1371 if (!is_string($pattern)) {
1372 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1375 if (!is_string($string)) {
1376 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1379 $constraint = new PHPUnit_Framework_Constraint_Not(
1380 new PHPUnit_Framework_Constraint_PCREMatch($pattern)
1383 self::assertThat($string, $constraint, $message);
1387 * Asserts that a string matches a given format string.
1389 * @param string $format
1390 * @param string $string
1391 * @param string $message
1392 * @since Method available since Release 3.5.0
1394 public static function assertStringMatchesFormat($format, $string, $message = '')
1396 if (!is_string($format)) {
1397 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1400 if (!is_string($string)) {
1401 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1404 $constraint = new PHPUnit_Framework_Constraint_StringMatches($format);
1406 self::assertThat($string, $constraint, $message);
1410 * Asserts that a string does not match a given format string.
1412 * @param string $format
1413 * @param string $string
1414 * @param string $message
1415 * @since Method available since Release 3.5.0
1417 public static function assertStringNotMatchesFormat($format, $string, $message = '')
1419 if (!is_string($format)) {
1420 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1423 if (!is_string($string)) {
1424 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1427 $constraint = new PHPUnit_Framework_Constraint_Not(
1428 new PHPUnit_Framework_Constraint_StringMatches($format)
1431 self::assertThat($string, $constraint, $message);
1435 * Asserts that a string matches a given format file.
1437 * @param string $formatFile
1438 * @param string $string
1439 * @param string $message
1440 * @since Method available since Release 3.5.0
1442 public static function assertStringMatchesFormatFile($formatFile, $string, $message = '')
1444 self::assertFileExists($formatFile, $message);
1446 if (!is_string($string)) {
1447 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1450 $constraint = new PHPUnit_Framework_Constraint_StringMatches(
1451 file_get_contents($formatFile)
1454 self::assertThat($string, $constraint, $message);
1458 * Asserts that a string does not match a given format string.
1460 * @param string $formatFile
1461 * @param string $string
1462 * @param string $message
1463 * @since Method available since Release 3.5.0
1465 public static function assertStringNotMatchesFormatFile($formatFile, $string, $message = '')
1467 self::assertFileExists($formatFile, $message);
1469 if (!is_string($string)) {
1470 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1473 $constraint = new PHPUnit_Framework_Constraint_Not(
1474 new PHPUnit_Framework_Constraint_StringMatches(
1475 file_get_contents($formatFile)
1479 self::assertThat($string, $constraint, $message);
1483 * Asserts that a string starts with a given prefix.
1485 * @param string $prefix
1486 * @param string $string
1487 * @param string $message
1488 * @since Method available since Release 3.4.0
1490 public static function assertStringStartsWith($prefix, $string, $message = '')
1492 if (!is_string($prefix)) {
1493 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1496 if (!is_string($string)) {
1497 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1500 $constraint = new PHPUnit_Framework_Constraint_StringStartsWith(
1504 self::assertThat($string, $constraint, $message);
1508 * Asserts that a string starts not with a given prefix.
1510 * @param string $prefix
1511 * @param string $string
1512 * @param string $message
1513 * @since Method available since Release 3.4.0
1515 public static function assertStringStartsNotWith($prefix, $string, $message = '')
1517 if (!is_string($prefix)) {
1518 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1521 if (!is_string($string)) {
1522 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1525 $constraint = new PHPUnit_Framework_Constraint_Not(
1526 new PHPUnit_Framework_Constraint_StringStartsWith($prefix)
1529 self::assertThat($string, $constraint, $message);
1533 * Asserts that a string ends with a given prefix.
1535 * @param string $suffix
1536 * @param string $string
1537 * @param string $message
1538 * @since Method available since Release 3.4.0
1540 public static function assertStringEndsWith($suffix, $string, $message = '')
1542 if (!is_string($suffix)) {
1543 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1546 if (!is_string($string)) {
1547 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1550 $constraint = new PHPUnit_Framework_Constraint_StringEndsWith($suffix);
1552 self::assertThat($string, $constraint, $message);
1556 * Asserts that a string ends not with a given prefix.
1558 * @param string $suffix
1559 * @param string $string
1560 * @param string $message
1561 * @since Method available since Release 3.4.0
1563 public static function assertStringEndsNotWith($suffix, $string, $message = '')
1565 if (!is_string($suffix)) {
1566 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
1569 if (!is_string($string)) {
1570 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
1573 $constraint = new PHPUnit_Framework_Constraint_Not(
1574 new PHPUnit_Framework_Constraint_StringEndsWith($suffix)
1577 self::assertThat($string, $constraint, $message);
1581 * Asserts that two XML files are equal.
1583 * @param string $expectedFile
1584 * @param string $actualFile
1585 * @param string $message
1586 * @since Method available since Release 3.1.0
1588 public static function assertXmlFileEqualsXmlFile($expectedFile, $actualFile, $message = '')
1590 self::assertFileExists($expectedFile);
1591 self::assertFileExists($actualFile);
1593 $expected = new DOMDocument;
1594 $expected->preserveWhiteSpace = FALSE;
1595 $expected->load($expectedFile);
1597 $actual = new DOMDocument;
1598 $actual->preserveWhiteSpace = FALSE;
1599 $actual->load($actualFile);
1601 self::assertEquals($expected, $actual, $message);
1605 * Asserts that two XML files are not equal.
1607 * @param string $expectedFile
1608 * @param string $actualFile
1609 * @param string $message
1610 * @since Method available since Release 3.1.0
1612 public static function assertXmlFileNotEqualsXmlFile($expectedFile, $actualFile, $message = '')
1614 self::assertFileExists($expectedFile);
1615 self::assertFileExists($actualFile);
1617 $expected = new DOMDocument;
1618 $expected->preserveWhiteSpace = FALSE;
1619 $expected->load($expectedFile);
1621 $actual = new DOMDocument;
1622 $actual->preserveWhiteSpace = FALSE;
1623 $actual->load($actualFile);
1625 self::assertNotEquals($expected, $actual, $message);
1629 * Asserts that two XML documents are equal.
1631 * @param string $expectedFile
1632 * @param string $actualXml
1633 * @param string $message
1634 * @since Method available since Release 3.3.0
1636 public static function assertXmlStringEqualsXmlFile($expectedFile, $actualXml, $message = '')
1638 self::assertFileExists($expectedFile);
1640 $expected = new DOMDocument;
1641 $expected->preserveWhiteSpace = FALSE;
1642 $expected->load($expectedFile);
1644 $actual = new DOMDocument;
1645 $actual->preserveWhiteSpace = FALSE;
1646 $actual->loadXML($actualXml);
1648 self::assertEquals($expected, $actual, $message);
1652 * Asserts that two XML documents are not equal.
1654 * @param string $expectedFile
1655 * @param string $actualXml
1656 * @param string $message
1657 * @since Method available since Release 3.3.0
1659 public static function assertXmlStringNotEqualsXmlFile($expectedFile, $actualXml, $message = '')
1661 self::assertFileExists($expectedFile);
1663 $expected = new DOMDocument;
1664 $expected->preserveWhiteSpace = FALSE;
1665 $expected->load($expectedFile);
1667 $actual = new DOMDocument;
1668 $actual->preserveWhiteSpace = FALSE;
1669 $actual->loadXML($actualXml);
1671 self::assertNotEquals($expected, $actual, $message);
1675 * Asserts that two XML documents are equal.
1677 * @param string $expectedXml
1678 * @param string $actualXml
1679 * @param string $message
1680 * @since Method available since Release 3.1.0
1682 public static function assertXmlStringEqualsXmlString($expectedXml, $actualXml, $message = '')
1684 $expected = new DOMDocument;
1685 $expected->preserveWhiteSpace = FALSE;
1686 $expected->loadXML($expectedXml);
1688 $actual = new DOMDocument;
1689 $actual->preserveWhiteSpace = FALSE;
1690 $actual->loadXML($actualXml);
1692 self::assertEquals($expected, $actual, $message);
1696 * Asserts that two XML documents are not equal.
1698 * @param string $expectedXml
1699 * @param string $actualXml
1700 * @param string $message
1701 * @since Method available since Release 3.1.0
1703 public static function assertXmlStringNotEqualsXmlString($expectedXml, $actualXml, $message = '')
1705 $expected = new DOMDocument;
1706 $expected->preserveWhiteSpace = FALSE;
1707 $expected->loadXML($expectedXml);
1709 $actual = new DOMDocument;
1710 $actual->preserveWhiteSpace = FALSE;
1711 $actual->loadXML($actualXml);
1713 self::assertNotEquals($expected, $actual, $message);
1717 * Asserts that a hierarchy of DOMNodes matches.
1719 * @param DOMNode $expectedNode
1720 * @param DOMNode $actualNode
1721 * @param boolean $checkAttributes
1722 * @param string $message
1723 * @author Mattis Stordalen Flister <mattis@xait.no>
1724 * @since Method available since Release 3.3.0
1726 public static function assertEqualXMLStructure(DOMNode $expectedNode, DOMNode $actualNode, $checkAttributes = FALSE, $message = '')
1729 $expectedNode->tagName,
1730 $actualNode->tagName,
1734 if ($checkAttributes) {
1736 $expectedNode->attributes->length,
1737 $actualNode->attributes->length,
1739 '%s%sNumber of attributes on node "%s" does not match',
1741 !empty($message) ? "\n" : '',
1742 $expectedNode->tagName
1746 for ($i = 0 ; $i < $expectedNode->attributes->length; $i++) {
1747 $expectedAttribute = $expectedNode->attributes->item($i);
1748 $actualAttribute = $actualNode->attributes->getNamedItem(
1749 $expectedAttribute->name
1752 if (!$actualAttribute) {
1755 '%s%sCould not find attribute "%s" on node "%s"',
1757 !empty($message) ? "\n" : '',
1758 $expectedAttribute->name,
1759 $expectedNode->tagName
1766 PHPUnit_Util_XML::removeCharacterDataNodes($expectedNode);
1767 PHPUnit_Util_XML::removeCharacterDataNodes($actualNode);
1770 $expectedNode->childNodes->length,
1771 $actualNode->childNodes->length,
1773 '%s%sNumber of child nodes of "%s" differs',
1775 !empty($message) ? "\n" : '',
1776 $expectedNode->tagName
1780 for ($i = 0; $i < $expectedNode->childNodes->length; $i++) {
1781 self::assertEqualXMLStructure(
1782 $expectedNode->childNodes->item($i),
1783 $actualNode->childNodes->item($i),
1791 * Assert the presence, absence, or count of elements in a document matching
1792 * the CSS $selector, regardless of the contents of those elements.
1794 * The first argument, $selector, is the CSS selector used to match
1795 * the elements in the $actual document.
1797 * The second argument, $count, can be either boolean or numeric.
1798 * When boolean, it asserts for presence of elements matching the selector
1799 * (TRUE) or absence of elements (FALSE).
1800 * When numeric, it asserts the count of elements.
1802 * assertSelectCount("#binder", true, $xml); // any?
1803 * assertSelectCount(".binder", 3, $xml); // exactly 3?
1805 * @param array $selector
1806 * @param integer $count
1807 * @param mixed $actual
1808 * @param string $message
1809 * @param boolean $isHtml
1810 * @since Method available since Release 3.3.0
1811 * @author Mike Naberezny <mike@maintainable.com>
1812 * @author Derek DeVries <derek@maintainable.com>
1814 public static function assertSelectCount($selector, $count, $actual, $message = '', $isHtml = TRUE)
1816 self::assertSelectEquals(
1817 $selector, TRUE, $count, $actual, $message, $isHtml
1822 * assertSelectRegExp("#binder .name", "/Mike|Derek/", true, $xml); // any?
1823 * assertSelectRegExp("#binder .name", "/Mike|Derek/", 3, $xml); // 3?
1825 * @param array $selector
1826 * @param string $pattern
1827 * @param integer $count
1828 * @param mixed $actual
1829 * @param string $message
1830 * @param boolean $isHtml
1831 * @since Method available since Release 3.3.0
1832 * @author Mike Naberezny <mike@maintainable.com>
1833 * @author Derek DeVries <derek@maintainable.com>
1835 public static function assertSelectRegExp($selector, $pattern, $count, $actual, $message = '', $isHtml = TRUE)
1837 self::assertSelectEquals(
1838 $selector, "regexp:$pattern", $count, $actual, $message, $isHtml
1843 * assertSelectEquals("#binder .name", "Chuck", true, $xml); // any?
1844 * assertSelectEquals("#binder .name", "Chuck", false, $xml); // none?
1846 * @param array $selector
1847 * @param string $content
1848 * @param integer $count
1849 * @param mixed $actual
1850 * @param string $message
1851 * @param boolean $isHtml
1852 * @since Method available since Release 3.3.0
1853 * @author Mike Naberezny <mike@maintainable.com>
1854 * @author Derek DeVries <derek@maintainable.com>
1856 public static function assertSelectEquals($selector, $content, $count, $actual, $message = '', $isHtml = TRUE)
1858 $tags = PHPUnit_Util_XML::cssSelect(
1859 $selector, $content, $actual, $isHtml
1862 // assert specific number of elements
1863 if (is_numeric($count)) {
1864 $counted = $tags ? count($tags) : 0;
1865 self::assertEquals($count, $counted, $message);
1868 // assert any elements exist if true, assert no elements exist if false
1869 else if (is_bool($count)) {
1870 $any = count($tags) > 0 && $tags[0] instanceof DOMNode;
1873 self::assertTrue($any, $message);
1875 self::assertFalse($any, $message);
1879 // check for range number of elements
1880 else if (is_array($count) &&
1881 (isset($count['>']) || isset($count['<']) ||
1882 isset($count['>=']) || isset($count['<=']))) {
1883 $counted = $tags ? count($tags) : 0;
1885 if (isset($count['>'])) {
1886 self::assertTrue($counted > $count['>'], $message);
1889 if (isset($count['>='])) {
1890 self::assertTrue($counted >= $count['>='], $message);
1893 if (isset($count['<'])) {
1894 self::assertTrue($counted < $count['<'], $message);
1897 if (isset($count['<='])) {
1898 self::assertTrue($counted <= $count['<='], $message);
1901 throw new InvalidArgumentException();
1906 * Evaluate an HTML or XML string and assert its structure and/or contents.
1908 * The first argument ($matcher) is an associative array that specifies the
1909 * match criteria for the assertion:
1911 * - `id` : the node with the given id attribute must match the
1912 * corresponsing value.
1913 * - `tag` : the node type must match the corresponding value.
1914 * - `attributes` : a hash. The node's attributres must match the
1915 * corresponsing values in the hash.
1916 * - `content` : The text content must match the given value.
1917 * - `parent` : a hash. The node's parent must match the
1918 * corresponsing hash.
1919 * - `child` : a hash. At least one of the node's immediate children
1920 * must meet the criteria described by the hash.
1921 * - `ancestor` : a hash. At least one of the node's ancestors must
1922 * meet the criteria described by the hash.
1923 * - `descendant` : a hash. At least one of the node's descendants must
1924 * meet the criteria described by the hash.
1925 * - `children` : a hash, for counting children of a node.
1927 * - `count` : a number which must equal the number of children
1929 * - `less_than` : the number of matching children must be greater
1931 * - `greater_than` : the number of matching children must be less than
1933 * - `only` : another hash consisting of the keys to use to match
1934 * on the children, and only matching children will be
1938 * // Matcher that asserts that there is an element with an id="my_id".
1939 * $matcher = array('id' => 'my_id');
1941 * // Matcher that asserts that there is a "span" tag.
1942 * $matcher = array('tag' => 'span');
1944 * // Matcher that asserts that there is a "span" tag with the content
1946 * $matcher = array('tag' => 'span', 'content' => 'Hello World');
1948 * // Matcher that asserts that there is a "span" tag with content matching
1949 * // the regular expression pattern.
1950 * $matcher = array('tag' => 'span', 'content' => '/Try P(HP|ython)/');
1952 * // Matcher that asserts that there is a "span" with an "list" class
1956 * 'attributes' => array('class' => 'list')
1959 * // Matcher that asserts that there is a "span" inside of a "div".
1962 * 'parent' => array('tag' => 'div')
1965 * // Matcher that asserts that there is a "span" somewhere inside a
1969 * 'ancestor' => array('tag' => 'table')
1972 * // Matcher that asserts that there is a "span" with at least one "em"
1976 * 'child' => array('tag' => 'em')
1979 * // Matcher that asserts that there is a "span" containing a (possibly
1980 * // nested) "strong" tag.
1983 * 'descendant' => array('tag' => 'strong')
1986 * // Matcher that asserts that there is a "span" containing 5-10 "em" tags
1987 * // as immediate children.
1990 * 'children' => array(
1991 * 'less_than' => 11,
1992 * 'greater_than' => 4,
1993 * 'only' => array('tag' => 'em')
1997 * // Matcher that asserts that there is a "div", with an "ul" ancestor and
1998 * // a "li" parent (with class="enum"), and containing a "span" descendant
1999 * // that contains an element with id="my_test" and the text "Hello World".
2002 * 'ancestor' => array('tag' => 'ul'),
2003 * 'parent' => array(
2005 * 'attributes' => array('class' => 'enum')
2007 * 'descendant' => array(
2010 * 'id' => 'my_test',
2011 * 'content' => 'Hello World'
2016 * // Use assertTag() to apply a $matcher to a piece of $html.
2017 * $this->assertTag($matcher, $html);
2019 * // Use assertTag() to apply a $matcher to a piece of $xml.
2020 * $this->assertTag($matcher, $xml, '', FALSE);
2023 * The second argument ($actual) is a string containing either HTML or
2024 * XML text to be tested.
2026 * The third argument ($message) is an optional message that will be
2027 * used if the assertion fails.
2029 * The fourth argument ($html) is an optional flag specifying whether
2030 * to load the $actual string into a DOMDocument using the HTML or
2031 * XML load strategy. It is TRUE by default, which assumes the HTML
2032 * load strategy. In many cases, this will be acceptable for XML as well.
2034 * @param array $matcher
2035 * @param string $actual
2036 * @param string $message
2037 * @param boolean $isHtml
2038 * @since Method available since Release 3.3.0
2039 * @author Mike Naberezny <mike@maintainable.com>
2040 * @author Derek DeVries <derek@maintainable.com>
2042 public static function assertTag($matcher, $actual, $message = '', $isHtml = TRUE)
2044 $dom = PHPUnit_Util_XML::load($actual, $isHtml);
2045 $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
2046 $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
2048 self::assertTrue($matched, $message);
2052 * This assertion is the exact opposite of assertTag().
2054 * Rather than asserting that $matcher results in a match, it asserts that
2055 * $matcher does not match.
2057 * @param array $matcher
2058 * @param string $actual
2059 * @param string $message
2060 * @param boolean $isHtml
2061 * @since Method available since Release 3.3.0
2062 * @author Mike Naberezny <mike@maintainable.com>
2063 * @author Derek DeVries <derek@maintainable.com>
2065 public static function assertNotTag($matcher, $actual, $message = '', $isHtml = TRUE)
2067 $dom = PHPUnit_Util_XML::load($actual, $isHtml);
2068 $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
2069 $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
2071 self::assertFalse($matched, $message);
2075 * Evaluates a PHPUnit_Framework_Constraint matcher object.
2077 * @param mixed $value
2078 * @param PHPUnit_Framework_Constraint $constraint
2079 * @param string $message
2080 * @since Method available since Release 3.0.0
2082 public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '')
2084 self::$count += count($constraint);
2086 if (!$constraint->evaluate($value)) {
2087 $constraint->fail($value, $message);
2092 * Returns a PHPUnit_Framework_Constraint_And matcher object.
2094 * @return PHPUnit_Framework_Constraint_And
2095 * @since Method available since Release 3.0.0
2097 public static function logicalAnd()
2099 $constraints = func_get_args();
2101 $constraint = new PHPUnit_Framework_Constraint_And;
2102 $constraint->setConstraints($constraints);
2108 * Returns a PHPUnit_Framework_Constraint_Or matcher object.
2110 * @return PHPUnit_Framework_Constraint_Or
2111 * @since Method available since Release 3.0.0
2113 public static function logicalOr()
2115 $constraints = func_get_args();
2117 $constraint = new PHPUnit_Framework_Constraint_Or;
2118 $constraint->setConstraints($constraints);
2124 * Returns a PHPUnit_Framework_Constraint_Not matcher object.
2126 * @param PHPUnit_Framework_Constraint $constraint
2127 * @return PHPUnit_Framework_Constraint_Not
2128 * @since Method available since Release 3.0.0
2130 public static function logicalNot(PHPUnit_Framework_Constraint $constraint)
2132 return new PHPUnit_Framework_Constraint_Not($constraint);
2136 * Returns a PHPUnit_Framework_Constraint_Xor matcher object.
2138 * @return PHPUnit_Framework_Constraint_Xor
2139 * @since Method available since Release 3.0.0
2141 public static function logicalXor()
2143 $constraints = func_get_args();
2145 $constraint = new PHPUnit_Framework_Constraint_Xor;
2146 $constraint->setConstraints($constraints);
2152 * Returns a PHPUnit_Framework_Constraint_IsAnything matcher object.
2154 * @return PHPUnit_Framework_Constraint_IsAnything
2155 * @since Method available since Release 3.0.0
2157 public static function anything()
2159 return new PHPUnit_Framework_Constraint_IsAnything;
2163 * Returns a PHPUnit_Framework_Constraint_IsTrue matcher object.
2165 * @return PHPUnit_Framework_Constraint_IsTrue
2166 * @since Method available since Release 3.3.0
2168 public static function isTrue()
2170 return new PHPUnit_Framework_Constraint_IsTrue;
2174 * Returns a PHPUnit_Framework_Constraint_IsFalse matcher object.
2176 * @return PHPUnit_Framework_Constraint_IsFalse
2177 * @since Method available since Release 3.3.0
2179 public static function isFalse()
2181 return new PHPUnit_Framework_Constraint_IsFalse;
2185 * Returns a PHPUnit_Framework_Constraint_IsNull matcher object.
2187 * @return PHPUnit_Framework_Constraint_IsNull
2188 * @since Method available since Release 3.3.0
2190 public static function isNull()
2192 return new PHPUnit_Framework_Constraint_IsNull;
2196 * Returns a PHPUnit_Framework_Constraint_Attribute matcher object.
2198 * @param PHPUnit_Framework_Constraint $constraint
2199 * @param string $attributeName
2200 * @return PHPUnit_Framework_Constraint_Attribute
2201 * @since Method available since Release 3.1.0
2203 public static function attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)
2205 return new PHPUnit_Framework_Constraint_Attribute(
2206 $constraint, $attributeName
2211 * Returns a PHPUnit_Framework_Constraint_TraversableContains matcher
2214 * @param mixed $value
2215 * @return PHPUnit_Framework_Constraint_TraversableContains
2216 * @since Method available since Release 3.0.0
2218 public static function contains($value)
2220 return new PHPUnit_Framework_Constraint_TraversableContains($value);
2224 * Returns a PHPUnit_Framework_Constraint_TraversableContainsOnly matcher
2227 * @param string $type
2228 * @return PHPUnit_Framework_Constraint_TraversableContainsOnly
2229 * @since Method available since Release 3.1.4
2231 public static function containsOnly($type)
2233 return new PHPUnit_Framework_Constraint_TraversableContainsOnly($type);
2237 * Returns a PHPUnit_Framework_Constraint_ArrayHasKey matcher object.
2240 * @return PHPUnit_Framework_Constraint_ArrayHasKey
2241 * @since Method available since Release 3.0.0
2243 public static function arrayHasKey($key)
2245 return new PHPUnit_Framework_Constraint_ArrayHasKey($key);
2249 * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object.
2251 * @param mixed $value
2252 * @param float $delta
2253 * @param integer $maxDepth
2254 * @param boolean $canonicalize
2255 * @param boolean $ignoreCase
2256 * @return PHPUnit_Framework_Constraint_IsEqual
2257 * @since Method available since Release 3.0.0
2259 public static function equalTo($value, $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
2261 return new PHPUnit_Framework_Constraint_IsEqual(
2262 $value, $delta, $maxDepth, $canonicalize, $ignoreCase
2267 * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object
2268 * that is wrapped in a PHPUnit_Framework_Constraint_Attribute matcher
2271 * @param string $attributeName
2272 * @param mixed $value
2273 * @param float $delta
2274 * @param integer $maxDepth
2275 * @param boolean $canonicalize
2276 * @param boolean $ignoreCase
2277 * @return PHPUnit_Framework_Constraint_Attribute
2278 * @since Method available since Release 3.1.0
2280 public static function attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
2282 return self::attribute(
2284 $value, $delta, $maxDepth, $canonicalize, $ignoreCase
2291 * Returns a PHPUnit_Framework_Constraint_IsEmpty matcher object.
2293 * @return PHPUnit_Framework_Constraint_IsEmpty
2294 * @since Method available since Release 3.5.0
2296 public static function isEmpty()
2298 return new PHPUnit_Framework_Constraint_IsEmpty;
2301 * Returns a PHPUnit_Framework_Constraint_FileExists matcher object.
2303 * @return PHPUnit_Framework_Constraint_FileExists
2304 * @since Method available since Release 3.0.0
2306 public static function fileExists()
2308 return new PHPUnit_Framework_Constraint_FileExists;
2312 * Returns a PHPUnit_Framework_Constraint_GreaterThan matcher object.
2314 * @param mixed $value
2315 * @return PHPUnit_Framework_Constraint_GreaterThan
2316 * @since Method available since Release 3.0.0
2318 public static function greaterThan($value)
2320 return new PHPUnit_Framework_Constraint_GreaterThan($value);
2324 * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
2325 * a PHPUnit_Framework_Constraint_IsEqual and a
2326 * PHPUnit_Framework_Constraint_GreaterThan matcher object.
2328 * @param mixed $value
2329 * @return PHPUnit_Framework_Constraint_Or
2330 * @since Method available since Release 3.1.0
2332 public static function greaterThanOrEqual($value)
2334 return self::logicalOr(
2335 new PHPUnit_Framework_Constraint_IsEqual($value),
2336 new PHPUnit_Framework_Constraint_GreaterThan($value)
2341 * Returns a PHPUnit_Framework_Constraint_ClassHasAttribute matcher object.
2343 * @param string $attributeName
2344 * @return PHPUnit_Framework_Constraint_ClassHasAttribute
2345 * @since Method available since Release 3.1.0
2347 public static function classHasAttribute($attributeName)
2349 return new PHPUnit_Framework_Constraint_ClassHasAttribute(
2355 * Returns a PHPUnit_Framework_Constraint_ClassHasStaticAttribute matcher
2358 * @param string $attributeName
2359 * @return PHPUnit_Framework_Constraint_ClassHasStaticAttribute
2360 * @since Method available since Release 3.1.0
2362 public static function classHasStaticAttribute($attributeName)
2364 return new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
2370 * Returns a PHPUnit_Framework_Constraint_ObjectHasAttribute matcher object.
2372 * @param string $attributeName
2373 * @return PHPUnit_Framework_Constraint_ObjectHasAttribute
2374 * @since Method available since Release 3.0.0
2376 public static function objectHasAttribute($attributeName)
2378 return new PHPUnit_Framework_Constraint_ObjectHasAttribute(
2384 * Returns a PHPUnit_Framework_Constraint_IsIdentical matcher object.
2386 * @param mixed $value
2387 * @return PHPUnit_Framework_Constraint_IsIdentical
2388 * @since Method available since Release 3.0.0
2390 public static function identicalTo($value)
2392 return new PHPUnit_Framework_Constraint_IsIdentical($value);
2396 * Returns a PHPUnit_Framework_Constraint_IsInstanceOf matcher object.
2398 * @param string $className
2399 * @return PHPUnit_Framework_Constraint_IsInstanceOf
2400 * @since Method available since Release 3.0.0
2402 public static function isInstanceOf($className)
2404 return new PHPUnit_Framework_Constraint_IsInstanceOf($className);
2408 * Returns a PHPUnit_Framework_Constraint_IsType matcher object.
2410 * @param string $type
2411 * @return PHPUnit_Framework_Constraint_IsType
2412 * @since Method available since Release 3.0.0
2414 public static function isType($type)
2416 return new PHPUnit_Framework_Constraint_IsType($type);
2420 * Returns a PHPUnit_Framework_Constraint_LessThan matcher object.
2422 * @param mixed $value
2423 * @return PHPUnit_Framework_Constraint_LessThan
2424 * @since Method available since Release 3.0.0
2426 public static function lessThan($value)
2428 return new PHPUnit_Framework_Constraint_LessThan($value);
2432 * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
2433 * a PHPUnit_Framework_Constraint_IsEqual and a
2434 * PHPUnit_Framework_Constraint_LessThan matcher object.
2436 * @param mixed $value
2437 * @return PHPUnit_Framework_Constraint_Or
2438 * @since Method available since Release 3.1.0
2440 public static function lessThanOrEqual($value)
2442 return self::logicalOr(
2443 new PHPUnit_Framework_Constraint_IsEqual($value),
2444 new PHPUnit_Framework_Constraint_LessThan($value)
2449 * Returns a PHPUnit_Framework_Constraint_PCREMatch matcher object.
2451 * @param string $pattern
2452 * @return PHPUnit_Framework_Constraint_PCREMatch
2453 * @since Method available since Release 3.0.0
2455 public static function matchesRegularExpression($pattern)
2457 return new PHPUnit_Framework_Constraint_PCREMatch($pattern);
2461 * Returns a PHPUnit_Framework_Constraint_StringMatches matcher object.
2463 * @param string $string
2464 * @return PHPUnit_Framework_Constraint_StringMatches
2465 * @since Method available since Release 3.5.0
2467 public static function matches($string)
2469 return new PHPUnit_Framework_Constraint_StringMatches($string);
2473 * Returns a PHPUnit_Framework_Constraint_StringStartsWith matcher object.
2475 * @param mixed $prefix
2476 * @return PHPUnit_Framework_Constraint_StringStartsWith
2477 * @since Method available since Release 3.4.0
2479 public static function stringStartsWith($prefix)
2481 return new PHPUnit_Framework_Constraint_StringStartsWith($prefix);
2485 * Returns a PHPUnit_Framework_Constraint_StringContains matcher object.
2487 * @param string $string
2488 * @param boolean $case
2489 * @return PHPUnit_Framework_Constraint_StringContains
2490 * @since Method available since Release 3.0.0
2492 public static function stringContains($string, $case = TRUE)
2494 return new PHPUnit_Framework_Constraint_StringContains($string, $case);
2498 * Returns a PHPUnit_Framework_Constraint_StringEndsWith matcher object.
2500 * @param mixed $suffix
2501 * @return PHPUnit_Framework_Constraint_StringEndsWith
2502 * @since Method available since Release 3.4.0
2504 public static function stringEndsWith($suffix)
2506 return new PHPUnit_Framework_Constraint_StringEndsWith($suffix);
2510 * Fails a test with the given message.
2512 * @param string $message
2513 * @throws PHPUnit_Framework_AssertionFailedError
2515 public static function fail($message = '')
2517 throw new PHPUnit_Framework_AssertionFailedError($message);
2521 * Fails a test with a synthetic error.
2523 * @param string $message
2524 * @param string $file
2525 * @param integer $line
2526 * @param array $trace
2527 * @throws PHPUnit_Framework_SyntheticError
2529 public static function syntheticFail($message = '', $file = '', $line = 0, $trace = array())
2531 throw new PHPUnit_Framework_SyntheticError($message, 0, $file, $line, $trace);
2535 * Returns the value of an attribute of a class or an object.
2536 * This also works for attributes that are declared protected or private.
2538 * @param mixed $classOrObject
2539 * @param string $attributeName
2541 * @throws InvalidArgumentException
2543 public static function readAttribute($classOrObject, $attributeName)
2545 if (!is_string($attributeName)) {
2546 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
2549 if (is_string($classOrObject)) {
2550 if (!class_exists($classOrObject)) {
2551 throw PHPUnit_Util_InvalidArgumentHelper::factory(
2556 return PHPUnit_Util_Class::getStaticAttribute(
2562 else if (is_object($classOrObject)) {
2563 return PHPUnit_Util_Class::getObjectAttribute(
2570 throw PHPUnit_Util_InvalidArgumentHelper::factory(
2571 1, 'class name or object'
2577 * Mark the test as incomplete.
2579 * @param string $message
2580 * @throws PHPUnit_Framework_IncompleteTestError
2581 * @since Method available since Release 3.0.0
2583 public static function markTestIncomplete($message = '')
2585 throw new PHPUnit_Framework_IncompleteTestError($message);
2589 * Mark the test as skipped.
2591 * @param string $message
2592 * @throws PHPUnit_Framework_SkippedTestError
2593 * @since Method available since Release 3.0.0
2595 public static function markTestSkipped($message = '')
2597 throw new PHPUnit_Framework_SkippedTestError($message);
2601 * Return the current assertion count.
2604 * @since Method available since Release 3.3.3
2606 public static function getCount()
2608 return self::$count;
2612 * Reset the assertion counter.
2614 * @since Method available since Release 3.3.3
2616 public static function resetCount()