diff --git a/tests/Doctrine/Tests/Models/Tweet/User.php b/tests/Doctrine/Tests/Models/Tweet/User.php index 4722e6317..68cf137f2 100644 --- a/tests/Doctrine/Tests/Models/Tweet/User.php +++ b/tests/Doctrine/Tests/Models/Tweet/User.php @@ -29,9 +29,15 @@ class User */ public $tweets; + /** + * @OneToMany(targetEntity="UserList", mappedBy="owner", fetch="EXTRA_LAZY", orphanRemoval=true) + */ + public $userLists; + public function __construct() { - $this->tweets = new ArrayCollection(); + $this->tweets = new ArrayCollection(); + $this->userLists = new ArrayCollection(); } public function addTweet(Tweet $tweet) @@ -39,4 +45,10 @@ class User $tweet->setAuthor($this); $this->tweets->add($tweet); } + + public function addUserList(UserList $userList) + { + $userList->owner = $this; + $this->userLists->add($userList); + } } diff --git a/tests/Doctrine/Tests/Models/Tweet/UserList.php b/tests/Doctrine/Tests/Models/Tweet/UserList.php new file mode 100644 index 000000000..1eb3e141f --- /dev/null +++ b/tests/Doctrine/Tests/Models/Tweet/UserList.php @@ -0,0 +1,29 @@ +_em->find(User::CLASSNAME, $userId); + $tweet = $this->_em->find(Tweet::CLASSNAME, $tweetId); - $e = $this->_em->find(Tweet::CLASSNAME, $tweetId); - - $user->tweets->removeElement($e); + $user->tweets->removeElement($tweet); $this->_em->clear(); @@ -835,6 +836,83 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertCount(0, $user->tweets); } + /** + * @group DDC-3343 + */ + public function testRemoveOrphanedManagedElementFromOneToManyExtraLazyCollection() + { + list($userId, $userListId) = $this->loadUserListFixture(); + + /* @var $user User */ + $user = $this->_em->find(User::CLASSNAME, $userId); + + $user->tweets->removeElement($this->_em->find(UserList::CLASSNAME, $userListId)); + + $this->_em->clear(); + + /* @var $user User */ + $user = $this->_em->find(User::CLASSNAME, $userId); + + $this->assertCount(0, $user->userLists, 'Element was removed from association due to orphan removal'); + $this->assertNull( + $this->_em->find(UserList::CLASSNAME, $userListId), + 'Element was deleted due to orphan removal' + ); + } + + /** + * @group DDC-3343 + */ + public function testRemoveOrphanedUnManagedElementFromOneToManyExtraLazyCollection() + { + list($userId, $userListId) = $this->loadUserListFixture(); + + /* @var $user User */ + $user = $this->_em->find(User::CLASSNAME, $userId); + + $user->userLists->removeElement(new UserList()); + + $this->_em->clear(); + + /* @var $userList UserList */ + $userList = $this->_em->find(UserList::CLASSNAME, $userListId); + $this->assertInstanceOf( + UserList::CLASSNAME, + $userList, + 'Even though the collection is extra lazy + orphan removal, the user list should not have been deleted' + ); + + $this->assertInstanceOf( + User::CLASSNAME, + $userList->owner, + 'User list to owner link has not been removed' + ); + } + + /** + * @group DDC-3343 + */ + public function testRemoveOrphanedManagedLazyProxyFromExtraLazyOneToMany() + { + list($userId, $userListId) = $this->loadUserListFixture(); + + /* @var $user User */ + $user = $this->_em->find(User::CLASSNAME, $userId); + + $user->tweets->removeElement($this->_em->getReference(UserList::CLASSNAME, $userListId)); + + $this->_em->clear(); + + /* @var $user User */ + $user = $this->_em->find(User::CLASSNAME, $userId); + + $this->assertCount(0, $user->userLists, 'Element was removed from association due to orphan removal'); + $this->assertNull( + $this->_em->find(UserList::CLASSNAME, $userListId), + 'Element was deleted due to orphan removal' + ); + } + /** * @return int[] ordered tuple: user id and tweet id */ @@ -855,4 +933,25 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase return array($user->id, $tweet->id); } + + /** + * @return int[] ordered tuple: user id and user list id + */ + private function loadUserListFixture() + { + $user = new User(); + $userList = new UserList(); + + $user->name = 'ocramius'; + $userList->listName = 'PHP Developers to follow closely'; + + $user->addUserList($userList); + + $this->_em->persist($user); + $this->_em->persist($userList); + $this->_em->flush(); + $this->_em->clear(); + + return array($user->id, $userList->id); + } } diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index ff0f2cb53..b9ccbcb66 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -166,6 +166,11 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\Taxi\Car', 'Doctrine\Tests\Models\Taxi\Driver', ), + 'tweet' => array( + 'Doctrine\Tests\Models\Tweet\User', + 'Doctrine\Tests\Models\Tweet\Tweet', + 'Doctrine\Tests\Models\Tweet\UserList', + ), ); /** @@ -305,6 +310,12 @@ abstract class OrmFunctionalTestCase extends OrmTestCase $conn->executeUpdate('DELETE FROM taxi_driver'); } + if (isset($this->_usedModelSets['tweet'])) { + $conn->executeUpdate('DELETE FROM tweet_tweet'); + $conn->executeUpdate('DELETE FROM tweet_user_list'); + $conn->executeUpdate('DELETE FROM tweet_user'); + } + $this->_em->clear(); }