Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC117Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use Doctrine\Tests\Models\DDC117\DDC117ArticleDetails;
6 use Doctrine\Tests\Models\DDC117\DDC117Article;
7 use Doctrine\Tests\Models\DDC117\DDC117Reference;
8 use Doctrine\Tests\Models\DDC117\DDC117Translation;
9 use Doctrine\Tests\Models\DDC117\DDC117ApproveChanges;
10 use Doctrine\Tests\Models\DDC117\DDC117Editor;
11
12 require_once __DIR__ . '/../../../TestInit.php';
13
14 class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase
15 {
16     private $article1;
17     private $article2;
18     private $reference;
19     private $translation;
20     private $articleDetails;
21
22     protected function setUp() {
23         $this->useModelSet('ddc117');
24         parent::setUp();
25
26         $this->article1 = new DDC117Article("Foo");
27         $this->article2 = new DDC117Article("Bar");
28
29         $this->_em->persist($this->article1);
30         $this->_em->persist($this->article2);
31         $this->_em->flush();
32
33         $this->reference = new DDC117Reference($this->article1, $this->article2, "Test-Description");
34         $this->_em->persist($this->reference);
35
36         $this->translation = new DDC117Translation($this->article1, "en", "Bar");
37         $this->_em->persist($this->translation);
38
39         $this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
40         $this->_em->persist($this->articleDetails);
41         $this->_em->flush();
42
43         $this->_em->clear();
44     }
45
46     /**
47      * @group DDC-117
48      */
49     public function testAssociationOnlyCompositeKey()
50     {
51         $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
52
53         $mapRef = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
54         $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $mapRef);
55         $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $mapRef->target());
56         $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $mapRef->source());
57         $this->assertSame($mapRef, $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
58
59         $this->_em->clear();
60
61         $dql = "SELECT r, s FROM "."Doctrine\Tests\Models\DDC117\DDC117Reference r JOIN r.source s WHERE r.source = ?1";
62         $dqlRef = $this->_em->createQuery($dql)->setParameter(1, 1)->getSingleResult();
63
64         $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $mapRef);
65         $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $mapRef->target());
66         $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $mapRef->source());
67         $this->assertSame($dqlRef, $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
68
69         $this->_em->clear();
70
71         $dql = "SELECT r, s FROM "."Doctrine\Tests\Models\DDC117\DDC117Reference r JOIN r.source s WHERE s.title = ?1";
72         $dqlRef = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getSingleResult();
73
74         $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $dqlRef);
75         $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $dqlRef->target());
76         $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Article", $dqlRef->source());
77         $this->assertSame($dqlRef, $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
78
79         $dql = "SELECT r, s FROM "."Doctrine\Tests\Models\DDC117\DDC117Reference r JOIN r.source s WHERE s.title = ?1";
80         $dqlRef = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getSingleResult();
81
82         $this->_em->contains($dqlRef);
83     }
84
85     /**
86      * @group DDC-117
87      */
88     public function testUpdateAssocationEntity()
89     {
90         $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
91
92         $mapRef = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
93         $this->assertNotNull($mapRef);
94         $mapRef->setDescription("New Description!!");
95         $this->_em->flush();
96         $this->_em->clear();
97
98         $mapRef = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
99
100         $this->assertEquals('New Description!!', $mapRef->getDescription());
101     }
102
103     /**
104      * @group DDC-117
105      */
106     public function testFetchDql()
107     {
108         $dql = "SELECT r, s FROM "."Doctrine\Tests\Models\DDC117\DDC117Reference r JOIN r.source s WHERE s.title = ?1";
109         $refs = $this->_em->createQuery($dql)->setParameter(1, 'Foo')->getResult();
110
111         $this->assertTrue(count($refs) > 0, "Has to contain at least one Reference.");
112
113         foreach ($refs AS $ref) {
114             $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $ref, "Contains only Reference instances.");
115             $this->assertTrue($this->_em->contains($ref), "Contains Reference in the IdentityMap.");
116         }
117     }
118
119     /**
120      * @group DDC-117
121      */
122     public function testRemoveCompositeElement()
123     {
124         $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
125
126         $refRep = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
127
128         $this->_em->remove($refRep);
129         $this->_em->flush();
130         $this->_em->clear();
131
132         $this->assertNull($this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
133     }
134
135     /**
136      * @group DDC-117
137      */
138     public function testDqlRemoveCompositeElement()
139     {
140         $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
141
142         $dql = "DELETE "."Doctrine\Tests\Models\DDC117\DDC117Reference r WHERE r.source = ?1 AND r.target = ?2";
143         $this->_em->createQuery($dql)
144                   ->setParameter(1, $this->article1->id())
145                   ->setParameter(2, $this->article2->id())
146                   ->execute();
147
148         $this->assertNull($this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
149     }
150
151     /**
152      * @group DDC-117
153      */
154     public function testInverseSideAccess()
155     {
156         $this->article1 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article1->id());
157
158         $this->assertEquals(1, count($this->article1->references()));
159
160         foreach ($this->article1->references() AS $this->reference) {
161             $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $this->reference);
162             $this->assertSame($this->article1, $this->reference->source());
163         }
164
165         $this->_em->clear();
166
167         $dql = 'SELECT a, r FROM '. 'Doctrine\Tests\Models\DDC117\DDC117Article a INNER JOIN a.references r WHERE a.id = ?1';
168         $articleDql = $this->_em->createQuery($dql)
169                                 ->setParameter(1, $this->article1->id())
170                                 ->getSingleResult();
171
172         $this->assertEquals(1, count($this->article1->references()));
173
174         foreach ($this->article1->references() AS $this->reference) {
175             $this->assertInstanceOf("Doctrine\Tests\Models\DDC117\DDC117Reference", $this->reference);
176             $this->assertSame($this->article1, $this->reference->source());
177         }
178     }
179
180     /**
181      * @group DDC-117
182      */
183     public function testMixedCompositeKey()
184     {
185         $idCriteria = array('article' => $this->article1->id(), 'language' => 'en');
186
187         $this->translation = $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Translation', $idCriteria);
188         $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $this->translation);
189
190         $this->assertSame($this->translation, $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Translation', $idCriteria));
191
192         $this->_em->clear();
193
194         $dql = 'SELECT t, a FROM ' . 'Doctrine\Tests\Models\DDC117\DDC117Translation t JOIN t.article a WHERE t.article = ?1 AND t.language = ?2';
195         $dqlTrans = $this->_em->createQuery($dql)
196                               ->setParameter(1, $this->article1->id())
197                               ->setParameter(2, 'en')
198                               ->getSingleResult();
199
200         $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $this->translation);
201     }
202
203     /**
204      * @group DDC-117
205      */
206     public function testMixedCompositeKeyViolateUniqueness()
207     {
208         $this->article1 = $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Article', $this->article1->id());
209         $this->article1->addTranslation('en', 'Bar');
210         $this->article1->addTranslation('en', 'Baz');
211
212         $exceptionThrown = false;
213         try {
214             // exception depending on the underyling Database Driver
215             $this->_em->flush();
216         } catch(\Exception $e) {
217             $exceptionThrown = true;
218         }
219
220         $this->assertTrue($exceptionThrown, "The underlying database driver throws an exception.");
221     }
222
223     /**
224      * @group DDC-117
225      */
226     public function testOneToOneForeignObjectId()
227     {
228         $this->article1 = new DDC117Article("Foo");
229         $this->_em->persist($this->article1);
230         $this->_em->flush();
231
232         $this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
233         $this->_em->persist($this->articleDetails);
234         $this->_em->flush();
235
236         $this->articleDetails->update("not so very long text!");
237         $this->_em->flush();
238         $this->_em->clear();
239
240         /* @var $article DDC117Article */
241         $article = $this->_em->find(get_class($this->article1), $this->article1->id());
242         $this->assertEquals('not so very long text!', $article->getText());
243     }
244
245     /**
246      * @group DDC-117
247      */
248     public function testOneToOneCascadeRemove()
249     {
250         $article = $this->_em->find(get_class($this->article1), $this->article1->id());
251         $this->_em->remove($article);
252         $this->_em->flush();
253
254         $this->assertFalse($this->_em->contains($article->getDetails()));
255     }
256
257     /**
258      * @group DDC-117
259      */
260     public function testOneToOneCascadePersist()
261     {
262         if (!$this->_em->getConnection()->getDatabasePlatform()->prefersSequences()) {
263             $this->markTestSkipped('Test only works with databases that prefer sequences as ID strategy.');
264         }
265
266         $this->article1 = new DDC117Article("Foo");
267
268         $this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
269
270         $this->_em->persist($this->article1);
271         $this->_em->flush();
272     }
273
274     /**
275      * @group DDC-117
276      */
277     public function testReferencesToForeignKeyEntities()
278     {
279         $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
280         $reference = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
281
282         $idCriteria = array('article' => $this->article1->id(), 'language' => 'en');
283         $translation = $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Translation', $idCriteria);
284
285         $approveChanges = new DDC117ApproveChanges($reference->source()->getDetails(), $reference, $translation);
286         $this->_em->persist($approveChanges);
287         $this->_em->flush();
288         $this->_em->clear();
289
290         $approveChanges = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117ApproveChanges", $approveChanges->getId());
291
292         $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails', $approveChanges->getArticleDetails());
293         $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Reference', $approveChanges->getReference());
294         $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $approveChanges->getTranslation());
295     }
296
297     /**
298      * @group DDC-117
299      */
300     public function testLoadOneToManyCollectionOfForeignKeyEntities()
301     {
302         /* @var $article DDC117Article */
303         $article = $this->_em->find(get_class($this->article1), $this->article1->id());
304
305         $translations = $article->getTranslations();
306         $this->assertFalse($translations->isInitialized());
307         $this->assertContainsOnly('Doctrine\Tests\Models\DDC117\DDC117Translation', $translations);
308         $this->assertTrue($translations->isInitialized());
309     }
310
311     /**
312      * @group DDC-117
313      */
314     public function testLoadManyToManyCollectionOfForeignKeyEntities()
315     {
316         $editor = $this->loadEditorFixture();
317
318         $this->assertFalse($editor->reviewingTranslations->isInitialized());
319         $this->assertContainsOnly("Doctrine\Tests\Models\DDC117\DDC117Translation", $editor->reviewingTranslations);
320         $this->assertTrue($editor->reviewingTranslations->isInitialized());
321
322         $this->_em->clear();
323
324         $dql = "SELECT e, t FROM Doctrine\Tests\Models\DDC117\DDC117Editor e JOIN e.reviewingTranslations t WHERE e.id = ?1";
325         $editor = $this->_em->createQuery($dql)->setParameter(1, $editor->id)->getSingleResult();
326         $this->assertTrue($editor->reviewingTranslations->isInitialized());
327         $this->assertContainsOnly("Doctrine\Tests\Models\DDC117\DDC117Translation", $editor->reviewingTranslations);
328     }
329
330     /**
331      * @group DDC-117
332      */
333     public function testClearManyToManyCollectionOfForeignKeyEntities()
334     {
335         $editor = $this->loadEditorFixture();
336         $this->assertEquals(3, count($editor->reviewingTranslations));
337
338         $editor->reviewingTranslations->clear();
339         $this->_em->flush();
340         $this->_em->clear();
341
342         $editor = $this->_em->find(get_class($editor), $editor->id);
343         $this->assertEquals(0, count($editor->reviewingTranslations));
344     }
345
346     /**
347      * @group DDC-117
348      */
349     public function testLoadInverseManyToManyCollection()
350     {
351         $editor = $this->loadEditorFixture();
352
353         $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $editor->reviewingTranslations[0]);
354
355         $reviewedBy = $editor->reviewingTranslations[0]->getReviewedByEditors();
356         $this->assertEquals(1, count($reviewedBy));
357         $this->assertSame($editor, $reviewedBy[0]);
358
359         $this->_em->clear();
360
361         $dql = "SELECT t, e FROM Doctrine\Tests\Models\DDC117\DDC117Translation t ".
362                "JOIN t.reviewedByEditors e WHERE t.article = ?1 AND t.language = ?2";
363         $trans = $this->_em->createQuery($dql)
364                            ->setParameter(1, $this->translation->getArticleId())
365                            ->setParameter(2, $this->translation->getLanguage())
366                            ->getSingleResult();
367
368         $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $trans);
369         $this->assertContainsOnly('Doctrine\Tests\Models\DDC117\DDC117Editor', $trans->reviewedByEditors);
370         $this->assertEquals(1, count($trans->reviewedByEditors));
371     }
372
373     /**
374      * @group DDC-117
375      */
376     public function testLoadOneToManyOfSourceEntityWithAssociationIdentifier()
377     {
378         $editor = $this->loadEditorFixture();
379
380         $editor->addLastTranslation($editor->reviewingTranslations[0]);
381         $this->_em->flush();
382         $this->_em->clear();
383
384         $editor = $this->_em->find(get_class($editor), $editor->id);
385         $lastTranslatedBy = $editor->reviewingTranslations[0]->getLastTranslatedBy();
386         $lastTranslatedBy->count();
387
388         $this->assertEquals(1, count($lastTranslatedBy));
389     }
390
391     /**
392      * @return DDC117Editor
393      */
394     private function loadEditorFixture()
395     {
396         $editor = new DDC117Editor("beberlei");
397
398         /* @var $article1 DDC117Article */
399         $article1 = $this->_em->find(get_class($this->article1), $this->article1->id());
400         foreach ($article1->getTranslations() AS $translation) {
401             $editor->reviewingTranslations[] = $translation;
402         }
403
404         /* @var $article2 DDC117Article */
405         $article2 = $this->_em->find(get_class($this->article2), $this->article2->id());
406         $article2->addTranslation("de", "Vanille-Krapferl"); // omnomnom
407         $article2->addTranslation("fr", "Sorry can't speak french!");
408
409         foreach ($article2->getTranslations() AS $translation) {
410             $this->_em->persist($translation); // otherwise persisting the editor won't work, reachability!
411             $editor->reviewingTranslations[] = $translation;
412         }
413
414         $this->_em->persist($editor);
415         $this->_em->flush();
416         $this->_em->clear();
417
418         return $this->_em->find(get_class($editor), $editor->id);
419     }
420
421     /**
422      * @group DDC-1519
423      */
424     public function testMergeForeignKeyIdentifierEntity()
425     {
426         $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
427
428         $refRep = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
429
430         $this->_em->detach($refRep);
431         $refRep = $this->_em->merge($refRep);
432
433         $this->assertEquals($this->article1->id(), $refRep->source()->id());
434         $this->assertEquals($this->article2->id(), $refRep->target()->id());
435     }
436
437     /**
438      * @group DDC-1652
439      */
440     public function testArrayHydrationWithCompositeKey()
441     {
442         $dql = "SELECT r,s,t FROM Doctrine\Tests\Models\DDC117\DDC117Reference r INNER JOIN r.source s INNER JOIN r.target t";
443         $before = count($this->_em->createQuery($dql)->getResult());
444
445         $this->article1 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article1->id());
446         $this->article2 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article2->id());
447
448         $this->reference = new DDC117Reference($this->article2, $this->article1, "Test-Description");
449         $this->_em->persist($this->reference);
450
451         $this->reference = new DDC117Reference($this->article1, $this->article1, "Test-Description");
452         $this->_em->persist($this->reference);
453
454         $this->reference = new DDC117Reference($this->article2, $this->article2, "Test-Description");
455         $this->_em->persist($this->reference);
456
457         $this->_em->flush();
458
459         $dql = "SELECT r,s,t FROM Doctrine\Tests\Models\DDC117\DDC117Reference r INNER JOIN r.source s INNER JOIN r.target t";
460         $data = $this->_em->createQuery($dql)->getArrayResult();
461
462         $this->assertEquals($before + 3, count($data));
463     }
464 }