From bd5393a3187ab25136bcd8e5efea894928697e95 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Wed, 7 Sep 2011 13:12:02 -0300 Subject: [PATCH] Added coverage for orphanRemoval in OneToOne when unlinking an entity. --- tests/Doctrine/Tests/Models/CMS/CmsEmail.php | 48 +++++++++++++++++++ tests/Doctrine/Tests/Models/CMS/CmsUser.php | 17 +++++++ .../ORM/Functional/CustomTreeWalkersTest.php | 6 +-- .../Functional/OneToOneOrphanRemovalTest.php | 34 +++++++++++++ .../ORM/Query/SelectSqlGenerationTest.php | 4 +- .../Doctrine/Tests/OrmFunctionalTestCase.php | 1 + 6 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/CMS/CmsEmail.php diff --git a/tests/Doctrine/Tests/Models/CMS/CmsEmail.php b/tests/Doctrine/Tests/Models/CMS/CmsEmail.php new file mode 100644 index 000000000..2d8818cbd --- /dev/null +++ b/tests/Doctrine/Tests/Models/CMS/CmsEmail.php @@ -0,0 +1,48 @@ +id; + } + + public function getEmail() { + return $this->email; + } + + public function setEmail($email) { + $this->email = $email; + } + + public function getUser() { + return $this->user; + } + + public function setUser(CmsUser $user) { + $this->user = $user; + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/CMS/CmsUser.php b/tests/Doctrine/Tests/Models/CMS/CmsUser.php index 4047a3635..f02d0441b 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsUser.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsUser.php @@ -42,6 +42,11 @@ class CmsUser * @OneToOne(targetEntity="CmsAddress", mappedBy="user", cascade={"persist"}, orphanRemoval=true) */ public $address; + /** + * @OneToOne(targetEntity="CmsEmail", inversedBy="user", cascade={"persist"}, orphanRemoval=true) + * @JoinColumn(referencedColumnName="id", nullable=true) + */ + public $email; /** * @ManyToMany(targetEntity="CmsGroup", inversedBy="users", cascade={"persist", "merge"}) * @JoinTable(name="cms_users_groups", @@ -119,4 +124,16 @@ class CmsUser $address->setUser($this); } } + + public function getEmail() { return $this->email; } + + public function setEmail(CmsEmail $email = null) { + if ($this->email !== $email) { + $this->email = $email; + + if ($email) { + $email->setUser($this); + } + } + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/CustomTreeWalkersTest.php b/tests/Doctrine/Tests/ORM/Functional/CustomTreeWalkersTest.php index c295af478..ada41d00b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/CustomTreeWalkersTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/CustomTreeWalkersTest.php @@ -59,7 +59,7 @@ class CustomTreeWalkersTest extends \Doctrine\Tests\OrmFunctionalTestCase { $this->assertSqlGeneration( 'select u from Doctrine\Tests\Models\CMS\CmsUser u', - "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id = 1" + "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ WHERE c0_.id = 1" ); } @@ -67,7 +67,7 @@ class CustomTreeWalkersTest extends \Doctrine\Tests\OrmFunctionalTestCase { $this->assertSqlGeneration( 'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name or u.name = :otherName', - "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (c0_.name = ? OR c0_.name = ?) AND c0_.id = 1" + "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ WHERE (c0_.name = ? OR c0_.name = ?) AND c0_.id = 1" ); } @@ -75,7 +75,7 @@ class CustomTreeWalkersTest extends \Doctrine\Tests\OrmFunctionalTestCase { $this->assertSqlGeneration( 'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name', - "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.name = ? AND c0_.id = 1" + "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ WHERE c0_.name = ? AND c0_.id = 1" ); } } diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToOneOrphanRemovalTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToOneOrphanRemovalTest.php index e82961aff..c9bd72fcf 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OneToOneOrphanRemovalTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OneToOneOrphanRemovalTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\Tests\Models\CMS\CmsUser, + Doctrine\Tests\Models\CMS\CmsEmail, Doctrine\Tests\Models\CMS\CmsAddress, Doctrine\Tests\Models\CMS\CmsPhonenumber; @@ -57,4 +58,37 @@ class OneToOneOrphanRemovalTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(0, count($result), 'CmsAddress should be removed by orphanRemoval'); } + + public function testOrphanRemovalWhenUnlink() + { + $user = new CmsUser; + $user->status = 'dev'; + $user->username = 'beberlei'; + $user->name = 'Bejamin Eberlei'; + + $email = new CmsEmail; + $email->email = 'beberlei@domain.com'; + + $user->setEmail($email); + + $this->_em->persist($user); + $this->_em->flush(); + + $userId = $user->getId(); + + $this->_em->clear(); + + $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId); + + $user->setEmail(null); + + $this->_em->persist($user); + $this->_em->flush(); + $this->_em->clear(); + + $query = $this->_em->createQuery('SELECT e FROM Doctrine\Tests\Models\CMS\CmsEmail e'); + $result = $query->getResult(); + + $this->assertEquals(0, count($result), 'CmsEmail should be removed by orphanRemoval'); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 4da9cf3e3..63a885497 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -614,7 +614,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u') ->setMaxResults(10); - $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LIMIT 10', $q->getSql()); + $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ LIMIT 10', $q->getSql()); } public function testLimitAndOffsetFromQueryClass() @@ -624,7 +624,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ->setMaxResults(10) ->setFirstResult(0); - $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LIMIT 10 OFFSET 0', $q->getSql()); + $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ LIMIT 10 OFFSET 0', $q->getSql()); } public function testSizeFunction() diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 56345bc6f..0900e4e99 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -44,6 +44,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\CMS\CmsUser', 'Doctrine\Tests\Models\CMS\CmsPhonenumber', 'Doctrine\Tests\Models\CMS\CmsAddress', + 'Doctrine\Tests\Models\CMS\CmsEmail', 'Doctrine\Tests\Models\CMS\CmsGroup', 'Doctrine\Tests\Models\CMS\CmsArticle', 'Doctrine\Tests\Models\CMS\CmsComment',