Conditions | 1 |
Paths | 1 |
Total Lines | 65 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
124 | public function testManyToManyMergeAssociationRemoves() |
||
125 | { |
||
126 | $this->setCascadeMergeFor(CmsUser::class); |
||
127 | $this->setCascadeMergeFor(CmsGroup::class); |
||
128 | |||
129 | $cmsUser = new CmsUser(); |
||
130 | $cmsUser->username = "dave"; |
||
131 | $cmsUser->name = "Dave Keen"; |
||
132 | $cmsUser->status = "testing"; |
||
133 | |||
134 | $group1 = new CmsGroup(); |
||
135 | $group1->name = "Group 1"; |
||
136 | |||
137 | $group2 = new CmsGroup(); |
||
138 | $group2->name = "Group 2"; |
||
139 | |||
140 | $cmsUser->addGroup($group1); |
||
141 | $cmsUser->addGroup($group2); |
||
142 | |||
143 | $this->_em->persist($cmsUser); |
||
144 | $this->_em->persist($group1); |
||
145 | $this->_em->persist($group2); |
||
146 | $this->_em->flush(); |
||
147 | |||
148 | $cmsUserId = $cmsUser->id; |
||
149 | $group1Id = $group1->id; |
||
150 | $group2Id = $group2->id; |
||
151 | |||
152 | $this->_em->clear(); |
||
153 | |||
154 | // Now create detached versions of the entities with NO associations. |
||
155 | $cmsUser = new CmsUser(); |
||
156 | $cmsUser->id = $cmsUserId; |
||
157 | $cmsUser->username = "dave"; |
||
158 | $cmsUser->name = "Dave Keen"; |
||
159 | $cmsUser->status = "testing"; |
||
160 | $cmsUser->groups = new ArrayCollection(); |
||
161 | |||
162 | $group1 = new CmsGroup(); |
||
163 | $group1->id = $group1Id; |
||
164 | $group1->name = "Group 1"; |
||
165 | $group1->users = new ArrayCollection(); |
||
166 | |||
167 | $group2 = new CmsGroup(); |
||
168 | $group2->id = $group2Id; |
||
169 | $group2->name = "Group 2"; |
||
170 | $group2->users = new ArrayCollection(); |
||
171 | |||
172 | // Cascade merge of cmsUser followed by a flush should result in the association array collection being empty |
||
173 | $this->_em->merge($cmsUser); |
||
174 | $this->_em->flush(); |
||
175 | |||
176 | $this->_em->clear(); |
||
177 | |||
178 | $cmsUsers = $this->_em->getRepository(CmsUser::class)->findAll(); |
||
179 | $cmsGroups = $this->_em->getRepository(CmsGroup::class)->findAll(); |
||
180 | |||
181 | // Check the entities are in the database |
||
182 | $this->assertEquals(1, sizeof($cmsUsers)); |
||
183 | $this->assertEquals(2, sizeof($cmsGroups)); |
||
184 | |||
185 | // Check the associations between the entities are now in the database |
||
186 | $this->assertEquals(0, sizeof($cmsUsers[0]->groups)); |
||
187 | $this->assertEquals(0, sizeof($cmsGroups[0]->users)); |
||
188 | $this->assertEquals(0, sizeof($cmsGroups[1]->users)); |
||
189 | } |
||
191 |