Skip to content

Commit

Permalink
clone object in hashCode() function so the original object is not cha…
Browse files Browse the repository at this point in the history
…nged when calling hashCode

provided test for the hasCode() function
  • Loading branch information
Daniel Tschinder committed May 3, 2013
1 parent 3ea2f2d commit 0272ba3
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runtime/lib/om/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public function hashCode()
return crc32(serialize($this->getPrimaryKey()));
}

return crc32(serialize($this));
return crc32(serialize(clone $this));
}

/**
Expand Down
100 changes: 100 additions & 0 deletions test/testsuite/runtime/om/BaseObjectHashCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';

/**
* Test class for BaseObject serialization.
*
* @author Francois Zaninotto
* @version $Id: PropelCollectionTest.php 1348 2009-12-03 21:49:00Z francois $
* @package runtime.om
* @group hashcode
*/
class BaseObjectHashCodeTest extends BookstoreTestBase
{
public function testUnsavedObjectCallingHashCodeIsNotChangingObject()
{
$book1 = new Book();
$book1->setTitle('Foo5');
$book1->setISBN('1234');

$author = new Author();
$author->setFirstName('JAne');
$author->setLastName('JAne');
$author->addBook($book1);

$a = clone $author;
$a->hashCode();

$this->assertEquals($a, $author);
}

public function testSavedObjectCallingHashCodeIsNotChangingObject()
{
$book1 = new Book();
$book1->setTitle('Foo5');
$book1->setISBN('1234');

$author = new Author();
$author->setFirstName('JAne');
$author->setLastName('JAne');
$author->addBook($book1);
$author->save();

$a = clone $author;
$a->hashCode();

$this->assertEquals($a, $author);
}

public function testUnsavedObjectCreatesSameHashForIdenticalObjects()
{
$book1 = new Book();
$book1->setTitle('Foo5');
$book1->setISBN('1234');

$author1 = new Author();
$author1->setFirstName('JAne');
$author1->setLastName('JAne');
$author1->addBook($book1);

$author2 = new Author();
$author2->setFirstName('JAne');
$author2->setLastName('JAne');
$author2->addBook($book1);

$this->assertEquals($author1->hashCode(), $author2->hashCode());
}

/**
* Primary key should differ
*/
public function testSavedObjectCreatesDifferentHashForIdenticalObjects()
{
$book1 = new Book();
$book1->setTitle('Foo5');
$book1->setISBN('1234');

$author1 = new Author();
$author1->setFirstName('JAne');
$author1->setLastName('JAne');
$author1->addBook($book1);
$author1->save();

$author2 = new Author();
$author2->setFirstName('JAne');
$author2->setLastName('JAne');
$author2->addBook($book1);
$author2->save();

$this->assertNotEquals($author1->hashCode(), $author2->hashCode());
}
}

0 comments on commit 0272ba3

Please sign in to comment.