Complex classes like Acceptance_Test_Case 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 Acceptance_Test_Case, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class Acceptance_Test_Case extends TestCase { |
||
16 | |||
17 | /** |
||
18 | * A constant for identifying the current plugin installed as an mu-plugin in the tests. |
||
19 | */ |
||
20 | const CURRENT_MU = Test_Plugin_Factory::CURRENT . 'mu'; |
||
21 | |||
22 | /** |
||
23 | * An array containing the versions and paths of all of the autoloaders we have installed for the test class. |
||
24 | * |
||
25 | * @var string[] |
||
26 | */ |
||
27 | private $installed_autoloaders; |
||
28 | |||
29 | /** |
||
30 | * An array containing the versions and paths of autoloaders that have been symlinked. |
||
31 | * |
||
32 | * @var string[] |
||
33 | */ |
||
34 | private $symlinked_autoloaders; |
||
35 | |||
36 | /** |
||
37 | * Setup runs before each test. |
||
38 | * |
||
39 | * @before |
||
40 | */ |
||
41 | public function set_up() { |
||
49 | |||
50 | /** |
||
51 | * Teardown runs after each test. |
||
52 | * |
||
53 | * @after |
||
54 | */ |
||
55 | public function tear_down() { |
||
62 | |||
63 | /** |
||
64 | * Installs the given autoloader or autoloaders so that they can be used by tests. |
||
65 | * |
||
66 | * @param string|string[] $version_or_versions The version or array of versions of the autoloader to install. |
||
67 | * A suffix of 'mu' designates that the plugin should be an mu-plugin. |
||
68 | */ |
||
69 | protected function install_autoloaders( $version_or_versions ) { |
||
89 | |||
90 | /** |
||
91 | * Installs a symlink to a plugin version. |
||
92 | * |
||
93 | * @param string $version The version of the autoloader we want to symlink to. |
||
94 | * @param string $is_mu_plugin Whether or not the symlink should be an mu-plugin. |
||
95 | * @param string $symlink_key The key for the symlink in the installed plugin list. |
||
96 | */ |
||
97 | protected function install_autoloader_symlink( $version, $is_mu_plugin, $symlink_key ) { |
||
119 | |||
120 | /** |
||
121 | * Fetches the path for an installed autoloader. |
||
122 | * |
||
123 | * @param string $version The version of autoloader we want a path to. |
||
124 | * @return string The path to the autoloader. |
||
125 | */ |
||
126 | protected function get_autoloader_path( $version ) { |
||
133 | |||
134 | /** |
||
135 | * Loads an autoloader and tracks whether or not a reset occurred. |
||
136 | * |
||
137 | * @param string $version The version of the autoloader we want to load. |
||
138 | */ |
||
139 | protected function load_plugin_autoloader( $version ) { |
||
163 | |||
164 | /** |
||
165 | * Executes all of the given autoloader versions and triggers a shutdown. |
||
166 | * |
||
167 | * Note: This method sorts all of the mu-plugins to the front of the array to replicate WordPress' loading order. |
||
168 | * |
||
169 | * @param string[] $versions The array of versions to execute in the order they should be loaded. |
||
170 | * @param bool $after_plugins_loaded Whether or not the shutdown should be after the plugins_loaded action. |
||
171 | */ |
||
172 | protected function execute_autoloader_chain( $versions, $after_plugins_loaded ) { |
||
197 | |||
198 | /** |
||
199 | * Adds an autoloader plugin to the activated list. |
||
200 | * |
||
201 | * @param string $version The version of autoloader that we want to activate. |
||
202 | * @param bool $sitewide Indicates whether or not the plugin should be site active. |
||
203 | */ |
||
204 | protected function activate_autoloader( $version, $sitewide = false ) { |
||
239 | |||
240 | /** |
||
241 | * Triggers a shutdown action for the autoloader. |
||
242 | * |
||
243 | * @param bool $after_plugins_loaded Whether or not we should execute 'plugins_loaded' before shutting down. |
||
244 | */ |
||
245 | protected function trigger_shutdown( $after_plugins_loaded ) { |
||
252 | |||
253 | /** |
||
254 | * Erases the autoloader cache. |
||
255 | */ |
||
256 | protected function erase_cache() { |
||
259 | |||
260 | /** |
||
261 | * Adds a version or array of versions to the autoloader cache. |
||
262 | * |
||
263 | * @param string|string[] $version_or_versions The version or array of versions of the autoloader that we want to cache. |
||
264 | */ |
||
265 | protected function cache_plugin( $version_or_versions ) { |
||
292 | |||
293 | /** |
||
294 | * Asserts that the autoloader was reset a given number of times. |
||
295 | * |
||
296 | * @param int $count The number of resets we expect. |
||
297 | */ |
||
298 | protected function assertAutoloaderResetCount( $count ) { |
||
303 | |||
304 | /** |
||
305 | * Asserts that the autoloader has been initialized to a specific version. |
||
306 | * |
||
307 | * @param string $version The version of the autoloader we expect. |
||
308 | */ |
||
309 | protected function assertAutoloaderVersion( $version ) { |
||
317 | |||
318 | /** |
||
319 | * Asserts that the autoloader is able to provide the given class. |
||
320 | * |
||
321 | * @param string $fqn The fully qualified name of the class we want to load. |
||
322 | */ |
||
323 | protected function assertAutoloaderProvidesClass( $fqn ) { |
||
347 | |||
348 | /** |
||
349 | * Asserts that the autoloader did not find an unknown plugin. |
||
350 | * |
||
351 | * @param string $version The version of autoloader we expect to be known. |
||
352 | */ |
||
353 | protected function assertAutoloaderNotFoundUnknown( $version ) { |
||
359 | |||
360 | /** |
||
361 | * Asserts that the autoloader found an unknown pugin. |
||
362 | * |
||
363 | * @param string $version The version of autoloader we expect to be unknown. |
||
364 | */ |
||
365 | protected function assertAutoloaderFoundUnknown( $version ) { |
||
371 | |||
372 | /** |
||
373 | * Asserts that the autoloader cache is empty. |
||
374 | */ |
||
375 | protected function assertAutoloaderCacheEmpty() { |
||
383 | |||
384 | /** |
||
385 | * Asserts that the autoloader cache only contains the given version or array of version. |
||
386 | * |
||
387 | * @param string|string[] $version_or_versions The version or array of versions that we expect. |
||
388 | */ |
||
389 | protected function assertAutoloaderCacheEquals( $version_or_versions ) { |
||
413 | } |
||
414 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: