Complex classes like TestingTrait 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 TestingTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | trait TestingTrait |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * the Logged in user, used for protected routes. |
||
| 32 | * |
||
| 33 | * @var User |
||
| 34 | */ |
||
| 35 | public $loggedInTestingUser; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param $endpoint |
||
| 39 | * @param string $verb |
||
| 40 | * @param array $data |
||
| 41 | * @param bool $protected |
||
| 42 | * @param array $headers |
||
| 43 | * |
||
| 44 | * @return mixed |
||
| 45 | * @throws \Symfony\Component\Debug\Exception\UndefinedMethodException |
||
| 46 | */ |
||
| 47 | public function apiCall($endpoint, $verb = 'get', array $data = [], $protected = true, array $headers = []) |
||
| 48 | { |
||
| 49 | // if endpoint is protected (requires token to access it's functionality) |
||
| 50 | if ($protected && !array_has($headers, 'Authorization')) { |
||
| 51 | // append the token to the header |
||
| 52 | $headers['Authorization'] = 'Bearer ' . $this->getLoggedInTestingUserToken(); |
||
| 53 | } |
||
| 54 | |||
| 55 | switch ($verb) { |
||
| 56 | case 'get': |
||
| 57 | $endpoint = $data ? $endpoint . '?' . http_build_query($data) : $endpoint; |
||
| 58 | $response = $this->get($endpoint, $headers)->response; |
||
|
|
|||
| 59 | break; |
||
| 60 | case 'post': |
||
| 61 | case 'put': |
||
| 62 | case 'patch': |
||
| 63 | case 'delete': |
||
| 64 | $response = $this->{$verb}($endpoint, $data, $headers)->response; |
||
| 65 | break; |
||
| 66 | case 'json:post': |
||
| 67 | $response = $this->json('post', $endpoint, $data, $headers)->response; |
||
| 68 | break; |
||
| 69 | default: |
||
| 70 | throw new UndefinedMethodException('Undefined HTTP Verb (' . $verb . ').'); |
||
| 71 | } |
||
| 72 | |||
| 73 | return $response; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param $fileName |
||
| 78 | * @param $stubDirPath |
||
| 79 | * @param string $mimeType |
||
| 80 | * @param null $size |
||
| 81 | * |
||
| 82 | * @return \Illuminate\Http\UploadedFile |
||
| 83 | */ |
||
| 84 | public function getTestingFile($fileName, $stubDirPath, $mimeType = 'text/plain', $size = null) |
||
| 85 | { |
||
| 86 | $file = $stubDirPath . $fileName; |
||
| 87 | |||
| 88 | return new UploadedFile($file, $fileName, $mimeType, $size, null, true); // null = null | $testMode = true |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param $imageName |
||
| 93 | * @param $stubDirPath |
||
| 94 | * @param string $mimeType |
||
| 95 | * @param null $size |
||
| 96 | * |
||
| 97 | * @return \Illuminate\Http\UploadedFile |
||
| 98 | */ |
||
| 99 | public function getTestingImage($imageName, $stubDirPath, $mimeType = 'image/jpeg', $size = null) |
||
| 100 | { |
||
| 101 | return $this->getTestingFile($imageName, $stubDirPath, $mimeType, $size); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param \Dingo\Api\Http\Response $response |
||
| 106 | * @param array $messages |
||
| 107 | */ |
||
| 108 | public function assertValidationErrorContain(DingoAPIResponse $response, array $messages) |
||
| 109 | { |
||
| 110 | $arrayResponse = json_decode($response->getContent()); |
||
| 111 | |||
| 112 | foreach ($messages as $key => $value) { |
||
| 113 | $this->assertEquals($arrayResponse->errors->{$key}[0], $value); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * get teh current logged in user OR create new one if no one exist |
||
| 119 | * |
||
| 120 | * @param null $access |
||
| 121 | * |
||
| 122 | * @return \App\Containers\User\Models\User|mixed |
||
| 123 | */ |
||
| 124 | public function getTestingUser($access = null) |
||
| 125 | { |
||
| 126 | if (!$user = $this->loggedInTestingUser) { |
||
| 127 | $user = $this->createTestingUser($access); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $user; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param null $permissions |
||
| 135 | * |
||
| 136 | * @return \App\Containers\User\Models\User|mixed |
||
| 137 | */ |
||
| 138 | public function getTestingAdmin($permissions = null) |
||
| 139 | { |
||
| 140 | return $this->getTestingUser([ |
||
| 141 | 'roles' => 'admin', |
||
| 142 | '$permissions' => $permissions, |
||
| 143 | ]); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * get teh current logged in user token. |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getLoggedInTestingUserToken() |
||
| 152 | { |
||
| 153 | return $this->getTestingUser()->token; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param null $access |
||
| 158 | * @param null $userDetails |
||
| 159 | * |
||
| 160 | * @return mixed |
||
| 161 | */ |
||
| 162 | public function createTestingUser($access = null, $userDetails = null) |
||
| 163 | { |
||
| 164 | // if no user detail provided, use the default details. |
||
| 165 | $userDetails = $userDetails ? : [ |
||
| 166 | 'name' => 'Mahmoud Zalt', |
||
| 167 | 'email' => '[email protected]', |
||
| 168 | 'password' => 'secret.Pass7', |
||
| 169 | ]; |
||
| 170 | |||
| 171 | // create new user and login (true) |
||
| 172 | $user = App::make(CreateUserAction::class)->run($userDetails['email'], $userDetails['password'], |
||
| 173 | $userDetails['name'], null, null, true |
||
| 174 | ); |
||
| 175 | |||
| 176 | $user = $this->setupTestingUserAccess($user, $access ? : (isset($this->access) ? $this->access : null)); |
||
| 177 | |||
| 178 | return $this->loggedInTestingUser = $user; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $user |
||
| 183 | * @param $access |
||
| 184 | * |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | private function setupTestingUserAccess($user, $access) |
||
| 188 | { |
||
| 189 | if (isset($access['permissions']) && !empty($access['permissions'])) { |
||
| 190 | $user->givePermissionTo($access['permissions']); |
||
| 191 | } |
||
| 192 | if (isset($access['roles']) && !empty($access['roles'])) { |
||
| 193 | if (!$user->hasRole($access['roles'])) { |
||
| 194 | $user->assignRole($access['roles']); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | return $user; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param null $userDetails |
||
| 203 | * |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | public function registerAndLoginTestingAdmin($userDetails = null) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Normal user with Developer Role |
||
| 217 | * |
||
| 218 | * @param null $userDetails |
||
| 219 | * |
||
| 220 | * @return \App\Containers\User\Models\User|mixed |
||
| 221 | */ |
||
| 222 | public function registerAndLoginTestingDeveloper($userDetails = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param $keys |
||
| 236 | * @param $response |
||
| 237 | */ |
||
| 238 | public function assertResponseContainKeys($keys, $response) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param $values |
||
| 251 | * @param $response |
||
| 252 | */ |
||
| 253 | public function assertResponseContainValues($values, $response) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param $data |
||
| 266 | * @param $response |
||
| 267 | */ |
||
| 268 | public function assertResponseContainKeyValue($data, $response) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Migrate the database. |
||
| 283 | */ |
||
| 284 | public function migrateDatabase() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param $response |
||
| 291 | * |
||
| 292 | * @return mixed |
||
| 293 | */ |
||
| 294 | private function responseToArray($response) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Format the given key and value into a JSON string for expectation checks. |
||
| 309 | * |
||
| 310 | * @param string $key |
||
| 311 | * @param mixed $value |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | private function formatToKeyValueToString($key, $value) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Mocking helper |
||
| 332 | * |
||
| 333 | * @param $class |
||
| 334 | * |
||
| 335 | * @return \Mockery\MockInterface |
||
| 336 | */ |
||
| 337 | public function mock($class) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * get response object, get the string content from it and convert it to an std object |
||
| 347 | * making it easier to read |
||
| 348 | * |
||
| 349 | * @param $response |
||
| 350 | * |
||
| 351 | * @return mixed |
||
| 352 | */ |
||
| 353 | public function getResponseObject(Response $response) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Inject the ID in the Endpoint URI |
||
| 360 | * |
||
| 361 | * Example: you give it ('users/{id}/stores', 100) it returns 'users/100/stores' |
||
| 362 | * |
||
| 363 | * @param $endpoint |
||
| 364 | * @param $id |
||
| 365 | * @param bool $skipEncoding |
||
| 366 | * |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | public function injectEndpointId($endpoint, $id, $skipEncoding = false) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * override default URL subDomain in case you want to change it for some tests |
||
| 384 | * |
||
| 385 | * @param $subDomain |
||
| 386 | * @param null $url |
||
| 387 | */ |
||
| 388 | public function overrideSubDomain($subDomain, $url = null) |
||
| 403 | |||
| 404 | } |
||
| 405 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.