3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: unit_tester.php,v 1.31 2006/02/06 06:05:18 lastcraft Exp $
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__
) . '/test_case.php');
13 require_once(dirname(__FILE__
) . '/dumper.php');
17 * Standard unit test class for day to day testing
18 * of PHP code XP style. Adds some useful standard
21 * @subpackage UnitTester
23 class UnitTestCase
extends SimpleTestCase
{
26 * Creates an empty test case. Should be subclassed
27 * with test methods for a functional test case.
28 * @param string $label Name of test case. Will use
29 * the class name if none specified.
32 function UnitTestCase($label = false) {
34 $label = get_class($this);
36 $this->SimpleTestCase($label);
40 * Will be true if the value is null.
41 * @param null $value Supposedly null value.
42 * @param string $message Message to display.
43 * @return boolean True on pass
46 function assertNull($value, $message = "%s") {
47 $dumper = &new SimpleDumper();
50 "[" . $dumper->describeValue($value) . "] should be null");
51 return $this->assertTrue(! isset($value), $message);
55 * Will be true if the value is set.
56 * @param mixed $value Supposedly set value.
57 * @param string $message Message to display.
58 * @return boolean True on pass.
61 function assertNotNull($value, $message = "%s") {
62 $dumper = &new SimpleDumper();
65 "[" . $dumper->describeValue($value) . "] should not be null");
66 return $this->assertTrue(isset($value), $message);
70 * Type and class test. Will pass if class
71 * matches the type name or is a subclass or
72 * if not an object, but the type is correct.
73 * @param mixed $object Object to test.
74 * @param string $type Type name as string.
75 * @param string $message Message to display.
76 * @return boolean True on pass.
79 function assertIsA($object, $type, $message = "%s") {
81 new IsAExpectation($type),
87 * Type and class mismatch test. Will pass if class
88 * name or underling type does not match the one
90 * @param mixed $object Object to test.
91 * @param string $type Type name as string.
92 * @param string $message Message to display.
93 * @return boolean True on pass.
96 function assertNotA($object, $type, $message = "%s") {
98 new NotAExpectation($type),
104 * Will trigger a pass if the two parameters have
105 * the same value only. Otherwise a fail.
106 * @param mixed $first Value to compare.
107 * @param mixed $second Value to compare.
108 * @param string $message Message to display.
109 * @return boolean True on pass
112 function assertEqual($first, $second, $message = "%s") {
113 return $this->assert(
114 new EqualExpectation($first),
120 * Will trigger a pass if the two parameters have
121 * a different value. Otherwise a fail.
122 * @param mixed $first Value to compare.
123 * @param mixed $second Value to compare.
124 * @param string $message Message to display.
125 * @return boolean True on pass
128 function assertNotEqual($first, $second, $message = "%s") {
129 return $this->assert(
130 new NotEqualExpectation($first),
136 * Will trigger a pass if the if the first parameter
137 * is near enough to the second by the margin.
138 * @param mixed $first Value to compare.
139 * @param mixed $second Value to compare.
140 * @param mixed $margin Fuzziness of match.
141 * @param string $message Message to display.
142 * @return boolean True on pass
145 function assertWithinMargin($first, $second, $margin, $message = "%s") {
146 return $this->assert(
147 new WithinMarginExpectation($first, $margin),
153 * Will trigger a pass if the two parameters differ
154 * by more than the margin.
155 * @param mixed $first Value to compare.
156 * @param mixed $second Value to compare.
157 * @param mixed $margin Fuzziness of match.
158 * @param string $message Message to display.
159 * @return boolean True on pass
162 function assertOutsideMargin($first, $second, $margin, $message = "%s") {
163 return $this->assert(
164 new OutsideMarginExpectation($first, $margin),
170 * Will trigger a pass if the two parameters have
171 * the same value and same type. Otherwise a fail.
172 * @param mixed $first Value to compare.
173 * @param mixed $second Value to compare.
174 * @param string $message Message to display.
175 * @return boolean True on pass
178 function assertIdentical($first, $second, $message = "%s") {
179 return $this->assert(
180 new IdenticalExpectation($first),
186 * Will trigger a pass if the two parameters have
187 * the different value or different type.
188 * @param mixed $first Value to compare.
189 * @param mixed $second Value to compare.
190 * @param string $message Message to display.
191 * @return boolean True on pass
194 function assertNotIdentical($first, $second, $message = "%s") {
195 return $this->assert(
196 new NotIdenticalExpectation($first),
202 * Will trigger a pass if both parameters refer
203 * to the same object. Fail otherwise.
204 * @param mixed $first Object reference to check.
205 * @param mixed $second Hopefully the same object.
206 * @param string $message Message to display.
207 * @return boolean True on pass
210 function assertReference(&$first, &$second, $message = "%s") {
211 $dumper = &new SimpleDumper();
214 "[" . $dumper->describeValue($first) .
215 "] and [" . $dumper->describeValue($second) .
216 "] should reference the same object");
217 return $this->assertTrue(
218 SimpleTestCompatibility
::isReference($first, $second),
223 * Will trigger a pass if both parameters refer
224 * to different objects. Fail otherwise. The objects
225 * have to be identical though.
226 * @param mixed $first Object reference to check.
227 * @param mixed $second Hopefully not the same object.
228 * @param string $message Message to display.
229 * @return boolean True on pass
232 function assertClone(&$first, &$second, $message = "%s") {
233 $dumper = &new SimpleDumper();
236 "[" . $dumper->describeValue($first) .
237 "] and [" . $dumper->describeValue($second) .
238 "] should not be the same object");
239 $identical = &new IdenticalExpectation($first);
240 return $this->assertTrue(
241 $identical->test($second) &&
242 ! SimpleTestCompatibility
::isReference($first, $second),
249 function assertCopy(&$first, &$second, $message = "%s") {
250 $dumper = &new SimpleDumper();
253 "[" . $dumper->describeValue($first) .
254 "] and [" . $dumper->describeValue($second) .
255 "] should not be the same object");
256 return $this->assertFalse(
257 SimpleTestCompatibility
::isReference($first, $second),
262 * Will trigger a pass if the Perl regex pattern
263 * is found in the subject. Fail otherwise.
264 * @param string $pattern Perl regex to look for including
265 * the regex delimiters.
266 * @param string $subject String to search in.
267 * @param string $message Message to display.
268 * @return boolean True on pass
271 function assertPattern($pattern, $subject, $message = "%s") {
272 return $this->assert(
273 new PatternExpectation($pattern),
281 function assertWantedPattern($pattern, $subject, $message = "%s") {
282 return $this->assertPattern($pattern, $subject, $message);
286 * Will trigger a pass if the perl regex pattern
287 * is not present in subject. Fail if found.
288 * @param string $pattern Perl regex to look for including
289 * the regex delimiters.
290 * @param string $subject String to search in.
291 * @param string $message Message to display.
292 * @return boolean True on pass
295 function assertNoPattern($pattern, $subject, $message = "%s") {
296 return $this->assert(
297 new NoPatternExpectation($pattern),
305 function assertNoUnwantedPattern($pattern, $subject, $message = "%s") {
306 return $this->assertNoPattern($pattern, $subject, $message);
310 * Confirms that no errors have occoured so
311 * far in the test method.
312 * @param string $message Message to display.
313 * @return boolean True on pass
316 function assertNoErrors($message = "%s") {
317 $queue = &SimpleErrorQueue
::instance();
318 return $this->assertTrue(
320 sprintf($message, "Should be no errors"));
324 * Confirms that an error has occoured and
325 * optionally that the error text matches exactly.
326 * @param string $expected Expected error text or
327 * false for no check.
328 * @param string $message Message to display.
329 * @return boolean True on pass
332 function assertError($expected = false, $message = "%s") {
333 $queue = &SimpleErrorQueue
::instance();
334 if ($queue->isEmpty()) {
335 $this->fail(sprintf($message, "Expected error not found"));
338 list($severity, $content, $file, $line, $globals) = $queue->extract();
339 $severity = SimpleErrorQueue
::getSeverityAsString($severity);
342 "Captured a PHP error of [$content] severity [$severity] in [$file] line [$line] -> %s");
344 $expected = $this->_coerceToExpectation($expected);
345 return $this->assert(
348 "Expected PHP error [$content] severity [$severity] in [$file] line [$line] -> %s");
352 * Creates an equality expectation if the
353 * object/value is not already some type
355 * @param mixed $expected Expected value.
356 * @return SimpleExpectation Expectation object.
359 function _coerceToExpectation($expected) {
360 if (SimpleTestCompatibility
::isA($expected, 'SimpleExpectation')) {
363 return new EqualExpectation($expected);
369 function assertErrorPattern($pattern, $message = "%s") {
370 return $this->assertError(new PatternExpectation($pattern), $message);