3 namespace Doctrine\Tests\ORM\Functional\Ticket;
5 use Doctrine\Common\Collections\ArrayCollection;
6 use Doctrine\Tests\Models\CMS\CmsUser;
7 use Doctrine\Tests\Models\CMS\CmsPhonenumber;
8 use Doctrine\Tests\Models\CMS\CmsGroup;
10 require_once __DIR__ . '/../../../TestInit.php';
12 class DDC758Test extends \Doctrine\Tests\OrmFunctionalTestCase
15 public function setUp()
17 $this->markTestSkipped('Destroys testsuite');
18 $this->useModelSet("cms");
24 * Helper method to set cascade to merge only
26 private function setCascadeMergeFor($class)
28 $metadata = $this->_em->getMetadataFactory()->getMetaDataFor($class);
29 foreach ($metadata->associationMappings as $key => $associationMapping) {
30 $metadata->associationMappings[$key]["isCascadePersist"] = false;
31 $metadata->associationMappings[$key]["isCascadeMerge"] = true;
32 $metadata->associationMappings[$key]["isCascadeRemove"] = false;
33 $metadata->associationMappings[$key]["isCascadeDetach"] = false;
38 * Test that changing associations on detached entities and then cascade merging them
39 * causes the database to be updated with the new associations.
40 * This specifically tests adding new associations.
42 public function testManyToManyMergeAssociationAdds()
44 $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsUser');
45 $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsGroup');
47 // Put entities in the database
48 $cmsUser = new CmsUser();
49 $cmsUser->username = "dave";
50 $cmsUser->name = "Dave Keen";
51 $cmsUser->status = "testing";
53 $group1 = new CmsGroup();
54 $group1->name = "Group 1";
56 $group2 = new CmsGroup();
57 $group2->name = "Group 2";
59 $this->_em->persist($cmsUser);
60 $this->_em->persist($group1);
61 $this->_em->persist($group2);
64 $cmsUserId = $cmsUser->id;
65 $group1Id = $group1->id;
66 $group2Id = $group2->id;
70 // Now create detached versions of the entities with some new associations.
71 $cmsUser = new CmsUser();
72 $cmsUser->id = $cmsUserId;
73 $cmsUser->username = "dave";
74 $cmsUser->name = "Dave Keen";
75 $cmsUser->status = "testing";
76 $cmsUser->groups = new ArrayCollection();
78 $group1 = new CmsGroup();
79 $group1->id = $group1Id;
80 $group1->name = "Group 1";
81 $group1->users = new ArrayCollection();
83 $group2 = new CmsGroup();
84 $group2->id = $group2Id;
85 $group2->name = "Group 2";
86 $group2->users = new ArrayCollection();
88 $cmsUser->addGroup($group1);
89 $cmsUser->addGroup($group2);
91 // Cascade merge of cmsUser followed by a flush should add in the birectional new many-to-many associations between the user and the groups
92 $this->_em->merge($cmsUser);
97 $cmsUsers = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findAll();
98 $cmsGroups = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsGroup')->findAll();
100 // Check the entities are in the database
101 $this->assertEquals(1, sizeof($cmsUsers));
102 $this->assertEquals(2, sizeof($cmsGroups));
104 // Check the associations between the entities are now in the database
105 $this->assertEquals(2, sizeof($cmsUsers[0]->groups));
106 $this->assertEquals(1, sizeof($cmsGroups[0]->users));
107 $this->assertEquals(1, sizeof($cmsGroups[1]->users));
109 $this->assertSame($cmsUsers[0]->groups[0], $cmsGroups[0]);
110 $this->assertSame($cmsUsers[0]->groups[1], $cmsGroups[1]);
111 $this->assertSame($cmsGroups[0]->users[0], $cmsUsers[0]);
112 $this->assertSame($cmsGroups[1]->users[0], $cmsUsers[0]);
116 * Test that changing associations on detached entities and then cascade merging them causes the
117 * database to be updated with the new associations.
119 public function testManyToManyMergeAssociationRemoves()
121 $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsUser');
122 $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsGroup');
124 $cmsUser = new CmsUser();
125 $cmsUser->username = "dave";
126 $cmsUser->name = "Dave Keen";
127 $cmsUser->status = "testing";
129 $group1 = new CmsGroup();
130 $group1->name = "Group 1";
132 $group2 = new CmsGroup();
133 $group2->name = "Group 2";
135 $cmsUser->addGroup($group1);
136 $cmsUser->addGroup($group2);
138 $this->_em->persist($cmsUser);
139 $this->_em->persist($group1);
140 $this->_em->persist($group2);
143 $cmsUserId = $cmsUser->id;
144 $group1Id = $group1->id;
145 $group2Id = $group2->id;
149 // Now create detached versions of the entities with NO associations.
150 $cmsUser = new CmsUser();
151 $cmsUser->id = $cmsUserId;
152 $cmsUser->username = "dave";
153 $cmsUser->name = "Dave Keen";
154 $cmsUser->status = "testing";
155 $cmsUser->groups = new ArrayCollection();
157 $group1 = new CmsGroup();
158 $group1->id = $group1Id;
159 $group1->name = "Group 1";
160 $group1->users = new ArrayCollection();
162 $group2 = new CmsGroup();
163 $group2->id = $group2Id;
164 $group2->name = "Group 2";
165 $group2->users = new ArrayCollection();
167 // Cascade merge of cmsUser followed by a flush should result in the association array collection being empty
168 $this->_em->merge($cmsUser);
173 $cmsUsers = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findAll();
174 $cmsGroups = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsGroup')->findAll();
176 // Check the entities are in the database
177 $this->assertEquals(1, sizeof($cmsUsers));
178 $this->assertEquals(2, sizeof($cmsGroups));
180 // Check the associations between the entities are now in the database
181 $this->assertEquals(0, sizeof($cmsUsers[0]->groups));
182 $this->assertEquals(0, sizeof($cmsGroups[0]->users));
183 $this->assertEquals(0, sizeof($cmsGroups[1]->users));