1
0
mirror of synced 2025-02-09 00:39:25 +03:00

#1169 DDC-3343 - adding tests for orphan-removal + extra-lazy + one-to-many element removal behavior

This commit is contained in:
Marco Pivetta 2015-01-27 07:42:48 +01:00
parent 94c0e46c96
commit cbe5575f38
4 changed files with 155 additions and 4 deletions

View File

@ -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->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);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace Doctrine\Tests\Models\Tweet;
/**
* @Entity
* @Table(name="tweet_user_list")
*/
class UserList
{
const CLASSNAME = __CLASS__;
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
public $id;
/**
* @Column(type="string")
*/
public $listName;
/**
* @ManyToOne(targetEntity="User", inversedBy="userLists")
*/
public $owner;
}

View File

@ -5,6 +5,8 @@ namespace Doctrine\Tests\ORM\Functional;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\Tests\Models\Tweet\Tweet;
use Doctrine\Tests\Models\Tweet\User;
use Doctrine\Tests\Models\Tweet\UserList;
use Doctrine\Tests\OrmFunctionalTestCase;
require_once __DIR__ . '/../../TestInit.php';
@ -685,10 +687,9 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase
/* @var $user User */
$user = $this->_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);
}
}

View File

@ -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();
}