5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
38 * @subpackage Framework_Constraint
39 * @author Kore Nordmann <kn@ez.no>
40 * @author Sebastian Bergmann <sebastian@phpunit.de>
41 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.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
48 * Constraint that checks if one value is equal to another.
50 * Equality is checked with PHP's == operator, the operator is explained in
51 * detail at {@url http://www.php.net/manual/en/types.comparisons.php}.
52 * Two values are equal if they have the same value disregarding type.
54 * The expected value is passed in the constructor.
57 * @subpackage Framework_Constraint
58 * @author Kore Nordmann <kn@ez.no>
59 * @author Sebastian Bergmann <sebastian@phpunit.de>
60 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
61 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
62 * @version Release: 3.5.13
63 * @link http://www.phpunit.de/
64 * @since Class available since Release 3.0.0
66 class PHPUnit_Framework_Constraint_IsEqual extends PHPUnit_Framework_Constraint
81 protected $maxDepth = 10;
86 protected $canonicalize = FALSE;
91 protected $ignoreCase = FALSE;
96 * @param integer $maxDepth
97 * @param boolean $canonicalize
98 * @param boolean $ignoreCase
100 public function __construct($value, $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
102 if (!is_numeric($delta)) {
103 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'numeric');
106 if (!is_int($maxDepth)) {
107 throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'integer');
110 if (!is_bool($canonicalize)) {
111 throw PHPUnit_Util_InvalidArgumentHelper::factory(4, 'boolean');
114 if (!is_bool($ignoreCase)) {
115 throw PHPUnit_Util_InvalidArgumentHelper::factory(5, 'boolean');
118 $this->value = $value;
119 $this->delta = $delta;
120 $this->maxDepth = $maxDepth;
121 $this->canonicalize = $canonicalize;
122 $this->ignoreCase = $ignoreCase;
126 * Evaluates the constraint for parameter $other. Returns TRUE if the
127 * constraint is met, FALSE otherwise.
129 * @param mixed $other Value or object to evaluate.
132 public function evaluate($other)
134 return $this->recursiveComparison($this->value, $other);
138 * @param mixed $other The value passed to evaluate() which failed the
140 * @param string $description A string with extra description of what was
141 * going on while the evaluation failed.
142 * @param boolean $not Flag to indicate negation.
143 * @throws PHPUnit_Framework_ExpectationFailedException
145 public function fail($other, $description, $not = FALSE)
147 $failureDescription = $this->failureDescription(
154 if ($this->value instanceof DOMDocument) {
155 $value = $this->domToText($this->value);
157 $value = $this->value;
160 if ($other instanceof DOMDocument) {
161 $other = $this->domToText($other);
164 throw new PHPUnit_Framework_ExpectationFailedException(
166 PHPUnit_Framework_ComparisonFailure::diffEqual($value, $other),
170 throw new PHPUnit_Framework_ExpectationFailedException(
178 * Returns a string representation of the constraint.
182 public function toString()
186 if (is_string($this->value)) {
187 if (strpos($this->value, "\n") !== FALSE) {
188 return 'is equal to <text>';
191 'is equal to <string:%s>',
197 if ($this->delta != 0) {
208 PHPUnit_Util_Type::toString($this->value),
215 * Perform the actual recursive comparision of two values
217 * @param mixed $a First value
218 * @param mixed $b Second value
219 * @param int $depth Depth
222 protected function recursiveComparison($a, $b, $depth = 0)
228 if ($depth >= $this->maxDepth) {
232 if (is_numeric($a) XOR is_numeric($b)) {
236 if (is_array($a) XOR is_array($b)) {
240 if (is_object($a) XOR is_object($b)) {
244 if ($a instanceof SplObjectStorage XOR $b instanceof SplObjectStorage) {
248 if ($a instanceof SplObjectStorage) {
249 foreach ($a as $object) {
250 if (!$b->contains($object)) {
255 foreach ($b as $object) {
256 if (!$a->contains($object)) {
264 if ($a instanceof DOMDocument || $b instanceof DOMDocument) {
265 if (!$a instanceof DOMDocument) {
266 $_a = new DOMDocument;
267 $_a->preserveWhiteSpace = FALSE;
273 if (!$b instanceof DOMDocument) {
274 $_b = new DOMDocument;
275 $_b->preserveWhiteSpace = FALSE;
281 return $a->C14N() == $b->C14N();
284 if (is_object($a) && is_object($b) &&
285 (get_class($a) !== get_class($b))) {
289 // Normal comparision for scalar values.
290 if ((!is_array($a) && !is_object($a)) ||
291 (!is_array($b) && !is_object($b))) {
292 if (is_numeric($a) && is_numeric($b)) {
293 // Optionally apply delta on numeric values.
294 return $this->numericComparison($a, $b);
297 if (is_string($a) && is_string($b)) {
298 if ($this->canonicalize && PHP_EOL != "\n") {
299 $a = str_replace(PHP_EOL, "\n", $a);
300 $b = str_replace(PHP_EOL, "\n", $b);
303 if ($this->ignoreCase) {
313 $isMock = $a instanceof PHPUnit_Framework_MockObject_MockObject;
318 unset($a["\0*\0invocationMocker"]);
320 if (isset($b["\0*\0invocationMocker"])) {
321 unset($b["\0*\0invocationMocker"]);
326 if ($this->canonicalize) {
331 $keysInB = array_flip(array_keys($b));
333 foreach ($a as $key => $v) {
334 if (!isset($keysInB[$key])) {
335 // Abort on missing key in $b.
339 if (!$this->recursiveComparison($a[$key], $b[$key], $depth + 1)) {
340 // FALSE, if child comparision fails.
344 // Unset key to check whether all keys of b are compared.
349 // There is something in $b, that is missing in $a.
357 * Compares two numeric values - use delta if applicable.
363 protected function numericComparison($a, $b)
365 if ($this->delta === FALSE) {
368 return (abs($a - $b) <= $this->delta);
373 * Returns the normalized, whitespace-cleaned, and indented textual
374 * representation of a DOMDocument.
376 * @param DOMDocument $document
379 protected function domToText(DOMDocument $document)
381 $document->formatOutput = TRUE;
382 $document->normalizeDocument();
384 return $document->saveXML();