-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Spl_object_hash conflict on Merge #1465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
54bebab
8ef5207
3c1d2d3
6568f22
f4215ce
07b4a24
06b3ae2
ce8bb13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ORM\Functional; | ||
|
||
use Doctrine\ORM\Query; | ||
use Doctrine\Tests\Models\CMS\CmsUser; | ||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
class MergeSplHashConflictTest extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
$this->useModelSet('cms'); | ||
parent::setUp(); | ||
} | ||
|
||
|
||
/** | ||
* @test | ||
* @covers Doctrine\ORM\UnitOfWork::forgetEntity | ||
*/ | ||
public function testForgetEntity() | ||
{ | ||
$user = new CmsUser; | ||
$user->username = 'bilouwan'; | ||
$user->name = 'moroine'; | ||
$hash = spl_object_hash($user); | ||
$this->_em->persist($user); | ||
$this->_em->flush(); | ||
|
||
$uow = $this->_em->getUnitOfWork(); | ||
$reflexion = new \ReflectionClass(get_class($uow)); | ||
$method = $reflexion->getMethod('forgetEntity'); | ||
$method->setAccessible(true); | ||
$method->invoke($uow, $user); | ||
|
||
$propertyNames = [ | ||
'entityInsertions', | ||
'entityUpdates', | ||
'entityDeletions', | ||
'entityIdentifiers', | ||
'entityStates', | ||
'originalEntityData', | ||
]; | ||
|
||
foreach ($propertyNames as $propertyName) { | ||
$property = $reflexion->getProperty($propertyName); | ||
$property->setAccessible(true); | ||
$originalEntityData = $property->getValue($uow); | ||
|
||
// As $user is unset, $hash could be reused by spl_object_hash | ||
$this->assertArrayNotHasKey($hash, $originalEntityData); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers Doctrine\ORM\UnitOfWork::merge | ||
*/ | ||
public function testMergeSplHashConflict() | ||
{ | ||
// Create | ||
$user = new CmsUser; | ||
$hash = spl_object_hash($user); | ||
|
||
$mergedUser = $this->_em->merge($user); | ||
unset($user); | ||
|
||
$uow = $this->_em->getUnitOfWork(); | ||
$reflexion = new \ReflectionClass(get_class($uow)); | ||
$property = $reflexion->getProperty('originalEntityData'); | ||
$property->setAccessible(true); | ||
$originalEntityData = $property->getValue($uow); | ||
|
||
// As $user is unset, $hash could be reused by spl_object_hash | ||
$this->assertArrayNotHasKey($hash, $originalEntityData); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The object still exists in memory here, since it is merged into the UoW. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of what I understood, the $user reference is never kept by the merge operation. As the bug that I uncounter is the fact my $user doesn't exists anymore and then I try to merge a new object but the uow kept old hash in their originalEntityData array. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't
self::assertSame($user, $mergedUser) here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should assert that $user and $mergedUser are two different object (by there reference) as merge operation should return a managed copy of input object but the input object has to remain unmanaged ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We couldn't assertSame as for object assertSame check if the two variables reference the same object. In the case of merge, it will return a copy so it's another object