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 | * Clicks on a link |
||
| 260 | * |
||
| 261 | * @param string $linkText |
||
| 262 | * @param callable $callback |
||
| 263 | * @return $this |
||
| 264 | */ |
||
| 265 | protected function clickLink($linkText, callable $callback = null) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Clicks on ajax-link |
||
| 276 | * |
||
| 277 | * @param string $linkText |
||
| 278 | * @param callable $callback |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | protected function clickAjaxLink($linkText, callable $callback = null) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $name |
||
| 292 | * @param string $value |
||
| 293 | * @return $this |
||
| 294 | */ |
||
| 295 | protected function clickSelect($name, $value) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Enters the input value |
||
| 304 | * |
||
| 305 | * @param string|PHPWebDriver_WebDriverElement $name |
||
| 306 | * @param string $value |
||
| 307 | * @param callable|false|null $callback |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | protected function enterInput($name, $value, $callback = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Submit the input value |
||
| 333 | * |
||
| 334 | * @param string|PHPWebDriver_WebDriverElement $name |
||
| 335 | * @param string $value |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | protected function submitInput($name, $value) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Assert that input value is same than expected |
||
| 348 | * |
||
| 349 | * @param string|PHPWebDriver_WebDriverElement $name |
||
| 350 | * @param string $expectedValue |
||
| 351 | * @param callable $callback |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | public function assertInput($name, $expectedValue, callable $callback = null) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Focus a browser window |
||
| 364 | * |
||
| 365 | * @param int $index |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | protected function focusWindow($index) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Close a browser window |
||
| 378 | * |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | protected function closeWindow() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Wait for something, then do something else |
||
| 390 | * |
||
| 391 | * @param callable $action |
||
| 392 | * @param callable $callback |
||
| 393 | * @return $this |
||
| 394 | */ |
||
| 395 | protected function waitFor(callable $action, callable $callback = null) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Ajax wait |
||
| 404 | * |
||
| 405 | * Depends on jQuery. |
||
| 406 | * |
||
| 407 | * @param float $delay Seconds |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | protected function waitForAjax($delay = .1) |
||
| 421 | } |
||
| 422 |