3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: unit_tester.php,v 1.21 2004/08/25 21:00:02 lastcraft Exp $
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__
) . '/simple_test.php');
13 require_once(dirname(__FILE__
) . '/errors.php');
14 require_once(dirname(__FILE__
) . '/dumper.php');
18 * Standard unit test class for day to day testing
19 * of PHP code XP style. Adds some useful standard
22 * @subpackage UnitTester
24 class UnitTestCase
extends SimpleTestCase
{
27 * Creates an empty test case. Should be subclassed
28 * with test methods for a functional test case.
29 * @param string $label Name of test case. Will use
30 * the class name if none specified.
33 function UnitTestCase($label = false
) {
35 $label = get_class($this);
37 $this->SimpleTestCase($label);
41 * Will be true if the value is null.
42 * @param null $value Supposedly null value.
43 * @param string $message Message to display.
44 * @return boolean True on pass
47 function assertNull($value, $message = "%s") {
48 $dumper = &new SimpleDumper();
51 "[" . $dumper->describeValue($value) . "] should be null");
52 return $this->assertTrue(! isset($value), $message);
56 * Will be true if the value is set.
57 * @param mixed $value Supposedly set value.
58 * @param string $message Message to display.
59 * @return boolean True on pass.
62 function assertNotNull($value, $message = "%s") {
63 $dumper = &new SimpleDumper();
66 "[" . $dumper->describeValue($value) . "] should not be null");
67 return $this->assertTrue(isset($value), $message);
71 * Type and class test. Will pass if class
72 * matches the type name or is a subclass or
73 * if not an object, but the type is correct.
74 * @param mixed $object Object to test.
75 * @param string $type Type name as string.
76 * @param string $message Message to display.
77 * @return boolean True on pass.
80 function assertIsA($object, $type, $message = "%s") {
81 return $this->assertExpectation(
82 new IsAExpectation($type),
88 * Type and class mismatch test. Will pass if class
89 * name or underling type does not match the one
91 * @param mixed $object Object to test.
92 * @param string $type Type name as string.
93 * @param string $message Message to display.
94 * @return boolean True on pass.
97 function assertNotA($object, $type, $message = "%s") {
98 return $this->assertExpectation(
99 new NotAExpectation($type),
105 * Will trigger a pass if the two parameters have
106 * the same value only. Otherwise a fail.
107 * @param mixed $first Value to compare.
108 * @param mixed $second Value to compare.
109 * @param string $message Message to display.
110 * @return boolean True on pass
113 function assertEqual($first, $second, $message = "%s") {
114 return $this->assertExpectation(
115 new EqualExpectation($first),
121 * Will trigger a pass if the two parameters have
122 * a different value. Otherwise a fail.
123 * @param mixed $first Value to compare.
124 * @param mixed $second Value to compare.
125 * @param string $message Message to display.
126 * @return boolean True on pass
129 function assertNotEqual($first, $second, $message = "%s") {
130 return $this->assertExpectation(
131 new NotEqualExpectation($first),
137 * Will trigger a pass if the two parameters have
138 * the same value and same type. Otherwise a fail.
139 * @param mixed $first Value to compare.
140 * @param mixed $second Value to compare.
141 * @param string $message Message to display.
142 * @return boolean True on pass
145 function assertIdentical($first, $second, $message = "%s") {
146 return $this->assertExpectation(
147 new IdenticalExpectation($first),
153 * Will trigger a pass if the two parameters have
154 * the different value or different type.
155 * @param mixed $first Value to compare.
156 * @param mixed $second Value to compare.
157 * @param string $message Message to display.
158 * @return boolean True on pass
161 function assertNotIdentical($first, $second, $message = "%s") {
162 return $this->assertExpectation(
163 new NotIdenticalExpectation($first),
169 * Will trigger a pass if both parameters refer
170 * to the same object. Fail otherwise.
171 * @param mixed $first Object reference to check.
172 * @param mixed $second Hopefully the same object.
173 * @param string $message Message to display.
174 * @return boolean True on pass
177 function assertReference(&$first, &$second, $message = "%s") {
178 $dumper = &new SimpleDumper();
181 "[" . $dumper->describeValue($first) .
182 "] and [" . $dumper->describeValue($second) .
183 "] should reference the same object");
185 $first = uniqid("test");
186 $is_ref = ($first === $second);
188 return $this->assertTrue($is_ref, $message);
192 * Will trigger a pass if both parameters refer
193 * to different objects. Fail otherwise.
194 * @param mixed $first Object reference to check.
195 * @param mixed $second Hopefully not the same object.
196 * @param string $message Message to display.
197 * @return boolean True on pass
200 function assertCopy(&$first, &$second, $message = "%s") {
201 $dumper = &new SimpleDumper();
204 "[" . $dumper->describeValue($first) .
205 "] and [" . $dumper->describeValue($second) .
206 "] should not be the same object");
208 $first = uniqid("test");
209 $is_ref = ($first === $second);
211 return $this->assertFalse($is_ref, $message);
215 * Will trigger a pass if the Perl regex pattern
216 * is found in the subject. Fail otherwise.
217 * @param string $pattern Perl regex to look for including
218 * the regex delimiters.
219 * @param string $subject String to search in.
220 * @param string $message Message to display.
221 * @return boolean True on pass
224 function assertWantedPattern($pattern, $subject, $message = "%s") {
225 return $this->assertExpectation(
226 new WantedPatternExpectation($pattern),
232 * Will trigger a pass if the perl regex pattern
233 * is not present in subject. Fail if found.
234 * @param string $pattern Perl regex to look for including
235 * the regex delimiters.
236 * @param string $subject String to search in.
237 * @param string $message Message to display.
238 * @return boolean True on pass
241 function assertNoUnwantedPattern($pattern, $subject, $message = "%s") {
242 return $this->assertExpectation(
243 new UnwantedPatternExpectation($pattern),
249 * Confirms that no errors have occoured so
250 * far in the test method.
251 * @param string $message Message to display.
252 * @return boolean True on pass
255 function assertNoErrors($message = "%s") {
256 $queue = &SimpleErrorQueue
::instance();
257 return $this->assertTrue(
259 sprintf($message, "Should be no errors"));
263 * Confirms that an error has occoured and
264 * optionally that the error text matches exactly.
265 * @param string $expected Expected error text or
266 * false for no check.
267 * @param string $message Message to display.
268 * @return boolean True on pass
271 function assertError($expected = false
, $message = "%s") {
272 $queue = &SimpleErrorQueue
::instance();
273 if ($queue->isEmpty()) {
274 $this->fail(sprintf($message, "Expected error not found"));
277 list($severity, $content, $file, $line, $globals) = $queue->extract();
278 $severity = SimpleErrorQueue
::getSeverityAsString($severity);
279 return $this->assertTrue(
280 ! $expected ||
($expected == $content),
281 "Expected [$expected] in PHP error [$content] severity [$severity] in [$file] line [$line]");
285 * Confirms that an error has occoured and
286 * that the error text matches a Perl regular
288 * @param string $expected Perl regular expresion to
290 * @param string $message Message to display.
291 * @return boolean True on pass
294 function assertErrorPattern($pattern, $message = "%s") {
295 $queue = &SimpleErrorQueue
::instance();
296 if ($queue->isEmpty()) {
297 $this->fail(sprintf($message, "Expected error not found"));
300 list($severity, $content, $file, $line, $globals) = $queue->extract();
301 $severity = SimpleErrorQueue
::getSeverityAsString($severity);
302 return $this->assertTrue(
303 (boolean
)preg_match($pattern, $content),
304 "Expected pattern match [$pattern] in PHP error [$content] severity [$severity] in [$file] line [$line]");