Permalink
Browse files

Fixed DDC-1608. Non-initialized PersistentCollection methods removeEl…

…ement and contains now deal correctly with managed entities.
  • Loading branch information...
1 parent c1012f7 commit d39760ba49418f42f235e9befe223896f11de4bd @guilhermeblanco guilhermeblanco committed Jan 18, 2012
@@ -253,10 +253,15 @@ public function contains(PersistentCollection $coll, $element)
{
$uow = $this->_em->getUnitOfWork();
- // shortcut for new entities
+ // Shortcut for new entities
$entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
- if ($entityState === UnitOfWork::STATE_NEW ||
- ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element))) {
+
+ if ($entityState === UnitOfWork::STATE_NEW) {
+ return false;
+ }
+
+ // Entity is scheduled for inclusion
+ if ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element)) {
return false;
}
@@ -277,7 +282,15 @@ public function removeElement(PersistentCollection $coll, $element)
$uow = $this->_em->getUnitOfWork();
// shortcut for new entities
- if ($uow->getEntityState($element, UnitOfWork::STATE_NEW) === UnitOfWork::STATE_NEW) {
+ $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
+
+ if ($entityState === UnitOfWork::STATE_NEW) {
+ return false;
+ }
+
+ // If Entity is scheduled for inclusion, it is not in this collection.
+ // We can assure that because it would have return true before on array check
+ if ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element)) {
return false;
}
@@ -159,7 +159,14 @@ public function contains(PersistentCollection $coll, $element)
$uow = $this->_em->getUnitOfWork();
// shortcut for new entities
- if ($uow->getEntityState($element, UnitOfWork::STATE_NEW) == UnitOfWork::STATE_NEW) {
+ $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
+
+ if ($entityState === UnitOfWork::STATE_NEW) {
+ return false;
+ }
+
+ // Entity is scheduled for inclusion
+ if ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element)) {
return false;
}
@@ -168,7 +175,7 @@ public function contains(PersistentCollection $coll, $element)
// only works with single id identifier entities. Will throw an
// exception in Entity Persisters if that is not the case for the
// 'mappedBy' field.
- $id = current( $uow->getEntityIdentifier($coll->getOwner()) );
+ $id = current( $uow->getEntityIdentifier($coll->getOwner()));
return $persister->exists($element, array($mapping['mappedBy'] => $id));
}
@@ -183,7 +190,15 @@ public function removeElement(PersistentCollection $coll, $element)
$uow = $this->_em->getUnitOfWork();
// shortcut for new entities
- if ($uow->getEntityState($element, UnitOfWork::STATE_NEW) == UnitOfWork::STATE_NEW) {
+ $entityState = $uow->getEntityState($element, UnitOfWork::STATE_NEW);
+
+ if ($entityState === UnitOfWork::STATE_NEW) {
+ return false;
+ }
+
+ // If Entity is scheduled for inclusion, it is not in this collection.
+ // We can assure that because it would have return true before on array check
+ if ($entityState === UnitOfWork::STATE_MANAGED && $uow->isScheduledForInsert($element)) {
return false;
}
@@ -64,7 +64,7 @@ public function testCountNotInitializesCollection()
/**
* @group DDC-546
*/
- public function testCountWhenNewEntitysPresent()
+ public function testCountWhenNewEntityPresent()
{
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
@@ -223,13 +223,15 @@ public function testContainsOneToMany()
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
$this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
- $article = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $this->articleId);
-
+ // Test One to Many existance retrieved from DB
+ $article = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $this->articleId);
$queryCount = $this->getCurrentQueryCount();
+
$this->assertTrue($user->articles->contains($article));
$this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
+ // Test One to Many existance with state new
$article = new \Doctrine\Tests\Models\CMS\CmsArticle();
$article->topic = "Testnew";
$article->text = "blub";
@@ -238,12 +240,26 @@ public function testContainsOneToMany()
$this->assertFalse($user->articles->contains($article));
$this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
+ // Test One to Many existance with state clear
$this->_em->persist($article);
$this->_em->flush();
$queryCount = $this->getCurrentQueryCount();
$this->assertFalse($user->articles->contains($article));
- $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
+ $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
+ $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
+
+ // Test One to Many existance with state managed
+ $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
+ $article->topic = "How to not fail anymore on tests";
+ $article->text = "That is simple! Just write more tests!";
+
+ $this->_em->persist($article);
+
+ $queryCount = $this->getCurrentQueryCount();
+
+ $this->assertFalse($user->articles->contains($article));
+ $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
$this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
}
@@ -255,13 +271,15 @@ public function testContainsManyToMany()
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
$this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
+ // Test Many to Many existance retrieved from DB
$group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
$queryCount = $this->getCurrentQueryCount();
$this->assertTrue($user->groups->contains($group));
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
$this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
+ // Test Many to Many existance with state new
$group = new \Doctrine\Tests\Models\CMS\CmsGroup();
$group->name = "A New group!";
@@ -271,13 +289,26 @@ public function testContainsManyToMany()
$this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
$this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
+ // Test Many to Many existance with state clear
$this->_em->persist($group);
$this->_em->flush();
$queryCount = $this->getCurrentQueryCount();
$this->assertFalse($user->groups->contains($group));
- $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
+ $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
+ $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
+
+ // Test Many to Many existance with state managed
+ $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
+ $group->name = "My managed group";
+
+ $this->_em->persist($group);
+
+ $queryCount = $this->getCurrentQueryCount();
+
+ $this->assertFalse($user->groups->contains($group));
+ $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
$this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
}
@@ -313,6 +344,7 @@ public function testRemoveElementOneToMany()
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
$this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
+ // Test One to Many removal with Entity retrieved from DB
$article = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $this->articleId);
$queryCount = $this->getCurrentQueryCount();
@@ -321,6 +353,7 @@ public function testRemoveElementOneToMany()
$this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
+ // Test One to Many removal with Entity state as new
$article = new \Doctrine\Tests\Models\CMS\CmsArticle();
$article->topic = "Testnew";
$article->text = "blub";
@@ -331,15 +364,29 @@ public function testRemoveElementOneToMany()
$this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a new entity should cause no query to be executed.");
+ // Test One to Many removal with Entity state as clean
$this->_em->persist($article);
$this->_em->flush();
$queryCount = $this->getCurrentQueryCount();
$user->articles->removeElement($article);
- $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a managed entity should cause one query to be executed.");
+ $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
$this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
+
+ // Test One to Many removal with Entity state as managed
+ $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
+ $article->topic = "How to not fail anymore on tests";
+ $article->text = "That is simple! Just write more tests!";
+
+ $this->_em->persist($article);
+
+ $queryCount = $this->getCurrentQueryCount();
+
+ $user->articles->removeElement($article);
+
+ $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
}
/**
@@ -350,14 +397,16 @@ public function testRemoveElementManyToMany()
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
$this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
+ // Test Many to Many removal with Entity retrieved from DB
$group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
$queryCount = $this->getCurrentQueryCount();
$user->groups->removeElement($group);
- $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a managed entity should cause one query to be executed.");
+ $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
$this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
+ // Test Many to Many removal with Entity state as new
$group = new \Doctrine\Tests\Models\CMS\CmsGroup();
$group->name = "A New group!";
@@ -368,14 +417,28 @@ public function testRemoveElementManyToMany()
$this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing new entity should cause no query to be executed.");
$this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
+ // Test Many to Many removal with Entity state as clean
$this->_em->persist($group);
$this->_em->flush();
$queryCount = $this->getCurrentQueryCount();
$user->groups->removeElement($group);
- $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a managed entity should cause one query to be executed.");
+ $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
+ $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
+
+ // Test Many to Many removal with Entity state as managed
+ $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
+ $group->name = "A New group!";
+
+ $this->_em->persist($group);
+
+ $queryCount = $this->getCurrentQueryCount();
+
+ $user->groups->removeElement($group);
+
+ $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
$this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
}
@@ -1,55 +0,0 @@
-<?php
-
-namespace Doctrine\Tests\ORM\Functional;
-
-use Doctrine\Common\Collections\ArrayCollection;
-require_once __DIR__ . '/../../TestInit.php';
-
-/**
- *
- */
-class ManyToManyExtraLazyContainsTest extends \Doctrine\Tests\OrmFunctionalTestCase
-{
- public function setUp()
- {
- $this->useModelSet('company');
- parent::setUp();
- }
-
- public function testManyToManyExtraLazyContainsAddedPendingInsertEntityIsTrue()
- {
- $contract = new \Doctrine\Tests\Models\Company\CompanyFlexContract();
-
- $this->_em->persist($contract);
- $this->_em->flush();
-
- $this->_em->clear();
- $contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexContract', $contract->getId());
-
- $pendingInsertManager = new \Doctrine\Tests\Models\Company\CompanyManager();
- $this->_em->persist($pendingInsertManager);
- $contract->getManagers()->add($pendingInsertManager);
-
- $result = $contract->getManagers()->contains($pendingInsertManager);
-
- $this->assertTrue($result);
- }
-
- public function testManyToManyExtraLazyContainsNonAddedPendingInsertEntityIsFalse()
- {
- $contract = new \Doctrine\Tests\Models\Company\CompanyFlexContract();
-
- $this->_em->persist($contract);
- $this->_em->flush();
-
- $this->_em->clear();
- $contract = $this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexContract', $contract->getId());
-
- $pendingInsertManager = new \Doctrine\Tests\Models\Company\CompanyManager();
- $this->_em->persist($pendingInsertManager);
-
- $result = $contract->getManagers()->contains($pendingInsertManager);
-
- $this->assertFalse($result);
- }
-}

1 comment on commit d39760b

Owner

guilhermeblanco commented on d39760b Jan 18, 2012

@beberlei Please merge into 2.2

Please sign in to comment.