| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* PHP Version 4
|
|
|
6 |
*
|
|
|
7 |
* Copyright (c) 2002-2005, Sebastian Bergmann <sb@sebastian-bergmann.de>.
|
|
|
8 |
* All rights reserved.
|
|
|
9 |
*
|
|
|
10 |
* Redistribution and use in source and binary forms, with or without
|
|
|
11 |
* modification, are permitted provided that the following conditions
|
|
|
12 |
* are met:
|
|
|
13 |
*
|
|
|
14 |
* * Redistributions of source code must retain the above copyright
|
|
|
15 |
* notice, this list of conditions and the following disclaimer.
|
|
|
16 |
*
|
|
|
17 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
18 |
* notice, this list of conditions and the following disclaimer in
|
|
|
19 |
* the documentation and/or other materials provided with the
|
|
|
20 |
* distribution.
|
|
|
21 |
*
|
|
|
22 |
* * Neither the name of Sebastian Bergmann nor the names of his
|
|
|
23 |
* contributors may be used to endorse or promote products derived
|
|
|
24 |
* from this software without specific prior written permission.
|
|
|
25 |
*
|
|
|
26 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
27 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
28 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
29 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
30 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
31 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
32 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
33 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
34 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
|
|
|
35 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
36 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
37 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
38 |
*
|
|
|
39 |
* @category Testing
|
|
|
40 |
* @package PHPUnit
|
|
|
41 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
42 |
* @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
43 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
44 |
* @version CVS: $Id: Assert.php,v 1.29 2005/11/10 09:47:14 sebastian Exp $
|
|
|
45 |
* @link http://pear.php.net/package/PHPUnit
|
|
|
46 |
* @since File available since Release 1.0.0
|
|
|
47 |
*/
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* A set of assert methods.
|
|
|
51 |
*
|
|
|
52 |
* @category Testing
|
|
|
53 |
* @package PHPUnit
|
|
|
54 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
55 |
* @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
56 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
57 |
* @version Release: 1.3.2
|
|
|
58 |
* @link http://pear.php.net/package/PHPUnit
|
|
|
59 |
* @since Class available since Release 1.0.0
|
|
|
60 |
*/
|
|
|
61 |
class PHPUnit_Assert {
|
|
|
62 |
/**
|
|
|
63 |
* @var boolean
|
|
|
64 |
* @access private
|
|
|
65 |
*/
|
|
|
66 |
var $_looselyTyped = FALSE;
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Asserts that a haystack contains a needle.
|
|
|
70 |
*
|
|
|
71 |
* @param mixed
|
|
|
72 |
* @param mixed
|
|
|
73 |
* @param string
|
|
|
74 |
* @access public
|
|
|
75 |
* @since Method available since Release 1.1.0
|
|
|
76 |
*/
|
|
|
77 |
function assertContains($needle, $haystack, $message = '') {
|
|
|
78 |
if (is_string($needle) && is_string($haystack)) {
|
|
|
79 |
$this->assertTrue(strpos($haystack, $needle) !== FALSE, $message);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
else if (is_array($haystack) && !is_object($needle)) {
|
|
|
83 |
$this->assertTrue(in_array($needle, $haystack), $message);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
else {
|
|
|
87 |
$this->fail('Unsupported parameter passed to assertContains().');
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Asserts that a haystack does not contain a needle.
|
|
|
93 |
*
|
|
|
94 |
* @param mixed
|
|
|
95 |
* @param mixed
|
|
|
96 |
* @param string
|
|
|
97 |
* @access public
|
|
|
98 |
* @since Method available since Release 1.1.0
|
|
|
99 |
*/
|
|
|
100 |
function assertNotContains($needle, $haystack, $message = '') {
|
|
|
101 |
if (is_string($needle) && is_string($haystack)) {
|
|
|
102 |
$this->assertFalse(strpos($haystack, $needle) !== FALSE, $message);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
else if (is_array($haystack) && !is_object($needle)) {
|
|
|
106 |
$this->assertFalse(in_array($needle, $haystack), $message);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
else {
|
|
|
110 |
$this->fail('Unsupported parameter passed to assertNotContains().');
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Asserts that two variables are equal.
|
|
|
116 |
*
|
|
|
117 |
* @param mixed
|
|
|
118 |
* @param mixed
|
|
|
119 |
* @param string
|
|
|
120 |
* @param mixed
|
|
|
121 |
* @access public
|
|
|
122 |
*/
|
|
|
123 |
function assertEquals($expected, $actual, $message = '', $delta = 0) {
|
|
|
124 |
if ((is_array($actual) && is_array($expected)) ||
|
|
|
125 |
(is_object($actual) && is_object($expected))) {
|
|
|
126 |
if (is_array($actual) && is_array($expected)) {
|
|
|
127 |
ksort($actual);
|
|
|
128 |
ksort($expected);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
if ($this->_looselyTyped) {
|
|
|
132 |
$actual = $this->_convertToString($actual);
|
|
|
133 |
$expected = $this->_convertToString($expected);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
$actual = serialize($actual);
|
|
|
137 |
$expected = serialize($expected);
|
|
|
138 |
|
|
|
139 |
$message = sprintf(
|
|
|
140 |
'%sexpected %s, actual %s',
|
|
|
141 |
|
|
|
142 |
!empty($message) ? $message . ' ' : '',
|
|
|
143 |
$expected,
|
|
|
144 |
$actual
|
|
|
145 |
);
|
|
|
146 |
|
|
|
147 |
if ($actual !== $expected) {
|
|
|
148 |
return $this->fail($message);
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
elseif (is_numeric($actual) && is_numeric($expected)) {
|
|
|
153 |
$message = sprintf(
|
|
|
154 |
'%sexpected %s%s, actual %s',
|
|
|
155 |
|
|
|
156 |
!empty($message) ? $message . ' ' : '',
|
|
|
157 |
$expected,
|
|
|
158 |
($delta != 0) ? ('+/- ' . $delta) : '',
|
|
|
159 |
$actual
|
|
|
160 |
);
|
|
|
161 |
|
|
|
162 |
if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) {
|
|
|
163 |
return $this->fail($message);
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
else {
|
|
|
168 |
$message = sprintf(
|
|
|
169 |
'%sexpected %s, actual %s',
|
|
|
170 |
|
|
|
171 |
!empty($message) ? $message . ' ' : '',
|
|
|
172 |
$expected,
|
|
|
173 |
$actual
|
|
|
174 |
);
|
|
|
175 |
|
|
|
176 |
if ($actual !== $expected) {
|
|
|
177 |
return $this->fail($message);
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Asserts that two variables reference the same object.
|
|
|
184 |
* This requires the Zend Engine 2 to work.
|
|
|
185 |
*
|
|
|
186 |
* @param object
|
|
|
187 |
* @param object
|
|
|
188 |
* @param string
|
|
|
189 |
* @access public
|
|
|
190 |
* @deprecated
|
|
|
191 |
*/
|
|
|
192 |
function assertSame($expected, $actual, $message = '') {
|
|
|
193 |
if (!version_compare(phpversion(), '5.0.0', '>=')) {
|
|
|
194 |
$this->fail('assertSame() only works with PHP >= 5.0.0.');
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
if ((is_object($expected) || is_null($expected)) &&
|
|
|
198 |
(is_object($actual) || is_null($actual))) {
|
|
|
199 |
$message = sprintf(
|
|
|
200 |
'%sexpected two variables to reference the same object',
|
|
|
201 |
|
|
|
202 |
!empty($message) ? $message . ' ' : ''
|
|
|
203 |
);
|
|
|
204 |
|
|
|
205 |
if ($expected !== $actual) {
|
|
|
206 |
return $this->fail($message);
|
|
|
207 |
}
|
|
|
208 |
} else {
|
|
|
209 |
$this->fail('Unsupported parameter passed to assertSame().');
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Asserts that two variables do not reference the same object.
|
|
|
215 |
* This requires the Zend Engine 2 to work.
|
|
|
216 |
*
|
|
|
217 |
* @param object
|
|
|
218 |
* @param object
|
|
|
219 |
* @param string
|
|
|
220 |
* @access public
|
|
|
221 |
* @deprecated
|
|
|
222 |
*/
|
|
|
223 |
function assertNotSame($expected, $actual, $message = '') {
|
|
|
224 |
if (!version_compare(phpversion(), '5.0.0', '>=')) {
|
|
|
225 |
$this->fail('assertNotSame() only works with PHP >= 5.0.0.');
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
if ((is_object($expected) || is_null($expected)) &&
|
|
|
229 |
(is_object($actual) || is_null($actual))) {
|
|
|
230 |
$message = sprintf(
|
|
|
231 |
'%sexpected two variables to reference different objects',
|
|
|
232 |
|
|
|
233 |
!empty($message) ? $message . ' ' : ''
|
|
|
234 |
);
|
|
|
235 |
|
|
|
236 |
if ($expected === $actual) {
|
|
|
237 |
return $this->fail($message);
|
|
|
238 |
}
|
|
|
239 |
} else {
|
|
|
240 |
$this->fail('Unsupported parameter passed to assertNotSame().');
|
|
|
241 |
}
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* Asserts that a variable is not NULL.
|
|
|
246 |
*
|
|
|
247 |
* @param mixed
|
|
|
248 |
* @param string
|
|
|
249 |
* @access public
|
|
|
250 |
*/
|
|
|
251 |
function assertNotNull($actual, $message = '') {
|
|
|
252 |
$message = sprintf(
|
|
|
253 |
'%sexpected NOT NULL, actual NULL',
|
|
|
254 |
|
|
|
255 |
!empty($message) ? $message . ' ' : ''
|
|
|
256 |
);
|
|
|
257 |
|
|
|
258 |
if (is_null($actual)) {
|
|
|
259 |
return $this->fail($message);
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
/**
|
|
|
264 |
* Asserts that a variable is NULL.
|
|
|
265 |
*
|
|
|
266 |
* @param mixed
|
|
|
267 |
* @param string
|
|
|
268 |
* @access public
|
|
|
269 |
*/
|
|
|
270 |
function assertNull($actual, $message = '') {
|
|
|
271 |
$message = sprintf(
|
|
|
272 |
'%sexpected NULL, actual NOT NULL',
|
|
|
273 |
|
|
|
274 |
!empty($message) ? $message . ' ' : ''
|
|
|
275 |
);
|
|
|
276 |
|
|
|
277 |
if (!is_null($actual)) {
|
|
|
278 |
return $this->fail($message);
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
/**
|
|
|
283 |
* Asserts that a condition is true.
|
|
|
284 |
*
|
|
|
285 |
* @param boolean
|
|
|
286 |
* @param string
|
|
|
287 |
* @access public
|
|
|
288 |
*/
|
|
|
289 |
function assertTrue($condition, $message = '') {
|
|
|
290 |
$message = sprintf(
|
|
|
291 |
'%sexpected TRUE, actual FALSE',
|
|
|
292 |
|
|
|
293 |
!empty($message) ? $message . ' ' : ''
|
|
|
294 |
);
|
|
|
295 |
|
|
|
296 |
if (!$condition) {
|
|
|
297 |
return $this->fail($message);
|
|
|
298 |
}
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/**
|
|
|
302 |
* Asserts that a condition is false.
|
|
|
303 |
*
|
|
|
304 |
* @param boolean
|
|
|
305 |
* @param string
|
|
|
306 |
* @access public
|
|
|
307 |
*/
|
|
|
308 |
function assertFalse($condition, $message = '') {
|
|
|
309 |
$message = sprintf(
|
|
|
310 |
'%sexpected FALSE, actual TRUE',
|
|
|
311 |
|
|
|
312 |
!empty($message) ? $message . ' ' : ''
|
|
|
313 |
);
|
|
|
314 |
|
|
|
315 |
if ($condition) {
|
|
|
316 |
return $this->fail($message);
|
|
|
317 |
}
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
/**
|
|
|
321 |
* Asserts that a string matches a given regular expression.
|
|
|
322 |
*
|
|
|
323 |
* @param string
|
|
|
324 |
* @param string
|
|
|
325 |
* @param string
|
|
|
326 |
* @access public
|
|
|
327 |
*/
|
|
|
328 |
function assertRegExp($pattern, $string, $message = '') {
|
|
|
329 |
$message = sprintf(
|
|
|
330 |
'%s"%s" does not match pattern "%s"',
|
|
|
331 |
|
|
|
332 |
!empty($message) ? $message . ' ' : '',
|
|
|
333 |
$string,
|
|
|
334 |
$pattern
|
|
|
335 |
);
|
|
|
336 |
|
|
|
337 |
if (!preg_match($pattern, $string)) {
|
|
|
338 |
return $this->fail($message);
|
|
|
339 |
}
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
/**
|
|
|
343 |
* Asserts that a string does not match a given regular expression.
|
|
|
344 |
*
|
|
|
345 |
* @param string
|
|
|
346 |
* @param string
|
|
|
347 |
* @param string
|
|
|
348 |
* @access public
|
|
|
349 |
* @since Method available since Release 1.1.0
|
|
|
350 |
*/
|
|
|
351 |
function assertNotRegExp($pattern, $string, $message = '') {
|
|
|
352 |
$message = sprintf(
|
|
|
353 |
'%s"%s" matches pattern "%s"',
|
|
|
354 |
|
|
|
355 |
!empty($message) ? $message . ' ' : '',
|
|
|
356 |
$string,
|
|
|
357 |
$pattern
|
|
|
358 |
);
|
|
|
359 |
|
|
|
360 |
if (preg_match($pattern, $string)) {
|
|
|
361 |
return $this->fail($message);
|
|
|
362 |
}
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
/**
|
|
|
366 |
* Asserts that a variable is of a given type.
|
|
|
367 |
*
|
|
|
368 |
* @param string $expected
|
|
|
369 |
* @param mixed $actual
|
|
|
370 |
* @param optional string $message
|
|
|
371 |
* @access public
|
|
|
372 |
*/
|
|
|
373 |
function assertType($expected, $actual, $message = '') {
|
|
|
374 |
return $this->assertEquals(
|
|
|
375 |
$expected,
|
|
|
376 |
gettype($actual),
|
|
|
377 |
$message
|
|
|
378 |
);
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
/**
|
|
|
382 |
* Converts a value to a string.
|
|
|
383 |
*
|
|
|
384 |
* @param mixed $value
|
|
|
385 |
* @access private
|
|
|
386 |
*/
|
|
|
387 |
function _convertToString($value) {
|
|
|
388 |
foreach ($value as $k => $v) {
|
|
|
389 |
if (is_array($v)) {
|
|
|
390 |
$value[$k] = $this->_convertToString($value[$k]);
|
|
|
391 |
} else {
|
|
|
392 |
settype($value[$k], 'string');
|
|
|
393 |
}
|
|
|
394 |
}
|
|
|
395 |
|
|
|
396 |
return $value;
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
/**
|
|
|
400 |
* @param boolean $looselyTyped
|
|
|
401 |
* @access public
|
|
|
402 |
*/
|
|
|
403 |
function setLooselyTyped($looselyTyped) {
|
|
|
404 |
if (is_bool($looselyTyped)) {
|
|
|
405 |
$this->_looselyTyped = $looselyTyped;
|
|
|
406 |
}
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
/**
|
|
|
410 |
* Fails a test with the given message.
|
|
|
411 |
*
|
|
|
412 |
* @param string
|
|
|
413 |
* @access protected
|
|
|
414 |
* @abstract
|
|
|
415 |
*/
|
|
|
416 |
function fail($message = '') { /* abstract */ }
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
/*
|
|
|
420 |
* Local variables:
|
|
|
421 |
* tab-width: 4
|
|
|
422 |
* c-basic-offset: 4
|
|
|
423 |
* c-hanging-comment-ender-p: nil
|
|
|
424 |
* End:
|
|
|
425 |
*/
|
|
|
426 |
?>
|