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 |
||
| 26 | trait TestingTrait |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * the Logged in user, used for protected routes. |
||
| 31 | * |
||
| 32 | * @var User |
||
| 33 | */ |
||
| 34 | public $loggedInTestingUser; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param $endpoint |
||
| 38 | * @param string $verb |
||
| 39 | * @param array $data |
||
| 40 | * @param bool $protected |
||
| 41 | * @param array $headers |
||
| 42 | * |
||
| 43 | * @return mixed |
||
| 44 | * @throws \Symfony\Component\Debug\Exception\UndefinedMethodException |
||
| 45 | */ |
||
| 46 | public function apiCall($endpoint, $verb = 'get', array $data = [], $protected = true, array $headers = []) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param \Dingo\Api\Http\Response $response |
||
| 77 | * @param array $messages |
||
| 78 | */ |
||
| 79 | public function assertValidationErrorContain(DingoAPIResponse $response, array $messages) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * get teh current logged in user. |
||
| 90 | * |
||
| 91 | * @return \App\Port\Tests\PHPUnit\Traits\User|mixed |
||
| 92 | */ |
||
| 93 | public function getLoggedInTestingUser() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * This returned visitor is a normal user, with `visitor_id` means |
||
| 106 | * before he became a registered user (can login) was a visitor. |
||
| 107 | * So this can be used to test endpoints that are protected by visitors |
||
| 108 | * access. |
||
| 109 | * |
||
| 110 | * @return \App\Port\Tests\PHPUnit\Traits\User|mixed |
||
| 111 | */ |
||
| 112 | public function getVisitor() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return \App\Port\Tests\PHPUnit\Traits\User|mixed |
||
| 125 | */ |
||
| 126 | public function getLoggedInTestingAdmin() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param $user |
||
| 137 | * |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | public function makeAdmin($user) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * get teh current logged in user token. |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function getLoggedInTestingUserToken() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param null $userDetails |
||
| 161 | * |
||
| 162 | * @return mixed |
||
| 163 | */ |
||
| 164 | public function registerAndLoginTestingUser($userDetails = null) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param null $userDetails |
||
| 192 | * |
||
| 193 | * @return mixed |
||
| 194 | */ |
||
| 195 | public function registerAndLoginTestingAdmin($userDetails = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Normal user with Developer Role |
||
| 206 | * |
||
| 207 | * @param null $userDetails |
||
| 208 | * |
||
| 209 | * @return mixed |
||
| 210 | */ |
||
| 211 | public function registerAndLoginTestingDeveloper($userDetails = null) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param $keys |
||
| 225 | * @param $response |
||
| 226 | */ |
||
| 227 | public function assertResponseContainKeys($keys, $response) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param $values |
||
| 240 | * @param $response |
||
| 241 | */ |
||
| 242 | public function assertResponseContainValues($values, $response) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param $data |
||
| 255 | * @param $response |
||
| 256 | */ |
||
| 257 | public function assertResponseContainKeyValue($data, $response) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Migrate the database. |
||
| 272 | */ |
||
| 273 | public function migrateDatabase() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param $response |
||
| 280 | * |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | private function responseToArray($response) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Format the given key and value into a JSON string for expectation checks. |
||
| 298 | * |
||
| 299 | * @param string $key |
||
| 300 | * @param mixed $value |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | private function formatToKeyValueToString($key, $value) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Mocking helper |
||
| 321 | * |
||
| 322 | * @param $class |
||
| 323 | * |
||
| 324 | * @return \Mockery\MockInterface |
||
| 325 | */ |
||
| 326 | public function mock($class) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * get response object, get the string content from it and convert it to an std object |
||
| 336 | * making it easier to read |
||
| 337 | * |
||
| 338 | * @param $response |
||
| 339 | * |
||
| 340 | * @return mixed |
||
| 341 | */ |
||
| 342 | public function getResponseObject(Response $response) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Inject the ID in the Endpoint URI |
||
| 349 | * |
||
| 350 | * Example: you give it ('users/{id}/stores', 100) it returns 'users/100/stores' |
||
| 351 | * |
||
| 352 | * @param $endpoint |
||
| 353 | * @param $id |
||
| 354 | * @param bool $skipEncoding |
||
| 355 | * |
||
| 356 | * @return mixed |
||
| 357 | */ |
||
| 358 | public function injectEndpointId($endpoint, $id, $skipEncoding = false) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Make sure you have an image in `storage/tests/` named to `a.jpeg` or anything else |
||
| 373 | * |
||
| 374 | * @param string $original_name |
||
| 375 | * @param string $mime_type |
||
| 376 | * @param int $size |
||
| 377 | * |
||
| 378 | * @return \Illuminate\Http\UploadedFile |
||
| 379 | */ |
||
| 380 | public static function getTestingFile($original_name = 'a.jpeg', $mime_type = 'image/jpeg', $size = 2476) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Create Application in the database with Token based on the User who made the request. |
||
| 393 | * And return headers array with the Application stored token in it. |
||
| 394 | * This is made to be used with the endpoints protected with `app.auth` middleware. |
||
| 395 | * |
||
| 396 | * @param string $endpoint |
||
| 397 | * @param string $verb |
||
| 398 | * @param array $data |
||
| 399 | */ |
||
| 400 | public function getApplicationTokenHeader( |
||
| 413 | |||
| 414 | /** |
||
| 415 | * override default URL subDomain in case you want to change it for some tests |
||
| 416 | * |
||
| 417 | * @param $subDomain |
||
| 418 | */ |
||
| 419 | public function overrideSubDomain($subDomain, $url = null) |
||
| 434 | |||
| 435 | } |
||
| 436 |
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.