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 |
||
20 | abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase |
||
21 | { |
||
22 | use ElementTrait; |
||
23 | use ElementsTrait; |
||
24 | use ScreenshotTrait; |
||
25 | |||
26 | /** |
||
27 | * Selenium Web Driver host URI |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected static $webDriverHost = 'http://localhost:%s/wd/hub'; |
||
32 | |||
33 | /** |
||
34 | * Selenium Web Driver port |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected static $webDriverPort = '4444'; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected static $browser = 'htmlunit'; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $browserBin; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $uri; |
||
54 | |||
55 | /** |
||
56 | * @var PHPWebDriver_WebDriver |
||
57 | */ |
||
58 | protected $webDriver; |
||
59 | |||
60 | /** |
||
61 | * @var \PHPWebDriver_WebDriverSession |
||
62 | */ |
||
63 | protected $session; |
||
64 | |||
65 | /** |
||
66 | * Resolves URI to open session |
||
67 | */ |
||
68 | protected function setUp() |
||
74 | |||
75 | /** |
||
76 | * Tears down the fixture, for example, closes a network connection. |
||
77 | * This method is called after a test is executed. |
||
78 | */ |
||
79 | protected function tearDown() |
||
83 | |||
84 | /** |
||
85 | * @return string |
||
86 | */ |
||
87 | protected function getBrowser() |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | protected function getBrowserBin() |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | protected function setBrowserBin($bin) |
||
108 | |||
109 | /** |
||
110 | * @return \PHPWebDriver_WebDriverSession |
||
111 | */ |
||
112 | protected function getSession() |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | protected function getSessionId() |
||
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | protected function getServerUrl() |
||
133 | |||
134 | /** |
||
135 | * Get raw source from URL |
||
136 | * |
||
137 | * @param string $url |
||
138 | * @param null $sessId |
||
139 | * @return string |
||
140 | */ |
||
141 | protected function source($url, $sessId = null) |
||
147 | |||
148 | /** |
||
149 | * Resolve Selenium WebDriver host |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | protected function resolveHost() |
||
159 | |||
160 | /** |
||
161 | * Resolve Selenium WebDriver port |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | protected function resolvePort() |
||
171 | |||
172 | /** |
||
173 | * Resolve test session browser |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | protected function resolveBrowser() |
||
191 | |||
192 | /** |
||
193 | * Resolve test session capabilities |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | protected function resolveCapabilities() |
||
209 | |||
210 | /** |
||
211 | * Resolve test target URI |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | protected function resolveUri() |
||
223 | |||
224 | /** |
||
225 | * Opens URI and asserts not error |
||
226 | * |
||
227 | * @param string $path |
||
228 | * @param string $caption |
||
229 | * @return $this |
||
230 | */ |
||
231 | protected function openOk($path = '', $caption = 'Home') |
||
238 | |||
239 | /** |
||
240 | * Assert that page is without errors |
||
241 | * |
||
242 | * @return $this |
||
243 | */ |
||
244 | protected function assertNotError() |
||
257 | |||
258 | /** |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function is404() |
||
271 | |||
272 | /** |
||
273 | * Clicks on a link |
||
274 | * |
||
275 | * @param string $linkText |
||
276 | * @param callable $callback |
||
277 | * @return $this |
||
278 | */ |
||
279 | protected function clickLink($linkText, callable $callback = null) |
||
287 | |||
288 | /** |
||
289 | * Clicks on ajax-link |
||
290 | * |
||
291 | * @param string $linkText |
||
292 | * @param callable $callback |
||
293 | * @return $this |
||
294 | */ |
||
295 | protected function clickAjaxLink($linkText, callable $callback = null) |
||
303 | |||
304 | /** |
||
305 | * @param string $name |
||
306 | * @param string $value |
||
307 | * @return $this |
||
308 | */ |
||
309 | protected function clickSelect($name, $value) |
||
315 | |||
316 | /** |
||
317 | * Enters the input value |
||
318 | * |
||
319 | * @param string|PHPWebDriver_WebDriverElement $name |
||
320 | * @param string $value |
||
321 | * @param callable|false|null $callback |
||
322 | * @return $this |
||
323 | */ |
||
324 | protected function enterInput($name, $value, $callback = null) |
||
344 | |||
345 | /** |
||
346 | * Submit the input value |
||
347 | * |
||
348 | * @param string|PHPWebDriver_WebDriverElement $name |
||
349 | * @param string $value |
||
350 | * @return $this |
||
351 | */ |
||
352 | protected function submitInput($name, $value) |
||
359 | |||
360 | /** |
||
361 | * Assert that input value is same than expected |
||
362 | * |
||
363 | * @param string|PHPWebDriver_WebDriverElement $name |
||
364 | * @param string $expectedValue |
||
365 | * @param callable $callback |
||
366 | * @return $this |
||
367 | */ |
||
368 | public function assertInput($name, $expectedValue, callable $callback = null) |
||
375 | |||
376 | /** |
||
377 | * Focus a browser window |
||
378 | * |
||
379 | * @param int $index |
||
380 | * @return $this |
||
381 | */ |
||
382 | protected function focusWindow($index) |
||
389 | |||
390 | /** |
||
391 | * Close a browser window |
||
392 | * |
||
393 | * @return $this |
||
394 | */ |
||
395 | protected function closeWindow() |
||
401 | |||
402 | /** |
||
403 | * Wait for something, then do something else |
||
404 | * |
||
405 | * @param callable $action |
||
406 | * @param callable $callback |
||
407 | * @return $this |
||
408 | */ |
||
409 | protected function waitFor(callable $action, callable $callback = null) |
||
415 | |||
416 | /** |
||
417 | * Ajax wait |
||
418 | * |
||
419 | * Depends on jQuery. |
||
420 | * |
||
421 | * @param float $delay Seconds |
||
422 | * @return $this |
||
423 | */ |
||
424 | protected function waitForAjax($delay = .1) |
||
435 | } |
||
436 |