| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PHPUnit
|
|
|
4 |
*
|
|
|
5 |
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
|
|
|
6 |
* All rights reserved.
|
|
|
7 |
*
|
|
|
8 |
* Redistribution and use in source and binary forms, with or without
|
|
|
9 |
* modification, are permitted provided that the following conditions
|
|
|
10 |
* are met:
|
|
|
11 |
*
|
|
|
12 |
* * Redistributions of source code must retain the above copyright
|
|
|
13 |
* notice, this list of conditions and the following disclaimer.
|
|
|
14 |
*
|
|
|
15 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
16 |
* notice, this list of conditions and the following disclaimer in
|
|
|
17 |
* the documentation and/or other materials provided with the
|
|
|
18 |
* distribution.
|
|
|
19 |
*
|
|
|
20 |
* * Neither the name of Sebastian Bergmann nor the names of his
|
|
|
21 |
* contributors may be used to endorse or promote products derived
|
|
|
22 |
* from this software without specific prior written permission.
|
|
|
23 |
*
|
|
|
24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
25 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
26 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
27 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
28 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
29 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
30 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
31 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
32 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
33 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
34 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
35 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
36 |
*
|
|
|
37 |
* @category Testing
|
|
|
38 |
* @package PHPUnit
|
|
|
39 |
* @author Kore Nordmann <kn@ez.no>
|
|
|
40 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
41 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
42 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
43 |
* @link http://www.phpunit.de/
|
|
|
44 |
* @since File available since Release 3.0.0
|
|
|
45 |
*/
|
|
|
46 |
|
|
|
47 |
require_once 'PHPUnit/Framework.php';
|
|
|
48 |
require_once 'PHPUnit/Util/Filter.php';
|
|
|
49 |
require_once 'PHPUnit/Util/Type.php';
|
|
|
50 |
|
|
|
51 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Constraint that checks if one value is equal to another.
|
|
|
55 |
*
|
|
|
56 |
* Equality is checked with PHP's == operator, the operator is explained in
|
|
|
57 |
* detail at {@url http://www.php.net/manual/en/types.comparisons.php}.
|
|
|
58 |
* Two values are equal if they have the same value disregarding type.
|
|
|
59 |
*
|
|
|
60 |
* The expected value is passed in the constructor.
|
|
|
61 |
*
|
|
|
62 |
* @category Testing
|
|
|
63 |
* @package PHPUnit
|
|
|
64 |
* @author Kore Nordmann <kn@ez.no>
|
|
|
65 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
66 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
67 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
68 |
* @version Release: 3.4.15
|
|
|
69 |
* @link http://www.phpunit.de/
|
|
|
70 |
* @since Class available since Release 3.0.0
|
|
|
71 |
*/
|
|
|
72 |
class PHPUnit_Framework_Constraint_IsEqual extends PHPUnit_Framework_Constraint
|
|
|
73 |
{
|
|
|
74 |
/**
|
|
|
75 |
* @var mixed
|
|
|
76 |
*/
|
|
|
77 |
protected $value;
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* @var float
|
|
|
81 |
*/
|
|
|
82 |
protected $delta = 0;
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* @var integer
|
|
|
86 |
*/
|
|
|
87 |
protected $maxDepth = 10;
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* @var boolean
|
|
|
91 |
*/
|
|
|
92 |
protected $canonicalizeEol = FALSE;
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* @var boolean
|
|
|
96 |
*/
|
|
|
97 |
protected $ignoreCase = FALSE;
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* @param mixed $value
|
|
|
101 |
* @param float $delta
|
|
|
102 |
* @param integer $maxDepth
|
|
|
103 |
* @param boolean $canonicalizeEol
|
|
|
104 |
* @param boolean $ignoreCase
|
|
|
105 |
*/
|
|
|
106 |
public function __construct($value, $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE, $ignoreCase = FALSE)
|
|
|
107 |
{
|
|
|
108 |
if (!is_numeric($delta)) {
|
|
|
109 |
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'numeric');
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if (!is_int($maxDepth)) {
|
|
|
113 |
throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'integer');
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if (!is_bool($canonicalizeEol)) {
|
|
|
117 |
throw PHPUnit_Util_InvalidArgumentHelper::factory(4, 'boolean');
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
if (!is_bool($ignoreCase)) {
|
|
|
121 |
throw PHPUnit_Util_InvalidArgumentHelper::factory(5, 'boolean');
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
$this->value = $value;
|
|
|
125 |
$this->delta = $delta;
|
|
|
126 |
$this->maxDepth = $maxDepth;
|
|
|
127 |
$this->canonicalizeEol = $canonicalizeEol;
|
|
|
128 |
$this->ignoreCase = $ignoreCase;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Evaluates the constraint for parameter $other. Returns TRUE if the
|
|
|
133 |
* constraint is met, FALSE otherwise.
|
|
|
134 |
*
|
|
|
135 |
* @param mixed $other Value or object to evaluate.
|
|
|
136 |
* @return bool
|
|
|
137 |
*/
|
|
|
138 |
public function evaluate($other)
|
|
|
139 |
{
|
|
|
140 |
return $this->recursiveComparison($this->value, $other);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* @param mixed $other The value passed to evaluate() which failed the
|
|
|
145 |
* constraint check.
|
|
|
146 |
* @param string $description A string with extra description of what was
|
|
|
147 |
* going on while the evaluation failed.
|
|
|
148 |
* @param boolean $not Flag to indicate negation.
|
|
|
149 |
* @throws PHPUnit_Framework_ExpectationFailedException
|
|
|
150 |
*/
|
|
|
151 |
public function fail($other, $description, $not = FALSE)
|
|
|
152 |
{
|
|
|
153 |
$failureDescription = $this->failureDescription(
|
|
|
154 |
$other,
|
|
|
155 |
$description,
|
|
|
156 |
$not
|
|
|
157 |
);
|
|
|
158 |
|
|
|
159 |
if (!$not) {
|
|
|
160 |
if ($this->value instanceof DOMDocument) {
|
|
|
161 |
$value = $this->domToText($this->value);
|
|
|
162 |
} else {
|
|
|
163 |
$value = $this->value;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
if ($other instanceof DOMDocument) {
|
|
|
167 |
$other = $this->domToText($other);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
throw new PHPUnit_Framework_ExpectationFailedException(
|
|
|
171 |
$failureDescription,
|
|
|
172 |
PHPUnit_Framework_ComparisonFailure::diffEqual($value, $other),
|
|
|
173 |
$description
|
|
|
174 |
);
|
|
|
175 |
} else {
|
|
|
176 |
throw new PHPUnit_Framework_ExpectationFailedException(
|
|
|
177 |
$failureDescription,
|
|
|
178 |
NULL
|
|
|
179 |
);
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Returns a string representation of the constraint.
|
|
|
185 |
*
|
|
|
186 |
* @return string
|
|
|
187 |
*/
|
|
|
188 |
public function toString()
|
|
|
189 |
{
|
|
|
190 |
$delta = '';
|
|
|
191 |
|
|
|
192 |
if (is_string($this->value)) {
|
|
|
193 |
if (strpos($this->value, "\n") !== FALSE) {
|
|
|
194 |
return 'is equal to <text>';
|
|
|
195 |
} else {
|
|
|
196 |
return sprintf(
|
|
|
197 |
'is equal to <string:%s>',
|
|
|
198 |
|
|
|
199 |
$this->value
|
|
|
200 |
);
|
|
|
201 |
}
|
|
|
202 |
} else {
|
|
|
203 |
if ($this->delta != 0) {
|
|
|
204 |
$delta = sprintf(
|
|
|
205 |
' with delta <%F>',
|
|
|
206 |
|
|
|
207 |
$this->delta
|
|
|
208 |
);
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
return sprintf(
|
|
|
212 |
'is equal to %s%s',
|
|
|
213 |
|
|
|
214 |
PHPUnit_Util_Type::toString($this->value),
|
|
|
215 |
$delta
|
|
|
216 |
);
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* Perform the actual recursive comparision of two values
|
|
|
222 |
*
|
|
|
223 |
* @param mixed $a First value
|
|
|
224 |
* @param mixed $b Second value
|
|
|
225 |
* @param int $depth Depth
|
|
|
226 |
* @return bool
|
|
|
227 |
*/
|
|
|
228 |
protected function recursiveComparison($a, $b, $depth = 0)
|
|
|
229 |
{
|
|
|
230 |
if ($a === $b) {
|
|
|
231 |
return TRUE;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
if ($depth >= $this->maxDepth) {
|
|
|
235 |
return TRUE;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
if (is_numeric($a) XOR is_numeric($b)) {
|
|
|
239 |
return FALSE;
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
if (is_array($a) XOR is_array($b)) {
|
|
|
243 |
return FALSE;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
if (is_object($a) XOR is_object($b)) {
|
|
|
247 |
return FALSE;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
if ($a instanceof SplObjectStorage XOR $b instanceof SplObjectStorage) {
|
|
|
251 |
return FALSE;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
if ($a instanceof SplObjectStorage) {
|
|
|
255 |
foreach ($a as $object) {
|
|
|
256 |
if (!$b->contains($object)) {
|
|
|
257 |
return FALSE;
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
foreach ($b as $object) {
|
|
|
262 |
if (!$a->contains($object)) {
|
|
|
263 |
return FALSE;
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
return TRUE;
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
if ($a instanceof DOMDocument || $b instanceof DOMDocument) {
|
|
|
271 |
if (!$a instanceof DOMDocument) {
|
|
|
272 |
$_a = new DOMDocument;
|
|
|
273 |
$_a->preserveWhiteSpace = FALSE;
|
|
|
274 |
$_a->loadXML($a);
|
|
|
275 |
$a = $_a;
|
|
|
276 |
unset($_a);
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
if (!$b instanceof DOMDocument) {
|
|
|
280 |
$_b = new DOMDocument;
|
|
|
281 |
$_b->preserveWhiteSpace = FALSE;
|
|
|
282 |
$_b->loadXML($b);
|
|
|
283 |
$b = $_b;
|
|
|
284 |
unset($_b);
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
if (version_compare(PHP_VERSION, '5.2.0RC1', '>=')) {
|
|
|
288 |
return ($a->C14N() == $b->C14N());
|
|
|
289 |
} else {
|
|
|
290 |
return ($a->saveXML() == $b->saveXML());
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
if (is_object($a) && is_object($b) &&
|
|
|
295 |
(get_class($a) !== get_class($b))) {
|
|
|
296 |
return FALSE;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
// Normal comparision for scalar values.
|
|
|
300 |
if ((!is_array($a) && !is_object($a)) ||
|
|
|
301 |
(!is_array($b) && !is_object($b))) {
|
|
|
302 |
if (is_numeric($a) && is_numeric($b)) {
|
|
|
303 |
// Optionally apply delta on numeric values.
|
|
|
304 |
return $this->numericComparison($a, $b);
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
if (is_string($a) && is_string($b)) {
|
|
|
308 |
if ($this->canonicalizeEol && PHP_EOL != "\n") {
|
|
|
309 |
$a = str_replace(PHP_EOL, "\n", $a);
|
|
|
310 |
$b = str_replace(PHP_EOL, "\n", $b);
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
if ($this->ignoreCase) {
|
|
|
314 |
$a = strtolower($a);
|
|
|
315 |
$b = strtolower($b);
|
|
|
316 |
}
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
return ($a == $b);
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
if (is_object($a)) {
|
|
|
323 |
$isMock = $a instanceof PHPUnit_Framework_MockObject_MockObject;
|
|
|
324 |
$a = (array)$a;
|
|
|
325 |
$b = (array)$b;
|
|
|
326 |
|
|
|
327 |
if ($isMock) {
|
|
|
328 |
unset($a["\0*\0invocationMocker"]);
|
|
|
329 |
|
|
|
330 |
if (isset($b["\0*\0invocationMocker"])) {
|
|
|
331 |
unset($b["\0*\0invocationMocker"]);
|
|
|
332 |
}
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
$keysInB = array_flip(array_keys($b));
|
|
|
337 |
|
|
|
338 |
foreach ($a as $key => $v) {
|
|
|
339 |
if (!isset($keysInB[$key])) {
|
|
|
340 |
// Abort on missing key in $b.
|
|
|
341 |
return FALSE;
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
if (!$this->recursiveComparison($a[$key], $b[$key], $depth + 1)) {
|
|
|
345 |
// FALSE, if child comparision fails.
|
|
|
346 |
return FALSE;
|
|
|
347 |
}
|
|
|
348 |
|
|
|
349 |
// Unset key to check whether all keys of b are compared.
|
|
|
350 |
unset($b[$key]);
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
if (count($b)) {
|
|
|
354 |
// There is something in $b, that is missing in $a.
|
|
|
355 |
return FALSE;
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
return TRUE;
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
/**
|
|
|
362 |
* Compares two numeric values - use delta if applicable.
|
|
|
363 |
*
|
|
|
364 |
* @param mixed $a
|
|
|
365 |
* @param mixed $b
|
|
|
366 |
* @return bool
|
|
|
367 |
*/
|
|
|
368 |
protected function numericComparison($a, $b)
|
|
|
369 |
{
|
|
|
370 |
if ($this->delta === FALSE) {
|
|
|
371 |
return ($a == $b);
|
|
|
372 |
} else {
|
|
|
373 |
return (abs($a - $b) <= $this->delta);
|
|
|
374 |
}
|
|
|
375 |
}
|
|
|
376 |
|
|
|
377 |
/**
|
|
|
378 |
* Returns the normalized, whitespace-cleaned, and indented textual
|
|
|
379 |
* representation of a DOMDocument.
|
|
|
380 |
*
|
|
|
381 |
* @param DOMDocument $document
|
|
|
382 |
* @return string
|
|
|
383 |
*/
|
|
|
384 |
protected function domToText(DOMDocument $document)
|
|
|
385 |
{
|
|
|
386 |
$document->formatOutput = TRUE;
|
|
|
387 |
$document->normalizeDocument();
|
|
|
388 |
|
|
|
389 |
return $document->saveXML();
|
|
|
390 |
}
|
|
|
391 |
}
|
|
|
392 |
?>
|