Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Separate source files for DualIterator and RecursiveDualIterator. Inc…

…ludes a patch by Kore Nordmann <mail@kore-nordmann.de> for the DualIterator implementation for comparison in PHPUnit_Framework_Constraint_IsEqual.
  • Loading branch information...
commit a90a575a3d2bfe3b2d43c85acff63f89f116a627 1 parent 53d21fa
@sebastianbergmann authored
View
168 PHPUnit/Framework/Constraint/IsEqual.php
@@ -49,164 +49,11 @@
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Util/Array.php';
+require_once 'PHPUnit/Util/DualIterator.php';
require_once 'PHPUnit/Util/Filter.php';
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
-if (!class_exists('DualIterator', FALSE)) {
-class DualIterator implements Iterator
-{
- const CURRENT_LHS = 0x01;
- const CURRENT_RHS = 0x02;
- const CURRENT_ARRAY = 0x03;
- const CURRENT_0 = 0x00;
-
- const KEY_LHS = 0x10;
- const KEY_RHS = 0x20;
- const KEY_ARRAY = 0x30;
- const KEY_0 = 0x00;
-
- const DEFAULT_FLAGS = 0x33;
-
- private $lhs;
- private $rhs;
- private $flags;
-
- public function __construct(Iterator $lhs, Iterator $rhs, $flags = 0x33)
- {
- $this->lhs = $lhs;
- $this->rhs = $rhs;
- $this->flags = $flags;
- }
-
- public function getLHS()
- {
- return $this->lhs;
- }
-
- public function getRHS()
- {
- return $this->rhs;
- }
-
- public function setFlags($flags)
- {
- $this->flags = $flags;
- }
-
- public function getFlags()
- {
- return $this->flags;
- }
-
- public function rewind()
- {
- $this->lhs->rewind();
- $this->rhs->rewind();
- }
-
- public function valid()
- {
- return $this->lhs->valid() && $this->rhs->valid();
- }
-
- public function current()
- {
- switch($this->flags & 0x0F) {
- default:
- case self::CURRENT_ARRAY: {
- return array($this->lhs->current(), $this->rhs->current());
- }
-
- case self::CURRENT_LHS: {
- return $this->lhs->current();
- }
-
- case self::CURRENT_RHS: {
- return $this->rhs->current();
- }
-
- case self::CURRENT_0: {
- return NULL;
- }
- }
- }
-
- public function key()
- {
- switch($this->flags & 0xF0) {
- default:
- case self::CURRENT_ARRAY: {
- return array($this->lhs->key(), $this->rhs->key());
- }
-
- case self::CURRENT_LHS: {
- return $this->lhs->key();
- }
-
- case self::CURRENT_RHS: {
- return $this->rhs->key();
- }
-
- case self::CURRENT_0: {
- return NULL;
- }
- }
- }
-
- public function next()
- {
- $this->lhs->next();
- $this->rhs->next();
- }
-
- public function areIdentical()
- {
- return $this->valid()
- ? $this->lhs->current() === $this->rhs->current()
- && $this->lhs->key() === $this->rhs->key()
- : $this->lhs->valid() == $this->rhs->valid();
- }
-
- public function areEqual()
- {
- return $this->valid()
- ? $this->lhs->current() == $this->rhs->current()
- && $this->lhs->key() == $this->rhs->key()
- : $this->lhs->valid() == $this->rhs->valid();
- }
-
- public static function compareIterators(Iterator $lhs, Iterator $rhs, $identical = FALSE)
- {
- if ($lhs instanceof RecursiveIterator) {
- if ($rhs instanceof RecursiveIterator) {
- $it = new RecursiveDualIterator($lhs, $rhs, self::CURRENT_0 | self::KEY_0);
- } else {
- return FALSE;
- }
- } else {
- $it = new DualIterator($lhs, $rhs, self::CURRENT_0 | self::KEY_0);
- }
-
- if ($identical) {
- foreach (new RecursiveIteratorIterator($it) as $n) {
- if (!$it->areIdentical()) {
- return FALSE;
- }
- }
- } else {
- foreach ($it as $n) {
- if (!$it->areEqual()) {
- return FALSE;
- }
- }
- }
-
- return $identical ? $it->areIdentical() : $it->areEqual();
- }
-}
-}
-
/**
* Constraint which checks if one value is equal to another.
*
@@ -246,24 +93,27 @@ public function __construct($value, $delta = 0)
*/
public function evaluate($other)
{
- if (is_array($this->value) && is_array($other)) {
+ if ((is_array($this->value) && is_array($other)) ||
+ (is_object($this->value) && is_object($other))) {
if (count($this->value) != count($other)) {
return FALSE;
}
- return DualIterator::compareIterators(
+ return PHPUnit_Util_DualIterator::compareIterators(
new RecursiveIteratorIterator(
new RecursiveArrayIterator(
- PHPUnit_Util_Array::sortRecursively($this->value)
+ PHPUnit_Util_Array::sortRecursively((array) $this->value)
),
RecursiveIteratorIterator::SELF_FIRST
),
new RecursiveIteratorIterator(
new RecursiveArrayIterator(
- PHPUnit_Util_Array::sortRecursively($other)
+ PHPUnit_Util_Array::sortRecursively((array) $other)
),
RecursiveIteratorIterator::SELF_FIRST
- )
+ ),
+ FALSE,
+ $this->delta
);
}
View
233 PHPUnit/Util/DualIterator.php
@@ -0,0 +1,233 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2006, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright 2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version SVN: $Id$
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.0.0
+ */
+
+require_once 'PHPUnit/Util/Filter.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ *
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Marcus Börger <helly@php.net>
+ * @author Kore Nordmann <mail@kore-nordmann.de>
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright 2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: @package_version@
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.0.0
+ */
+class PHPUnit_Util_DualIterator implements Iterator
+{
+ const CURRENT_LHS = 0x01;
+ const CURRENT_RHS = 0x02;
+ const CURRENT_ARRAY = 0x03;
+ const CURRENT_0 = 0x00;
+
+ const KEY_LHS = 0x10;
+ const KEY_RHS = 0x20;
+ const KEY_ARRAY = 0x30;
+ const KEY_0 = 0x00;
+
+ const DEFAULT_FLAGS = 0x33;
+
+ private $lhs;
+ private $rhs;
+ private $flags;
+
+ public function __construct(Iterator $lhs, Iterator $rhs, $flags = 0x33)
+ {
+ $this->lhs = $lhs;
+ $this->rhs = $rhs;
+ $this->flags = $flags;
+ }
+
+ public function getLHS()
+ {
+ return $this->lhs;
+ }
+
+ public function getRHS()
+ {
+ return $this->rhs;
+ }
+
+ public function setFlags($flags)
+ {
+ $this->flags = $flags;
+ }
+
+ public function getFlags()
+ {
+ return $this->flags;
+ }
+
+ public function rewind()
+ {
+ $this->lhs->rewind();
+ $this->rhs->rewind();
+ }
+
+ public function valid()
+ {
+ return $this->lhs->valid() && $this->rhs->valid();
+ }
+
+ public function current()
+ {
+ switch($this->flags & 0x0F) {
+ default:
+ case self::CURRENT_ARRAY: {
+ return array($this->lhs->current(), $this->rhs->current());
+ }
+
+ case self::CURRENT_LHS: {
+ return $this->lhs->current();
+ }
+
+ case self::CURRENT_RHS: {
+ return $this->rhs->current();
+ }
+
+ case self::CURRENT_0: {
+ return NULL;
+ }
+ }
+ }
+
+ public function key()
+ {
+ switch($this->flags & 0xF0) {
+ default:
+ case self::CURRENT_ARRAY: {
+ return array($this->lhs->key(), $this->rhs->key());
+ }
+
+ case self::CURRENT_LHS: {
+ return $this->lhs->key();
+ }
+
+ case self::CURRENT_RHS: {
+ return $this->rhs->key();
+ }
+
+ case self::CURRENT_0: {
+ return NULL;
+ }
+ }
+ }
+
+ public function next()
+ {
+ $this->lhs->next();
+ $this->rhs->next();
+ }
+
+ public function areIdentical()
+ {
+ return $this->valid()
+ ? $this->lhs->current() === $this->rhs->current()
+ && $this->lhs->key() === $this->rhs->key()
+ : $this->lhs->valid() == $this->rhs->valid();
+ }
+
+ public function areEqual($delta)
+ {
+ return $this->valid()
+ ? $this->lhs->key() == $this->rhs->key()
+ && ( $this->lhs->current() == $this->rhs->current()
+ || ( is_float($this->lhs->current())
+ && is_float($this->rhs->current())
+ && abs($this->lhs->current() - $this->rhs->current()) < $delta
+ )
+ )
+ : $this->lhs->valid() == $this->rhs->valid();
+ }
+
+ public static function compareIterators(Iterator $lhs, Iterator $rhs, $identical = FALSE, $delta = .0)
+ {
+ if ($lhs instanceof RecursiveIterator) {
+ if ($rhs instanceof RecursiveIterator) {
+ $it = new PHPUnit_Util_RecursiveDualIterator($lhs, $rhs, self::CURRENT_0 | self::KEY_0);
+ } else {
+ return FALSE;
+ }
+ } else {
+ $it = new PHPUnit_Util_DualIterator($lhs, $rhs, self::CURRENT_0 | self::KEY_0);
+ }
+
+ if ($identical) {
+ foreach (new RecursiveIteratorIterator($it) as $n) {
+ if (!$it->areIdentical()) {
+ return FALSE;
+ }
+ }
+ } else {
+ foreach ($it as $n) {
+ if (!$it->areEqual($delta)) {
+ return FALSE;
+ }
+ }
+ }
+
+ return $identical ? $it->areIdentical() : $it->areEqual($delta);
+ }
+}
+
+require_once 'PHPUnit/Util/RecursiveDualIterator.php';
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
View
113 PHPUnit/Util/RecursiveDualIterator.php
@@ -0,0 +1,113 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2006, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright 2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version SVN: $Id$
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.0.0
+ */
+
+require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Util/DualIterator.php';
+
+PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
+
+/**
+ *
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Marcus Börger <helly@php.net>
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright 2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: @package_version@
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.0.0
+ */
+class PHPUnit_Util_RecursiveDualIterator extends PHPUnit_Util_DualIterator implements RecursiveIterator
+{
+ private $ref;
+
+ public function __construct(RecursiveIterator $lhs, RecursiveIterator $rhs, $flags = 0x33)
+ {
+ parent::__construct($lhs, $rhs, $flags);
+ }
+
+ public function hasChildren()
+ {
+ return $this->getLHS()->hasChildren() && $this->getRHS()->hasChildren();
+ }
+
+ public function getChildren()
+ {
+ if (empty($this->ref))
+ {
+ $this->ref = new ReflectionClass($this);
+ }
+
+ return $this->ref->newInstance(
+ $this->getLHS()->getChildren(),
+ $this->getRHS()->getChildren(),
+ $this->getFlags()
+ );
+ }
+
+ public function areIdentical()
+ {
+ return $this->getLHS()->hasChildren() === $this->getRHS()->hasChildren() && parent::areIdentical();
+ }
+
+ public function areEqual($delta)
+ {
+ return $this->getLHS()->hasChildren() === $this->getRHS()->hasChildren() && parent::areEqual($delta);
+ }
+}
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
+?>
View
6 package.xml
@@ -697,6 +697,9 @@ Internal Changes
<file name="Array.php">
<replace type="package-info" from="@package_version@" to="version"/>
</file>
+ <file name="DualIterator.php">
+ <replace type="package-info" from="@package_version@" to="version"/>
+ </file>
<file name="ErrorHandler.php">
<replace type="package-info" from="@package_version@" to="version"/>
</file>
@@ -712,6 +715,9 @@ Internal Changes
<file name="Getopt.php">
<replace type="package-info" from="@package_version@" to="version"/>
</file>
+ <file name="RecursiveDualIterator.php">
+ <replace type="package-info" from="@package_version@" to="version"/>
+ </file>
<file name="Report.php">
<replace type="package-info" from="@package_version@" to="version"/>
</file>
Please sign in to comment.
Something went wrong with that request. Please try again.