Skip to content

Commit

Permalink
doctrine#1169 DDC-3343 - adding tests for orphan-removal + extra-laz…
Browse files Browse the repository at this point in the history
…y + one-to-many element removal behavior
  • Loading branch information
Ocramius committed Feb 5, 2015
1 parent 94c0e46 commit cbe5575
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 4 deletions.
14 changes: 13 additions & 1 deletion tests/Doctrine/Tests/Models/Tweet/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,26 @@ 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)
{
$tweet->setAuthor($this);
$this->tweets->add($tweet);
}

public function addUserList(UserList $userList)
{
$userList->owner = $this;
$this->userLists->add($userList);
}
}
29 changes: 29 additions & 0 deletions tests/Doctrine/Tests/Models/Tweet/UserList.php
Original file line number Diff line number Diff line change
@@ -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;
}
105 changes: 102 additions & 3 deletions tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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';

Expand Down Expand Up @@ -685,10 +687,9 @@ public function testRemoveManagedElementFromOneToManyExtraLazyCollectionWithoutD

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

Expand Down Expand Up @@ -835,6 +836,83 @@ public function testRemovingManagedLazyProxyFromExtraLazyOneToManyDoesRemoveTheA
$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
*/
Expand All @@ -855,4 +933,25 @@ private function loadTweetFixture()

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);
}
}
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);

/**
Expand Down Expand Up @@ -305,6 +310,12 @@ protected function tearDown()
$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();
}

Expand Down

0 comments on commit cbe5575

Please sign in to comment.