Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / ExtraLazyCollectionTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\ORM\Mapping\ClassMetadataInfo;
6
7 require_once __DIR__ . '/../../TestInit.php';
8
9 /**
10  * Description of ExtraLazyCollectionTest
11  *
12  * @author beberlei
13  */
14 class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase
15 {
16     private $userId;
17     private $groupId;
18     private $articleId;
19
20     public function setUp()
21     {
22         $this->useModelSet('cms');
23         parent::setUp();
24
25         $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
26         $class->associationMappings['groups']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
27         $class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
28
29         $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup');
30         $class->associationMappings['users']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
31
32         $this->loadFixture();
33     }
34
35     public function tearDown()
36     {
37         parent::tearDown();
38
39         $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
40         $class->associationMappings['groups']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
41         $class->associationMappings['articles']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
42
43         $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup');
44         $class->associationMappings['users']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
45     }
46
47     /**
48      * @group DDC-546
49      */
50     public function testCountNotInitializesCollection()
51     {
52         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
53         $queryCount = $this->getCurrentQueryCount();
54
55         $this->assertFalse($user->groups->isInitialized());
56         $this->assertEquals(3, count($user->groups));
57         $this->assertFalse($user->groups->isInitialized());
58
59         foreach ($user->groups AS $group) { }
60
61         $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
62     }
63
64     /**
65      * @group DDC-546
66      */
67     public function testCountWhenNewEntityPresent()
68     {
69         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
70
71         $newGroup = new \Doctrine\Tests\Models\CMS\CmsGroup();
72         $newGroup->name = "Test4";
73
74         $user->addGroup($newGroup);
75         $this->_em->persist($newGroup);
76
77         $this->assertFalse($user->groups->isInitialized());
78         $this->assertEquals(4, count($user->groups));
79         $this->assertFalse($user->groups->isInitialized());
80     }
81
82     /**
83      * @group DDC-546
84      */
85     public function testCountWhenInitialized()
86     {
87         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
88         $queryCount = $this->getCurrentQueryCount();
89
90         foreach ($user->groups AS $group) { }
91
92         $this->assertTrue($user->groups->isInitialized());
93         $this->assertEquals(3, count($user->groups));
94         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Should only execute one query to initialize colleciton, no extra query for count() more.");
95     }
96
97     /**
98      * @group DDC-546
99      */
100     public function testCountInverseCollection()
101     {
102         $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
103         $this->assertFalse($group->users->isInitialized(), "Pre-Condition");
104
105         $this->assertEquals(4, count($group->users));
106         $this->assertFalse($group->users->isInitialized(), "Extra Lazy collection should not be initialized by counting the collection.");
107     }
108
109     /**
110      * @group DDC-546
111      */
112     public function testCountOneToMany()
113     {
114         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
115         $this->assertFalse($user->groups->isInitialized(), "Pre-Condition");
116
117         $this->assertEquals(2, count($user->articles));
118     }
119
120     /**
121      * @group DDC-546
122      */
123     public function testFullSlice()
124     {
125         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
126         $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
127
128         $someGroups = $user->groups->slice(null);
129         $this->assertEquals(3, count($someGroups));
130     }
131
132     /**
133      * @group DDC-546
134      */
135     public function testSlice()
136     {
137         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
138         $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
139
140         $queryCount = $this->getCurrentQueryCount();
141
142         $someGroups = $user->groups->slice(0, 2);
143
144         $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsGroup', $someGroups);
145         $this->assertEquals(2, count($someGroups));
146         $this->assertFalse($user->groups->isInitialized(), "Slice should not initialize the collection if it wasn't before!");
147
148         $otherGroup = $user->groups->slice(2, 1);
149
150         $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsGroup', $otherGroup);
151         $this->assertEquals(1, count($otherGroup));
152         $this->assertFalse($user->groups->isInitialized());
153
154         foreach ($user->groups AS $group) { }
155
156         $this->assertTrue($user->groups->isInitialized());
157         $this->assertEquals(3, count($user->groups));
158
159         $this->assertEquals($queryCount + 3, $this->getCurrentQueryCount());
160     }
161
162     /**
163      * @group DDC-546
164      */
165     public function testSliceInitializedCollection()
166     {
167         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
168         $queryCount = $this->getCurrentQueryCount();
169
170         foreach ($user->groups AS $group) { }
171
172         $someGroups = $user->groups->slice(0, 2);
173
174         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
175
176         $this->assertEquals(2, count($someGroups));
177         $this->assertTrue($user->groups->contains($someGroups[0]));
178         $this->assertTrue($user->groups->contains($someGroups[1]));
179     }
180
181     /**
182      * @group DDC-546
183      */
184     public function testSliceInverseCollection()
185     {
186         $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
187         $this->assertFalse($group->users->isInitialized(), "Pre-Condition");
188         $queryCount = $this->getCurrentQueryCount();
189
190         $someUsers = $group->users->slice(0, 2);
191         $otherUsers = $group->users->slice(2, 2);
192
193         $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsUser', $someUsers);
194         $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsUser', $otherUsers);
195         $this->assertEquals(2, count($someUsers));
196         $this->assertEquals(2, count($otherUsers));
197
198         // +2 queries executed by slice
199         $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Slicing two parts should only execute two additional queries.");
200     }
201
202     /**
203      * @group DDC-546
204      */
205     public function testSliceOneToMany()
206     {
207         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
208         $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
209
210         $queryCount = $this->getCurrentQueryCount();
211
212         $someArticle = $user->articles->slice(0, 1);
213         $otherArticle = $user->articles->slice(1, 1);
214
215         $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount());
216     }
217
218     /**
219      * @group DDC-546
220      */
221     public function testContainsOneToMany()
222     {
223         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
224         $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
225
226         // Test One to Many existance retrieved from DB
227         $article    = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $this->articleId);
228         $queryCount = $this->getCurrentQueryCount();
229
230         $this->assertTrue($user->articles->contains($article));
231         $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
232         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
233
234         // Test One to Many existance with state new
235         $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
236         $article->topic = "Testnew";
237         $article->text = "blub";
238
239         $queryCount = $this->getCurrentQueryCount();
240         $this->assertFalse($user->articles->contains($article));
241         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
242
243         // Test One to Many existance with state clear
244         $this->_em->persist($article);
245         $this->_em->flush();
246
247         $queryCount = $this->getCurrentQueryCount();
248         $this->assertFalse($user->articles->contains($article));
249         $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
250         $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
251
252         // Test One to Many existance with state managed
253         $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
254         $article->topic = "How to not fail anymore on tests";
255         $article->text = "That is simple! Just write more tests!";
256
257         $this->_em->persist($article);
258
259         $queryCount = $this->getCurrentQueryCount();
260
261         $this->assertFalse($user->articles->contains($article));
262         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
263         $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
264     }
265
266     /**
267      * @group DDC-546
268      */
269     public function testContainsManyToMany()
270     {
271         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
272         $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
273
274         // Test Many to Many existance retrieved from DB
275         $group      = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
276         $queryCount = $this->getCurrentQueryCount();
277
278         $this->assertTrue($user->groups->contains($group));
279         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
280         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
281
282         // Test Many to Many existance with state new
283         $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
284         $group->name = "A New group!";
285
286         $queryCount = $this->getCurrentQueryCount();
287
288         $this->assertFalse($user->groups->contains($group));
289         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
290         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
291
292         // Test Many to Many existance with state clear
293         $this->_em->persist($group);
294         $this->_em->flush();
295
296         $queryCount = $this->getCurrentQueryCount();
297
298         $this->assertFalse($user->groups->contains($group));
299         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed.");
300         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
301
302         // Test Many to Many existance with state managed
303         $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
304         $group->name = "My managed group";
305
306         $this->_em->persist($group);
307
308         $queryCount = $this->getCurrentQueryCount();
309
310         $this->assertFalse($user->groups->contains($group));
311         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed.");
312         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
313     }
314
315     /**
316      * @group DDC-546
317      */
318     public function testContainsManyToManyInverse()
319     {
320         $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
321         $this->assertFalse($group->users->isInitialized(), "Pre-Condition: Collection is not initialized.");
322
323         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
324
325         $queryCount = $this->getCurrentQueryCount();
326         $this->assertTrue($group->users->contains($user));
327         $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
328         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
329
330         $newUser = new \Doctrine\Tests\Models\CMS\CmsUser();
331         $newUser->name = "A New group!";
332
333         $queryCount = $this->getCurrentQueryCount();
334         $this->assertFalse($group->users->contains($newUser));
335         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
336         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
337     }
338
339     /**
340      *
341      */
342     public function testRemoveElementOneToMany()
343     {
344         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
345         $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized.");
346
347         // Test One to Many removal with Entity retrieved from DB
348         $article    = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $this->articleId);
349         $queryCount = $this->getCurrentQueryCount();
350
351         $user->articles->removeElement($article);
352
353         $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
354         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
355
356         // Test One to Many removal with Entity state as new
357         $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
358         $article->topic = "Testnew";
359         $article->text = "blub";
360
361         $queryCount = $this->getCurrentQueryCount();
362
363         $user->articles->removeElement($article);
364
365         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a new entity should cause no query to be executed.");
366
367         // Test One to Many removal with Entity state as clean
368         $this->_em->persist($article);
369         $this->_em->flush();
370
371         $queryCount = $this->getCurrentQueryCount();
372
373         $user->articles->removeElement($article);
374
375         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
376         $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized.");
377
378         // Test One to Many removal with Entity state as managed
379         $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
380         $article->topic = "How to not fail anymore on tests";
381         $article->text = "That is simple! Just write more tests!";
382
383         $this->_em->persist($article);
384
385         $queryCount = $this->getCurrentQueryCount();
386
387         $user->articles->removeElement($article);
388
389         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
390     }
391
392     /**
393      *
394      */
395     public function testRemoveElementManyToMany()
396     {
397         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
398         $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized.");
399
400         // Test Many to Many removal with Entity retrieved from DB
401         $group      = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
402         $queryCount = $this->getCurrentQueryCount();
403
404         $user->groups->removeElement($group);
405
406         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
407         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
408
409         // Test Many to Many removal with Entity state as new
410         $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
411         $group->name = "A New group!";
412
413         $queryCount = $this->getCurrentQueryCount();
414
415         $user->groups->removeElement($group);
416
417         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing new entity should cause no query to be executed.");
418         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
419
420         // Test Many to Many removal with Entity state as clean
421         $this->_em->persist($group);
422         $this->_em->flush();
423
424         $queryCount = $this->getCurrentQueryCount();
425
426         $user->groups->removeElement($group);
427
428         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed.");
429         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
430
431         // Test Many to Many removal with Entity state as managed
432         $group = new \Doctrine\Tests\Models\CMS\CmsGroup();
433         $group->name = "A New group!";
434
435         $this->_em->persist($group);
436
437         $queryCount = $this->getCurrentQueryCount();
438
439         $user->groups->removeElement($group);
440
441         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed.");
442         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
443     }
444
445     /**
446      *
447      */
448     public function testRemoveElementManyToManyInverse()
449     {
450         $group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
451         $this->assertFalse($group->users->isInitialized(), "Pre-Condition: Collection is not initialized.");
452
453         $user       = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
454         $queryCount = $this->getCurrentQueryCount();
455
456         $group->users->removeElement($user);
457
458         $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a managed entity should cause one query to be executed.");
459         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
460
461         $newUser = new \Doctrine\Tests\Models\CMS\CmsUser();
462         $newUser->name = "A New group!";
463
464         $queryCount = $this->getCurrentQueryCount();
465
466         $group->users->removeElement($newUser);
467
468         $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a new entity should cause no query to be executed.");
469         $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
470     }
471
472     /**
473      * @group DDC-1399
474      */
475     public function testCountAfterAddThenFlush()
476     {
477         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
478
479         $newGroup = new \Doctrine\Tests\Models\CMS\CmsGroup();
480         $newGroup->name = "Test4";
481
482         $user->addGroup($newGroup);
483         $this->_em->persist($newGroup);
484
485         $this->assertFalse($user->groups->isInitialized());
486         $this->assertEquals(4, count($user->groups));
487         $this->assertFalse($user->groups->isInitialized());
488
489         $this->_em->flush();
490
491         $this->assertEquals(4, count($user->groups));
492     }
493
494     /**
495      * @group DDC-1462
496      */
497     public function testSliceOnDirtyCollection()
498     {
499         $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
500         /* @var $user CmsUser */
501
502         $newGroup = new \Doctrine\Tests\Models\CMS\CmsGroup();
503         $newGroup->name = "Test4";
504
505         $user->addGroup($newGroup);
506         $this->_em->persist($newGroup);
507
508         $qc = $this->getCurrentQueryCount();
509         $groups = $user->groups->slice(0, 10);
510
511         $this->assertEquals(4, count($groups));
512         $this->assertEquals($qc + 1, $this->getCurrentQueryCount());
513     }
514
515     private function loadFixture()
516     {
517         $user1 = new \Doctrine\Tests\Models\CMS\CmsUser();
518         $user1->username = "beberlei";
519         $user1->name = "Benjamin";
520         $user1->status = "active";
521
522         $user2 = new \Doctrine\Tests\Models\CMS\CmsUser();
523         $user2->username = "jwage";
524         $user2->name = "Jonathan";
525         $user2->status = "active";
526
527         $user3 = new \Doctrine\Tests\Models\CMS\CmsUser();
528         $user3->username = "romanb";
529         $user3->name = "Roman";
530         $user3->status = "active";
531
532         $user4 = new \Doctrine\Tests\Models\CMS\CmsUser();
533         $user4->username = "gblanco";
534         $user4->name = "Guilherme";
535         $user4->status = "active";
536
537         $this->_em->persist($user1);
538         $this->_em->persist($user2);
539         $this->_em->persist($user3);
540         $this->_em->persist($user4);
541
542         $group1 = new \Doctrine\Tests\Models\CMS\CmsGroup();
543         $group1->name = "Test1";
544
545         $group2 = new \Doctrine\Tests\Models\CMS\CmsGroup();
546         $group2->name = "Test2";
547
548         $group3 = new \Doctrine\Tests\Models\CMS\CmsGroup();
549         $group3->name = "Test3";
550
551         $user1->addGroup($group1);
552         $user1->addGroup($group2);
553         $user1->addGroup($group3);
554
555         $user2->addGroup($group1);
556         $user3->addGroup($group1);
557         $user4->addGroup($group1);
558
559         $this->_em->persist($group1);
560         $this->_em->persist($group2);
561         $this->_em->persist($group3);
562
563         $article1 = new \Doctrine\Tests\Models\CMS\CmsArticle();
564         $article1->topic = "Test";
565         $article1->text = "Test";
566         $article1->setAuthor($user1);
567
568         $article2 = new \Doctrine\Tests\Models\CMS\CmsArticle();
569         $article2->topic = "Test";
570         $article2->text = "Test";
571         $article2->setAuthor($user1);
572
573         $this->_em->persist($article1);
574         $this->_em->persist($article2);
575
576         $this->_em->flush();
577         $this->_em->clear();
578
579         $this->articleId = $article1->id;
580         $this->userId = $user1->getId();
581         $this->groupId = $group1->id;
582     }
583 }