Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC758Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
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;
9
10 require_once __DIR__ . '/../../../TestInit.php';
11
12 class DDC758Test extends \Doctrine\Tests\OrmFunctionalTestCase
13 {
14
15     public function setUp()
16     {
17         $this->markTestSkipped('Destroys testsuite');
18         $this->useModelSet("cms");
19
20         parent::setUp();
21     }
22
23     /**
24      * Helper method to set cascade to merge only
25      */
26     private function setCascadeMergeFor($class)
27     {
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;
34         }
35     }
36
37     /**
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.
41      */
42     public function testManyToManyMergeAssociationAdds()
43     {
44         $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsUser');
45         $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsGroup');
46
47         // Put entities in the database
48         $cmsUser = new CmsUser();
49         $cmsUser->username = "dave";
50         $cmsUser->name = "Dave Keen";
51         $cmsUser->status = "testing";
52
53         $group1 = new CmsGroup();
54         $group1->name = "Group 1";
55
56         $group2 = new CmsGroup();
57         $group2->name = "Group 2";
58
59         $this->_em->persist($cmsUser);
60         $this->_em->persist($group1);
61         $this->_em->persist($group2);
62         $this->_em->flush();
63
64         $cmsUserId = $cmsUser->id;
65         $group1Id = $group1->id;
66         $group2Id = $group2->id;
67
68         $this->_em->clear();
69
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();
77
78         $group1 = new CmsGroup();
79         $group1->id = $group1Id;
80         $group1->name = "Group 1";
81         $group1->users = new ArrayCollection();
82
83         $group2 = new CmsGroup();
84         $group2->id = $group2Id;
85         $group2->name = "Group 2";
86         $group2->users = new ArrayCollection();
87
88         $cmsUser->addGroup($group1);
89         $cmsUser->addGroup($group2);
90
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);
93         $this->_em->flush();
94
95         $this->_em->clear();
96
97         $cmsUsers = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findAll();
98         $cmsGroups = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsGroup')->findAll();
99
100         // Check the entities are in the database
101         $this->assertEquals(1, sizeof($cmsUsers));
102         $this->assertEquals(2, sizeof($cmsGroups));
103
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));
108
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]);
113     }
114
115     /**
116      * Test that changing associations on detached entities and then cascade merging them causes the
117      * database to be updated with the new associations.
118      */
119     public function testManyToManyMergeAssociationRemoves()
120     {
121         $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsUser');
122         $this->setCascadeMergeFor('Doctrine\Tests\Models\CMS\CmsGroup');
123
124         $cmsUser = new CmsUser();
125         $cmsUser->username = "dave";
126         $cmsUser->name = "Dave Keen";
127         $cmsUser->status = "testing";
128
129         $group1 = new CmsGroup();
130         $group1->name = "Group 1";
131
132         $group2 = new CmsGroup();
133         $group2->name = "Group 2";
134
135         $cmsUser->addGroup($group1);
136         $cmsUser->addGroup($group2);
137
138         $this->_em->persist($cmsUser);
139         $this->_em->persist($group1);
140         $this->_em->persist($group2);
141         $this->_em->flush();
142
143         $cmsUserId = $cmsUser->id;
144         $group1Id = $group1->id;
145         $group2Id = $group2->id;
146
147         $this->_em->clear();
148
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();
156
157         $group1 = new CmsGroup();
158         $group1->id = $group1Id;
159         $group1->name = "Group 1";
160         $group1->users = new ArrayCollection();
161
162         $group2 = new CmsGroup();
163         $group2->id = $group2Id;
164         $group2->name = "Group 2";
165         $group2->users = new ArrayCollection();
166
167         // Cascade merge of cmsUser followed by a flush should result in the association array collection being empty
168         $this->_em->merge($cmsUser);
169         $this->_em->flush();
170
171         $this->_em->clear();
172
173         $cmsUsers = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findAll();
174         $cmsGroups = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsGroup')->findAll();
175
176         // Check the entities are in the database
177         $this->assertEquals(1, sizeof($cmsUsers));
178         $this->assertEquals(2, sizeof($cmsGroups));
179
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));
184     }
185 }