Total Complexity | 80 |
Total Lines | 482 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BirdCreateModelTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BirdCreateModelTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class BirdCreateModelTest extends TestModelTest |
||
16 | { |
||
17 | protected $tweety; |
||
18 | protected $hedwig; |
||
19 | protected $timmy; |
||
20 | protected $egg1; |
||
21 | protected $egg2; |
||
22 | protected $egg3; |
||
23 | |||
24 | protected function setUp(): void |
||
25 | { |
||
26 | $this->modelParser = new TestModelParser(); |
||
27 | $this->modelInstance = $this->modelParser->getEmptyModel(); |
||
28 | |||
29 | $animals = $this->modelInstance->newInstance(Animals::class); |
||
30 | $this->modelInstance->setDocumentElement($animals); |
||
31 | |||
32 | // add a tns namespace prefix for QName testing |
||
33 | $animals->getDomElement()->registerNamespace("tns", TestModelConstants::MODEL_NAMESPACE); |
||
34 | |||
35 | $this->tweety = $this->createBird($this->modelInstance, "tweety", Gender::FEMALE); |
||
36 | $this->hedwig = $this->createBird($this->modelInstance, "hedwig", Gender::FEMALE); |
||
37 | $this->timmy = $this->createBird($this->modelInstance, "timmy", Gender::FEMALE); |
||
38 | $this->egg1 = $this->createEgg($this->modelInstance, "egg1"); |
||
39 | $this->egg1->setMother($this->tweety); |
||
40 | $this->egg1->addGuardian($this->hedwig); |
||
41 | $this->egg1->addGuardian($this->timmy); |
||
42 | |||
43 | $this->egg2 = $this->createEgg($this->modelInstance, "egg2"); |
||
44 | $this->egg2->setMother($this->tweety); |
||
45 | $this->egg2->addGuardian($this->hedwig); |
||
46 | $this->egg2->addGuardian($this->timmy); |
||
47 | |||
48 | $this->egg3 = $this->createEgg($this->modelInstance, "egg3"); |
||
49 | $this->egg3->addGuardian($this->timmy); |
||
50 | |||
51 | $this->tweety->setSpouse($this->hedwig); |
||
52 | $this->tweety->addEgg($this->egg1); |
||
53 | $this->tweety->addEgg($this->egg2); |
||
54 | $this->tweety->addEgg($this->egg3); |
||
55 | |||
56 | $this->hedwig->addGuardedEgg($this->egg1); |
||
57 | $this->hedwig->addGuardedEgg($this->egg2); |
||
58 | |||
59 | $guardEgg = $this->modelInstance->newInstance(GuardEgg::class); |
||
60 | $guardEgg->setTextContent($this->egg1->getId() . " " . $this->egg2->getId()); |
||
61 | $this->timmy->addGuardedEggRef($guardEgg); |
||
62 | $this->timmy->addGuardedEgg($this->egg3); |
||
63 | } |
||
64 | |||
65 | public function testAddEggsByHelper(): void |
||
66 | { |
||
67 | $this->assertCount(3, $this->tweety->getEggs()); |
||
68 | $eggs = [$this->egg1, $this->egg2, $this->egg3]; |
||
69 | foreach ($eggs as $egg) { |
||
70 | $exists = false; |
||
71 | foreach ($this->tweety->getEggs() as $egg2) { |
||
72 | if ($egg2->equals($egg)) { |
||
73 | $exists = true; |
||
74 | } |
||
75 | } |
||
76 | $this->assertTrue($exists); |
||
77 | } |
||
78 | |||
79 | $egg4 = $this->createEgg($this->modelInstance, "egg4"); |
||
80 | $this->tweety->addEgg($egg4); |
||
81 | $egg5 = $this->createEgg($this->modelInstance, "egg5"); |
||
82 | $this->tweety->addEgg($egg5); |
||
83 | |||
84 | $this->assertCount(5, $this->tweety->getEggs()); |
||
85 | |||
86 | $eggs = [$this->egg1, $this->egg2, $this->egg3, $egg4, $egg5]; |
||
87 | foreach ($eggs as $egg) { |
||
88 | $exists = false; |
||
89 | foreach ($this->tweety->getEggs() as $egg2) { |
||
90 | if ($egg2->equals($egg)) { |
||
91 | $exists = true; |
||
92 | } |
||
93 | } |
||
94 | $this->assertTrue($exists); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | public function testUpdateEggsByIdByHelper(): void |
||
99 | { |
||
100 | $this->egg1->setId("new-" . $this->egg1->getId()); |
||
101 | $this->egg2->setId("new-" . $this->egg2->getId()); |
||
102 | |||
103 | $this->assertCount(3, $this->tweety->getEggs()); |
||
104 | $eggs = [$this->egg1, $this->egg2, $this->egg3]; |
||
105 | foreach ($eggs as $egg) { |
||
106 | $exists = false; |
||
107 | foreach ($this->tweety->getEggs() as $egg2) { |
||
108 | if ($egg2->equals($egg)) { |
||
109 | $exists = true; |
||
110 | } |
||
111 | } |
||
112 | $this->assertTrue($exists); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | public function testUpdateEggsByIdByAttributeName(): void |
||
117 | { |
||
118 | $this->egg1->setAttributeValue("id", "new-" . $this->egg1->getId(), true); |
||
119 | $this->egg2->setAttributeValue("id", "new-" . $this->egg2->getId(), true); |
||
120 | |||
121 | $this->assertCount(3, $this->tweety->getEggs()); |
||
122 | $eggs = [$this->egg1, $this->egg2, $this->egg3]; |
||
123 | foreach ($eggs as $egg) { |
||
124 | $exists = false; |
||
125 | foreach ($this->tweety->getEggs() as $egg2) { |
||
126 | if ($egg2->equals($egg)) { |
||
127 | $exists = true; |
||
128 | } |
||
129 | } |
||
130 | $this->assertTrue($exists); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public function testUpdateEggsByReplaceElements(): void |
||
135 | { |
||
136 | $egg4 = $this->createEgg($this->modelInstance, "egg4"); |
||
137 | $egg5 = $this->createEgg($this->modelInstance, "egg5"); |
||
138 | $this->egg1->replaceWithElement($egg4); |
||
139 | $this->egg2->replaceWithElement($egg5); |
||
140 | $this->assertCount(3, $this->tweety->getEggs()); |
||
141 | $eggs = [$this->egg3, $egg4, $egg5]; |
||
142 | foreach ($eggs as $egg) { |
||
143 | $exists = false; |
||
144 | foreach ($this->tweety->getEggs() as $egg2) { |
||
145 | if ($egg2->equals($egg)) { |
||
146 | $exists = true; |
||
147 | } |
||
148 | } |
||
149 | $this->assertTrue($exists); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | public function testUpdateEggsByRemoveElement(): void |
||
154 | { |
||
155 | $this->tweety->removeEgg($this->egg1); |
||
156 | $this->assertCount(2, $this->tweety->getEggs()); |
||
157 | $eggs = [$this->egg2, $this->egg3]; |
||
158 | foreach ($eggs as $egg) { |
||
159 | $exists = false; |
||
160 | foreach ($this->tweety->getEggs() as $egg2) { |
||
161 | if ($egg2->equals($egg)) { |
||
162 | $exists = true; |
||
163 | } |
||
164 | } |
||
165 | $this->assertTrue($exists); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | public function testClearEggs(): void |
||
170 | { |
||
171 | $this->tweety->clearEggs(); |
||
172 | $this->assertEmpty($this->tweety->getEggs()); |
||
173 | } |
||
174 | |||
175 | public function testSetSpouseRefByHelper(): void |
||
176 | { |
||
177 | $this->tweety->setSpouse($this->timmy); |
||
178 | $this->assertTrue($this->tweety->getSpouse()->equals($this->timmy)); |
||
179 | } |
||
180 | |||
181 | public function testUpdateSpouseByIdHelper(): void |
||
182 | { |
||
183 | $this->hedwig->setId("new-" . $this->hedwig->getId()); |
||
184 | $this->assertTrue($this->tweety->getSpouse()->equals($this->hedwig)); |
||
185 | } |
||
186 | |||
187 | public function testUpdateSpouseByIdByAttributeName(): void |
||
188 | { |
||
189 | $this->hedwig->setAttributeValue("id", "new-" . $this->hedwig->getId(), true); |
||
190 | $this->assertTrue($this->tweety->getSpouse()->equals($this->hedwig)); |
||
191 | } |
||
192 | |||
193 | public function testUpdateSpouseByReplaceElement(): void |
||
194 | { |
||
195 | $this->hedwig->replaceWithElement($this->timmy); |
||
196 | $this->assertTrue($this->tweety->getSpouse()->equals($this->timmy)); |
||
197 | } |
||
198 | |||
199 | public function testUpdateSpouseByRemoveElement(): void |
||
200 | { |
||
201 | $animals = $this->modelInstance->getDocumentElement(); |
||
202 | $animals->removeAnimal($this->hedwig); |
||
203 | $this->assertNull($this->tweety->getSpouse()); |
||
204 | } |
||
205 | |||
206 | public function testClearSpouse(): void |
||
207 | { |
||
208 | $this->tweety->removeSpouse(); |
||
209 | $this->assertNull($this->tweety->getSpouse()); |
||
210 | } |
||
211 | |||
212 | public function testSetSpouseRefsByHelper(): void |
||
213 | { |
||
214 | $spouseRef = $this->modelInstance->newInstance(SpouseRef::class); |
||
215 | $spouseRef->setTextContent($this->timmy->getId()); |
||
216 | $this->tweety->getSpouseRef()->replaceWithElement($spouseRef); |
||
217 | $this->assertTrue($this->tweety->getSpouse()->equals($this->timmy)); |
||
218 | } |
||
219 | |||
220 | public function testSpouseRefsByTextContent(): void |
||
221 | { |
||
222 | $spouseRef = $this->tweety->getSpouseRef(); |
||
223 | $this->assertEquals($spouseRef->getTextContent(), $this->hedwig->getId()); |
||
224 | } |
||
225 | |||
226 | public function testUpdateSpouseRefsByTextContent(): void |
||
227 | { |
||
228 | $spouseRef = $this->tweety->getSpouseRef(); |
||
229 | $spouseRef->setTextContent($this->timmy->getId()); |
||
230 | $this->assertTrue($this->tweety->getSpouse()->equals($this->timmy)); |
||
231 | } |
||
232 | |||
233 | public function testUpdateSpouseRefsByTextContentWithNamespace(): void |
||
234 | { |
||
235 | $spouseRef = $this->tweety->getSpouseRef(); |
||
236 | $spouseRef->setTextContent("tns:" . $this->timmy->getId()); |
||
237 | $this->assertTrue($this->tweety->getSpouse()->equals($this->timmy)); |
||
238 | } |
||
239 | |||
240 | public function testGetMother(): void |
||
241 | { |
||
242 | $mother = $this->egg1->getMother(); |
||
243 | $this->assertTrue($mother->equals($this->tweety)); |
||
244 | |||
245 | $mother = $this->egg2->getMother(); |
||
246 | $this->assertTrue($mother->equals($this->tweety)); |
||
247 | } |
||
248 | |||
249 | public function testSetMotherRefByHelper(): void |
||
250 | { |
||
251 | $this->egg1->setMother($this->timmy); |
||
252 | $this->assertTrue($this->egg1->getMother()->equals($this->timmy)); |
||
253 | } |
||
254 | |||
255 | public function testUpdateMotherByIdHelper(): void |
||
256 | { |
||
257 | $this->tweety->setId("new-" . $this->tweety->getId()); |
||
258 | $this->assertTrue($this->egg1->getMother()->equals($this->tweety)); |
||
259 | } |
||
260 | |||
261 | public function testUpdateMotherByIdByAttributeName(): void |
||
262 | { |
||
263 | $this->tweety->setAttributeValue("id", "new-" . $this->tweety->getId(), true); |
||
264 | $this->assertTrue($this->egg1->getMother()->equals($this->tweety)); |
||
265 | } |
||
266 | |||
267 | public function testUpdateMotherByReplaceElement(): void |
||
268 | { |
||
269 | $this->tweety->replaceWithElement($this->timmy); |
||
270 | $this->assertTrue($this->egg1->getMother()->equals($this->timmy)); |
||
271 | } |
||
272 | |||
273 | public function testUpdateMotherByRemoveElement(): void |
||
274 | { |
||
275 | $this->egg1->setMother($this->hedwig); |
||
276 | $animals = $this->modelInstance->getDocumentElement(); |
||
277 | $animals->removeAnimal($this->hedwig); |
||
278 | $this->assertNull($this->egg1->getMother()); |
||
279 | } |
||
280 | |||
281 | public function testClearMother(): void |
||
282 | { |
||
283 | $this->egg1->removeMother(); |
||
284 | $this->assertNull($this->egg1->getMother()); |
||
285 | } |
||
286 | |||
287 | public function testSetMotherRefsByHelper(): void |
||
288 | { |
||
289 | $mother = $this->modelInstance->newInstance(Mother::class); |
||
290 | $mother->setHref("#" . $this->timmy->getId()); |
||
291 | $this->egg1->getMotherRef()->replaceWithElement($mother); |
||
292 | $this->assertTrue($this->egg1->getMother()->equals($this->timmy)); |
||
293 | } |
||
294 | |||
295 | public function testMotherRefsByTextContent(): void |
||
296 | { |
||
297 | $mother = $this->egg1->getMotherRef(); |
||
298 | $this->assertEquals("#" . $this->tweety->getId(), $mother->getHref()); |
||
299 | } |
||
300 | |||
301 | public function testUpdateMotherRefsByTextContent(): void |
||
302 | { |
||
303 | $mother = $this->egg1->getMotherRef(); |
||
304 | $mother->setHref("#" . $this->timmy->getId()); |
||
305 | $this->assertTrue($this->egg1->getMother()->equals($this->timmy)); |
||
306 | } |
||
307 | |||
308 | public function testGetGuards(): void |
||
309 | { |
||
310 | $guards = $this->egg1->getGuardians(); |
||
311 | $this->assertCount(2, $guards); |
||
312 | $birds = [$this->hedwig, $this->timmy]; |
||
313 | foreach ($birds as $bird) { |
||
314 | $exists = false; |
||
315 | foreach ($guards as $guard) { |
||
316 | if ($bird->equals($guard)) { |
||
317 | $exists = true; |
||
318 | } |
||
319 | } |
||
320 | $this->assertTrue($exists); |
||
321 | } |
||
322 | |||
323 | $guards = $this->egg2->getGuardians(); |
||
324 | $this->assertCount(2, $guards); |
||
325 | $birds = [$this->hedwig, $this->timmy]; |
||
326 | foreach ($birds as $bird) { |
||
327 | $exists = false; |
||
328 | foreach ($guards as $guard) { |
||
329 | if ($bird->equals($guard)) { |
||
330 | $exists = true; |
||
331 | } |
||
332 | } |
||
333 | $this->assertTrue($exists); |
||
334 | } |
||
335 | } |
||
336 | |||
337 | public function testAddGuardianRefsByHelper(): void |
||
338 | { |
||
339 | $this->assertCount(2, $this->egg1->getGuardianRefs()); |
||
340 | |||
341 | $tweetyGuardian = $this->modelInstance->newInstance(Guardian::class); |
||
342 | $tweetyGuardian->setHref("#" . $this->tweety->getId()); |
||
343 | $this->egg1->addGuardianRef($tweetyGuardian); |
||
344 | |||
345 | $this->assertCount(3, $this->egg1->getGuardianRefs()); |
||
346 | $exists = false; |
||
347 | foreach ($this->egg1->getGuardianRefs() as $ref) { |
||
348 | if ($ref->equals($tweetyGuardian)) { |
||
349 | $exists = true; |
||
350 | } |
||
351 | } |
||
352 | $this->assertTrue($exists); |
||
353 | } |
||
354 | |||
355 | public function testGuardianRefsByTextContent(): void |
||
367 | } |
||
368 | |||
369 | public function testUpdateGuardianRefsByTextContent(): void |
||
370 | { |
||
371 | $guardianRefs = $this->egg1->getGuardianRefs(); |
||
372 | |||
373 | $guardianRefs[0]->setHref("#" . $this->tweety->getId()); |
||
374 | |||
375 | $birds = [$this->tweety, $this->timmy]; |
||
376 | foreach ($birds as $bird) { |
||
377 | $exists = false; |
||
378 | foreach ($this->egg1->getGuardians() as $guard) { |
||
379 | if ($bird->equals($guard)) { |
||
380 | $exists = true; |
||
381 | } |
||
382 | } |
||
383 | $this->assertTrue($exists); |
||
384 | } |
||
385 | } |
||
386 | |||
387 | public function testUpdateGuardianRefsByRemoveElements(): void |
||
388 | { |
||
389 | $guardianRefs = $this->egg1->getGuardianRefs(); |
||
390 | $this->egg1->removeGuardianRef($guardianRefs[1]); |
||
391 | $this->assertCount(1, $this->egg1->getGuardians()); |
||
392 | } |
||
393 | |||
394 | public function testClearGuardianRefs(): void |
||
402 | } |
||
403 | |||
404 | public function testGetGuardedEggs(): void |
||
405 | { |
||
417 | } |
||
418 | } |
||
419 | |||
420 | public function testAddGuardedEggRefsByHelper(): void |
||
421 | { |
||
422 | $this->assertCount(2, $this->hedwig->getGuardedEggRefs()); |
||
423 | |||
424 | $egg3GuardedEgg = $this->modelInstance->newInstance(GuardEgg::class); |
||
425 | $egg3GuardedEgg->setTextContent($this->egg3->getId()); |
||
426 | $this->hedwig->addGuardedEggRef($egg3GuardedEgg); |
||
427 | |||
428 | $this->assertCount(3, $this->hedwig->getGuardedEggRefs()); |
||
429 | |||
430 | $exists = false; |
||
431 | foreach ($this->hedwig->getGuardedEggRefs() as $ref) { |
||
432 | if ($ref->equals($egg3GuardedEgg)) { |
||
433 | $exists = true; |
||
434 | } |
||
435 | } |
||
436 | $this->assertTrue($exists); |
||
437 | } |
||
438 | |||
439 | public function testGuardedEggRefsByTextContent(): void |
||
440 | { |
||
441 | $guardianRefs = $this->timmy->getGuardedEggRefs(); |
||
442 | $textContents = []; |
||
443 | foreach ($guardianRefs as $guardianRef) { |
||
444 | $textContent = $guardianRef->getTextContent(); |
||
445 | $this->assertFalse(empty($textContent)); |
||
446 | $textContents = array_merge($textContents, StringUtil::splitListBySeparator($textContent, " ")); |
||
447 | } |
||
448 | $this->assertCount(3, $textContents); |
||
449 | $this->assertContains($this->egg1->getId(), $textContents); |
||
450 | $this->assertContains($this->egg2->getId(), $textContents); |
||
451 | $this->assertContains($this->egg3->getId(), $textContents); |
||
452 | } |
||
453 | |||
454 | public function testUpdateGuardedEggRefsByTextContent(): void |
||
471 | } |
||
472 | } |
||
473 | |||
474 | public function testUpdateGuardedEggRefsByRemoveElements(): void |
||
475 | { |
||
476 | $guardianRefs = $this->timmy->getGuardedEggRefs(); |
||
477 | $this->timmy->removeGuardedEggRef($guardianRefs[0]); |
||
478 | |||
487 | } |
||
488 | |||
489 | public function testClearGuardedEggRefs(): void |
||
490 | { |
||
491 | $this->timmy->clearGuardedEggRefs(); |
||
492 | $this->assertEmpty($this->timmy->getGuardedEggRefs()); |
||
493 | |||
494 | // should not affect animals collection |
||
495 | $animals = $this->modelInstance->getDocumentElement(); |
||
496 | $this->assertCount(3, $animals->getAnimals()); |
||
497 | } |
||
498 | } |
||
499 |