Total Complexity | 43 |
Total Lines | 565 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like NamespaceHelperTest 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 NamespaceHelperTest, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
23 | class NamespaceHelperTest extends AbstractTest |
||
24 | { |
||
25 | public const WORK_DIR = AbstractTest::VAR_PATH . '/' . self::TEST_TYPE_LARGE . '/NamespaceHelperTest'; |
||
26 | |||
27 | public const TEST_ENTITY_FQN_BASE = self::TEST_PROJECT_ROOT_NAMESPACE . |
||
28 | '\\' . |
||
29 | AbstractGenerator::ENTITIES_FOLDER_NAME; |
||
30 | |||
31 | public const TEST_ENTITIES = [ |
||
32 | self::TEST_ENTITY_FQN_BASE . '\\Blah\\Foo', |
||
33 | self::TEST_ENTITY_FQN_BASE . '\\Bar\\Baz', |
||
34 | self::TEST_ENTITY_FQN_BASE . '\\No\\Relative', |
||
35 | self::TEST_ENTITY_FQN_BASE . '\\Meh', |
||
36 | self::TEST_ENTITY_FQN_BASE . '\\Nested\\Something\\Ho\\Hum', |
||
37 | ]; |
||
38 | |||
39 | public const TEST_ENTITY_POST_CREATED = self::TEST_ENTITY_FQN_BASE . '\\Meh'; |
||
40 | public const TEST_ENTITY_POST_CREATED_NESTED = self::TEST_ENTITY_FQN_BASE . '\\Nested\\Something\\Ho\\Hum'; |
||
41 | protected static $buildOnce = true; |
||
42 | /** |
||
43 | * @var NamespaceHelper |
||
44 | */ |
||
45 | private static $helper; |
||
46 | |||
47 | public static function setupBeforeClass() |
||
48 | { |
||
49 | parent::setUpBeforeClass(); |
||
50 | self::$helper = new NamespaceHelper(); |
||
51 | } |
||
52 | |||
53 | public function setup() |
||
54 | { |
||
55 | parent::setup(); |
||
56 | if (true === self::$built) { |
||
57 | return; |
||
58 | } |
||
59 | $entityGenerator = $this->getEntityGenerator(); |
||
60 | $relationsGenerator = $this->getRelationsGenerator(); |
||
61 | foreach (self::TEST_ENTITIES as $fqn) { |
||
62 | $entityGenerator->generateEntity($fqn); |
||
63 | $relationsGenerator->generateRelationCodeForEntity($fqn); |
||
64 | } |
||
65 | $relationsGenerator->setEntityHasRelationToEntity( |
||
66 | self::TEST_ENTITIES[0], |
||
67 | RelationsGenerator::HAS_MANY_TO_MANY, |
||
68 | self::TEST_ENTITIES[1] |
||
69 | ); |
||
70 | self::$built = true; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @test |
||
75 | * @small |
||
76 | */ |
||
77 | public function getFixtureFqnFromEntityFqn() |
||
78 | { |
||
79 | $expected = self::TEST_PROJECT_ROOT_NAMESPACE . '\\Assets\\EntityFixtures\\Blah\\FooFixture'; |
||
80 | $actual = self::$helper->getFixtureFqnFromEntityFqn(self::TEST_ENTITIES[0]); |
||
81 | self::assertSame($expected, $actual); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @test |
||
86 | * @large |
||
87 | * @covers ::cropSuffix |
||
88 | */ |
||
89 | public function cropSuffix(): void |
||
90 | { |
||
91 | $fqn = 'FooBar'; |
||
92 | $suffix = 'Bar'; |
||
93 | $expected = 'Foo'; |
||
94 | $actual = self::$helper->cropSuffix($fqn, $suffix); |
||
95 | self::assertSame($expected, $actual); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @test |
||
100 | * @large |
||
101 | * @covers ::swapSuffix |
||
102 | */ |
||
103 | public function swapSuffix(): void |
||
104 | { |
||
105 | $fqn = 'FooBar'; |
||
106 | $currentSuffix = 'Bar'; |
||
107 | $newSuffix = 'Baz'; |
||
108 | $expected = 'FooBaz'; |
||
109 | $actual = self::$helper->swapSuffix($fqn, $currentSuffix, $newSuffix); |
||
110 | self::assertSame($expected, $actual); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @test |
||
115 | * @large |
||
116 | * @covers ::cropSuffix |
||
117 | */ |
||
118 | public function cropSuffixWhereSuffixNotInThere(): void |
||
119 | { |
||
120 | $fqn = 'FooBar'; |
||
121 | $suffix = 'Cheese'; |
||
122 | $expected = 'FooBar'; |
||
123 | $actual = self::$helper->cropSuffix($fqn, $suffix); |
||
124 | self::assertSame($expected, $actual); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @test |
||
129 | * @large |
||
130 | * @covers ::getObjectShortName |
||
131 | */ |
||
132 | public function getObjectShortName(): void |
||
133 | { |
||
134 | |||
135 | $expectedToObjects = [ |
||
136 | 'NamespaceHelperTest' => $this, |
||
137 | 'NamespaceHelper' => self::$helper, |
||
138 | ]; |
||
139 | $actual = []; |
||
140 | foreach ($expectedToObjects as $object) { |
||
141 | $actual[self::$helper->getObjectShortName($object)] = $object; |
||
142 | } |
||
143 | self::assertSame($expectedToObjects, $actual); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @test |
||
148 | * @large |
||
149 | * @covers ::getObjectFqn |
||
150 | */ |
||
151 | public function getObjectFqn(): void |
||
152 | { |
||
153 | |||
154 | $expectedToObjects = [ |
||
155 | \get_class($this) => $this, |
||
156 | \get_class(self::$helper) => self::$helper, |
||
157 | ]; |
||
158 | $actual = []; |
||
159 | foreach ($expectedToObjects as $object) { |
||
160 | $actual[self::$helper->getObjectFqn($object)] = $object; |
||
161 | } |
||
162 | self::assertSame($expectedToObjects, $actual); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @test |
||
167 | * @large |
||
168 | * @covers ::getClassShortName |
||
169 | */ |
||
170 | public function getClassShortName(): void |
||
171 | { |
||
172 | $expectedToFqns = [ |
||
173 | 'NamespaceHelperTest' => \get_class($this), |
||
174 | 'Cheese' => '\\Super\\Cheese', |
||
175 | ]; |
||
176 | $actual = []; |
||
177 | foreach ($expectedToFqns as $fqn) { |
||
178 | $actual[self::$helper->getClassShortName($fqn)] = $fqn; |
||
179 | } |
||
180 | self::assertSame($expectedToFqns, $actual); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @test |
||
185 | * @large |
||
186 | * @covers ::getFakerProviderFqnFromFieldTraitReflection |
||
187 | */ |
||
188 | public function getFakerProviderFqnFromFieldTraitReflection(): void |
||
189 | { |
||
190 | $expected = [ |
||
191 | BusinessIdentifierCodeFieldTrait::class => BusinessIdentifierCodeFakerData::class, |
||
192 | CountryCodeFieldTrait::class => CountryCodeFakerData::class, |
||
193 | ]; |
||
194 | $actual = []; |
||
195 | foreach (array_keys($expected) as $fieldFqn) { |
||
196 | $actual[$fieldFqn] = self::$helper->getFakerProviderFqnFromFieldTraitReflection( |
||
197 | new \ts\Reflection\ReflectionClass($fieldFqn) |
||
198 | ); |
||
199 | } |
||
200 | self::assertSame($expected, $actual); |
||
201 | } |
||
202 | |||
203 | public function testTidy(): void |
||
204 | { |
||
205 | $namespaceToExpected = [ |
||
206 | 'Test\\\\Multiple\\\\\\\Separators' => 'Test\\Multiple\\Separators', |
||
207 | 'No\\Changes\\Required' => 'No\\Changes\\Required', |
||
208 | ]; |
||
209 | foreach ($namespaceToExpected as $namespace => $expected) { |
||
210 | self::assertSame($expected, self::$helper->tidy($namespace)); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | public function testRoot(): void |
||
215 | { |
||
216 | $namespaceToExpected = [ |
||
217 | '\\Test\\\\Multiple\\\\\\\Separators' => 'Test\\Multiple\\Separators', |
||
218 | 'No\\Changes\\Required' => 'No\\Changes\\Required', |
||
219 | ]; |
||
220 | foreach ($namespaceToExpected as $namespace => $expected) { |
||
221 | self::assertSame($expected, self::$helper->root($namespace)); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @test |
||
227 | * @large |
||
228 | */ |
||
229 | public function testCalculateProjectNamespaceRootFromEntitFqn(): void |
||
230 | { |
||
231 | $entity1Fqn = self::TEST_ENTITIES[0]; |
||
232 | |||
233 | $expected = self::TEST_PROJECT_ROOT_NAMESPACE; |
||
234 | $actual = self::$helper->getProjectNamespaceRootFromEntityFqn($entity1Fqn); |
||
235 | self::assertSame($expected, $actual); |
||
236 | |||
237 | $entityFqnWithEntitiesInProjectName = self::TEST_ENTITIES[0]; |
||
238 | $expected = self::TEST_PROJECT_ROOT_NAMESPACE; |
||
239 | $actual = self::$helper->getProjectNamespaceRootFromEntityFqn( |
||
240 | $entityFqnWithEntitiesInProjectName |
||
241 | ); |
||
242 | self::assertSame($expected, $actual); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
||
247 | */ |
||
248 | public function testParseFullyQualifiedName(): void |
||
249 | { |
||
250 | $entity1Fqn = self::TEST_ENTITIES[0]; |
||
251 | $srcOrTestSubFolder = 'src'; |
||
252 | $projectRootNamespace = self::TEST_PROJECT_ROOT_NAMESPACE; |
||
253 | $expected = [ |
||
254 | 'Foo', |
||
255 | $projectRootNamespace . '\\' . AbstractGenerator::ENTITIES_FOLDER_NAME . '\\Blah', |
||
256 | [ |
||
257 | 'src', |
||
258 | 'Entities', |
||
259 | 'Blah', |
||
260 | ], |
||
261 | ]; |
||
262 | $actual = self::$helper->parseFullyQualifiedName( |
||
263 | $entity1Fqn, |
||
264 | $srcOrTestSubFolder, |
||
265 | $projectRootNamespace |
||
266 | ); |
||
267 | self::assertSame($expected, $actual); |
||
268 | |||
269 | $srcOrTestSubFolder = 'src'; |
||
270 | $projectRootNamespace = '\\' . self::TEST_PROJECT_ROOT_NAMESPACE; |
||
271 | $expected = [ |
||
272 | 'Foo', |
||
273 | ltrim($projectRootNamespace . '\\' . AbstractGenerator::ENTITIES_FOLDER_NAME . '\\Blah', '\\'), |
||
274 | [ |
||
275 | 'src', |
||
276 | 'Entities', |
||
277 | 'Blah', |
||
278 | ], |
||
279 | ]; |
||
280 | $actual = self::$helper->parseFullyQualifiedName( |
||
281 | self::TEST_ENTITIES[0], |
||
282 | $srcOrTestSubFolder, |
||
283 | $projectRootNamespace |
||
284 | ); |
||
285 | self::assertSame($expected, $actual); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @test |
||
290 | * @large |
||
291 | */ |
||
292 | public function testCalculcateOwnedHasName(): void |
||
293 | { |
||
294 | $hasType = RelationsGenerator::HAS_MANY_TO_MANY; |
||
295 | $ownedEntityFqn = self::TEST_ENTITIES[0]; |
||
296 | $expected = 'BlahFoos'; |
||
297 | $srcOrTestSubFolder = 'src'; |
||
298 | $projectRootNamespace = '\\' . self::TEST_PROJECT_ROOT_NAMESPACE; |
||
299 | |||
300 | $actual = self::$helper->getOwnedHasName( |
||
301 | $hasType, |
||
302 | $ownedEntityFqn, |
||
303 | $srcOrTestSubFolder, |
||
304 | $projectRootNamespace |
||
305 | ); |
||
306 | |||
307 | self::assertSame($expected, $actual); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @test |
||
312 | * @large |
||
313 | */ |
||
314 | public function testGetEntitySubNamespace(): void |
||
315 | { |
||
316 | $entityFqn = self::TEST_ENTITIES[0]; |
||
317 | $expected = 'Blah\\Foo'; |
||
318 | $actual = self::$helper->getEntitySubNamespace($entityFqn); |
||
319 | self::assertSame($expected, $actual); |
||
320 | |||
321 | $entityFqn = '\\My\\Test\\Project\\Entities\\No\\Relatives'; |
||
322 | $expected = 'No\\Relatives'; |
||
323 | $actual = self::$helper->getEntitySubNamespace($entityFqn); |
||
324 | self::assertSame($expected, $actual); |
||
325 | |||
326 | $entityFqn = '\\My\\Test\\Project\\Entities\\Person'; |
||
327 | $expected = 'Person'; |
||
328 | $actual = self::$helper->getEntitySubNamespace($entityFqn); |
||
329 | self::assertSame($expected, $actual); |
||
330 | |||
331 | $entityFqn = '\\My\\Test\\EntitiesProject\\Entities\\Person'; |
||
332 | $expected = 'Person'; |
||
333 | $actual = self::$helper->getEntitySubNamespace($entityFqn); |
||
334 | self::assertSame($expected, $actual); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @test |
||
339 | * @large |
||
340 | */ |
||
341 | public function testGetEntitySubFilePath(): void |
||
342 | { |
||
343 | $entityFqn = '\\My\\Test\\Project\\Entities\\Person'; |
||
344 | $expected = '/Person.php'; |
||
345 | $actual = self::$helper->getEntityFileSubPath($entityFqn); |
||
346 | self::assertSame($expected, $actual); |
||
347 | |||
348 | $entityFqn = '\\My\\Test\\EntitiesProject\\Entities\\Person'; |
||
349 | $expected = '/Person.php'; |
||
350 | $actual = self::$helper->getEntityFileSubPath($entityFqn); |
||
351 | self::assertSame($expected, $actual); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @test |
||
356 | * @large |
||
357 | */ |
||
358 | public function testGetEntitySubPath(): void |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @test |
||
373 | * @large |
||
374 | */ |
||
375 | public function testGetInterfacesNamespaceForEntity(): void |
||
376 | { |
||
377 | $entityFqn = self::TEST_ENTITIES[0]; |
||
378 | $entityRelationsRootNamespace = self::TEST_PROJECT_ROOT_NAMESPACE |
||
379 | . AbstractGenerator::ENTITY_RELATIONS_NAMESPACE; |
||
380 | $expected = $entityRelationsRootNamespace . '\\Blah\\Foo\\Interfaces'; |
||
381 | $actual = self::$helper->getInterfacesNamespaceForEntity($entityFqn); |
||
382 | self::assertSame($expected, $actual); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @test |
||
387 | * @large |
||
388 | */ |
||
389 | public function testGetTraitsNamespaceForEntity(): void |
||
390 | { |
||
391 | $entityFqn = self::TEST_ENTITIES[0]; |
||
392 | $entityRelationsRootNamespace = self::TEST_PROJECT_ROOT_NAMESPACE |
||
393 | . AbstractGenerator::ENTITY_RELATIONS_NAMESPACE; |
||
394 | $expected = $entityRelationsRootNamespace . '\\Blah\\Foo\\Traits'; |
||
395 | $actual = self::$helper->getTraitsNamespaceForEntity($entityFqn); |
||
396 | self::assertSame($expected, $actual); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * @throws \ReflectionException |
||
401 | */ |
||
402 | public function testGetEntityNamespaceRootFromEntityReflection(): void |
||
403 | { |
||
404 | |||
405 | $entityReflection = new \ts\Reflection\ReflectionClass(self::TEST_ENTITIES[0]); |
||
406 | $expected = self::TEST_PROJECT_ROOT_NAMESPACE . '\\' . AbstractGenerator::ENTITIES_FOLDER_NAME; |
||
407 | $actual = self::$helper->getEntityNamespaceRootFromEntityReflection($entityReflection); |
||
408 | self::assertSame($expected, $actual); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @test |
||
413 | * @large |
||
414 | */ |
||
415 | public function testGetHasPluralInterfaceFqnForEntity(): void |
||
416 | { |
||
417 | $entityFqn = self::TEST_ENTITY_POST_CREATED; |
||
418 | $expected = self::TEST_PROJECT_ROOT_NAMESPACE |
||
419 | . AbstractGenerator::ENTITY_RELATIONS_NAMESPACE |
||
420 | . '\\Meh\\Interfaces\\HasMehsInterface'; |
||
421 | $actual = self::$helper->getHasPluralInterfaceFqnForEntity($entityFqn); |
||
422 | self::assertSame($expected, $actual); |
||
423 | |||
424 | $entityFqn = self::TEST_ENTITY_POST_CREATED_NESTED; |
||
425 | $expected = self::TEST_PROJECT_ROOT_NAMESPACE |
||
426 | . AbstractGenerator::ENTITY_RELATIONS_NAMESPACE |
||
427 | . '\\Nested\\Something\\Ho\\Hum\\Interfaces\\HasNestedSomethingHoHumsInterface'; |
||
428 | $actual = self::$helper->getHasPluralInterfaceFqnForEntity($entityFqn); |
||
429 | self::assertSame($expected, $actual); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * @test |
||
434 | * @large |
||
435 | */ |
||
436 | public function testgetHasSingularInterfaceFqnForEntity(): void |
||
437 | { |
||
438 | $entityFqn = self::TEST_ENTITY_POST_CREATED; |
||
439 | $expected = self::TEST_PROJECT_ROOT_NAMESPACE |
||
440 | . AbstractGenerator::ENTITY_RELATIONS_NAMESPACE |
||
441 | . '\\Meh\\Interfaces\\HasMehInterface'; |
||
442 | $actual = self::$helper->getHasSingularInterfaceFqnForEntity($entityFqn); |
||
443 | self::assertSame($expected, $actual); |
||
444 | |||
445 | $entityFqn = self::TEST_ENTITY_POST_CREATED_NESTED; |
||
446 | $expected = self::TEST_PROJECT_ROOT_NAMESPACE |
||
447 | . AbstractGenerator::ENTITY_RELATIONS_NAMESPACE |
||
448 | . '\\Nested\\Something\\Ho\\Hum\\Interfaces\\HasNestedSomethingHoHumInterface'; |
||
449 | $actual = self::$helper->getHasSingularInterfaceFqnForEntity($entityFqn); |
||
450 | self::assertSame($expected, $actual); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
||
455 | */ |
||
456 | public function testGetProjectRootNamespaceFromComposerJson(): void |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @test |
||
465 | * @large |
||
466 | */ |
||
467 | public function testStripPrefixFromHasType(): void |
||
468 | { |
||
469 | $expected = [ |
||
470 | 'OwningOneToOne' => 'OwningOneToOne', |
||
471 | 'InverseOneToOne' => 'InverseOneToOne', |
||
472 | 'UnidirectionalOneToOne' => 'UnidirectionalOneToOne', |
||
473 | 'OneToMany' => 'OneToMany', |
||
474 | 'UnidirectionalOneToMany' => 'UnidirectionalOneToMany', |
||
475 | 'ManyToOne' => 'ManyToOne', |
||
476 | 'UnidirectionalManyToOne' => 'UnidirectionalManyToOne', |
||
477 | 'OwningManyToMany' => 'OwningManyToMany', |
||
478 | 'InverseManyToMany' => 'InverseManyToMany', |
||
479 | ]; |
||
480 | $actual = []; |
||
481 | foreach (RelationsGenerator::HAS_TYPES as $hasType) { |
||
482 | $actual[$hasType] = self::$helper->stripPrefixFromHasType($hasType); |
||
483 | } |
||
484 | self::assertSame($expected, $actual); |
||
485 | foreach ($actual as $hasType => $stripped) { |
||
486 | $ownedHasName = self::$helper->getOwnedHasName( |
||
487 | $hasType, |
||
488 | "\\TemplateNamespace\\Entities\\TemplateEntity", |
||
489 | 'src', |
||
490 | '\\TemplateNamespace' |
||
491 | ); |
||
492 | $filePath = realpath(AbstractGenerator::TEMPLATE_PATH) |
||
493 | . '/src/Entity/Relations/TemplateEntity/Traits/Has' |
||
494 | . $ownedHasName . '/Has' . $ownedHasName . $stripped . '.php'; |
||
495 | $longestExisting = ''; |
||
496 | foreach (explode('/', $filePath) as $part) { |
||
497 | $maybeLongestExisting = $longestExisting . '/' . $part; |
||
498 | if (is_file($maybeLongestExisting) || is_dir($maybeLongestExisting)) { |
||
499 | $longestExisting = $maybeLongestExisting; |
||
500 | continue; |
||
501 | } |
||
502 | break; |
||
503 | } |
||
504 | $longestExisting = substr($longestExisting, 1); |
||
505 | self::assertFileExists($filePath, "\n$filePath\nexists up to:\n$longestExisting\n"); |
||
506 | } |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * @test |
||
511 | * @large |
||
512 | */ |
||
513 | public function testGetOwningTraitFqn(): void |
||
541 | ); |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * @test |
||
546 | * @large |
||
547 | */ |
||
548 | public function testGetOwningInterfaceFqn(): void |
||
575 | ); |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * @test |
||
580 | * @large |
||
581 | * @covers ::getFactoryFqnFromEntityFqn |
||
582 | */ |
||
583 | public function itCanGetTheEntityFactoryFqnFromEntityFqn(): void |
||
588 | } |
||
589 | } |
||
590 |