Skip to content

Instantly share code, notes, and snippets.

@Ostico
Last active April 11, 2016 18:59
Show Gist options
  • Save Ostico/eadfbbaabe4fd8a50e662199ea5f0f1e to your computer and use it in GitHub Desktop.
Save Ostico/eadfbbaabe4fd8a50e662199ea5f0f1e to your computer and use it in GitHub Desktop.
ObjectComparator does not check for Object Hash but internally knows that objects are different.
<?php
/**
* @author Domenico Lupinetti ostico@gmail.com
* Date: 11/04/16
* Time: 19.52
*
* The try block is executed but the comparison are made on Exporter::toArray()
* and if the object has no properties $expected and $actual are equals but refers to two different instances
* ( tried with PDO as example )
*
* @see https://github.com/sebastianbergmann/comparator/blob/master/src/ObjectComparator.php#L74
* @see https://github.com/sebastianbergmann/exporter/blob/master/src/Exporter.php#L152
*
*/
class ObjectTest extends PHPUnit_Framework_TestCase
{
public function testObjectEquality(){
$x = new PDO(
"mysql:host=mysql;dbname=mydb;charset=UTF8",
'user',
'user1',
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // Raise exceptions on errors
) );
$y = new PDO(
"mysql:host=mysql;dbname=mydb;charset=UTF8",
'user',
'user1',
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // Raise exceptions on errors
) );
echo("\n");
var_dump( spl_object_hash( $x ) );
var_dump( spl_object_hash( $y ) );
//this is expected to fail
$this->assertEquals( $x, $y ); // <--- PASS but is WRONG
//this is expected to fail
$this->assertTrue( $x === $y ); // <--- NOT PASS ( RIGHT )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment