Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
    /**
3
     *	base include file for SimpleTest
4
     *	@package	SimpleTest
5
     *	@subpackage	UnitTester
6
     *	@version	$Id: unit_tester.php 1398 2006-09-08 19:31:03Z xue $
7
     */
8
 
9
    /**#@+
10
     *	include other SimpleTest class files
11
     */
12
    require_once(dirname(__FILE__) . '/test_case.php');
13
    require_once(dirname(__FILE__) . '/dumper.php');
14
    /**#@-*/
15
 
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
22
     */
23
    class UnitTestCase extends SimpleTestCase {
24
 
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
31
         */
32
        function UnitTestCase($label = false) {
33
            if (! $label) {
34
                $label = get_class($this);
35
            }
36
            $this->SimpleTestCase($label);
37
        }
38
 
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
45
         */
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);
52
        }
53
 
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
60
         */
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);
67
        }
68
 
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
78
         */
79
        function assertIsA($object, $type, $message = "%s") {
80
            return $this->assert(
81
                    new IsAExpectation($type),
82
                    $object,
83
                    $message);
84
        }
85
 
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
95
         */
96
        function assertNotA($object, $type, $message = "%s") {
97
            return $this->assert(
98
                    new NotAExpectation($type),
99
                    $object,
100
                    $message);
101
        }
102
 
103
        /**
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
111
         */
112
        function assertEqual($first, $second, $message = "%s") {
113
            return $this->assert(
114
                    new EqualExpectation($first),
115
                    $second,
116
                    $message);
117
        }
118
 
119
        /**
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
127
         */
128
        function assertNotEqual($first, $second, $message = "%s") {
129
            return $this->assert(
130
                    new NotEqualExpectation($first),
131
                    $second,
132
                    $message);
133
        }
134
 
135
        /**
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
144
         */
145
        function assertWithinMargin($first, $second, $margin, $message = "%s") {
146
            return $this->assert(
147
                    new WithinMarginExpectation($first, $margin),
148
                    $second,
149
                    $message);
150
        }
151
 
152
        /**
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
161
         */
162
        function assertOutsideMargin($first, $second, $margin, $message = "%s") {
163
            return $this->assert(
164
                    new OutsideMarginExpectation($first, $margin),
165
                    $second,
166
                    $message);
167
        }
168
 
169
        /**
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
177
         */
178
        function assertIdentical($first, $second, $message = "%s") {
179
            return $this->assert(
180
                    new IdenticalExpectation($first),
181
                    $second,
182
                    $message);
183
        }
184
 
185
        /**
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
193
         */
194
        function assertNotIdentical($first, $second, $message = "%s") {
195
            return $this->assert(
196
                    new NotIdenticalExpectation($first),
197
                    $second,
198
                    $message);
199
        }
200
 
201
        /**
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
209
         */
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);
220
        }
221
 
222
        /**
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
231
         */
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);
244
        }
245
 
246
        /**
247
         *    @deprecated
248
         */
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);
259
        }
260
 
261
        /**
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
270
         */
271
        function assertPattern($pattern, $subject, $message = "%s") {
272
            return $this->assert(
273
                    new PatternExpectation($pattern),
274
                    $subject,
275
                    $message);
276
        }
277
 
278
        /**
279
         *	  @deprecated
280
         */
281
        function assertWantedPattern($pattern, $subject, $message = "%s") {
282
        	return $this->assertPattern($pattern, $subject, $message);
283
        }
284
 
285
        /**
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
294
         */
295
        function assertNoPattern($pattern, $subject, $message = "%s") {
296
            return $this->assert(
297
                    new NoPatternExpectation($pattern),
298
                    $subject,
299
                    $message);
300
        }
301
 
302
        /**
303
         *	  @deprecated
304
         */
305
        function assertNoUnwantedPattern($pattern, $subject, $message = "%s") {
306
        	return $this->assertNoPattern($pattern, $subject, $message);
307
        }
308
 
309
        /**
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
315
         */
316
        function assertNoErrors($message = "%s") {
317
            $queue = &SimpleErrorQueue::instance();
318
            return $this->assertTrue(
319
                    $queue->isEmpty(),
320
                    sprintf($message, "Should be no errors"));
321
        }
322
 
323
        /**
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
331
         */
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;
337
            }
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");
343
            }
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");
349
        }
350
 
351
        /**
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
358
         */
359
        function _coerceToExpectation($expected) {
360
            if (SimpleTestCompatibility::isA($expected, 'SimpleExpectation')) {
361
                return $expected;
362
            }
363
            return new EqualExpectation($expected);
364
        }
365
 
366
        /**
367
         *    @deprecated
368
         */
369
        function assertErrorPattern($pattern, $message = "%s") {
370
            return $this->assertError(new PatternExpectation($pattern), $message);
371
        }
372
    }
373
?>