Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BaseIntegrationTest 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 BaseIntegrationTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class BaseIntegrationTest extends TestCase |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Property indicating whether the DB already has been set up. |
||
| 32 | * |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected static $setUp = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Id of test content type. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected static $contentTypeId; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Id of test content. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected static $contentId; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Current version of test content. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected static $contentVersion; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \Symfony\Component\DependencyInjection\ContainerBuilder |
||
| 60 | */ |
||
| 61 | protected static $container; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | View Code Duplication | protected static function getInstallationDir() |
|
| 67 | { |
||
| 68 | static $installDir = null; |
||
| 69 | if ($installDir === null) { |
||
| 70 | $config = require __DIR__ . '/../../../../../config.php'; |
||
| 71 | $installDir = $config['install_dir']; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $installDir; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var \eZ\Publish\Core\Persistence\TransformationProcessor |
||
| 79 | */ |
||
| 80 | protected $transformationProcessor; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return \eZ\Publish\Core\Persistence\TransformationProcessor |
||
| 84 | */ |
||
| 85 | public function getTransformationProcessor() |
||
| 86 | { |
||
| 87 | if (!isset($this->transformationProcessor)) { |
||
| 88 | $this->transformationProcessor = new DefinitionBased( |
||
| 89 | new Persistence\TransformationProcessor\DefinitionBased\Parser(self::getInstallationDir()), |
||
| 90 | new Persistence\TransformationProcessor\PcreCompiler(new Persistence\Utf8Converter()), |
||
| 91 | glob(__DIR__ . '/../../../Core/Persistence/Tests/TransformationProcessor/_fixtures/transformations/*.tr') |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | |||
| 95 | return $this->transformationProcessor; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns the identifier of the FieldType under test. |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | abstract public function getTypeName(); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns the Handler with all necessary objects registered. |
||
| 107 | * |
||
| 108 | * Returns an instance of the Persistence Handler where the |
||
| 109 | * FieldType\Storage has been registered. |
||
| 110 | * |
||
| 111 | * @return \eZ\Publish\SPI\Persistence\Handler |
||
| 112 | */ |
||
| 113 | abstract public function getCustomHandler(); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns the FieldTypeConstraints to be used to create a field definition |
||
| 117 | * of the FieldType under test. |
||
| 118 | * |
||
| 119 | * @return \eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints |
||
| 120 | */ |
||
| 121 | abstract public function getTypeConstraints(); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the field definition data expected after loading the newly |
||
| 125 | * created field definition with the FieldType under test. |
||
| 126 | * |
||
| 127 | * This is a PHPUnit data provider |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | abstract public function getFieldDefinitionData(); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get initial field value. |
||
| 135 | * |
||
| 136 | * @return \eZ\Publish\SPI\Persistence\Content\FieldValue |
||
| 137 | */ |
||
| 138 | abstract public function getInitialValue(); |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Asserts that the loaded field data is correct. |
||
| 142 | * |
||
| 143 | * Performs assertions on the loaded field, mainly checking that the |
||
| 144 | * $field->value->externalData is loaded correctly. If the loading of |
||
| 145 | * external data manipulates other aspects of $field, their correctness |
||
| 146 | * also needs to be asserted. Make sure you implement this method agnostic |
||
| 147 | * to the used SPI\Persistence implementation! |
||
| 148 | */ |
||
| 149 | public function assertLoadedFieldDataCorrect(Field $field) |
||
| 150 | { |
||
| 151 | $this->assertEquals( |
||
| 152 | $this->getInitialValue(), |
||
| 153 | $field->value |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get update field value. |
||
| 159 | * |
||
| 160 | * Use to update the field |
||
| 161 | * |
||
| 162 | * @return \eZ\Publish\SPI\Persistence\Content\FieldValue |
||
| 163 | */ |
||
| 164 | abstract public function getUpdatedValue(); |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Asserts that the updated field data is loaded correct. |
||
| 168 | * |
||
| 169 | * Performs assertions on the loaded field after it has been updated, |
||
| 170 | * mainly checking that the $field->value->externalData is loaded |
||
| 171 | * correctly. If the loading of external data manipulates other aspects of |
||
| 172 | * $field, their correctness also needs to be asserted. Make sure you |
||
| 173 | * implement this method agnostic to the used SPI\Persistence |
||
| 174 | * implementation! |
||
| 175 | */ |
||
| 176 | public function assertUpdatedFieldDataCorrect(Field $field) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Method called after content creation. |
||
| 186 | * |
||
| 187 | * Useful, if additional stuff should be executed (like creating the actual |
||
| 188 | * user). |
||
| 189 | * |
||
| 190 | * @param Legacy\Handler $handler |
||
| 191 | * @param Content $content |
||
| 192 | */ |
||
| 193 | public function postCreationHook(Legacy\Handler $handler, Content $content) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Can be overwritten to assert that additional data has been deleted. |
||
| 200 | * |
||
| 201 | * @param Content $content |
||
| 202 | */ |
||
| 203 | public function assertDeletedFieldDataCorrect(Content $content) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Only set up once for these read only tests on a large fixture. |
||
| 210 | * |
||
| 211 | * Skipping the reset-up, since setting up for these tests takes quite some |
||
| 212 | * time, which is not required to spent, since we are only reading from the |
||
| 213 | * database anyways. |
||
| 214 | */ |
||
| 215 | public function setUp() |
||
| 228 | |||
| 229 | public function testCreateContentType() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Performs the creation of the content type with a field of the field type |
||
| 241 | * under test. |
||
| 242 | * |
||
| 243 | * @return \eZ\Publish\SPI\Persistence\Content\Type |
||
| 244 | */ |
||
| 245 | protected function createContentType() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @depends testCreateContentType |
||
| 295 | */ |
||
| 296 | public function testContentTypeField($contentType) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @depends testCreateContentType |
||
| 306 | */ |
||
| 307 | public function testLoadContentTypeField() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @depends testLoadContentTypeField |
||
| 317 | */ |
||
| 318 | public function testLoadContentTypeFieldType($contentType) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @depends testLoadContentTypeFieldType |
||
| 330 | * @dataProvider getFieldDefinitionData |
||
| 331 | */ |
||
| 332 | public function testLoadContentTypeFieldData($name, $value, $field) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @depends testLoadContentTypeField |
||
| 342 | */ |
||
| 343 | public function testCreateContent($contentType) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Creates content of the given $contentType with $fieldValue in |
||
| 359 | * $languageCode. |
||
| 360 | * |
||
| 361 | * @param Type $contentType |
||
| 362 | * @param mixed $fieldValue |
||
| 363 | * @param string $languageCode |
||
| 364 | * |
||
| 365 | * @return Content |
||
| 366 | */ |
||
| 367 | protected function createContent(Type $contentType, $fieldValue, $languageCode = 'eng-GB') |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @depends testCreateContent |
||
| 424 | */ |
||
| 425 | public function testCreatedFieldType($content) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @depends testCreateContent |
||
| 437 | */ |
||
| 438 | public function testLoadField() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @depends testLoadField |
||
| 449 | */ |
||
| 450 | public function testLoadFieldType($content) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @depends testLoadFieldType |
||
| 462 | */ |
||
| 463 | public function testLoadExternalData($field) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @depends testLoadFieldType |
||
| 470 | */ |
||
| 471 | public function testUpdateField($field) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Performs an update on $contentId in $contentVersion setting $field. |
||
| 480 | * |
||
| 481 | * @param mixed $contentId |
||
| 482 | * @param mixed $contentVersion |
||
| 483 | * @param Field $field |
||
| 484 | * |
||
| 485 | * @return Content |
||
| 486 | */ |
||
| 487 | protected function updateContent($contentId, $contentVersion, Field $field) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @depends testUpdateField |
||
| 510 | */ |
||
| 511 | public function testUpdateFieldType($content) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @depends testUpdateFieldType |
||
| 523 | */ |
||
| 524 | public function testUpdateExternalData($field) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @depends testUpdateField |
||
| 531 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 532 | */ |
||
| 533 | public function testDeleteField($content) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Deletes the given $content. |
||
| 550 | * |
||
| 551 | * @param Content $content |
||
| 552 | */ |
||
| 553 | protected function deleteContent(Content $content) |
||
| 562 | |||
| 563 | View Code Duplication | protected function getContainer() |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Returns the Handler. |
||
| 606 | * |
||
| 607 | * @param string $identifier |
||
| 608 | * @param \eZ\Publish\SPI\FieldType\FieldType $fieldType |
||
| 609 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter $fieldValueConverter |
||
| 610 | * @param \eZ\Publish\SPI\FieldType\FieldStorage $externalStorage |
||
| 611 | * |
||
| 612 | * @return \eZ\Publish\SPI\Persistence\Handler |
||
| 613 | */ |
||
| 614 | protected function getHandler($identifier, $fieldType, $fieldValueConverter, $externalStorage) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Overrides all services to be public. |
||
| 639 | * |
||
| 640 | * It is a workaround to the change in Symfony 4 which makes all services private by default. |
||
| 641 | * Our integration tests are not prepared for this as they get services directly from the Container. |
||
| 642 | * |
||
| 643 | * Inspired by {@link \Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass} |
||
| 644 | * |
||
| 645 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $containerBuilder |
||
| 646 | */ |
||
| 647 | View Code Duplication | private function setAllServicesPublic(ContainerBuilder $containerBuilder): void |
|
| 681 | } |
||
| 682 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.