3 namespace Doctrine\Tests\ORM\Functional\Ticket;
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;
12 require_once __DIR__ . '/../../../TestInit.php';
14 class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase
20 private $articleDetails;
22 protected function setUp() {
23 $this->useModelSet('ddc117');
26 $this->article1 = new DDC117Article("Foo");
27 $this->article2 = new DDC117Article("Bar");
29 $this->_em->persist($this->article1);
30 $this->_em->persist($this->article2);
33 $this->reference = new DDC117Reference($this->article1, $this->article2, "Test-Description");
34 $this->_em->persist($this->reference);
36 $this->translation = new DDC117Translation($this->article1, "en", "Bar");
37 $this->_em->persist($this->translation);
39 $this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
40 $this->_em->persist($this->articleDetails);
49 public function testAssociationOnlyCompositeKey()
51 $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
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));
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();
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));
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();
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));
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();
82 $this->_em->contains($dqlRef);
88 public function testUpdateAssocationEntity()
90 $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
92 $mapRef = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
93 $this->assertNotNull($mapRef);
94 $mapRef->setDescription("New Description!!");
98 $mapRef = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
100 $this->assertEquals('New Description!!', $mapRef->getDescription());
106 public function testFetchDql()
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();
111 $this->assertTrue(count($refs) > 0, "Has to contain at least one Reference.");
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.");
122 public function testRemoveCompositeElement()
124 $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
126 $refRep = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
128 $this->_em->remove($refRep);
132 $this->assertNull($this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
138 public function testDqlRemoveCompositeElement()
140 $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
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())
148 $this->assertNull($this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria));
154 public function testInverseSideAccess()
156 $this->article1 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article1->id());
158 $this->assertEquals(1, count($this->article1->references()));
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());
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())
172 $this->assertEquals(1, count($this->article1->references()));
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());
183 public function testMixedCompositeKey()
185 $idCriteria = array('article' => $this->article1->id(), 'language' => 'en');
187 $this->translation = $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Translation', $idCriteria);
188 $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $this->translation);
190 $this->assertSame($this->translation, $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Translation', $idCriteria));
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')
200 $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $this->translation);
206 public function testMixedCompositeKeyViolateUniqueness()
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');
212 $exceptionThrown = false;
214 // exception depending on the underyling Database Driver
216 } catch(\Exception $e) {
217 $exceptionThrown = true;
220 $this->assertTrue($exceptionThrown, "The underlying database driver throws an exception.");
226 public function testOneToOneForeignObjectId()
228 $this->article1 = new DDC117Article("Foo");
229 $this->_em->persist($this->article1);
232 $this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
233 $this->_em->persist($this->articleDetails);
236 $this->articleDetails->update("not so very long text!");
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());
248 public function testOneToOneCascadeRemove()
250 $article = $this->_em->find(get_class($this->article1), $this->article1->id());
251 $this->_em->remove($article);
254 $this->assertFalse($this->_em->contains($article->getDetails()));
260 public function testOneToOneCascadePersist()
262 if (!$this->_em->getConnection()->getDatabasePlatform()->prefersSequences()) {
263 $this->markTestSkipped('Test only works with databases that prefer sequences as ID strategy.');
266 $this->article1 = new DDC117Article("Foo");
268 $this->articleDetails = new DDC117ArticleDetails($this->article1, "Very long text");
270 $this->_em->persist($this->article1);
277 public function testReferencesToForeignKeyEntities()
279 $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
280 $reference = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
282 $idCriteria = array('article' => $this->article1->id(), 'language' => 'en');
283 $translation = $this->_em->find('Doctrine\Tests\Models\DDC117\DDC117Translation', $idCriteria);
285 $approveChanges = new DDC117ApproveChanges($reference->source()->getDetails(), $reference, $translation);
286 $this->_em->persist($approveChanges);
290 $approveChanges = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117ApproveChanges", $approveChanges->getId());
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());
300 public function testLoadOneToManyCollectionOfForeignKeyEntities()
302 /* @var $article DDC117Article */
303 $article = $this->_em->find(get_class($this->article1), $this->article1->id());
305 $translations = $article->getTranslations();
306 $this->assertFalse($translations->isInitialized());
307 $this->assertContainsOnly('Doctrine\Tests\Models\DDC117\DDC117Translation', $translations);
308 $this->assertTrue($translations->isInitialized());
314 public function testLoadManyToManyCollectionOfForeignKeyEntities()
316 $editor = $this->loadEditorFixture();
318 $this->assertFalse($editor->reviewingTranslations->isInitialized());
319 $this->assertContainsOnly("Doctrine\Tests\Models\DDC117\DDC117Translation", $editor->reviewingTranslations);
320 $this->assertTrue($editor->reviewingTranslations->isInitialized());
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);
333 public function testClearManyToManyCollectionOfForeignKeyEntities()
335 $editor = $this->loadEditorFixture();
336 $this->assertEquals(3, count($editor->reviewingTranslations));
338 $editor->reviewingTranslations->clear();
342 $editor = $this->_em->find(get_class($editor), $editor->id);
343 $this->assertEquals(0, count($editor->reviewingTranslations));
349 public function testLoadInverseManyToManyCollection()
351 $editor = $this->loadEditorFixture();
353 $this->assertInstanceOf('Doctrine\Tests\Models\DDC117\DDC117Translation', $editor->reviewingTranslations[0]);
355 $reviewedBy = $editor->reviewingTranslations[0]->getReviewedByEditors();
356 $this->assertEquals(1, count($reviewedBy));
357 $this->assertSame($editor, $reviewedBy[0]);
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())
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));
376 public function testLoadOneToManyOfSourceEntityWithAssociationIdentifier()
378 $editor = $this->loadEditorFixture();
380 $editor->addLastTranslation($editor->reviewingTranslations[0]);
384 $editor = $this->_em->find(get_class($editor), $editor->id);
385 $lastTranslatedBy = $editor->reviewingTranslations[0]->getLastTranslatedBy();
386 $lastTranslatedBy->count();
388 $this->assertEquals(1, count($lastTranslatedBy));
392 * @return DDC117Editor
394 private function loadEditorFixture()
396 $editor = new DDC117Editor("beberlei");
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;
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!");
409 foreach ($article2->getTranslations() AS $translation) {
410 $this->_em->persist($translation); // otherwise persisting the editor won't work, reachability!
411 $editor->reviewingTranslations[] = $translation;
414 $this->_em->persist($editor);
418 return $this->_em->find(get_class($editor), $editor->id);
424 public function testMergeForeignKeyIdentifierEntity()
426 $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
428 $refRep = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
430 $this->_em->detach($refRep);
431 $refRep = $this->_em->merge($refRep);
433 $this->assertEquals($this->article1->id(), $refRep->source()->id());
434 $this->assertEquals($this->article2->id(), $refRep->target()->id());
440 public function testArrayHydrationWithCompositeKey()
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());
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());
448 $this->reference = new DDC117Reference($this->article2, $this->article1, "Test-Description");
449 $this->_em->persist($this->reference);
451 $this->reference = new DDC117Reference($this->article1, $this->article1, "Test-Description");
452 $this->_em->persist($this->reference);
454 $this->reference = new DDC117Reference($this->article2, $this->article2, "Test-Description");
455 $this->_em->persist($this->reference);
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();
462 $this->assertEquals($before + 3, count($data));