DDC1545Test.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Tests\Models\CMS\CmsComment;
  5. use Doctrine\Tests\Models\CMS\CmsArticle;
  6. use Doctrine\Tests\Models\CMS\CmsUser;
  7. require_once __DIR__ . '/../../../TestInit.php';
  8. /**
  9. * @group DDC-1545
  10. */
  11. class DDC1545Test extends \Doctrine\Tests\OrmFunctionalTestCase
  12. {
  13. private $articleId;
  14. private $userId;
  15. private $user2Id;
  16. public function setUp()
  17. {
  18. $this->useModelSet('cms');
  19. parent::setUp();
  20. }
  21. private function initDb($link)
  22. {
  23. $article = new CmsArticle();
  24. $article->topic = 'foo';
  25. $article->text = 'foo';
  26. $user = new CmsUser();
  27. $user->status = 'foo';
  28. $user->username = 'foo';
  29. $user->name = 'foo';
  30. $user2 = new CmsUser();
  31. $user2->status = 'bar';
  32. $user2->username = 'bar';
  33. $user2->name = 'bar';
  34. if ($link) {
  35. $article->user = $user;
  36. }
  37. $this->_em->persist($article);
  38. $this->_em->persist($user);
  39. $this->_em->persist($user2);
  40. $this->_em->flush();
  41. $this->_em->clear();
  42. $this->articleId = $article->id;
  43. $this->userId = $user->id;
  44. $this->user2Id = $user2->id;
  45. }
  46. public function testLinkObjects()
  47. {
  48. $this->initDb(false);
  49. // don't join association
  50. $article = $this->_em->find('Doctrine\Tests\Models\Cms\CmsArticle', $this->articleId);
  51. $user = $this->_em->find('Doctrine\Tests\Models\Cms\CmsUser', $this->userId);
  52. $article->user = $user;
  53. $this->_em->flush();
  54. $this->_em->clear();
  55. $article = $this->_em
  56. ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
  57. ->setParameter('id', $this->articleId)
  58. ->getOneOrNullResult();
  59. $this->assertNotNull($article->user);
  60. $this->assertEquals($user->id, $article->user->id);
  61. }
  62. public function testLinkObjectsWithAssociationLoaded()
  63. {
  64. $this->initDb(false);
  65. // join association
  66. $article = $this->_em
  67. ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
  68. ->setParameter('id', $this->articleId)
  69. ->getOneOrNullResult();
  70. $user = $this->_em->find('Doctrine\Tests\Models\Cms\CmsUser', $this->userId);
  71. $article->user = $user;
  72. $this->_em->flush();
  73. $this->_em->clear();
  74. $article = $this->_em
  75. ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
  76. ->setParameter('id', $this->articleId)
  77. ->getOneOrNullResult();
  78. $this->assertNotNull($article->user);
  79. $this->assertEquals($user->id, $article->user->id);
  80. }
  81. public function testUnlinkObjects()
  82. {
  83. $this->initDb(true);
  84. // don't join association
  85. $article = $this->_em->find('Doctrine\Tests\Models\Cms\CmsArticle', $this->articleId);
  86. $article->user = null;
  87. $this->_em->flush();
  88. $this->_em->clear();
  89. $article = $this->_em
  90. ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
  91. ->setParameter('id', $this->articleId)
  92. ->getOneOrNullResult();
  93. $this->assertNull($article->user);
  94. }
  95. public function testUnlinkObjectsWithAssociationLoaded()
  96. {
  97. $this->initDb(true);
  98. // join association
  99. $article = $this->_em
  100. ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
  101. ->setParameter('id', $this->articleId)
  102. ->getOneOrNullResult();
  103. $article->user = null;
  104. $this->_em->flush();
  105. $this->_em->clear();
  106. $article = $this->_em
  107. ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
  108. ->setParameter('id', $this->articleId)
  109. ->getOneOrNullResult();
  110. $this->assertNull($article->user);
  111. }
  112. public function testChangeLink()
  113. {
  114. $this->initDb(false);
  115. // don't join association
  116. $article = $this->_em->find('Doctrine\Tests\Models\Cms\CmsArticle', $this->articleId);
  117. $user2 = $this->_em->find('Doctrine\Tests\Models\Cms\CmsUser', $this->user2Id);
  118. $article->user = $user2;
  119. $this->_em->flush();
  120. $this->_em->clear();
  121. $article = $this->_em
  122. ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
  123. ->setParameter('id', $this->articleId)
  124. ->getOneOrNullResult();
  125. $this->assertNotNull($article->user);
  126. $this->assertEquals($user2->id, $article->user->id);
  127. }
  128. public function testChangeLinkWithAssociationLoaded()
  129. {
  130. $this->initDb(false);
  131. // join association
  132. $article = $this->_em
  133. ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
  134. ->setParameter('id', $this->articleId)
  135. ->getOneOrNullResult();
  136. $user2 = $this->_em->find('Doctrine\Tests\Models\Cms\CmsUser', $this->user2Id);
  137. $article->user = $user2;
  138. $this->_em->flush();
  139. $this->_em->clear();
  140. $article = $this->_em
  141. ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
  142. ->setParameter('id', $this->articleId)
  143. ->getOneOrNullResult();
  144. $this->assertNotNull($article->user);
  145. $this->assertEquals($user2->id, $article->user->id);
  146. }
  147. }