Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FunctionalTest 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 FunctionalTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class FunctionalTest extends SapphireTest |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * Set this to true on your sub-class to disable the use of themes in this test. |
||
| 40 | * This can be handy for functional testing of modules without having to worry about whether a user has changed |
||
| 41 | * behaviour by replacing the theme. |
||
| 42 | * |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected static $disable_themes = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Set this to true on your sub-class to use the draft site by default for every test in this class. |
||
| 49 | * |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected static $use_draft_site = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var TestSession |
||
| 56 | */ |
||
| 57 | protected $mainSession = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * CSSContentParser for the most recently requested page. |
||
| 61 | * |
||
| 62 | * @var CSSContentParser |
||
| 63 | */ |
||
| 64 | protected $cssParser = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * If this is true, then 30x Location headers will be automatically followed. |
||
| 68 | * If not, then you will have to manaully call $this->mainSession->followRedirection() to follow them. |
||
| 69 | * However, this will let you inspect the intermediary headers |
||
| 70 | * |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | protected $autoFollowRedirection = true; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns the {@link Session} object for this test |
||
| 77 | * |
||
| 78 | * @return Session |
||
| 79 | */ |
||
| 80 | public function session() |
||
| 81 | { |
||
| 82 | return $this->mainSession->session(); |
||
| 83 | } |
||
| 84 | |||
| 85 | protected function setUp() |
||
| 86 | { |
||
| 87 | // Skip calling FunctionalTest directly. |
||
| 88 | if (static::class == __CLASS__) { |
||
| 89 | $this->markTestSkipped(sprintf('Skipping %s ', static::class)); |
||
| 90 | } |
||
| 91 | |||
| 92 | parent::setUp(); |
||
| 93 | $this->mainSession = new TestSession(); |
||
| 94 | |||
| 95 | // Disable theme, if necessary |
||
| 96 | if (static::get_disable_themes()) { |
||
| 97 | SSViewer::config()->update('theme_enabled', false); |
||
| 98 | } |
||
| 99 | |||
| 100 | // Switch to draft site, if necessary |
||
| 101 | if (static::get_use_draft_site()) { |
||
| 102 | $this->useDraftSite(); |
||
| 103 | } |
||
| 104 | |||
| 105 | // Unprotect the site, tests are running with the assumption it's off. They will enable it on a case-by-case |
||
| 106 | // basis. |
||
| 107 | BasicAuth::protect_entire_site(false); |
||
| 108 | |||
| 109 | $this->logOut(); |
||
| 110 | |||
| 111 | SecurityToken::disable(); |
||
| 112 | } |
||
| 113 | |||
| 114 | protected function tearDown() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Run a test while mocking the base url with the provided value |
||
| 124 | * @param string $url The base URL to use for this test |
||
| 125 | * @param callable $callback The test to run |
||
| 126 | */ |
||
| 127 | protected function withBaseURL($url, $callback) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Run a test while mocking the base folder with the provided value |
||
| 137 | * @param string $folder The base folder to use for this test |
||
| 138 | * @param callable $callback The test to run |
||
| 139 | */ |
||
| 140 | protected function withBaseFolder($folder, $callback) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Submit a get request |
||
| 150 | * @uses Director::test() |
||
| 151 | * |
||
| 152 | * @param string $url |
||
| 153 | * @param Session $session |
||
| 154 | * @param array $headers |
||
| 155 | * @param array $cookies |
||
| 156 | * @return HTTPResponse |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function get($url, $session = null, $headers = null, $cookies = null) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Submit a post request |
||
| 170 | * |
||
| 171 | * @uses Director::test() |
||
| 172 | * @param string $url |
||
| 173 | * @param array $data |
||
| 174 | * @param array $headers |
||
| 175 | * @param Session $session |
||
| 176 | * @param string $body |
||
| 177 | * @param array $cookies |
||
| 178 | * @return HTTPResponse |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Submit the form with the given HTML ID, filling it out with the given data. |
||
| 192 | * Acts on the most recent response. |
||
| 193 | * |
||
| 194 | * Any data parameters have to be present in the form, with exact form field name |
||
| 195 | * and values, otherwise they are removed from the submission. |
||
| 196 | * |
||
| 197 | * Caution: Parameter names have to be formatted |
||
| 198 | * as they are in the form submission, not as they are interpreted by PHP. |
||
| 199 | * Wrong: array('mycheckboxvalues' => array(1 => 'one', 2 => 'two')) |
||
| 200 | * Right: array('mycheckboxvalues[1]' => 'one', 'mycheckboxvalues[2]' => 'two') |
||
| 201 | * |
||
| 202 | * @see http://www.simpletest.org/en/form_testing_documentation.html |
||
| 203 | * |
||
| 204 | * @param string $formID HTML 'id' attribute of a form (loaded through a previous response) |
||
| 205 | * @param string $button HTML 'name' attribute of the button (NOT the 'id' attribute) |
||
| 206 | * @param array $data Map of GET/POST data. |
||
| 207 | * @return HTTPResponse |
||
| 208 | */ |
||
| 209 | public function submitForm($formID, $button = null, $data = array()) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Return the most recent content |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function content() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Find an attribute in a SimpleXMLElement object by name. |
||
| 231 | * @param SimpleXMLElement $object |
||
| 232 | * @param string $attribute Name of attribute to find |
||
| 233 | * @return SimpleXMLElement object of the attribute |
||
| 234 | */ |
||
| 235 | public function findAttribute($object, $attribute) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Return a CSSContentParser for the most recent content. |
||
| 248 | * |
||
| 249 | * @return CSSContentParser |
||
| 250 | */ |
||
| 251 | public function cssParser() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Assert that the most recently queried page contains a number of content tags specified by a CSS selector. |
||
| 261 | * The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag |
||
| 262 | * will be examined. The assertion fails if one of the expectedMatches fails to appear. |
||
| 263 | * |
||
| 264 | * Note: characters are stripped from the content; make sure that your assertions take this into account. |
||
| 265 | * |
||
| 266 | * @param string $selector A basic CSS selector, e.g. 'li.jobs h3' |
||
| 267 | * @param array|string $expectedMatches The content of at least one of the matched tags |
||
| 268 | * @param string $message |
||
| 269 | * @throws PHPUnit_Framework_AssertionFailedError |
||
| 270 | */ |
||
| 271 | public function assertPartialMatchBySelector($selector, $expectedMatches, $message = null) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Assert that the most recently queried page contains a number of content tags specified by a CSS selector. |
||
| 298 | * The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag |
||
| 299 | * will be examined. The assertion fails if one of the expectedMatches fails to appear. |
||
| 300 | * |
||
| 301 | * Note: characters are stripped from the content; make sure that your assertions take this into account. |
||
| 302 | * |
||
| 303 | * @param string $selector A basic CSS selector, e.g. 'li.jobs h3' |
||
| 304 | * @param array|string $expectedMatches The content of *all* matching tags as an array |
||
| 305 | * @param string $message |
||
| 306 | * @throws PHPUnit_Framework_AssertionFailedError |
||
| 307 | */ |
||
| 308 | public function assertExactMatchBySelector($selector, $expectedMatches, $message = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Assert that the most recently queried page contains a number of content tags specified by a CSS selector. |
||
| 333 | * The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag |
||
| 334 | * will be examined. The assertion fails if one of the expectedMatches fails to appear. |
||
| 335 | * |
||
| 336 | * Note: characters are stripped from the content; make sure that your assertions take this into account. |
||
| 337 | * |
||
| 338 | * @param string $selector A basic CSS selector, e.g. 'li.jobs h3' |
||
| 339 | * @param array|string $expectedMatches The content of at least one of the matched tags |
||
| 340 | * @param string $message |
||
| 341 | * @throws PHPUnit_Framework_AssertionFailedError |
||
| 342 | */ |
||
| 343 | public function assertPartialHTMLMatchBySelector($selector, $expectedMatches, $message = null) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Assert that the most recently queried page contains a number of content tags specified by a CSS selector. |
||
| 371 | * The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag |
||
| 372 | * will be examined. The assertion fails if one of the expectedMatches fails to appear. |
||
| 373 | * |
||
| 374 | * Note: characters are stripped from the content; make sure that your assertions take this into account. |
||
| 375 | * |
||
| 376 | * @param string $selector A basic CSS selector, e.g. 'li.jobs h3' |
||
| 377 | * @param array|string $expectedMatches The content of *all* matched tags as an array |
||
| 378 | * @param string $message |
||
| 379 | * @throws PHPUnit_Framework_AssertionFailedError |
||
| 380 | */ |
||
| 381 | public function assertExactHTMLMatchBySelector($selector, $expectedMatches, $message = null) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Use the draft (stage) site for testing. |
||
| 403 | * This is helpful if you're not testing publication functionality and don't want "stage management" cluttering |
||
| 404 | * your test. |
||
| 405 | * |
||
| 406 | * @param bool $enabled toggle the use of the draft site |
||
| 407 | */ |
||
| 408 | public function useDraftSite($enabled = true) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | public static function get_disable_themes() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | public static function get_use_draft_site() |
||
| 434 | } |
||
| 435 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: