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 BaseTest 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 BaseTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class BaseTest extends TestCase |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Maximum integer number accepted by the different backends. |
||
| 30 | */ |
||
| 31 | const DB_INT_MAX = 2147483647; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 35 | */ |
||
| 36 | private $setupFactory; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \eZ\Publish\API\Repository\Repository |
||
| 40 | */ |
||
| 41 | private $repository; |
||
| 42 | |||
| 43 | protected function setUp() |
||
| 44 | { |
||
| 45 | parent::setUp(); |
||
| 46 | |||
| 47 | try { |
||
| 48 | // Use setup factory instance here w/o clearing data in case test don't need to |
||
| 49 | $repository = $this->getSetupFactory()->getRepository(false); |
||
| 50 | |||
| 51 | // Set session if we are testing the REST backend to make it |
||
| 52 | // possible to persist data in the memory backend during multiple |
||
| 53 | // requests. |
||
| 54 | if ($repository instanceof Sessionable) { |
||
| 55 | $repository->setSession($id = md5(microtime())); |
||
| 56 | } |
||
| 57 | } catch (PDOException $e) { |
||
| 58 | $this->fail( |
||
| 59 | 'The communication with the database cannot be established. ' . |
||
| 60 | "This is required in order to perform the tests.\n\n" . |
||
| 61 | 'Exception: ' . $e |
||
| 62 | ); |
||
| 63 | } catch (Exception $e) { |
||
| 64 | $this->fail( |
||
| 65 | 'Cannot create a repository with predefined user. ' . |
||
| 66 | 'Check the UserService or RoleService implementation. ' . |
||
| 67 | PHP_EOL . PHP_EOL . |
||
| 68 | 'Exception: ' . $e |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Resets the temporary used repository between each test run. |
||
| 75 | */ |
||
| 76 | protected function tearDown() |
||
| 77 | { |
||
| 78 | $this->repository = null; |
||
| 79 | parent::tearDown(); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns the ID generator, fitting to the repository implementation. |
||
| 84 | * |
||
| 85 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
| 86 | */ |
||
| 87 | protected function getIdManager() |
||
| 88 | { |
||
| 89 | return $this->getSetupFactory()->getIdManager(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Generates a repository specific ID value. |
||
| 94 | * |
||
| 95 | * @param string $type |
||
| 96 | * @param mixed $rawId |
||
| 97 | * |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | protected function generateId($type, $rawId) |
||
| 101 | { |
||
| 102 | return $this->getIdManager()->generateId($type, $rawId); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Parses a repository specific ID value. |
||
| 107 | * |
||
| 108 | * @param string $type |
||
| 109 | * @param mixed $id |
||
| 110 | * |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | protected function parseId($type, $id) |
||
| 114 | { |
||
| 115 | return $this->getIdManager()->parseId($type, $id); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns a config setting provided by the setup factory. |
||
| 120 | * |
||
| 121 | * @param string $configKey |
||
| 122 | * |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | protected function getConfigValue($configKey) |
||
| 126 | { |
||
| 127 | return $this->getSetupFactory()->getConfigValue($configKey); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Tests if the currently tested api is based on a V4 implementation. |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | protected function isVersion4() |
||
| 136 | { |
||
| 137 | return isset($_ENV['backendVersion']) && '4' === $_ENV['backendVersion']; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param bool $initialInitializeFromScratch Only has an effect if set in first call within a test |
||
| 142 | * |
||
| 143 | * @return \eZ\Publish\API\Repository\Repository |
||
| 144 | */ |
||
| 145 | protected function getRepository($initialInitializeFromScratch = true) |
||
| 146 | { |
||
| 147 | if (null === $this->repository) { |
||
| 148 | $this->repository = $this->getSetupFactory()->getRepository($initialInitializeFromScratch); |
||
| 149 | } |
||
| 150 | |||
| 151 | return $this->repository; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 156 | */ |
||
| 157 | protected function getSetupFactory() |
||
| 158 | { |
||
| 159 | if (null === $this->setupFactory) { |
||
| 160 | if (false === isset($_ENV['setupFactory'])) { |
||
| 161 | throw new \ErrorException('Missing mandatory setting $_ENV["setupFactory"].'); |
||
| 162 | } |
||
| 163 | |||
| 164 | $setupClass = $_ENV['setupFactory']; |
||
| 165 | if (false === class_exists($setupClass)) { |
||
| 166 | throw new \ErrorException('$_ENV["setupFactory"] does not reference an existing class.'); |
||
| 167 | } |
||
| 168 | |||
| 169 | $this->setupFactory = new $setupClass(); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $this->setupFactory; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 177 | * $actualObject. |
||
| 178 | * |
||
| 179 | * @param mixed[] $expectedValues |
||
| 180 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 181 | */ |
||
| 182 | protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject) |
||
| 183 | { |
||
| 184 | foreach ($expectedValues as $propertyName => $propertyValue) { |
||
| 185 | $this->assertPropertiesEqual($propertyName, $propertyValue, $actualObject->$propertyName); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 191 | * $actualObject. |
||
| 192 | * |
||
| 193 | * If the property type is array, it will be sorted before comparison. |
||
| 194 | * |
||
| 195 | * @TODO: introduced because of randomly failing tests, ref: https://jira.ez.no/browse/EZP-21734 |
||
| 196 | * |
||
| 197 | * @param mixed[] $expectedValues |
||
| 198 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 199 | */ |
||
| 200 | protected function assertPropertiesCorrectUnsorted(array $expectedValues, ValueObject $actualObject) |
||
| 201 | { |
||
| 202 | foreach ($expectedValues as $propertyName => $propertyValue) { |
||
| 203 | $this->assertPropertiesEqual($propertyName, $propertyValue, $actualObject->$propertyName, true); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Asserts all properties from $expectedValues are correctly set in |
||
| 209 | * $actualObject. Additional (virtual) properties can be asserted using |
||
| 210 | * $additionalProperties. |
||
| 211 | * |
||
| 212 | * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues |
||
| 213 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 214 | * @param array $propertyNames |
||
|
|
|||
| 215 | */ |
||
| 216 | protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $additionalProperties = array()) |
||
| 217 | { |
||
| 218 | foreach ($expectedValues as $propertyName => $propertyValue) { |
||
| 219 | $this->assertPropertiesEqual($propertyName, $propertyValue, $actualObject->$propertyName); |
||
| 220 | } |
||
| 221 | |||
| 222 | foreach ($additionalProperties as $propertyName) { |
||
| 223 | $this->assertPropertiesEqual($propertyName, $expectedValues->$propertyName, $actualObject->$propertyName); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @see \eZ\Publish\API\Repository\Tests\BaseTest::assertPropertiesCorrectUnsorted() |
||
| 229 | * |
||
| 230 | * @param array $items An array of scalar values |
||
| 231 | */ |
||
| 232 | private function sortItems(array &$items) |
||
| 233 | { |
||
| 234 | $sorter = function ($a, $b) { |
||
| 235 | if (!is_scalar($a) || !is_scalar($b)) { |
||
| 236 | $this->fail('Wrong usage: method ' . __METHOD__ . ' accepts only an array of scalar values'); |
||
| 237 | } |
||
| 238 | |||
| 239 | return strcmp($a, $b); |
||
| 240 | }; |
||
| 241 | usort($items, $sorter); |
||
| 242 | } |
||
| 243 | |||
| 244 | private function assertPropertiesEqual($propertyName, $expectedValue, $actualValue, $sortArray = false) |
||
| 245 | { |
||
| 246 | if ($expectedValue instanceof ArrayObject) { |
||
| 247 | $expectedValue = $expectedValue->getArrayCopy(); |
||
| 248 | } elseif ($expectedValue instanceof DateTime) { |
||
| 249 | $expectedValue = $expectedValue->format(DateTime::RFC850); |
||
| 250 | } |
||
| 251 | if ($actualValue instanceof ArrayObject) { |
||
| 252 | $actualValue = $actualValue->getArrayCopy(); |
||
| 253 | } elseif ($actualValue instanceof DateTime) { |
||
| 254 | $actualValue = $actualValue->format(DateTime::RFC850); |
||
| 255 | } |
||
| 256 | |||
| 257 | if ($sortArray && is_array($actualValue) && is_array($expectedValue)) { |
||
| 258 | $this->sortItems($actualValue); |
||
| 259 | $this->sortItems($expectedValue); |
||
| 260 | } |
||
| 261 | |||
| 262 | $this->assertEquals( |
||
| 263 | $expectedValue, |
||
| 264 | $actualValue, |
||
| 265 | sprintf('Object property "%s" incorrect.', $propertyName) |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Create a user in editor user group. |
||
| 271 | * |
||
| 272 | * @param string $login |
||
| 273 | * |
||
| 274 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 275 | */ |
||
| 276 | protected function createUserVersion1($login = 'user') |
||
| 277 | { |
||
| 278 | $repository = $this->getRepository(); |
||
| 279 | |||
| 280 | /* BEGIN: Inline */ |
||
| 281 | // ID of the "Editors" user group in an eZ Publish demo installation |
||
| 282 | $editorsGroupId = 13; |
||
| 283 | |||
| 284 | $userService = $repository->getUserService(); |
||
| 285 | |||
| 286 | // Instantiate a create struct with mandatory properties |
||
| 287 | $userCreate = $userService->newUserCreateStruct( |
||
| 288 | $login, |
||
| 289 | "{$login}@example.com", |
||
| 290 | 'secret', |
||
| 291 | 'eng-US' |
||
| 292 | ); |
||
| 293 | $userCreate->enabled = true; |
||
| 294 | |||
| 295 | // Set some fields required by the user ContentType |
||
| 296 | $userCreate->setField('first_name', 'Example'); |
||
| 297 | $userCreate->setField('last_name', 'User'); |
||
| 298 | |||
| 299 | // Load parent group for the user |
||
| 300 | $group = $userService->loadUserGroup($editorsGroupId); |
||
| 301 | |||
| 302 | // Create a new user instance. |
||
| 303 | $user = $userService->createUser($userCreate, array($group)); |
||
| 304 | /* END: Inline */ |
||
| 305 | |||
| 306 | return $user; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Create a user in new user group with editor rights limited to Media Library (/1/48/). |
||
| 311 | * |
||
| 312 | * @uses ::createCustomUserVersion1() |
||
| 313 | * |
||
| 314 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 315 | */ |
||
| 316 | protected function createMediaUserVersion1() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 327 | * |
||
| 328 | * @param string $userGroupName Name of the new user group to create |
||
| 329 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 330 | * @param RoleLimitation|null $roleLimitation |
||
| 331 | * |
||
| 332 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 333 | */ |
||
| 334 | protected function createCustomUserVersion1($userGroupName, $roleIdentifier, RoleLimitation $roleLimitation = null) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 347 | * |
||
| 348 | * @param string $login User login |
||
| 349 | * @param string $email User e-mail |
||
| 350 | * @param string $userGroupName Name of the new user group to create |
||
| 351 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 352 | * @param RoleLimitation|null $roleLimitation |
||
| 353 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 354 | */ |
||
| 355 | protected function createCustomUserWithLogin( |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Create a user using given data. |
||
| 408 | * |
||
| 409 | * @param string $login |
||
| 410 | * @param string $firstName |
||
| 411 | * @param string $lastName |
||
| 412 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 413 | */ |
||
| 414 | protected function createUser($login, $firstName, $lastName) |
||
| 415 | { |
||
| 416 | $repository = $this->getRepository(); |
||
| 417 | |||
| 418 | $userService = $repository->getUserService(); |
||
| 419 | $userGroup = $userService->loadUserGroup(13); |
||
| 420 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * Only for internal use. |
||
| 442 | * |
||
| 443 | * Creates a \DateTime object for $timestamp in the current time zone |
||
| 444 | * |
||
| 445 | * @param int $timestamp |
||
| 446 | * |
||
| 447 | * @return \DateTime |
||
| 448 | */ |
||
| 449 | View Code Duplication | public function createDateTime($timestamp = null) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
| 461 | * |
||
| 462 | * Currently implemented only in Solr search engine. |
||
| 463 | * |
||
| 464 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 465 | */ |
||
| 466 | protected function refreshSearch(Repository $repository) |
||
| 494 | } |
||
| 495 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.