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 |
||
29 | trait TestingTrait |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * the Logged in user, used for protected routes. |
||
34 | * |
||
35 | * @var User |
||
36 | */ |
||
37 | public $loggedInTestingUser; |
||
38 | |||
39 | /** |
||
40 | * @param $endpoint |
||
41 | * @param string $verb |
||
42 | * @param array $data |
||
43 | * @param bool $protected |
||
44 | * @param array $headers |
||
45 | * |
||
46 | * @return mixed |
||
47 | * @throws \Symfony\Component\Debug\Exception\UndefinedMethodException |
||
48 | */ |
||
49 | public function apiCall($endpoint, $verb = 'get', array $data = [], $protected = true, array $headers = []) |
||
77 | |||
78 | /** |
||
79 | * @param $fileName |
||
80 | * @param $stubDirPath |
||
81 | * @param string $mimeType |
||
82 | * @param null $size |
||
83 | * |
||
84 | * @return \Illuminate\Http\UploadedFile |
||
85 | */ |
||
86 | public function getTestingFile($fileName, $stubDirPath, $mimeType = 'text/plain', $size = null) |
||
92 | |||
93 | /** |
||
94 | * @param $imageName |
||
95 | * @param $stubDirPath |
||
96 | * @param string $mimeType |
||
97 | * @param null $size |
||
98 | * |
||
99 | * @return \Illuminate\Http\UploadedFile |
||
100 | */ |
||
101 | public function getTestingImage($imageName, $stubDirPath, $mimeType = 'image/jpeg', $size = null) |
||
105 | |||
106 | /** |
||
107 | * @param \Dingo\Api\Http\Response $response |
||
108 | * @param array $messages |
||
109 | */ |
||
110 | public function assertValidationErrorContain(DingoAPIResponse $response, array $messages) |
||
118 | |||
119 | /** |
||
120 | * get teh current logged in user. |
||
121 | * |
||
122 | * @return App\Containers\User\Models\User |
||
123 | */ |
||
124 | public function getLoggedInTestingUser() |
||
134 | |||
135 | /** |
||
136 | * This returned visitor is a normal user, with `visitor_id` means |
||
137 | * before he became a registered user (can login) was a visitor. |
||
138 | * So this can be used to test endpoints that are protected by visitors |
||
139 | * access. |
||
140 | * |
||
141 | * @return App\Containers\User\Models\User|mixed |
||
142 | */ |
||
143 | public function getVisitor() |
||
153 | |||
154 | /** |
||
155 | * @return App\Containers\User\Models\User|mixed |
||
156 | */ |
||
157 | public function getLoggedInTestingAdmin() |
||
165 | |||
166 | /** |
||
167 | * @param $user |
||
168 | * |
||
169 | * @return App\Containers\User\Models\User |
||
170 | */ |
||
171 | public function makeAdmin(User $user) |
||
179 | |||
180 | /** |
||
181 | * get teh current logged in user token. |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | public function getLoggedInTestingUserToken() |
||
189 | |||
190 | /** |
||
191 | * @param null $userDetails |
||
192 | * |
||
193 | * @return mixed |
||
194 | */ |
||
195 | public function registerAndLoginTestingUser($userDetails = null) |
||
220 | |||
221 | /** |
||
222 | * @param null $userDetails |
||
223 | * |
||
224 | * @return mixed |
||
225 | */ |
||
226 | public function registerAndLoginTestingAdmin($userDetails = null) |
||
234 | |||
235 | /** |
||
236 | * Normal user with Developer Role |
||
237 | * |
||
238 | * @param null $userDetails |
||
239 | * |
||
240 | * @return mixed |
||
241 | */ |
||
242 | public function registerAndLoginTestingDeveloper($userDetails = null) |
||
253 | |||
254 | /** |
||
255 | * @param $keys |
||
256 | * @param $response |
||
257 | */ |
||
258 | public function assertResponseContainKeys($keys, $response) |
||
268 | |||
269 | /** |
||
270 | * @param $values |
||
271 | * @param $response |
||
272 | */ |
||
273 | public function assertResponseContainValues($values, $response) |
||
283 | |||
284 | /** |
||
285 | * @param $data |
||
286 | * @param $response |
||
287 | */ |
||
288 | public function assertResponseContainKeyValue($data, $response) |
||
300 | |||
301 | /** |
||
302 | * Migrate the database. |
||
303 | */ |
||
304 | public function migrateDatabase() |
||
308 | |||
309 | /** |
||
310 | * @param $response |
||
311 | * |
||
312 | * @return mixed |
||
313 | */ |
||
314 | private function responseToArray($response) |
||
326 | |||
327 | /** |
||
328 | * Format the given key and value into a JSON string for expectation checks. |
||
329 | * |
||
330 | * @param string $key |
||
331 | * @param mixed $value |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | private function formatToKeyValueToString($key, $value) |
||
349 | |||
350 | /** |
||
351 | * Mocking helper |
||
352 | * |
||
353 | * @param $class |
||
354 | * |
||
355 | * @return \Mockery\MockInterface |
||
356 | */ |
||
357 | public function mock($class) |
||
364 | |||
365 | /** |
||
366 | * get response object, get the string content from it and convert it to an std object |
||
367 | * making it easier to read |
||
368 | * |
||
369 | * @param $response |
||
370 | * |
||
371 | * @return mixed |
||
372 | */ |
||
373 | public function getResponseObject(Response $response) |
||
377 | |||
378 | /** |
||
379 | * Inject the ID in the Endpoint URI |
||
380 | * |
||
381 | * Example: you give it ('users/{id}/stores', 100) it returns 'users/100/stores' |
||
382 | * |
||
383 | * @param $endpoint |
||
384 | * @param $id |
||
385 | * @param bool $skipEncoding |
||
386 | * |
||
387 | * @return mixed |
||
388 | */ |
||
389 | public function injectEndpointId($endpoint, $id, $skipEncoding = false) |
||
401 | |||
402 | /** |
||
403 | * Create Application in the database with Token based on the User who made the request. |
||
404 | * And return headers array with the Application stored token in it. |
||
405 | * This is made to be used with the endpoints protected with `app.auth` middleware. |
||
406 | * |
||
407 | * NOTE: make sure you call this function before getting the `logged in testing user` in your tests. |
||
408 | * TODO: will fix this line above later. |
||
409 | * |
||
410 | * @param string $appName |
||
411 | * @param null $userDetails |
||
412 | * |
||
413 | * @return mixed |
||
414 | */ |
||
415 | public function getApplicationTokenHeader($appName = 'Testing App', $userDetails = null) |
||
425 | |||
426 | /** |
||
427 | * override default URL subDomain in case you want to change it for some tests |
||
428 | * |
||
429 | * @param $subDomain |
||
430 | * @param null $url |
||
431 | */ |
||
432 | public function overrideSubDomain($subDomain, $url = null) |
||
447 | |||
448 | } |
||
449 |
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
Idable
provides a methodequalsId
that 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.