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