3 * base include file for SimpleTest
5 * @subpackage UnitTester
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 * Called from within the test methods to register
41 * passes and failures.
42 * @param boolean $result Pass on true.
43 * @param string $message Message to display describing
45 * @return boolean True on pass
48 function assertTrue($result, $message = false) {
49 return $this->assert(new TrueExpectation(), $result, $message);
53 * Will be true on false and vice versa. False
54 * is the PHP definition of false, so that null,
55 * empty strings, zero and an empty array all count
57 * @param boolean $result Pass on false.
58 * @param string $message Message to display.
59 * @return boolean True on pass
62 function assertFalse($result, $message = '%s') {
63 return $this->assert(new FalseExpectation(), $result, $message);
67 * Will be true if the value is null.
68 * @param null $value Supposedly null value.
69 * @param string $message Message to display.
70 * @return boolean True on pass
73 function assertNull($value, $message = '%s') {
74 $dumper = &new SimpleDumper();
77 '[' . $dumper->describeValue($value) . '] should be null');
78 return $this->assertTrue(! isset($value), $message);
82 * Will be true if the value is set.
83 * @param mixed $value Supposedly set value.
84 * @param string $message Message to display.
85 * @return boolean True on pass.
88 function assertNotNull($value, $message = '%s') {
89 $dumper = &new SimpleDumper();
92 '[' . $dumper->describeValue($value) . '] should not be null');
93 return $this->assertTrue(isset($value), $message);
97 * Type and class test. Will pass if class
98 * matches the type name or is a subclass or
99 * if not an object, but the type is correct.
100 * @param mixed $object Object to test.
101 * @param string $type Type name as string.
102 * @param string $message Message to display.
103 * @return boolean True on pass.
106 function assertIsA($object, $type, $message = '%s') {
107 return $this->assert(
108 new IsAExpectation($type),
114 * Type and class mismatch test. Will pass if class
115 * name or underling type does not match the one
117 * @param mixed $object Object to test.
118 * @param string $type Type name as string.
119 * @param string $message Message to display.
120 * @return boolean True on pass.
123 function assertNotA($object, $type, $message = '%s') {
124 return $this->assert(
125 new NotAExpectation($type),
131 * Will trigger a pass if the two parameters have
132 * the same value only. Otherwise a fail.
133 * @param mixed $first Value to compare.
134 * @param mixed $second Value to compare.
135 * @param string $message Message to display.
136 * @return boolean True on pass
139 function assertEqual($first, $second, $message = '%s') {
140 return $this->assert(
141 new EqualExpectation($first),
147 * Will trigger a pass if the two parameters have
148 * a different value. Otherwise a fail.
149 * @param mixed $first Value to compare.
150 * @param mixed $second Value to compare.
151 * @param string $message Message to display.
152 * @return boolean True on pass
155 function assertNotEqual($first, $second, $message = '%s') {
156 return $this->assert(
157 new NotEqualExpectation($first),
163 * Will trigger a pass if the if the first parameter
164 * is near enough to the second by the margin.
165 * @param mixed $first Value to compare.
166 * @param mixed $second Value to compare.
167 * @param mixed $margin Fuzziness of match.
168 * @param string $message Message to display.
169 * @return boolean True on pass
172 function assertWithinMargin($first, $second, $margin, $message = '%s') {
173 return $this->assert(
174 new WithinMarginExpectation($first, $margin),
180 * Will trigger a pass if the two parameters differ
181 * by more than the margin.
182 * @param mixed $first Value to compare.
183 * @param mixed $second Value to compare.
184 * @param mixed $margin Fuzziness of match.
185 * @param string $message Message to display.
186 * @return boolean True on pass
189 function assertOutsideMargin($first, $second, $margin, $message = '%s') {
190 return $this->assert(
191 new OutsideMarginExpectation($first, $margin),
197 * Will trigger a pass if the two parameters have
198 * the same value and same type. Otherwise a fail.
199 * @param mixed $first Value to compare.
200 * @param mixed $second Value to compare.
201 * @param string $message Message to display.
202 * @return boolean True on pass
205 function assertIdentical($first, $second, $message = '%s') {
206 return $this->assert(
207 new IdenticalExpectation($first),
213 * Will trigger a pass if the two parameters have
214 * the different value or different type.
215 * @param mixed $first Value to compare.
216 * @param mixed $second Value to compare.
217 * @param string $message Message to display.
218 * @return boolean True on pass
221 function assertNotIdentical($first, $second, $message = '%s') {
222 return $this->assert(
223 new NotIdenticalExpectation($first),
229 * Will trigger a pass if both parameters refer
230 * to the same object. Fail otherwise.
231 * @param mixed $first Object reference to check.
232 * @param mixed $second Hopefully the same object.
233 * @param string $message Message to display.
234 * @return boolean True on pass
237 function assertReference(&$first, &$second, $message = '%s') {
238 $dumper = &new SimpleDumper();
241 '[' . $dumper->describeValue($first) .
242 '] and [' . $dumper->describeValue($second) .
243 '] should reference the same object');
244 return $this->assertTrue(
245 SimpleTestCompatibility
::isReference($first, $second),
250 * Will trigger a pass if both parameters refer
251 * to different objects. Fail otherwise. The objects
252 * have to be identical though.
253 * @param mixed $first Object reference to check.
254 * @param mixed $second Hopefully not the same object.
255 * @param string $message Message to display.
256 * @return boolean True on pass
259 function assertClone(&$first, &$second, $message = '%s') {
260 $dumper = &new SimpleDumper();
263 '[' . $dumper->describeValue($first) .
264 '] and [' . $dumper->describeValue($second) .
265 '] should not be the same object');
266 $identical = &new IdenticalExpectation($first);
267 return $this->assertTrue(
268 $identical->test($second) &&
269 ! SimpleTestCompatibility
::isReference($first, $second),
276 function assertCopy(&$first, &$second, $message = "%s") {
277 $dumper = &new SimpleDumper();
280 "[" . $dumper->describeValue($first) .
281 "] and [" . $dumper->describeValue($second) .
282 "] should not be the same object");
283 return $this->assertFalse(
284 SimpleTestCompatibility
::isReference($first, $second),
289 * Will trigger a pass if the Perl regex pattern
290 * is found in the subject. Fail otherwise.
291 * @param string $pattern Perl regex to look for including
292 * the regex delimiters.
293 * @param string $subject String to search in.
294 * @param string $message Message to display.
295 * @return boolean True on pass
298 function assertPattern($pattern, $subject, $message = '%s') {
299 return $this->assert(
300 new PatternExpectation($pattern),
308 function assertWantedPattern($pattern, $subject, $message = '%s') {
309 return $this->assertPattern($pattern, $subject, $message);
313 * Will trigger a pass if the perl regex pattern
314 * is not present in subject. Fail if found.
315 * @param string $pattern Perl regex to look for including
316 * the regex delimiters.
317 * @param string $subject String to search in.
318 * @param string $message Message to display.
319 * @return boolean True on pass
322 function assertNoPattern($pattern, $subject, $message = '%s') {
323 return $this->assert(
324 new NoPatternExpectation($pattern),
332 function assertNoUnwantedPattern($pattern, $subject, $message = '%s') {
333 return $this->assertNoPattern($pattern, $subject, $message);
339 function swallowErrors() {
340 $context = &SimpleTest
::getContext();
341 $queue = &$context->get('SimpleErrorQueue');
348 function assertNoErrors($message = '%s') {
349 $context = &SimpleTest
::getContext();
350 $queue = &$context->get('SimpleErrorQueue');
351 return $queue->assertNoErrors($message);
357 function assertError($expected = false, $message = '%s') {
358 $context = &SimpleTest
::getContext();
359 $queue = &$context->get('SimpleErrorQueue');
360 return $queue->assertError($this->_coerceExpectation($expected), $message);
364 * Prepares for an error. If the error mismatches it
365 * passes through, otherwise it is swallowed. Any
366 * left over errors trigger failures.
367 * @param SimpleExpectation/string $expected The error to match.
368 * @param string $message Message on failure.
371 function expectError($expected = false, $message = '%s') {
372 $context = &SimpleTest
::getContext();
373 $queue = &$context->get('SimpleErrorQueue');
374 $queue->expectError($this->_coerceExpectation($expected), $message);
378 * Prepares for an exception. If the error mismatches it
379 * passes through, otherwise it is swallowed. Any
380 * left over errors trigger failures.
381 * @param SimpleExpectation/Exception $expected The error to match.
382 * @param string $message Message on failure.
385 function expectException($expected = false, $message = '%s') {
386 $context = &SimpleTest
::getContext();
387 $queue = &$context->get('SimpleExceptionTrap');
388 $queue->expectException($expected, $message . $this->getAssertionLine());
392 * Creates an equality expectation if the
393 * object/value is not already some type
395 * @param mixed $expected Expected value.
396 * @return SimpleExpectation Expectation object.
399 function _coerceExpectation($expected) {
400 if ($expected == false) {
401 return new AnythingExpectation();
403 if (SimpleTestCompatibility
::isA($expected, 'SimpleExpectation')) {
406 if(is_string($expected)) {
407 $expected = str_replace('%', '%%', $expected);
409 return new EqualExpectation($expected);
415 function assertErrorPattern($pattern, $message = '%s') {
416 return $this->assertError(new PatternExpectation($pattern), $message);