From 6a2b7c2a8e869cf750d464abf2e83113c61c74fd Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 24 Jan 2015 12:20:33 +0100 Subject: [PATCH] #1169 DDC-3343 - correcting query count assertions on extra-lazy specific tests (some DELETE operations became UPDATE operations) --- .../Collection/OneToManyPersister.php | 5 ++++ .../Functional/ExtraLazyCollectionTest.php | 27 +++++-------------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/Collection/OneToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/OneToManyPersister.php index fb376033c31..9dc2fad548d 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/OneToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/OneToManyPersister.php @@ -20,6 +20,7 @@ namespace Doctrine\ORM\Persisters\Collection; use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Proxy\Proxy; use Doctrine\ORM\PersistentCollection; /** @@ -165,6 +166,10 @@ public function removeElement(PersistentCollection $collection, $element) $targetMetadata = $this->em->getClassMetadata($mapping['targetEntity']); + if ($element instanceof Proxy && ! $element->__isInitialized()) { + $element->__load(); + } + // clearing owning side value $targetMetadata->reflFields[$mapping['mappedBy']]->setValue($element, null); diff --git a/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php b/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php index 2807ccfd7c1..2aa7241a9c7 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php @@ -480,7 +480,8 @@ public function testRemoveElementOneToMany() $user->articles->removeElement($article); $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized."); - $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount()); + // NOTE: +2 queries because CmsArticle is a versioned entity, and that needs to be handled accordingly + $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount()); // Test One to Many removal with Entity state as new $article = new \Doctrine\Tests\Models\CMS\CmsArticle(); @@ -501,7 +502,7 @@ public function testRemoveElementOneToMany() $user->articles->removeElement($article); - $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed."); + $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a persisted entity will not cause queries when the owning side doesn't actually change."); $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized."); // Test One to Many removal with Entity state as managed @@ -532,17 +533,10 @@ public function testRemovalOfManagedElementFromOneToManyJoinedInheritanceCollect $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.'); - $expectedQueryCount = $queryCount + 1; - - if (! $this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { - // the ORM emulates cascades in a JTI if the underlying platform does not support them - $expectedQueryCount = $queryCount + 2; - }; - $this->assertEquals( - $expectedQueryCount, + $queryCount + 1, $this->getCurrentQueryCount(), - 'One removal per table in the JTI has been executed' + 'The owning side of the association is updated' ); $this->assertFalse($otherClass->childClasses->contains($childClass)); @@ -601,17 +595,10 @@ public function testRemovalOfNewManagedElementFromOneToManyJoinedInheritanceColl $otherClass->childClasses->removeElement($childClass); - $expectedQueryCount = $queryCount + 1; - - if (! $this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { - // the ORM emulates cascades in a JTI if the underlying platform does not support them - $expectedQueryCount = $queryCount + 2; - }; - $this->assertEquals( - $expectedQueryCount, + $queryCount, $this->getCurrentQueryCount(), - 'Removing a persisted entity should cause two queries to be executed.' + 'No queries are executed, as the owning side of the association is not actually updated.' ); $this->assertFalse($otherClass->childClasses->isInitialized(), 'Collection is not initialized.'); }