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 | |||
| 24 | protected static $baseNamespaces = []; |
||
| 25 | |||
| 26 | protected $baseThemeClass = 'Magium\Themes\ThemeConfigurationInterface'; |
||
| 27 | |||
| 28 | protected $postCallbacks = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var MasterListener |
||
| 32 | */ |
||
| 33 | |||
| 34 | protected static $masterListener; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \Magium\WebDriver\WebDriver |
||
| 38 | */ |
||
| 39 | protected $webdriver; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Di |
||
| 43 | */ |
||
| 44 | |||
| 45 | protected $di; |
||
| 46 | |||
| 47 | protected $textElementNodeSearch = [ |
||
| 48 | 'button', 'span', 'a', 'li', 'label', 'option', 'h1', 'h2', 'h3', 'td' |
||
| 49 | ]; |
||
| 50 | |||
| 51 | protected $initializer; |
||
| 52 | |||
| 53 | const BY_XPATH = 'byXpath'; |
||
| 54 | const BY_ID = 'byId'; |
||
| 55 | const BY_CSS_SELECTOR = 'byCssSelector'; |
||
| 56 | const BY_TEXT = 'byText'; |
||
| 57 | |||
| 58 | protected static $registrationCallbacks; |
||
| 59 | |||
| 60 | protected function setUp() |
||
| 61 | { |
||
| 62 | /* |
||
| 63 | * Putting this in the setup and not in the property means that an extending class can inject itself easily |
||
| 64 | * before the Magium namespace, thus, taking preference over the base namespace |
||
| 65 | */ |
||
| 66 | self::addBaseNamespace('Magium'); |
||
| 67 | if (!$this->initializer instanceof Initializer) { |
||
| 68 | $this->initializer = new Initializer(); |
||
| 69 | } |
||
| 70 | $this->initializer->initialize($this); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return Di |
||
| 75 | */ |
||
| 76 | public function getDi() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param Di $di |
||
| 83 | */ |
||
| 84 | public function setDi($di) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return WebDriver |
||
| 91 | */ |
||
| 92 | public function getWebdriver() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param WebDriver $webdriver |
||
| 99 | */ |
||
| 100 | public function setWebdriver(WebDriver $webdriver) |
||
| 104 | |||
| 105 | public function getInitializer() |
||
| 109 | |||
| 110 | |||
| 111 | public function __construct($name = null, array $data = [], $dataName = null, Initializer $initializer = null) |
||
| 121 | |||
| 122 | public function setTestResultObject(PHPUnit_Framework_TestResult $result) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return MasterListener |
||
| 133 | */ |
||
| 134 | |||
| 135 | public static function getMasterListener() |
||
| 142 | |||
| 143 | protected function tearDown() |
||
| 162 | |||
| 163 | |||
| 164 | public function filterWebDriverAction($by) |
||
| 177 | |||
| 178 | public function assertElementClickable($selector, $by = WebDriver::BY_ID) |
||
| 182 | |||
| 183 | |||
| 184 | public function assertElementNotClickable($selector, $by = WebDriver::BY_ID) |
||
| 189 | |||
| 190 | public static function addBaseNamespace($namespace) |
||
| 196 | |||
| 197 | public static function resolveClass( $class, $prefix = null) |
||
| 218 | |||
| 219 | public function setTypePreference($type, $preference) |
||
| 227 | |||
| 228 | protected function normalizeClassRequest($class) |
||
| 232 | |||
| 233 | public function addPostTestCallback($callback) |
||
| 240 | |||
| 241 | /** |
||
| 242 | |||
| 243 | * @param string $theme |
||
| 244 | * @return \Magium\Themes\ThemeConfigurationInterface |
||
| 245 | */ |
||
| 246 | |||
| 247 | public function getTheme($theme = null) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * |
||
| 258 | * @param string $action |
||
| 259 | * @return mixed |
||
| 260 | */ |
||
| 261 | |||
| 262 | public function getAction($action) |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $name |
||
| 272 | * @return \Magium\Magento\Identities\AbstractEntity |
||
| 273 | */ |
||
| 274 | |||
| 275 | public function getIdentity($name = 'Customer') |
||
| 281 | |||
| 282 | /** |
||
| 283 | * |
||
| 284 | * @param string $navigator |
||
| 285 | * @return \Magium\Magento\Navigators\BaseMenuNavigator |
||
| 286 | */ |
||
| 287 | |||
| 288 | public function getNavigator($navigator = 'BaseMenu') |
||
| 294 | |||
| 295 | public function getAssertion($assertion) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * |
||
| 304 | * @param string $extractor |
||
| 305 | * @return \Magium\Extractors\AbstractExtractor |
||
| 306 | */ |
||
| 307 | |||
| 308 | public function getExtractor($extractor) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Sleep the specified amount of time. |
||
| 317 | * |
||
| 318 | * Options: 1s (1 second), 1ms (1 millisecond), 1us (1 microsecond), 1ns (1 nanosecond) |
||
| 319 | * |
||
| 320 | * @param $time |
||
| 321 | */ |
||
| 322 | |||
| 323 | public function sleep($time) |
||
| 337 | |||
| 338 | |||
| 339 | public function commandOpen($url) |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * @return \Magium\Util\Log\Logger |
||
| 347 | */ |
||
| 348 | |||
| 349 | public function getLogger() |
||
| 353 | |||
| 354 | public function get($class) |
||
| 363 | |||
| 364 | |||
| 365 | public function assertElementExists($selector, $by = 'byId') |
||
| 369 | |||
| 370 | public function assertTitleEquals($title) |
||
| 375 | |||
| 376 | |||
| 377 | public function assertTitleContains($title) |
||
| 382 | |||
| 383 | |||
| 384 | public function assertNotTitleEquals($title) |
||
| 389 | |||
| 390 | |||
| 391 | public function assertNotTitleContains($title) |
||
| 396 | |||
| 397 | public function assertURLEquals($url) |
||
| 402 | |||
| 403 | public function assertURLContains($url) |
||
| 408 | |||
| 409 | |||
| 410 | public function assertURLNotEquals($url) |
||
| 415 | |||
| 416 | public function assertURLNotContains($url) |
||
| 421 | |||
| 422 | protected function elementAssertion($selector, $by, $name) |
||
| 429 | |||
| 430 | public function assertElementDisplayed($selector, $by = 'byId') |
||
| 434 | |||
| 435 | public function assertElementNotDisplayed($selector, $by = 'byId') |
||
| 447 | |||
| 448 | public function assertElementNotExists($selector, $by = 'byId') |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return LoggingAssertionExecutor |
||
| 455 | */ |
||
| 456 | |||
| 457 | public function getAssertionLogger() |
||
| 461 | |||
| 462 | public function switchThemeConfiguration($fullyQualifiedClassName) |
||
| 485 | |||
| 486 | public static function assertWebDriverElement($element) |
||
| 490 | |||
| 491 | public function assertElementHasText($node, $text) |
||
| 499 | |||
| 500 | public function assertPageHasText($text) |
||
| 510 | |||
| 511 | public function assertPageNotHasText($text) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @param $xpath |
||
| 523 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 524 | */ |
||
| 525 | |||
| 526 | public function byXpath($xpath) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param $id |
||
| 533 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 534 | */ |
||
| 535 | |||
| 536 | public function byId($id) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param $selector |
||
| 543 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 544 | */ |
||
| 545 | |||
| 546 | public function byCssSelector($selector) |
||
| 550 | |||
| 551 | protected function getElementByTextXpath($xpathTemplate, $text, $specificNodeType = null, $parentElementSelector = null) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @param string $text |
||
| 573 | * @param string $specificNodeType |
||
| 574 | * @param string $parentElementSelector |
||
| 575 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 576 | */ |
||
| 577 | public function byText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 582 | |||
| 583 | |||
| 584 | /** |
||
| 585 | * @param string $text |
||
| 586 | * @param string $specificNodeType |
||
| 587 | * @param string $parentElementSelector |
||
| 588 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 589 | */ |
||
| 590 | public function byContainsText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @return \Magium\Util\Translator\Translator |
||
| 598 | */ |
||
| 599 | |||
| 600 | public function getTranslator() |
||
| 604 | |||
| 605 | public function addTranslationCsvFile($file, $locale) |
||
| 609 | } |
||
| 610 |