forked from doctrine/orm
/
DDC0001Test
129 lines (103 loc) · 2.76 KB
/
DDC0001Test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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;
}