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 |
||
35 | class FunctionalTest extends SapphireTest |
||
36 | { |
||
37 | /** |
||
38 | * Set this to true on your sub-class to disable the use of themes in this test. |
||
39 | * This can be handy for functional testing of modules without having to worry about whether a user has changed |
||
40 | * behaviour by replacing the theme. |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected static $disable_themes = false; |
||
45 | |||
46 | /** |
||
47 | * Set this to true on your sub-class to use the draft site by default for every test in this class. |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected static $use_draft_site = false; |
||
52 | |||
53 | /** |
||
54 | * @var TestSession |
||
55 | */ |
||
56 | protected $mainSession = null; |
||
57 | |||
58 | /** |
||
59 | * CSSContentParser for the most recently requested page. |
||
60 | * |
||
61 | * @var CSSContentParser |
||
62 | */ |
||
63 | protected $cssParser = null; |
||
64 | |||
65 | /** |
||
66 | * If this is true, then 30x Location headers will be automatically followed. |
||
67 | * If not, then you will have to manaully call $this->mainSession->followRedirection() to follow them. |
||
68 | * However, this will let you inspect the intermediary headers |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $autoFollowRedirection = true; |
||
73 | |||
74 | /** |
||
75 | * Returns the {@link Session} object for this test |
||
76 | * |
||
77 | * @return Session |
||
78 | */ |
||
79 | public function session() |
||
83 | |||
84 | protected function setUp() |
||
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 | * Log in as the given member |
||
403 | * |
||
404 | * @param Member|int|string $member The ID, fixture codename, or Member object of the member that you want to log in |
||
405 | */ |
||
406 | public function logInAs($member) |
||
417 | |||
418 | |||
419 | /** |
||
420 | * Log in as the given member |
||
421 | * |
||
422 | * @param Member|int|string $member The ID, fixture codename, or Member object of the member that you want to log in |
||
423 | */ |
||
424 | public function logOut() |
||
429 | |||
430 | /** |
||
431 | * Use the draft (stage) site for testing. |
||
432 | * This is helpful if you're not testing publication functionality and don't want "stage management" cluttering |
||
433 | * your test. |
||
434 | * |
||
435 | * @param bool $enabled toggle the use of the draft site |
||
436 | */ |
||
437 | public function useDraftSite($enabled = true) |
||
447 | |||
448 | /** |
||
449 | * @return bool |
||
450 | */ |
||
451 | public static function get_disable_themes() |
||
455 | |||
456 | /** |
||
457 | * @return bool |
||
458 | */ |
||
459 | public static function get_use_draft_site() |
||
463 | } |
||
464 |
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: