Unit test for #149.
[akelos.git] / vendor / simpletest / unit_tester.php
blob7b444e988b152f1368a42a3fb7fea2535e326a6e
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: unit_tester.php,v 1.31 2006/02/06 06:05:18 lastcraft Exp $
7 */
9 /**#@+
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__) . '/test_case.php');
13 require_once(dirname(__FILE__) . '/dumper.php');
14 /**#@-*/
16 /**
17 * Standard unit test class for day to day testing
18 * of PHP code XP style. Adds some useful standard
19 * assertions.
20 * @package SimpleTest
21 * @subpackage UnitTester
23 class UnitTestCase extends SimpleTestCase {
25 /**
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.
30 * @access public
32 function UnitTestCase($label = false) {
33 if (! $label) {
34 $label = get_class($this);
36 $this->SimpleTestCase($label);
39 /**
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
44 * @access public
46 function assertNull($value, $message = "%s") {
47 $dumper = &new SimpleDumper();
48 $message = sprintf(
49 $message,
50 "[" . $dumper->describeValue($value) . "] should be null");
51 return $this->assertTrue(! isset($value), $message);
54 /**
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.
59 * @access public
61 function assertNotNull($value, $message = "%s") {
62 $dumper = &new SimpleDumper();
63 $message = sprintf(
64 $message,
65 "[" . $dumper->describeValue($value) . "] should not be null");
66 return $this->assertTrue(isset($value), $message);
69 /**
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.
77 * @access public
79 function assertIsA($object, $type, $message = "%s") {
80 return $this->assert(
81 new IsAExpectation($type),
82 $object,
83 $message);
86 /**
87 * Type and class mismatch test. Will pass if class
88 * name or underling type does not match the one
89 * specified.
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.
94 * @access public
96 function assertNotA($object, $type, $message = "%s") {
97 return $this->assert(
98 new NotAExpectation($type),
99 $object,
100 $message);
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
110 * @access public
112 function assertEqual($first, $second, $message = "%s") {
113 return $this->assert(
114 new EqualExpectation($first),
115 $second,
116 $message);
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
126 * @access public
128 function assertNotEqual($first, $second, $message = "%s") {
129 return $this->assert(
130 new NotEqualExpectation($first),
131 $second,
132 $message);
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
143 * @access public
145 function assertWithinMargin($first, $second, $margin, $message = "%s") {
146 return $this->assert(
147 new WithinMarginExpectation($first, $margin),
148 $second,
149 $message);
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
160 * @access public
162 function assertOutsideMargin($first, $second, $margin, $message = "%s") {
163 return $this->assert(
164 new OutsideMarginExpectation($first, $margin),
165 $second,
166 $message);
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
176 * @access public
178 function assertIdentical($first, $second, $message = "%s") {
179 return $this->assert(
180 new IdenticalExpectation($first),
181 $second,
182 $message);
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
192 * @access public
194 function assertNotIdentical($first, $second, $message = "%s") {
195 return $this->assert(
196 new NotIdenticalExpectation($first),
197 $second,
198 $message);
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
208 * @access public
210 function assertReference(&$first, &$second, $message = "%s") {
211 $dumper = &new SimpleDumper();
212 $message = sprintf(
213 $message,
214 "[" . $dumper->describeValue($first) .
215 "] and [" . $dumper->describeValue($second) .
216 "] should reference the same object");
217 return $this->assertTrue(
218 SimpleTestCompatibility::isReference($first, $second),
219 $message);
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
230 * @access public
232 function assertClone(&$first, &$second, $message = "%s") {
233 $dumper = &new SimpleDumper();
234 $message = sprintf(
235 $message,
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),
243 $message);
247 * @deprecated
249 function assertCopy(&$first, &$second, $message = "%s") {
250 $dumper = &new SimpleDumper();
251 $message = sprintf(
252 $message,
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),
258 $message);
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
269 * @access public
271 function assertPattern($pattern, $subject, $message = "%s") {
272 return $this->assert(
273 new PatternExpectation($pattern),
274 $subject,
275 $message);
279 * @deprecated
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
293 * @access public
295 function assertNoPattern($pattern, $subject, $message = "%s") {
296 return $this->assert(
297 new NoPatternExpectation($pattern),
298 $subject,
299 $message);
303 * @deprecated
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
314 * @access public
316 function assertNoErrors($message = "%s") {
317 $queue = &SimpleErrorQueue::instance();
318 return $this->assertTrue(
319 $queue->isEmpty(),
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
330 * @access public
332 function assertError($expected = false, $message = "%s") {
333 $queue = &SimpleErrorQueue::instance();
334 if ($queue->isEmpty()) {
335 $this->fail(sprintf($message, "Expected error not found"));
336 return;
338 list($severity, $content, $file, $line, $globals) = $queue->extract();
339 $severity = SimpleErrorQueue::getSeverityAsString($severity);
340 if (! $expected) {
341 return $this->pass(
342 "Captured a PHP error of [$content] severity [$severity] in [$file] line [$line] -> %s");
344 $expected = $this->_coerceToExpectation($expected);
345 return $this->assert(
346 $expected,
347 $content,
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
354 * of expectation.
355 * @param mixed $expected Expected value.
356 * @return SimpleExpectation Expectation object.
357 * @access private
359 function _coerceToExpectation($expected) {
360 if (SimpleTestCompatibility::isA($expected, 'SimpleExpectation')) {
361 return $expected;
363 return new EqualExpectation($expected);
367 * @deprecated
369 function assertErrorPattern($pattern, $message = "%s") {
370 return $this->assertError(new PatternExpectation($pattern), $message);