Skip to content

Commit

Permalink
Success Test - cascade remove without orphanRemoval
Browse files Browse the repository at this point in the history
  • Loading branch information
raziel057 committed May 21, 2014
1 parent 2e03235 commit 7288077
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC0001Test
@@ -0,0 +1,129 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\ORM\Event\LifecycleEventArgs;

/**
* Functional tests for cascade remove without orphanRemoval.
*
* @author Lallement Thomas <thomas.lallement@9online.fr>
*/
class DDC0001Test extends OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();

$this->setUpEntitySchema(array(
'Doctrine\Tests\ORM\Functional\Ticket\User',
'Doctrine\Tests\ORM\Functional\Ticket\Role',
'Doctrine\Tests\ORM\Functional\Ticket\ActionLog',
));
}

public function testIssueCascadeRemoveNotOrphanRemoval()
{
$user = new User();
$role = new Role();

$user->addRole($role);

$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();

$user = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\User', $user->id);
$role = $user->roles->get(0);

$this->assertEquals(1, count($user->roles));

$user->roles->removeElement($role);
$this->_em->remove($role);

$this->assertEquals(0, count($user->roles));

$this->_em->flush();
$this->_em->clear();

$user = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\User', $user->id);

$this->assertEquals(0, count($user->roles));

$actionLog = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\ActionLog', 1);

$this->assertEquals('1 - remove', $actionLog->id.' - '.$actionLog->action);
}
}

/**
* @Entity @Table(name="ddc0001_role") @HasLifecycleCallbacks
*/
class Role
{
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
public $id;

/**
* @ManyToOne(targetEntity="User", inversedBy="roles")
*/
public $user;

/**
* @PreRemove
*/
public function preRemove(LifecycleEventArgs $eventArgs)
{
$em = $eventArgs->getEntityManager();

$actionLog = new ActionLog();
$actionLog->action = 'remove';
$em->persist($actionLog);
}
}

/**
* @Entity @Table(name="ddc0001_user")
*/
class User
{
public $changeSet = array();

/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;

/**
* @OneToMany(targetEntity="Role", mappedBy="user", cascade={"all"})
*/
public $roles;

public function addRole(Role $role)
{
$this->roles[] = $role;
$role->user = $this;
}
}

/**
* @Entity @Table(name="ddc0001_action_log")
*/
class ActionLog
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;

/**
* @Column(name="action", type="string", length=255, nullable=true)
*/
public $action;
}

0 comments on commit 7288077

Please sign in to comment.