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 |
||
| 32 | abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase |
||
| 33 | { |
||
| 34 | |||
| 35 | protected static $baseNamespaces = []; |
||
| 36 | |||
| 37 | protected $baseThemeClass = 'Magium\Themes\ThemeConfigurationInterface'; |
||
| 38 | |||
| 39 | protected $postCallbacks = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var MasterListener |
||
| 43 | */ |
||
| 44 | |||
| 45 | protected static $masterListener; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Magium\WebDriver\WebDriver |
||
| 49 | */ |
||
| 50 | protected $webdriver; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var Di |
||
| 54 | */ |
||
| 55 | |||
| 56 | protected $di; |
||
| 57 | |||
| 58 | protected $textElementNodeSearch = [ |
||
| 59 | 'button', 'span', 'a', 'li', 'label', 'option', 'h1', 'h2', 'h3', 'td' |
||
| 60 | ]; |
||
| 61 | |||
| 62 | protected $initializer; |
||
| 63 | |||
| 64 | const BY_XPATH = 'byXpath'; |
||
| 65 | const BY_ID = 'byId'; |
||
| 66 | const BY_CSS_SELECTOR = 'byCssSelector'; |
||
| 67 | const BY_TEXT = 'byText'; |
||
| 68 | |||
| 69 | protected static $registrationCallbacks; |
||
| 70 | |||
| 71 | protected function setUp() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return Di |
||
| 84 | */ |
||
| 85 | public function getDi() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param Di $di |
||
| 92 | */ |
||
| 93 | public function setDi($di) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return WebDriver |
||
| 100 | */ |
||
| 101 | public function getWebdriver() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param WebDriver $webdriver |
||
| 108 | */ |
||
| 109 | public function setWebdriver(WebDriver $webdriver) |
||
| 113 | |||
| 114 | public function getInitializer() |
||
| 118 | |||
| 119 | |||
| 120 | public function __construct($name = null, array $data = [], $dataName = null, Initializer $initializer = null) |
||
| 130 | |||
| 131 | public function setTestResultObject(PHPUnit_Framework_TestResult $result) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return MasterListener |
||
| 142 | */ |
||
| 143 | |||
| 144 | public static function getMasterListener() |
||
| 151 | |||
| 152 | protected function tearDown() |
||
| 165 | |||
| 166 | |||
| 167 | public function filterWebDriverAction($by) |
||
| 180 | |||
| 181 | public function assertElementClickable($selector, $by = WebDriver::BY_ID) |
||
| 185 | |||
| 186 | |||
| 187 | public function assertElementNotClickable($selector, $by = WebDriver::BY_ID) |
||
| 192 | |||
| 193 | public function setTestCaseConfigurationClass($class) |
||
| 197 | |||
| 198 | public static function addBaseNamespace($namespace) |
||
| 204 | |||
| 205 | public static function resolveClass( $class, $prefix = null) |
||
| 226 | |||
| 227 | public function setTypePreference($type, $preference) |
||
| 235 | |||
| 236 | protected function normalizeClassRequest($class) |
||
| 240 | |||
| 241 | public function addPostTestCallback($callback) |
||
| 248 | |||
| 249 | /** |
||
| 250 | |||
| 251 | * @param string $theme |
||
| 252 | * @return \Magium\Themes\ThemeConfigurationInterface |
||
| 253 | */ |
||
| 254 | |||
| 255 | public function getTheme($theme = null) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * |
||
| 266 | * @param string $action |
||
| 267 | * @return mixed |
||
| 268 | */ |
||
| 269 | |||
| 270 | public function getAction($action) |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $name |
||
| 280 | * @return \Magium\Magento\Identities\AbstractEntity |
||
| 281 | */ |
||
| 282 | |||
| 283 | public function getIdentity($name = 'Customer') |
||
| 289 | |||
| 290 | /** |
||
| 291 | * |
||
| 292 | * @param string $navigator |
||
| 293 | * @return \Magium\Magento\Navigators\BaseMenuNavigator |
||
| 294 | */ |
||
| 295 | |||
| 296 | public function getNavigator($navigator = 'BaseMenu') |
||
| 302 | |||
| 303 | public function getAssertion($assertion) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * |
||
| 312 | * @param string $extractor |
||
| 313 | * @return \Magium\Extractors\AbstractExtractor |
||
| 314 | */ |
||
| 315 | |||
| 316 | public function getExtractor($extractor) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Sleep the specified amount of time. |
||
| 325 | * |
||
| 326 | * Options: 1s (1 second), 1ms (1 millisecond), 1us (1 microsecond), 1ns (1 nanosecond) |
||
| 327 | * |
||
| 328 | * @param $time |
||
| 329 | */ |
||
| 330 | |||
| 331 | public function sleep($time) |
||
| 345 | |||
| 346 | |||
| 347 | public function commandOpen($url) |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * @return \Magium\Util\Log\Logger |
||
| 355 | */ |
||
| 356 | |||
| 357 | public function getLogger() |
||
| 361 | |||
| 362 | public function get($class) |
||
| 371 | |||
| 372 | |||
| 373 | public function assertElementExists($selector, $by = 'byId') |
||
| 377 | |||
| 378 | public function assertTitleEquals($title) |
||
| 383 | |||
| 384 | |||
| 385 | public function assertTitleContains($title) |
||
| 390 | |||
| 391 | |||
| 392 | public function assertNotTitleEquals($title) |
||
| 397 | |||
| 398 | |||
| 399 | public function assertNotTitleContains($title) |
||
| 404 | |||
| 405 | public function assertURLEquals($url) |
||
| 410 | |||
| 411 | public function assertURLContains($url) |
||
| 416 | |||
| 417 | |||
| 418 | public function assertURLNotEquals($url) |
||
| 423 | |||
| 424 | public function assertURLNotContains($url) |
||
| 429 | |||
| 430 | protected function elementAssertion($selector, $by, $name) |
||
| 437 | |||
| 438 | public function assertElementDisplayed($selector, $by = 'byId') |
||
| 442 | |||
| 443 | public function assertElementNotDisplayed($selector, $by = 'byId') |
||
| 455 | |||
| 456 | public function assertElementNotExists($selector, $by = 'byId') |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return LoggingAssertionExecutor |
||
| 463 | */ |
||
| 464 | |||
| 465 | public function getAssertionLogger() |
||
| 469 | |||
| 470 | public function switchThemeConfiguration($fullyQualifiedClassName) |
||
| 493 | |||
| 494 | public static function assertWebDriverElement($element) |
||
| 498 | |||
| 499 | public function assertElementHasText($node, $text) |
||
| 507 | |||
| 508 | public function assertPageHasText($text) |
||
| 518 | |||
| 519 | public function assertPageNotHasText($text) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @param $xpath |
||
| 531 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 532 | */ |
||
| 533 | |||
| 534 | public function byXpath($xpath) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @param $id |
||
| 541 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 542 | */ |
||
| 543 | |||
| 544 | public function byId($id) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param $selector |
||
| 551 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 552 | */ |
||
| 553 | |||
| 554 | public function byCssSelector($selector) |
||
| 558 | |||
| 559 | protected function getElementByTextXpath($xpathTemplate, $text, $specificNodeType = null, $parentElementSelector = null) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param string $text |
||
| 581 | * @param string $specificNodeType |
||
| 582 | * @param string $parentElementSelector |
||
| 583 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 584 | */ |
||
| 585 | public function byText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * @param string $text |
||
| 594 | * @param string $specificNodeType |
||
| 595 | * @param string $parentElementSelector |
||
| 596 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 597 | */ |
||
| 598 | public function byContainsText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @return \Magium\Util\Translator\Translator |
||
| 606 | */ |
||
| 607 | |||
| 608 | public function getTranslator() |
||
| 612 | |||
| 613 | public function addTranslationCsvFile($file, $locale) |
||
| 617 | } |
||
| 618 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: