DDC117Test.php 17KB

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