PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Testing » PHPUnit2 » Bug #7707

assertEquals does not ignore type in arrays and objects

Details

Submitted2006-05-23 15:49 UTC
Fromkore at php dot net
Assignedsebastian
StatusClosed
PackagePHPUnit2
PHP Version5.1.4
OSLinux 2.6.16-gentoo-r3
Roadmaps(Not assigned)

Comments

[2006-05-23 15:49 UTC] kore at php dot net

Description:
------------
I would expect PHPUnit to ignore the type of array values, or object properties, as it does for simple values using assertEquals().

This behaviour makes it more difficult to compare structs (represented by an object or array).

Normally this is no problem because you would be able to access dedicated properties or values to check their equality. But this is not the case, if you this check cmpined with mockObjects like:
$mockObject
->expects( $this->once() )
->method( 'method' )
->with(
$this->equalTo( new struct( 1., 2 ) )
);

Test script:
---------------
http://kore.phpugdo.de/equal_to.phps

Expected result:
----------------
PHPUnit 3.0.0alpha3 by Sebastian Bergmann.

..

Time: 00:00

OK (2 tests)

Actual result:
--------------
PHPUnit 3.0.0alpha3 by Sebastian Bergmann.

FF.

Time: 00:00
There were 2 failures:
1) testEqualsStruct(testEqual)
Structs are not equalfailed asserting that <struct::__set_state(array(
'a' => 1,
'b' => 2,
))> is equal to <object:struct::__set_state(array(
'a' => 1,
'b' => 2,
))>
in object of class <struct>:
property <a>: expected integer <1>
got double <1>
/home/kore/devel/ezcomponents/trunk/Graph/tests/equal_to.php:27
/usr/lib/php/php5.1-cvs/bin/phpunit:40
2) testEqualsArray(testEqual)
Arrays are not equalfailed asserting that <array (
0 => 1,
1 => 2,
)> is equal to <array:array (
0 => 1,
1 => 2,
)>
array key <0>: expected integer <1>
got double <1>

/home/kore/devel/ezcomponents/trunk/Graph/tests/equal_to.php:42
/usr/lib/php/php5.1-cvs/bin/phpunit:40

FAILURES!
Tests: 3, Failures: 2.

[2006-05-23 15:54 UTC] kore at php dot net

The "Actual result" has a minor error, as it shows the result from a slightly modified test, which also had a method which simply tested:
> $this->assertSame( 1., 1);

[2006-05-23 18:10 UTC] sebastian at php dot net

Please test the patch below:

Index: Framework/Constraint/IsEqual.php
===================================================================
RCS file: /repository/pear/PHPUnit2/Framework/Constraint/IsEqual.php,v
retrieving revision 1.8
diff -u -B -r1.8 IsEqual.php
--- Framework/Constraint/IsEqual.php 30 Mar 2006 09:25:07 -0000 1.8
+++ Framework/Constraint/IsEqual.php 23 May 2006 18:09:25 -0000
@@ -99,29 +99,50 @@
*/
public function evaluate($other)
{
- $equal = FALSE;
-
if (is_array($this->value)) {
if (is_array($other)) {
- $other = PHPUnit2_Util_Array::sortRecursively($other);
- $value = PHPUnit2_Util_Array::sortRecursively($this->value);
- $equal = (serialize($value) == serialize($other));
+ $other = new RecursiveIteratorIterator(
+ new RecursiveArrayIterator(
+ PHPUnit2_Util_Array::sortRecursively($other)
+ ),
+ RecursiveIteratorIterator::SELF_FIRST
+ );
+
+ $value = new RecursiveIteratorIterator(
+ new RecursiveArrayIterator(
+ PHPUnit2_Util_Array::sortRecursively($this->value)
+ ),
+ RecursiveIteratorIterator::SELF_FIRST
+ );
+
+ $other->rewind();
+ $value->rewind();
+
+ while ($other->valid() && $value->valid()) {
+ if ($other->key() != $value->key() ||
+ $other->current() != $value->current()) {
+ return FALSE;
+ }
+
+ $other->next();
+ $value->next();
+ }
+
+ return TRUE;
}
}

else if (is_float($this->value) && is_float($other) && is_float($this->delta)) {
- $equal = (abs($this->value - $other) <= $this->delta);
+ return (abs($this->value - $other) <= $this->delta);
}

else if (is_object($this->value) && is_object($other)) {
- $equal = serialize($this->value) == serialize($other);
+ return serialize($this->value) == serialize($other);
}

else {
- $equal = $this->value == $other;
+ return $this->value == $other;
}
-
- return $equal;
}

/**

[2006-05-23 18:48 UTC] kore at php dot net

With this patch it now works for arrays, but still not for objects.

[2006-05-25 11:07 UTC] sebastian at php dot net

This bug has been fixed in CVS.

If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET).

If this was a problem with the pear.php.net website, the change should be live shortly.

Otherwise, the fix will appear in the package's next release.

Thank you for the report and for helping us make PEAR better.