DDC758Test.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Tests\Models\CMS\CmsUser;
  5. use Doctrine\Tests\Models\CMS\CmsPhonenumber;
  6. use Doctrine\Tests\Models\CMS\CmsGroup;
  7. require_once __DIR__ . '/../../../TestInit.php';
  8. class DDC758Test extends \Doctrine\Tests\OrmFunctionalTestCase
  9. {
  10. public function setUp()
  11. {
  12. $this->markTestSkipped('Destroys testsuite');
  13. $this->useModelSet("cms");
  14. parent::setUp();
  15. }
  16. /**
  17. * Helper method to set cascade to merge only
  18. */
  19. private function setCascadeMergeFor($class)
  20. {
  21. $metadata = $this->_em->getMetadataFactory()->getMetaDataFor($class);
  22. foreach ($metadata->associationMappings as $key => $associationMapping) {
  23. $metadata->associationMappings[$key]["isCascadePersist"] = false;
  24. $metadata->associationMappings[$key]["isCascadeMerge"] = true;
  25. $metadata->associationMappings[$key]["isCascadeRemove"] = false;
  26. $metadata->associationMappings[$key]["isCascadeDetach"] = false;
  27. }
  28. }
  29. /**
  30. * Test that changing associations on detached entities and then cascade merging them
  31. * causes the database to be updated with the new associations.
  32. * This specifically tests adding new associations.
  33. */
  34. public function testManyToManyMergeAssociationAdds()
  35. {
  36. $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsUser');
  37. $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsGroup');
  38. // Put entities in the database
  39. $cmsUser = new CmsUser();
  40. $cmsUser->username = "dave";
  41. $cmsUser->name = "Dave Keen";
  42. $cmsUser->status = "testing";
  43. $group1 = new CmsGroup();
  44. $group1->name = "Group 1";
  45. $group2 = new CmsGroup();
  46. $group2->name = "Group 2";
  47. $this->_em->persist($cmsUser);
  48. $this->_em->persist($group1);
  49. $this->_em->persist($group2);
  50. $this->_em->flush();
  51. $cmsUserId = $cmsUser->id;
  52. $group1Id = $group1->id;
  53. $group2Id = $group2->id;
  54. $this->_em->clear();
  55. // Now create detached versions of the entities with some new associations.
  56. $cmsUser = new CmsUser();
  57. $cmsUser->id = $cmsUserId;
  58. $cmsUser->username = "dave";
  59. $cmsUser->name = "Dave Keen";
  60. $cmsUser->status = "testing";
  61. $cmsUser->groups = new ArrayCollection();
  62. $group1 = new CmsGroup();
  63. $group1->id = $group1Id;
  64. $group1->name = "Group 1";
  65. $group1->users = new ArrayCollection();
  66. $group2 = new CmsGroup();
  67. $group2->id = $group2Id;
  68. $group2->name = "Group 2";
  69. $group2->users = new ArrayCollection();
  70. $cmsUser->addGroup($group1);
  71. $cmsUser->addGroup($group2);
  72. // Cascade merge of cmsUser followed by a flush should add in the birectional new many-to-many associations between the user and the groups
  73. $this->_em->merge($cmsUser);
  74. $this->_em->flush();
  75. $this->_em->clear();
  76. $cmsUsers = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findAll();
  77. $cmsGroups = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsGroup')->findAll();
  78. // Check the entities are in the database
  79. $this->assertEquals(1, sizeof($cmsUsers));
  80. $this->assertEquals(2, sizeof($cmsGroups));
  81. // Check the associations between the entities are now in the database
  82. $this->assertEquals(2, sizeof($cmsUsers[0]->groups));
  83. $this->assertEquals(1, sizeof($cmsGroups[0]->users));
  84. $this->assertEquals(1, sizeof($cmsGroups[1]->users));
  85. $this->assertSame($cmsUsers[0]->groups[0], $cmsGroups[0]);
  86. $this->assertSame($cmsUsers[0]->groups[1], $cmsGroups[1]);
  87. $this->assertSame($cmsGroups[0]->users[0], $cmsUsers[0]);
  88. $this->assertSame($cmsGroups[1]->users[0], $cmsUsers[0]);
  89. }
  90. /**
  91. * Test that changing associations on detached entities and then cascade merging them causes the
  92. * database to be updated with the new associations.
  93. */
  94. public function testManyToManyMergeAssociationRemoves()
  95. {
  96. $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsUser');
  97. $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsGroup');
  98. $cmsUser = new CmsUser();
  99. $cmsUser->username = "dave";
  100. $cmsUser->name = "Dave Keen";
  101. $cmsUser->status = "testing";
  102. $group1 = new CmsGroup();
  103. $group1->name = "Group 1";
  104. $group2 = new CmsGroup();
  105. $group2->name = "Group 2";
  106. $cmsUser->addGroup($group1);
  107. $cmsUser->addGroup($group2);
  108. $this->_em->persist($cmsUser);
  109. $this->_em->persist($group1);
  110. $this->_em->persist($group2);
  111. $this->_em->flush();
  112. $cmsUserId = $cmsUser->id;
  113. $group1Id = $group1->id;
  114. $group2Id = $group2->id;
  115. $this->_em->clear();
  116. // Now create detached versions of the entities with NO associations.
  117. $cmsUser = new CmsUser();
  118. $cmsUser->id = $cmsUserId;
  119. $cmsUser->username = "dave";
  120. $cmsUser->name = "Dave Keen";
  121. $cmsUser->status = "testing";
  122. $cmsUser->groups = new ArrayCollection();
  123. $group1 = new CmsGroup();
  124. $group1->id = $group1Id;
  125. $group1->name = "Group 1";
  126. $group1->users = new ArrayCollection();
  127. $group2 = new CmsGroup();
  128. $group2->id = $group2Id;
  129. $group2->name = "Group 2";
  130. $group2->users = new ArrayCollection();
  131. // Cascade merge of cmsUser followed by a flush should result in the association array collection being empty
  132. $this->_em->merge($cmsUser);
  133. $this->_em->flush();
  134. $this->_em->clear();
  135. $cmsUsers = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findAll();
  136. $cmsGroups = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsGroup')->findAll();
  137. // Check the entities are in the database
  138. $this->assertEquals(1, sizeof($cmsUsers));
  139. $this->assertEquals(2, sizeof($cmsGroups));
  140. // Check the associations between the entities are now in the database
  141. $this->assertEquals(0, sizeof($cmsUsers[0]->groups));
  142. $this->assertEquals(0, sizeof($cmsGroups[0]->users));
  143. $this->assertEquals(0, sizeof($cmsGroups[1]->users));
  144. }
  145. }