- Initial import of the mob, reorganizing dirs to match packaged version
[plcapi.git] / test / PHPUnit / Assert.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5  * PHP Version 4
6  *
7  * LICENSE: This source file is subject to version 3.0 of the PHP license
8  * that is available through the world-wide-web at the following URI:
9  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
10  * the PHP License and are unable to obtain it through the web, please
11  * send a note to license@php.net so we can mail you a copy immediately.
12  *
13  * @category   Testing
14  * @package    PHPUnit
15  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
16  * @copyright  2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
17  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
18  * @version    CVS: $Id$
19  * @link       http://pear.php.net/package/PHPUnit
20  * @since      File available since Release 1.0.0
21  */
22
23 /**
24  * A set of assert methods.
25  *
26  * @category   Testing
27  * @package    PHPUnit
28  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
29  * @copyright  2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
30  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
31  * @version    Release: @package_version@
32  * @link       http://pear.php.net/package/PHPUnit
33  * @since      Class available since Release 1.0.0
34  */
35 class PHPUnit_Assert {
36     /**
37      * @var    boolean
38      * @access private
39      */
40     var $_looselyTyped = FALSE;
41
42     /**
43      * Asserts that a haystack contains a needle.
44      *
45      * @param  mixed
46      * @param  mixed
47      * @param  string
48      * @access public
49      * @since  Method available since Release 1.1.0
50      */
51     function assertContains($needle, $haystack, $message = '') {
52         if (is_string($needle) && is_string($haystack)) {
53             $this->assertTrue(strpos($haystack, $needle) !== FALSE, $message);
54         }
55
56         else if (is_array($haystack) && !is_object($needle)) {
57             $this->assertTrue(in_array($needle, $haystack), $message);
58         }
59
60         else {
61             $this->fail('Unsupported parameter passed to assertContains().');
62         }
63     }
64
65     /**
66      * Asserts that a haystack does not contain a needle.
67      *
68      * @param  mixed
69      * @param  mixed
70      * @param  string
71      * @access public
72      * @since  Method available since Release 1.1.0
73      */
74     function assertNotContains($needle, $haystack, $message = '') {
75         if (is_string($needle) && is_string($haystack)) {
76             $this->assertFalse(strpos($haystack, $needle) !== FALSE, $message);
77         }
78
79         else if (is_array($haystack) && !is_object($needle)) {
80             $this->assertFalse(in_array($needle, $haystack), $message);
81         }
82
83         else {
84             $this->fail('Unsupported parameter passed to assertNotContains().');
85         }
86     }
87
88     /**
89      * Asserts that two variables are equal.
90      *
91      * @param  mixed
92      * @param  mixed
93      * @param  string
94      * @param  mixed
95      * @access public
96      */
97     function assertEquals($expected, $actual, $message = '', $delta = 0) {
98         if ((is_array($actual)  && is_array($expected)) ||
99             (is_object($actual) && is_object($expected))) {
100             if (is_array($actual) && is_array($expected)) {
101                 ksort($actual);
102                 ksort($expected);
103             }
104
105             if ($this->_looselyTyped) {
106                 $actual   = $this->_convertToString($actual);
107                 $expected = $this->_convertToString($expected);
108             }
109
110             $actual   = serialize($actual);
111             $expected = serialize($expected);
112
113             $message = sprintf(
114               '%sexpected %s, actual %s',
115
116               !empty($message) ? $message . ' ' : '',
117               $expected,
118               $actual
119             );
120
121             if ($actual !== $expected) {
122                 return $this->fail($message);
123             }
124         }
125
126         elseif (is_numeric($actual) && is_numeric($expected)) {
127             $message = sprintf(
128               '%sexpected %s%s, actual %s',
129
130               !empty($message) ? $message . ' ' : '',
131               $expected,
132               ($delta != 0) ? ('+/- ' . $delta) : '',
133               $actual
134             );
135
136             if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) {
137                 return $this->fail($message);
138             }
139         }
140
141         else {
142             $message = sprintf(
143               '%sexpected %s, actual %s',
144
145               !empty($message) ? $message . ' ' : '',
146               $expected,
147               $actual
148             );
149
150             if ($actual !== $expected) {
151                 return $this->fail($message);
152             }
153         }
154     }
155
156     /**
157      * Asserts that two variables reference the same object.
158      * This requires the Zend Engine 2 to work.
159      *
160      * @param  object
161      * @param  object
162      * @param  string
163      * @access public
164      * @deprecated
165      */
166     function assertSame($expected, $actual, $message = '') {
167         if (!version_compare(phpversion(), '5.0.0', '>=')) {
168             $this->fail('assertSame() only works with PHP >= 5.0.0.');
169         }
170
171         if ((is_object($expected) || is_null($expected)) &&
172             (is_object($actual)   || is_null($actual))) {
173             $message = sprintf(
174               '%sexpected two variables to reference the same object',
175
176               !empty($message) ? $message . ' ' : ''
177             );
178
179             if ($expected !== $actual) {
180                 return $this->fail($message);
181             }
182         } else {
183             $this->fail('Unsupported parameter passed to assertSame().');
184         }
185     }
186
187     /**
188      * Asserts that two variables do not reference the same object.
189      * This requires the Zend Engine 2 to work.
190      *
191      * @param  object
192      * @param  object
193      * @param  string
194      * @access public
195      * @deprecated
196      */
197     function assertNotSame($expected, $actual, $message = '') {
198         if (!version_compare(phpversion(), '5.0.0', '>=')) {
199             $this->fail('assertNotSame() only works with PHP >= 5.0.0.');
200         }
201
202         if ((is_object($expected) || is_null($expected)) &&
203             (is_object($actual)   || is_null($actual))) {
204             $message = sprintf(
205               '%sexpected two variables to reference different objects',
206
207               !empty($message) ? $message . ' ' : ''
208             );
209
210             if ($expected === $actual) {
211                 return $this->fail($message);
212             }
213         } else {
214             $this->fail('Unsupported parameter passed to assertNotSame().');
215         }
216     }
217
218     /**
219      * Asserts that a variable is not NULL.
220      *
221      * @param  mixed
222      * @param  string
223      * @access public
224      */
225     function assertNotNull($actual, $message = '') {
226         $message = sprintf(
227           '%sexpected NOT NULL, actual NULL',
228
229           !empty($message) ? $message . ' ' : ''
230         );
231
232         if (is_null($actual)) {
233             return $this->fail($message);
234         }
235     }
236
237     /**
238      * Asserts that a variable is NULL.
239      *
240      * @param  mixed
241      * @param  string
242      * @access public
243      */
244     function assertNull($actual, $message = '') {
245         $message = sprintf(
246           '%sexpected NULL, actual NOT NULL',
247
248           !empty($message) ? $message . ' ' : ''
249         );
250
251         if (!is_null($actual)) {
252             return $this->fail($message);
253         }
254     }
255
256     /**
257      * Asserts that a condition is true.
258      *
259      * @param  boolean
260      * @param  string
261      * @access public
262      */
263     function assertTrue($condition, $message = '') {
264         $message = sprintf(
265           '%sexpected TRUE, actual FALSE',
266
267           !empty($message) ? $message . ' ' : ''
268         );
269
270         if (!$condition) {
271             return $this->fail($message);
272         }
273     }
274
275     /**
276      * Asserts that a condition is false.
277      *
278      * @param  boolean
279      * @param  string
280      * @access public
281      */
282     function assertFalse($condition, $message = '') {
283         $message = sprintf(
284           '%sexpected FALSE, actual TRUE',
285
286           !empty($message) ? $message . ' ' : ''
287         );
288
289         if ($condition) {
290             return $this->fail($message);
291         }
292     }
293
294     /**
295      * Asserts that a string matches a given regular expression.
296      *
297      * @param  string
298      * @param  string
299      * @param  string
300      * @access public
301      */
302     function assertRegExp($pattern, $string, $message = '') {
303         $message = sprintf(
304           '%s"%s" does not match pattern "%s"',
305
306           !empty($message) ? $message . ' ' : '',
307           $string,
308           $pattern
309         );
310
311         if (!preg_match($pattern, $string)) {
312             return $this->fail($message);
313         }
314     }
315
316     /**
317      * Asserts that a string does not match a given regular expression.
318      *
319      * @param  string
320      * @param  string
321      * @param  string
322      * @access public
323      * @since  Method available since Release 1.1.0
324      */
325     function assertNotRegExp($pattern, $string, $message = '') {
326         $message = sprintf(
327           '%s"%s" matches pattern "%s"',
328
329           !empty($message) ? $message . ' ' : '',
330           $string,
331           $pattern
332         );
333
334         if (preg_match($pattern, $string)) {
335             return $this->fail($message);
336         }
337     }
338
339     /**
340      * Asserts that a variable is of a given type.
341      *
342      * @param  string          $expected
343      * @param  mixed           $actual
344      * @param  optional string $message
345      * @access public
346      */
347     function assertType($expected, $actual, $message = '') {
348         return $this->assertEquals(
349           $expected,
350           gettype($actual),
351           $message
352         );
353     }
354
355     /**
356      * Converts a value to a string.
357      *
358      * @param  mixed   $value
359      * @access private
360      */
361     function _convertToString($value) {
362         foreach ($value as $k => $v) {
363             if (is_array($v)) {
364                 $value[$k] = $this->_convertToString($value[$k]);
365             } else {
366                 settype($value[$k], 'string');
367             }
368         }
369
370         return $value;
371     }
372
373     /**
374      * @param  boolean $looselyTyped
375      * @access public
376      */
377     function setLooselyTyped($looselyTyped) {
378         if (is_bool($looselyTyped)) {
379             $this->_looselyTyped = $looselyTyped;
380         }
381     }
382
383     /**
384      * Fails a test with the given message.
385      *
386      * @param  string
387      * @access protected
388      * @abstract
389      */
390     function fail($message = '') { /* abstract */ }
391 }
392
393 /*
394  * Local variables:
395  * tab-width: 4
396  * c-basic-offset: 4
397  * c-hanging-comment-ender-p: nil
398  * End:
399  */
400 ?>