make javascript smaller, and nicer
[platal.git] / htdocs / TESTS / simpletest / unit_tester.php
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: unit_tester.php,v 1.21 2004/08/25 21:00:02 lastcraft Exp $
7 */
8
9 /**#@+
10 * include other SimpleTest class files
11 */
12 require_once(dirname(__FILE__) . '/simple_test.php');
13 require_once(dirname(__FILE__) . '/errors.php');
14 require_once(dirname(__FILE__) . '/dumper.php');
15 /**#@-*/
16
17 /**
18 * Standard unit test class for day to day testing
19 * of PHP code XP style. Adds some useful standard
20 * assertions.
21 * @package SimpleTest
22 * @subpackage UnitTester
23 */
24 class UnitTestCase extends SimpleTestCase {
25
26 /**
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.
31 * @access public
32 */
33 function UnitTestCase($label = false) {
34 if (! $label) {
35 $label = get_class($this);
36 }
37 $this->SimpleTestCase($label);
38 }
39
40 /**
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
45 * @access public
46 */
47 function assertNull($value, $message = "%s") {
48 $dumper = &new SimpleDumper();
49 $message = sprintf(
50 $message,
51 "[" . $dumper->describeValue($value) . "] should be null");
52 return $this->assertTrue(! isset($value), $message);
53 }
54
55 /**
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.
60 * @access public
61 */
62 function assertNotNull($value, $message = "%s") {
63 $dumper = &new SimpleDumper();
64 $message = sprintf(
65 $message,
66 "[" . $dumper->describeValue($value) . "] should not be null");
67 return $this->assertTrue(isset($value), $message);
68 }
69
70 /**
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.
78 * @access public
79 */
80 function assertIsA($object, $type, $message = "%s") {
81 return $this->assertExpectation(
82 new IsAExpectation($type),
83 $object,
84 $message);
85 }
86
87 /**
88 * Type and class mismatch test. Will pass if class
89 * name or underling type does not match the one
90 * specified.
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.
95 * @access public
96 */
97 function assertNotA($object, $type, $message = "%s") {
98 return $this->assertExpectation(
99 new NotAExpectation($type),
100 $object,
101 $message);
102 }
103
104 /**
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
111 * @access public
112 */
113 function assertEqual($first, $second, $message = "%s") {
114 return $this->assertExpectation(
115 new EqualExpectation($first),
116 $second,
117 $message);
118 }
119
120 /**
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
127 * @access public
128 */
129 function assertNotEqual($first, $second, $message = "%s") {
130 return $this->assertExpectation(
131 new NotEqualExpectation($first),
132 $second,
133 $message);
134 }
135
136 /**
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
143 * @access public
144 */
145 function assertIdentical($first, $second, $message = "%s") {
146 return $this->assertExpectation(
147 new IdenticalExpectation($first),
148 $second,
149 $message);
150 }
151
152 /**
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
159 * @access public
160 */
161 function assertNotIdentical($first, $second, $message = "%s") {
162 return $this->assertExpectation(
163 new NotIdenticalExpectation($first),
164 $second,
165 $message);
166 }
167
168 /**
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
175 * @access public
176 */
177 function assertReference(&$first, &$second, $message = "%s") {
178 $dumper = &new SimpleDumper();
179 $message = sprintf(
180 $message,
181 "[" . $dumper->describeValue($first) .
182 "] and [" . $dumper->describeValue($second) .
183 "] should reference the same object");
184 $temp = $first;
185 $first = uniqid("test");
186 $is_ref = ($first === $second);
187 $first = $temp;
188 return $this->assertTrue($is_ref, $message);
189 }
190
191 /**
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
198 * @access public
199 */
200 function assertCopy(&$first, &$second, $message = "%s") {
201 $dumper = &new SimpleDumper();
202 $message = sprintf(
203 $message,
204 "[" . $dumper->describeValue($first) .
205 "] and [" . $dumper->describeValue($second) .
206 "] should not be the same object");
207 $temp = $first;
208 $first = uniqid("test");
209 $is_ref = ($first === $second);
210 $first = $temp;
211 return $this->assertFalse($is_ref, $message);
212 }
213
214 /**
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
222 * @access public
223 */
224 function assertWantedPattern($pattern, $subject, $message = "%s") {
225 return $this->assertExpectation(
226 new WantedPatternExpectation($pattern),
227 $subject,
228 $message);
229 }
230
231 /**
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
239 * @access public
240 */
241 function assertNoUnwantedPattern($pattern, $subject, $message = "%s") {
242 return $this->assertExpectation(
243 new UnwantedPatternExpectation($pattern),
244 $subject,
245 $message);
246 }
247
248 /**
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
253 * @access public
254 */
255 function assertNoErrors($message = "%s") {
256 $queue = &SimpleErrorQueue::instance();
257 return $this->assertTrue(
258 $queue->isEmpty(),
259 sprintf($message, "Should be no errors"));
260 }
261
262 /**
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
269 * @access public
270 */
271 function assertError($expected = false, $message = "%s") {
272 $queue = &SimpleErrorQueue::instance();
273 if ($queue->isEmpty()) {
274 $this->fail(sprintf($message, "Expected error not found"));
275 return;
276 }
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]");
282 }
283
284 /**
285 * Confirms that an error has occoured and
286 * that the error text matches a Perl regular
287 * expression.
288 * @param string $expected Perl regular expresion to
289 * match against.
290 * @param string $message Message to display.
291 * @return boolean True on pass
292 * @access public
293 */
294 function assertErrorPattern($pattern, $message = "%s") {
295 $queue = &SimpleErrorQueue::instance();
296 if ($queue->isEmpty()) {
297 $this->fail(sprintf($message, "Expected error not found"));
298 return;
299 }
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]");
305 }
306 }
307 ?>