Complex classes like AbstractTestCase 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 AbstractTestCase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase |
||
22 | { |
||
23 | use ElementTrait; |
||
24 | use ElementsTrait; |
||
25 | use NotifyTrait; |
||
26 | use ScreenshotTrait; |
||
27 | |||
28 | /** |
||
29 | * Selenium Web Driver host URI |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected static $webDriverHost = 'http://localhost:%s/wd/hub'; |
||
34 | |||
35 | /** |
||
36 | * Selenium Web Driver port |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected static $webDriverPort = '4444'; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected static $browser = 'htmlunit'; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $browserBin; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $uri; |
||
56 | |||
57 | /** |
||
58 | * @var PHPWebDriver_WebDriver |
||
59 | */ |
||
60 | protected $webDriver; |
||
61 | |||
62 | /** |
||
63 | * @var \PHPWebDriver_WebDriverSession |
||
64 | */ |
||
65 | protected $session; |
||
66 | |||
67 | /** |
||
68 | * Resolves URI to open session |
||
69 | */ |
||
70 | protected function setUp() |
||
80 | |||
81 | /** |
||
82 | * @return void |
||
83 | */ |
||
84 | protected function tearDown() |
||
88 | |||
89 | /** |
||
90 | * @param Exception $exc |
||
91 | * @throws Exception |
||
92 | */ |
||
93 | protected function onNotSuccessfulTest(Exception $exc) |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | protected function getBrowser() |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | protected function getBrowserBin() |
||
115 | |||
116 | /** |
||
117 | * @param string $bin |
||
118 | * @return string |
||
119 | */ |
||
120 | protected function setBrowserBin($bin) |
||
125 | |||
126 | /** |
||
127 | * @return \PHPWebDriver_WebDriverSession |
||
128 | */ |
||
129 | public function getSession() |
||
133 | |||
134 | /** |
||
135 | * @return string |
||
136 | */ |
||
137 | protected function getSessionId() |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | protected function getServerUrl() |
||
150 | |||
151 | /** |
||
152 | * Get raw source from URL |
||
153 | * |
||
154 | * @param string $url |
||
155 | * @param null $sessId |
||
156 | * @return string |
||
157 | */ |
||
158 | protected function source($url, $sessId = null) |
||
164 | |||
165 | /** |
||
166 | * Resolve Selenium WebDriver host |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | protected function resolveHost() |
||
176 | |||
177 | /** |
||
178 | * Resolve Selenium WebDriver port |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | protected function resolvePort() |
||
188 | |||
189 | /** |
||
190 | * Resolve test session browser |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | protected function resolveBrowser() |
||
208 | |||
209 | /** |
||
210 | * Resolve test session capabilities |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | protected function resolveCapabilities() |
||
226 | |||
227 | /** |
||
228 | * Resolve test target URI |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | protected function resolveUri() |
||
240 | |||
241 | /** |
||
242 | * @param float $sec |
||
243 | * @return $this |
||
244 | */ |
||
245 | protected function sleep($sec) |
||
250 | |||
251 | /** |
||
252 | * Opens URI and asserts not error |
||
253 | * |
||
254 | * @param string $path |
||
255 | * @param string $caption |
||
256 | * @return $this |
||
257 | */ |
||
258 | protected function openOk($path = '', $caption = 'Home') |
||
266 | |||
267 | /** |
||
268 | * Assert that page is without errors |
||
269 | * |
||
270 | * @return $this |
||
271 | */ |
||
272 | protected function assertNotError() |
||
285 | |||
286 | /** |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function is404() |
||
299 | |||
300 | /** |
||
301 | * Clicks on a link |
||
302 | * |
||
303 | * @param string $linkText |
||
304 | * @param callable $callback |
||
305 | * @return $this |
||
306 | */ |
||
307 | protected function clickLink($linkText, callable $callback = null) |
||
315 | |||
316 | /** |
||
317 | * Clicks on ajax-link |
||
318 | * |
||
319 | * @param string $linkText |
||
320 | * @param callable $callback |
||
321 | * @return $this |
||
322 | */ |
||
323 | protected function clickAjaxLink($linkText, callable $callback = null) |
||
331 | |||
332 | /** |
||
333 | * @param string $class |
||
334 | * @return $this |
||
335 | */ |
||
336 | protected function clickByClass($class) |
||
341 | |||
342 | /** |
||
343 | * @param string $name |
||
344 | * @param string $value |
||
345 | * @return $this |
||
346 | * @deprecated use getSelect() |
||
347 | */ |
||
348 | protected function clickSelect($name, $value) |
||
354 | |||
355 | /** |
||
356 | * @return WebDriver\Select |
||
357 | */ |
||
358 | protected function getSelect($name) |
||
362 | |||
363 | /** |
||
364 | * Enters the input value |
||
365 | * |
||
366 | * @param string|PHPWebDriver_WebDriverElement $name |
||
367 | * @param string $value |
||
368 | * @param callable|false|null $callback |
||
369 | * @return $this |
||
370 | */ |
||
371 | protected function enterInput($name, $value, $callback = null) |
||
391 | |||
392 | /** |
||
393 | * Submit the input value |
||
394 | * |
||
395 | * @param string|PHPWebDriver_WebDriverElement $name |
||
396 | * @param string $value |
||
397 | * @return $this |
||
398 | */ |
||
399 | protected function submitInput($name, $value) |
||
406 | |||
407 | /** |
||
408 | * Assert that input value is same than expected |
||
409 | * |
||
410 | * @param string|PHPWebDriver_WebDriverElement $name |
||
411 | * @param string $expectedValue |
||
412 | * @param callable $callback |
||
413 | * @return $this |
||
414 | */ |
||
415 | public function assertInput($name, $expectedValue, callable $callback = null) |
||
422 | |||
423 | /** |
||
424 | * Focus a browser window |
||
425 | * |
||
426 | * @param int $index |
||
427 | * @return $this |
||
428 | */ |
||
429 | protected function focusWindow($index) |
||
436 | |||
437 | /** |
||
438 | * Close a browser window |
||
439 | * |
||
440 | * @return $this |
||
441 | */ |
||
442 | protected function closeWindow() |
||
448 | |||
449 | /** |
||
450 | * Wait for something, then do something else |
||
451 | * |
||
452 | * @param callable $action |
||
453 | * @param callable $callback |
||
454 | * @return $this |
||
455 | */ |
||
456 | protected function waitFor(callable $action, callable $callback = null) |
||
462 | |||
463 | /** |
||
464 | * Ajax wait |
||
465 | * |
||
466 | * Depends on jQuery. |
||
467 | * |
||
468 | * @param float $delay Seconds |
||
469 | * @return $this |
||
470 | */ |
||
471 | protected function waitForAjax($delay = .1) |
||
482 | } |
||
483 |