forked from doctrine/orm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Success Test - cascade remove without orphanRemoval
- Loading branch information
Showing
1 changed file
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |