Complex classes like SapphireTest 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 SapphireTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class SapphireTest extends PHPUnit_Framework_TestCase |
||
| 51 | { |
||
| 52 | |||
| 53 | /** @config */ |
||
| 54 | private static $dependencies = array( |
||
| 55 | 'fixtureFactory' => '%$FixtureFactory', |
||
| 56 | ); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Path to fixture data for this test run. |
||
| 60 | * If passed as an array, multiple fixture files will be loaded. |
||
| 61 | * Please note that you won't be able to refer with "=>" notation |
||
| 62 | * between the fixtures, they act independent of each other. |
||
| 63 | * |
||
| 64 | * @var string|array |
||
| 65 | */ |
||
| 66 | protected static $fixture_file = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var FixtureFactory |
||
| 70 | */ |
||
| 71 | protected $fixtureFactory; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var Boolean If set to TRUE, this will force a test database to be generated |
||
| 75 | * in {@link setUp()}. Note that this flag is overruled by the presence of a |
||
| 76 | * {@link $fixture_file}, which always forces a database build. |
||
| 77 | */ |
||
| 78 | protected $usesDatabase = null; |
||
| 79 | protected $originalMemberPasswordValidator; |
||
| 80 | protected $originalRequirements; |
||
| 81 | protected $originalIsRunningTest; |
||
| 82 | protected $originalNestedURLsState; |
||
| 83 | protected $originalMemoryLimit; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var TestMailer |
||
| 87 | */ |
||
| 88 | protected $mailer; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Pointer to the manifest that isn't a test manifest |
||
| 92 | */ |
||
| 93 | protected static $regular_manifest; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var boolean |
||
| 97 | */ |
||
| 98 | protected static $is_running_test = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var ClassManifest |
||
| 102 | */ |
||
| 103 | protected static $test_class_manifest; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * By default, setUp() does not require default records. Pass |
||
| 107 | * class names in here, and the require/augment default records |
||
| 108 | * function will be called on them. |
||
| 109 | */ |
||
| 110 | protected $requireDefaultRecordsFrom = array(); |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * A list of extensions that can't be applied during the execution of this run. If they are |
||
| 115 | * applied, they will be temporarily removed and a database migration called. |
||
| 116 | * |
||
| 117 | * The keys of the are the classes that the extensions can't be applied the extensions to, and |
||
| 118 | * the values are an array of illegal extensions on that class. |
||
| 119 | */ |
||
| 120 | protected $illegalExtensions = array( |
||
| 121 | ); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * A list of extensions that must be applied during the execution of this run. If they are |
||
| 125 | * not applied, they will be temporarily added and a database migration called. |
||
| 126 | * |
||
| 127 | * The keys of the are the classes to apply the extensions to, and the values are an array |
||
| 128 | * of required extensions on that class. |
||
| 129 | * |
||
| 130 | * Example: |
||
| 131 | * <code> |
||
| 132 | * array("MyTreeDataObject" => array("Versioned", "Hierarchy")) |
||
| 133 | * </code> |
||
| 134 | */ |
||
| 135 | protected $requiredExtensions = array( |
||
| 136 | ); |
||
| 137 | |||
| 138 | /** |
||
| 139 | * By default, the test database won't contain any DataObjects that have the interface TestOnly. |
||
| 140 | * This variable lets you define additional TestOnly DataObjects to set up for this test. |
||
| 141 | * Set it to an array of DataObject subclass names. |
||
| 142 | */ |
||
| 143 | protected $extraDataObjects = array(); |
||
| 144 | |||
| 145 | /** |
||
| 146 | * List of class names of {@see Controller} objects to register routes for |
||
| 147 | * Controllers must implement Link() method |
||
| 148 | * |
||
| 149 | * @var array |
||
| 150 | */ |
||
| 151 | protected $extraControllers = []; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * We need to disabling backing up of globals to avoid overriding |
||
| 155 | * the few globals SilverStripe relies on, like $lang for the i18n subsystem. |
||
| 156 | * |
||
| 157 | * @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html |
||
| 158 | */ |
||
| 159 | protected $backupGlobals = false; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Helper arrays for illegalExtensions/requiredExtensions code |
||
| 163 | */ |
||
| 164 | private $extensionsToReapply = array(), $extensionsToRemove = array(); |
||
|
|
|||
| 165 | |||
| 166 | /** |
||
| 167 | * Check flushables on setupOnce() |
||
| 168 | * |
||
| 169 | * @var bool |
||
| 170 | */ |
||
| 171 | protected static $flushedFlushables = false; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Determines if unit tests are currently run, flag set during test bootstrap. |
||
| 175 | * This is used as a cheap replacement for fully mockable state |
||
| 176 | * in certain contiditions (e.g. access checks). |
||
| 177 | * Caution: When set to FALSE, certain controllers might bypass |
||
| 178 | * access checks, so this is a very security sensitive setting. |
||
| 179 | * |
||
| 180 | * @return boolean |
||
| 181 | */ |
||
| 182 | public static function is_running_test() |
||
| 186 | |||
| 187 | public static function set_is_running_test($bool) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set the manifest to be used to look up test classes by helper functions |
||
| 194 | * |
||
| 195 | * @param ClassManifest $manifest |
||
| 196 | */ |
||
| 197 | public static function set_test_class_manifest($manifest) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return the manifest being used to look up test classes by helper functions |
||
| 204 | * |
||
| 205 | * @return ClassManifest |
||
| 206 | */ |
||
| 207 | public static function get_test_class_manifest() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return String |
||
| 214 | */ |
||
| 215 | public static function get_fixture_file() |
||
| 219 | |||
| 220 | protected $model; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * State of Versioned before this test is run |
||
| 224 | * |
||
| 225 | * @var string |
||
| 226 | */ |
||
| 227 | protected $originalReadingMode = null; |
||
| 228 | |||
| 229 | protected $originalEnv = null; |
||
| 230 | |||
| 231 | protected function setUp() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Called once per test case ({@link SapphireTest} subclass). |
||
| 334 | * This is different to {@link setUp()}, which gets called once |
||
| 335 | * per method. Useful to initialize expensive operations which |
||
| 336 | * don't change state for any called method inside the test, |
||
| 337 | * e.g. dynamically adding an extension. See {@link tearDownOnce()} |
||
| 338 | * for tearing down the state again. |
||
| 339 | */ |
||
| 340 | public function setUpOnce() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * tearDown method that's called once per test class rather once per test method. |
||
| 409 | */ |
||
| 410 | public function tearDownOnce() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return FixtureFactory |
||
| 446 | */ |
||
| 447 | public function getFixtureFactory() |
||
| 454 | |||
| 455 | public function setFixtureFactory(FixtureFactory $factory) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Get the ID of an object from the fixture. |
||
| 463 | * |
||
| 464 | * @param string $className The data class, as specified in your fixture file. Parent classes won't work |
||
| 465 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 466 | * @return int |
||
| 467 | */ |
||
| 468 | protected function idFromFixture($className, $identifier) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Return all of the IDs in the fixture of a particular class name. |
||
| 485 | * Will collate all IDs form all fixtures if multiple fixtures are provided. |
||
| 486 | * |
||
| 487 | * @param string $className |
||
| 488 | * @return array A map of fixture-identifier => object-id |
||
| 489 | */ |
||
| 490 | protected function allFixtureIDs($className) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Get an object from the fixture. |
||
| 497 | * |
||
| 498 | * @param string $className The data class, as specified in your fixture file. Parent classes won't work |
||
| 499 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 500 | * |
||
| 501 | * @return DataObject |
||
| 502 | */ |
||
| 503 | protected function objFromFixture($className, $identifier) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Load a YAML fixture file into the database. |
||
| 520 | * Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. |
||
| 521 | * Doesn't clear existing fixtures. |
||
| 522 | * |
||
| 523 | * @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir |
||
| 524 | */ |
||
| 525 | public function loadFixture($fixtureFile) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Clear all fixtures which were previously loaded through |
||
| 533 | * {@link loadFixture()} |
||
| 534 | */ |
||
| 535 | public function clearFixtures() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Useful for writing unit tests without hardcoding folder structures. |
||
| 542 | * |
||
| 543 | * @return String Absolute path to current class. |
||
| 544 | */ |
||
| 545 | protected function getCurrentAbsolutePath() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @return String File path relative to webroot |
||
| 556 | */ |
||
| 557 | protected function getCurrentRelativePath() |
||
| 566 | |||
| 567 | protected function tearDown() |
||
| 610 | |||
| 611 | public static function assertContains( |
||
| 624 | |||
| 625 | public static function assertNotContains( |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Clear the log of emails sent |
||
| 641 | */ |
||
| 642 | public function clearEmails() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Search for an email that was sent. |
||
| 649 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 650 | * @param $to |
||
| 651 | * @param $from |
||
| 652 | * @param $subject |
||
| 653 | * @param $content |
||
| 654 | * @return array Contains keys: 'type', 'to', 'from', 'subject','content', 'plainContent', 'attachedFiles', |
||
| 655 | * 'customHeaders', 'htmlContent', 'inlineImages' |
||
| 656 | */ |
||
| 657 | public function findEmail($to, $from = null, $subject = null, $content = null) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Assert that the matching email was sent since the last call to clearEmails() |
||
| 664 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 665 | * @param $to |
||
| 666 | * @param $from |
||
| 667 | * @param $subject |
||
| 668 | * @param $content |
||
| 669 | * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
||
| 670 | * 'customHeaders', 'htmlContent', inlineImages' |
||
| 671 | */ |
||
| 672 | public function assertEmailSent($to, $from = null, $subject = null, $content = null) |
||
| 699 | |||
| 700 | |||
| 701 | /** |
||
| 702 | * Assert that the given {@link SS_List} includes DataObjects matching the given key-value |
||
| 703 | * pairs. Each match must correspond to 1 distinct record. |
||
| 704 | * |
||
| 705 | * @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 706 | * either pass a single pattern or an array of patterns. |
||
| 707 | * @param SS_List $dataObjectSet The {@link SS_List} to test. |
||
| 708 | * |
||
| 709 | * Examples |
||
| 710 | * -------- |
||
| 711 | * Check that $members includes an entry with Email = [email protected]: |
||
| 712 | * $this->assertDOSContains(array('Email' => '[email protected]'), $members); |
||
| 713 | * |
||
| 714 | * Check that $members includes entries with Email = [email protected] and with |
||
| 715 | * Email = [email protected]: |
||
| 716 | * $this->assertDOSContains(array( |
||
| 717 | * array('Email' => '[email protected]'), |
||
| 718 | * array('Email' => '[email protected]'), |
||
| 719 | * ), $members); |
||
| 720 | */ |
||
| 721 | public function assertDOSContains($matches, $dataObjectSet) |
||
| 749 | /** |
||
| 750 | * Asserts that no items in a given list appear in the given dataobject list |
||
| 751 | * |
||
| 752 | * @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 753 | * either pass a single pattern or an array of patterns. |
||
| 754 | * @param SS_List $dataObjectSet The {@link SS_List} to test. |
||
| 755 | * |
||
| 756 | * Examples |
||
| 757 | * -------- |
||
| 758 | * Check that $members doesn't have an entry with Email = [email protected]: |
||
| 759 | * $this->assertNotDOSContains(array('Email' => '[email protected]'), $members); |
||
| 760 | * |
||
| 761 | * Check that $members doesn't have entries with Email = [email protected] and with |
||
| 762 | * Email = [email protected]: |
||
| 763 | * $this->assertNotDOSContains(array( |
||
| 764 | * array('Email' => '[email protected]'), |
||
| 765 | * array('Email' => '[email protected]'), |
||
| 766 | * ), $members); |
||
| 767 | */ |
||
| 768 | public function assertNotDOSContains($matches, $dataObjectSet) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Assert that the given {@link SS_List} includes only DataObjects matching the given |
||
| 796 | * key-value pairs. Each match must correspond to 1 distinct record. |
||
| 797 | * |
||
| 798 | * Example |
||
| 799 | * -------- |
||
| 800 | * Check that *only* the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't |
||
| 801 | * matter: |
||
| 802 | * $this->assertDOSEquals(array( |
||
| 803 | * array('FirstName' =>'Sam', 'Surname' => 'Minnee'), |
||
| 804 | * array('FirstName' => 'Ingo', 'Surname' => 'Schommer'), |
||
| 805 | * ), $members); |
||
| 806 | * |
||
| 807 | * @param mixed $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 808 | * either pass a single pattern or an array of patterns. |
||
| 809 | * @param mixed $dataObjectSet The {@link SS_List} to test. |
||
| 810 | */ |
||
| 811 | public function assertDOSEquals($matches, $dataObjectSet) |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Assert that the every record in the given {@link SS_List} matches the given key-value |
||
| 856 | * pairs. |
||
| 857 | * |
||
| 858 | * Example |
||
| 859 | * -------- |
||
| 860 | * Check that every entry in $members has a Status of 'Active': |
||
| 861 | * $this->assertDOSAllMatch(array('Status' => 'Active'), $members); |
||
| 862 | * |
||
| 863 | * @param mixed $match The pattern to match. The pattern is a map of key-value pairs. |
||
| 864 | * @param mixed $dataObjectSet The {@link SS_List} to test. |
||
| 865 | */ |
||
| 866 | public function assertDOSAllMatch($match, $dataObjectSet) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * Removes sequences of repeated whitespace characters from SQL queries |
||
| 885 | * making them suitable for string comparison |
||
| 886 | * |
||
| 887 | * @param string $sql |
||
| 888 | * @return string The cleaned and normalised SQL string |
||
| 889 | */ |
||
| 890 | protected function normaliseSQL($sql) |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Asserts that two SQL queries are equivalent |
||
| 897 | * |
||
| 898 | * @param string $expectedSQL |
||
| 899 | * @param string $actualSQL |
||
| 900 | * @param string $message |
||
| 901 | * @param float|int $delta |
||
| 902 | * @param integer $maxDepth |
||
| 903 | * @param boolean $canonicalize |
||
| 904 | * @param boolean $ignoreCase |
||
| 905 | */ |
||
| 906 | public function assertSQLEquals( |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Asserts that a SQL query contains a SQL fragment |
||
| 924 | * |
||
| 925 | * @param string $needleSQL |
||
| 926 | * @param string $haystackSQL |
||
| 927 | * @param string $message |
||
| 928 | * @param boolean $ignoreCase |
||
| 929 | * @param boolean $checkForObjectIdentity |
||
| 930 | */ |
||
| 931 | public function assertSQLContains( |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Asserts that a SQL query contains a SQL fragment |
||
| 946 | * |
||
| 947 | * @param string $needleSQL |
||
| 948 | * @param string $haystackSQL |
||
| 949 | * @param string $message |
||
| 950 | * @param boolean $ignoreCase |
||
| 951 | * @param boolean $checkForObjectIdentity |
||
| 952 | */ |
||
| 953 | public function assertSQLNotContains( |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Helper function for the DOS matchers |
||
| 968 | * |
||
| 969 | * @param array $item |
||
| 970 | * @param array $match |
||
| 971 | * @return bool |
||
| 972 | */ |
||
| 973 | private function dataObjectArrayMatch($item, $match) |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Helper function for the DOS matchers |
||
| 985 | * |
||
| 986 | * @param SS_List|array $dataObjectSet |
||
| 987 | * @param array $match |
||
| 988 | * @return string |
||
| 989 | */ |
||
| 990 | private function DOSSummaryForMatch($dataObjectSet, $match) |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * Start test environment |
||
| 1001 | */ |
||
| 1002 | public static function start() |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Pushes a class and template manifest instance that include tests onto the |
||
| 1013 | * top of the loader stacks. |
||
| 1014 | */ |
||
| 1015 | protected static function use_test_manifest() |
||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * Returns true if we are currently using a temporary database |
||
| 1045 | */ |
||
| 1046 | public static function using_temp_db() |
||
| 1053 | |||
| 1054 | public static function kill_temp_db() |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Remove all content from the temporary database. |
||
| 1078 | */ |
||
| 1079 | public static function empty_temp_db() |
||
| 1095 | |||
| 1096 | public static function create_temp_db() |
||
| 1123 | |||
| 1124 | public static function delete_all_temp_dbs() |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * Reset the testing database's schema. |
||
| 1142 | * @param bool $includeExtraDataObjects If true, the extraDataObjects tables will also be included |
||
| 1143 | */ |
||
| 1144 | public function resetDBSchema($includeExtraDataObjects = false) |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Create a member and group with the given permission code, and log in with it. |
||
| 1187 | * Returns the member ID. |
||
| 1188 | * |
||
| 1189 | * @param string|array $permCode Either a permission, or list of permissions |
||
| 1190 | * @return int Member ID |
||
| 1191 | */ |
||
| 1192 | public function logInWithPermission($permCode = "ADMIN") |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Cache for logInWithPermission() |
||
| 1239 | */ |
||
| 1240 | protected $cache_generatedMembers = array(); |
||
| 1241 | |||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Test against a theme. |
||
| 1245 | * |
||
| 1246 | * @param string $themeBaseDir themes directory |
||
| 1247 | * @param string $theme Theme name |
||
| 1248 | * @param callable $callback |
||
| 1249 | * @throws Exception |
||
| 1250 | */ |
||
| 1251 | protected function useTestTheme($themeBaseDir, $theme, $callback) |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Get fixture paths for this test |
||
| 1278 | * |
||
| 1279 | * @return array List of paths |
||
| 1280 | */ |
||
| 1281 | protected function getFixturePaths() |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Return all extra objects to scaffold for this test |
||
| 1297 | * |
||
| 1298 | * @return array |
||
| 1299 | */ |
||
| 1300 | protected function getExtraDataObjects() |
||
| 1304 | |||
| 1305 | /** |
||
| 1306 | * Get additional controller classes to register routes for |
||
| 1307 | * |
||
| 1308 | * @return array |
||
| 1309 | */ |
||
| 1310 | protected function getExtraControllers() |
||
| 1314 | |||
| 1315 | /** |
||
| 1316 | * Map a fixture path to a physical file |
||
| 1317 | * |
||
| 1318 | * @param string $fixtureFilePath |
||
| 1319 | * @return string |
||
| 1320 | */ |
||
| 1321 | protected function resolveFixturePath($fixtureFilePath) |
||
| 1344 | |||
| 1345 | protected function setUpRoutes() |
||
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Get extra routes to merge into Director.rules |
||
| 1366 | * |
||
| 1367 | * @return array |
||
| 1368 | */ |
||
| 1369 | protected function getExtraRoutes() |
||
| 1380 | } |
||
| 1381 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.