Most of the time, though, you'll be checking the assertions inside of tests.
There are two variants of each of the assertion methods: one takes a message to be displayed with the error as a parameter, and one does not. Example 20 demonstrates an assertion method with a message. The optional message is typically displayed when a failure is displayed, which can make debugging easier.
Table 6. Assertions
Assertion | Description |
---|
void assertTrue(Boolean $condition)
| Reports an error if $condition is FALSE. |
void assertTrue(Boolean $condition, String $message) | Reports an error identified by $message if $condition is FALSE. |
void assertFalse(Boolean $condition, String $message) | Reports an error identified by $message if $condition is trUE. |
void assertNull(Mixed $variable) | Reports an error if $variable is not NULL. |
void assertNull(Mixed $variable, String $message) | Reports an error identified by $message if $variable is not NULL. |
void assertNotNull(Mixed $variable) | Reports an error if $variable is NULL. |
void assertNotNull(Mixed $variable) | Reports an error if $variable is NULL. |
void assertNotNull(Mixed $variable, String $message) | Reports an error identified by $message if $variable is NULL. |
void assertSame(Object $expected, Object $actual) | Reports an error if the two variables $expected and $actual do not reference the same object. |
void assertSame(Object $expected, Object $actual, String $message) | Reports an error identified by $message if the two variables $expected and $actual do not reference the same object. |
void assertSame(Mixed $expected, Mixed $actual) | Reports an error if the two variables $expected and $actual do not have the same type and value. |
void assertSame(Mixed $expected, Mixed $actual, String $message) | Reports an error identified by $message if the two variables $expected and $actual do not have the same type and value. |
void assertNotSame(Object $expected, Object $actual) | Reports an error if the two variables $expected and $actual reference the same object. |
void assertNotSame(Object $expected, Object $actual, String $message) | Reports an error identified by $message if the two variables $expected and $actual reference the same object. |
void assertNotSame(Mixed $expected, Mixed $actual) | Reports an error if the two variables $expected and $actual have the same type and value. |
void assertNotSame(Mixed $expected, Mixed $actual, String $message) | Reports an error identified by $message if the two variables $expected and $actual have the same type and value. |
void assertEquals(Array $expected, Array $actual) | Reports an error if the two arrays $expected and $actual are not equal. |
void assertEquals(Array $expected, Array $actual, String $message) | Reports an error identified by $message if the two arrays $expected and $actual are not equal. |
void assertNotEquals(Array $expected, Array $actual) | Reports an error if the two arrays $expected and $actual are equal. |
void assertNotEquals(Array $expected, Array $actual, String $message) | Reports an error identified by $message if the two arrays $expected and $actual are equal. |
void assertEquals(Float $expected, Float $actual, Float $delta = 0) | Reports an error if the two floats $expected and $actual are not within $delta of each other. |
void assertEquals(Float $expected, Float $actual, String $message, Float $delta = 0) | Reports an error identified by $message if the two floats $expected and $actual are not within $delta of each other. |
void assertNotEquals(Float $expected, Float $actual, Float $delta = 0) | Reports an error if the two floats $expected and $actual are within $delta of each other. |
void assertNotEquals(Float $expected, Float $actual, String $message, Float $delta = 0) | Reports an error identified by $message if the two floats $expected and $actual are within $delta of each other. |
void assertEquals(String $expected, String $actual) | Reports an error if the two strings $expected and $actual are not equal. The error is reported as the delta between the two strings. |
void assertEquals(String $expected, String $actual, String $message) | Reports an error identified by $message if the two strings $expected and $actual are not equal. The error is reported as the delta between the two strings. |
void assertNotEquals(String $expected, String $actual) | Reports an error if the two strings $expected and $actual are equal. |
void assertNotEquals(String $expected, String $actual, String $message) | Reports an error identified by $message if the two strings $expected and $actual are equal. |
void assertEquals(Mixed $expected, Mixed $actual) | Reports an error if the two variables $expected and $actual are not equal. |
void assertEquals(Mixed $expected, Mixed $actual, String $message) | Reports an error identified by $message if the two variables $expected and $actual are not equal. |
void assertNotEquals(Mixed $expected, Mixed $actual) | Reports an error if the two variables $expected and $actual are equal. |
void assertNotEquals(Mixed $expected, Mixed $actual, String $message) | Reports an error identified by $message if the two variables $expected and $actual are equal. |
void assertContains(Mixed $needle, Array $haystack) | Reports an error if $needle is not an element of $haystack. |
void assertContains(Mixed $needle, Array $haystack, String $message) | Reports an error identified by $message if $needle is not an element of $haystack. |
void assertNotContains(Mixed $needle, Array $haystack) | Reports an error if $needle is an element of $haystack. |
void assertNotContains(Mixed $needle, Array $haystack, String $message) | Reports an error identified by $message if $needle is an element of $haystack. |
void assertContains(Mixed $needle, Iterator $haystack) | Reports an error if $needle is not an element of $haystack. |
void assertContains(Mixed $needle, Iterator $haystack, String $message) | Reports an error identified by $message if $needle is not an element of $haystack. |
void assertNotContains(Mixed $needle, Iterator $haystack) | Reports an error if $needle is an element of $haystack. |
void assertNotContains(Mixed $needle, Iterator $haystack, String $message) | Reports an error identified by $message if $needle is an element of $haystack. |
void assertRegExp(String $pattern, String $string) | Reports an error if $string does not match the regular expression $pattern. |
void assertRegExp(String $pattern, String $string, String $message) | Reports an error identified by $message if $string does not match the regular expression $pattern. |
void assertNotRegExp(String $pattern, String $string) | Reports an error if $string matches the regular expression $pattern. |
void assertNotRegExp(String $pattern, String $string, String $message) | Reports an error identified by $message if $string matches the regular expression $pattern. |
void assertType(String $expected, Mixed $actual) | Reports an error if the variable $actual is not of type $expected. |
void assertType(String $expected, Mixed $actual, String $message) | Reports an error identified by $message if the variable $actual is not of type $expected. |
void assertNotType(String $expected, Mixed $actual) | Reports an error if the variable $actual is of type $expected. |
void assertNotType(String $expected, Mixed $actual, String $message) | Reports an error identified by $message if the variable $actual is of type $expected. |
You may find that you need other assertions than these to compare objects specific to your project. Create your own Assert class to contain these assertions to simplify your tests.