Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / LifecycleCallbackTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4 use Doctrine\ORM\Event\PreUpdateEventArgs;
5
6 require_once __DIR__ . '/../../TestInit.php';
7
8 class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
9 {
10     protected function setUp() {
11         parent::setUp();
12         try {
13             $this->_schemaTool->createSchema(array(
14                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity'),
15                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser'),
16                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackCascader'),
17             ));
18         } catch (\Exception $e) {
19             // Swallow all exceptions. We do not test the schema tool here.
20         }
21     }
22
23     public function testPreSavePostSaveCallbacksAreInvoked()
24     {
25         $entity = new LifecycleCallbackTestEntity;
26         $entity->value = 'hello';
27         $this->_em->persist($entity);
28         $this->_em->flush();
29
30         $this->assertTrue($entity->prePersistCallbackInvoked);
31         $this->assertTrue($entity->postPersistCallbackInvoked);
32
33         $this->_em->clear();
34
35         $query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity e");
36         $result = $query->getResult();
37         $this->assertTrue($result[0]->postLoadCallbackInvoked);
38
39         $result[0]->value = 'hello again';
40
41         $this->_em->flush();
42
43         $this->assertEquals('changed from preUpdate callback!', $result[0]->value);
44     }
45
46     public function testPreFlushCallbacksAreInvoked()
47     {
48         $entity = new LifecycleCallbackTestEntity;
49         $entity->value = 'hello';
50         $this->_em->persist($entity);
51
52         $this->_em->flush();
53
54         $this->assertTrue($entity->prePersistCallbackInvoked);
55         $this->assertTrue($entity->preFlushCallbackInvoked);
56
57         $entity->preFlushCallbackInvoked = false;
58         $this->_em->flush();
59
60         $this->assertTrue($entity->preFlushCallbackInvoked);
61
62         $entity->value = 'bye';
63         $entity->preFlushCallbackInvoked = false;
64         $this->_em->flush();
65
66         $this->assertTrue($entity->preFlushCallbackInvoked);
67     }
68
69     public function testChangesDontGetLost()
70     {
71         $user = new LifecycleCallbackTestUser;
72         $user->setName('Bob');
73         $user->setValue('value');
74         $this->_em->persist($user);
75         $this->_em->flush();
76
77         $user->setName('Alice');
78         $this->_em->flush(); // Triggers preUpdate
79
80         $this->_em->clear();
81
82         $user2 = $this->_em->find(get_class($user), $user->getId());
83
84         $this->assertEquals('Alice', $user2->getName());
85         $this->assertEquals('Hello World', $user2->getValue());
86     }
87
88     /**
89      * @group DDC-194
90      */
91     public function testGetReferenceWithPostLoadEventIsDelayedUntilProxyTrigger()
92     {
93         $entity = new LifecycleCallbackTestEntity;
94         $entity->value = 'hello';
95         $this->_em->persist($entity);
96         $this->_em->flush();
97         $id = $entity->getId();
98
99         $this->_em->clear();
100
101         $reference = $this->_em->getReference('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
102         $this->assertFalse($reference->postLoadCallbackInvoked);
103
104         $reference->getValue(); // trigger proxy load
105         $this->assertTrue($reference->postLoadCallbackInvoked);
106     }
107
108     /**
109      * @group DDC-958
110      */
111     public function testPostLoadTriggeredOnRefresh()
112     {
113         $entity = new LifecycleCallbackTestEntity;
114         $entity->value = 'hello';
115         $this->_em->persist($entity);
116         $this->_em->flush();
117         $id = $entity->getId();
118
119         $this->_em->clear();
120
121         $reference = $this->_em->find('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
122         $this->assertTrue($reference->postLoadCallbackInvoked);
123         $reference->postLoadCallbackInvoked = false;
124
125         $this->_em->refresh($reference);
126         $this->assertTrue($reference->postLoadCallbackInvoked, "postLoad should be invoked when refresh() is called.");
127     }
128
129     /**
130      * @group DDC-113
131      */
132     public function testCascadedEntitiesCallsPrePersist()
133     {
134         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
135
136         $e1 = new LifecycleCallbackTestEntity;
137         $e2 = new LifecycleCallbackTestEntity;
138
139         $c = new LifecycleCallbackCascader();
140         $this->_em->persist($c);
141
142         $c->entities[] = $e1;
143         $c->entities[] = $e2;
144         $e1->cascader = $c;
145         $e2->cascader = $c;
146
147         //$this->_em->persist($c);
148         $this->_em->flush();
149
150         $this->assertTrue($e1->prePersistCallbackInvoked);
151         $this->assertTrue($e2->prePersistCallbackInvoked);
152     }
153
154     public function testLifecycleCallbacksGetInherited()
155     {
156         $childMeta = $this->_em->getClassMetadata(__NAMESPACE__ . '\LifecycleCallbackChildEntity');
157         $this->assertEquals(array('prePersist' => array(0 => 'doStuff')), $childMeta->lifecycleCallbacks);
158     }
159
160     public function testLifecycleListener_ChangeUpdateChangeSet()
161     {
162         $listener = new LifecycleListenerPreUpdate;
163         $this->_em->getEventManager()->addEventListener(array('preUpdate'), $listener);
164
165         $user = new LifecycleCallbackTestUser;
166         $user->setName('Bob');
167         $user->setValue('value');
168         $this->_em->persist($user);
169         $this->_em->flush();
170         $this->_em->clear();
171
172         $dql = "SELECT u FROM Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser u WHERE u.name = 'Bob'";
173         $bob = $this->_em->createQuery($dql)->getSingleResult();
174         $bob->setName('Alice');
175
176         $this->_em->flush(); // preUpdate reverts Alice to Bob
177         $this->_em->clear();
178
179         $this->_em->getEventManager()->removeEventListener(array('preUpdate'), $listener);
180
181         $bob = $this->_em->createQuery($dql)->getSingleResult();
182
183         $this->assertEquals('Bob', $bob->getName());
184     }
185 }
186
187 /** @Entity @HasLifecycleCallbacks */
188 class LifecycleCallbackTestUser {
189     /** @Id @Column(type="integer") @GeneratedValue */
190     private $id;
191     /** @Column(type="string") */
192     private $value;
193     /** @Column(type="string") */
194     private $name;
195     public function getId() {return $this->id;}
196     public function getValue() {return $this->value;}
197     public function setValue($value) {$this->value = $value;}
198     public function getName() {return $this->name;}
199     public function setName($name) {$this->name = $name;}
200     /** @PreUpdate */
201     public function testCallback() {$this->value = 'Hello World';}
202 }
203
204 /**
205  * @Entity
206  * @HasLifecycleCallbacks
207  * @Table(name="lc_cb_test_entity")
208  */
209 class LifecycleCallbackTestEntity
210 {
211     /* test stuff */
212     public $prePersistCallbackInvoked = false;
213     public $postPersistCallbackInvoked = false;
214     public $postLoadCallbackInvoked = false;
215
216     public $preFlushCallbackInvoked = false;
217
218     /**
219      * @Id @Column(type="integer")
220      * @GeneratedValue(strategy="AUTO")
221      */
222     private $id;
223     /**
224      * @Column(type="string", nullable=true)
225      */
226     public $value;
227
228     /**
229      * @ManyToOne(targetEntity="LifecycleCallbackCascader")
230      * @JoinColumn(name="cascader_id", referencedColumnName="id")
231      */
232     public $cascader;
233
234     public function getId() {
235         return $this->id;
236     }
237
238     public function getValue() {
239         return $this->value;
240     }
241
242     /** @PrePersist */
243     public function doStuffOnPrePersist() {
244         $this->prePersistCallbackInvoked = true;
245     }
246
247     /** @PostPersist */
248     public function doStuffOnPostPersist() {
249         $this->postPersistCallbackInvoked = true;
250     }
251
252     /** @PostLoad */
253     public function doStuffOnPostLoad() {
254         $this->postLoadCallbackInvoked = true;
255     }
256
257     /** @PreUpdate */
258     public function doStuffOnPreUpdate() {
259         $this->value = 'changed from preUpdate callback!';
260     }
261
262     /** @PreFlush */
263     public function doStuffOnPreFlush() {
264         $this->preFlushCallbackInvoked = true;
265     }
266 }
267
268 /**
269  * @Entity
270  * @Table(name="lc_cb_test_cascade")
271  */
272 class LifecycleCallbackCascader
273 {
274     /**
275      * @Id @Column(type="integer")
276      * @GeneratedValue(strategy="AUTO")
277      */
278     private $id;
279
280     /**
281      * @OneToMany(targetEntity="LifecycleCallbackTestEntity", mappedBy="cascader", cascade={"persist"})
282      */
283     public $entities;
284
285     public function __construct()
286     {
287         $this->entities = new \Doctrine\Common\Collections\ArrayCollection();
288     }
289 }
290
291 /** @MappedSuperclass @HasLifecycleCallbacks */
292 class LifecycleCallbackParentEntity {
293     /** @PrePersist */
294     function doStuff() {
295
296     }
297 }
298
299 /** @Entity @Table(name="lc_cb_childentity") */
300 class LifecycleCallbackChildEntity extends LifecycleCallbackParentEntity {
301     /** @Id @Column(type="integer") @GeneratedValue */
302     private $id;
303 }
304
305 class LifecycleListenerPreUpdate
306 {
307     public function preUpdate(PreUpdateEventArgs $eventArgs)
308     {
309         $eventArgs->setNewValue('name', 'Bob');
310     }
311 }