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 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 static $illegal_extensions = []; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * A list of extensions that must be applied during the execution of this run. If they are |
||
| 124 | * not applied, they will be temporarily added and a database migration called. |
||
| 125 | * |
||
| 126 | * The keys of the are the classes to apply the extensions to, and the values are an array |
||
| 127 | * of required extensions on that class. |
||
| 128 | * |
||
| 129 | * Example: |
||
| 130 | * <code> |
||
| 131 | * array("MyTreeDataObject" => array("Versioned", "Hierarchy")) |
||
| 132 | * </code> |
||
| 133 | */ |
||
| 134 | protected static $required_extensions = []; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * By default, the test database won't contain any DataObjects that have the interface TestOnly. |
||
| 138 | * This variable lets you define additional TestOnly DataObjects to set up for this test. |
||
| 139 | * Set it to an array of DataObject subclass names. |
||
| 140 | */ |
||
| 141 | protected static $extra_dataobjects = []; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * List of class names of {@see Controller} objects to register routes for |
||
| 145 | * Controllers must implement Link() method |
||
| 146 | * |
||
| 147 | * @var array |
||
| 148 | */ |
||
| 149 | protected static $extra_controllers = []; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * We need to disabling backing up of globals to avoid overriding |
||
| 153 | * the few globals SilverStripe relies on, like $lang for the i18n subsystem. |
||
| 154 | * |
||
| 155 | * @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html |
||
| 156 | */ |
||
| 157 | protected $backupGlobals = false; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Helper arrays for illegal_extensions/required_extensions code |
||
| 161 | */ |
||
| 162 | private static $extensions_to_reapply = []; |
||
| 163 | |||
| 164 | private static $extensions_to_remove = []; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Check flushables on setupBeforeClass() |
||
| 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 teardownAfterClass()} |
||
| 338 | * for tearing down the state again. |
||
| 339 | */ |
||
| 340 | public static function setUpBeforeClass() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * tearDown method that's called once per test class rather once per test method. |
||
| 421 | */ |
||
| 422 | public static function tearDownAfterClass() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return FixtureFactory |
||
| 458 | */ |
||
| 459 | public function getFixtureFactory() |
||
| 466 | |||
| 467 | public function setFixtureFactory(FixtureFactory $factory) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get the ID of an object from the fixture. |
||
| 475 | * |
||
| 476 | * @param string $className The data class, as specified in your fixture file. Parent classes won't work |
||
| 477 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 478 | * @return int |
||
| 479 | */ |
||
| 480 | View Code Duplication | protected function idFromFixture($className, $identifier) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Return all of the IDs in the fixture of a particular class name. |
||
| 497 | * Will collate all IDs form all fixtures if multiple fixtures are provided. |
||
| 498 | * |
||
| 499 | * @param string $className |
||
| 500 | * @return array A map of fixture-identifier => object-id |
||
| 501 | */ |
||
| 502 | protected function allFixtureIDs($className) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get an object from the fixture. |
||
| 509 | * |
||
| 510 | * @param string $className The data class, as specified in your fixture file. Parent classes won't work |
||
| 511 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 512 | * |
||
| 513 | * @return DataObject |
||
| 514 | */ |
||
| 515 | View Code Duplication | protected function objFromFixture($className, $identifier) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Load a YAML fixture file into the database. |
||
| 532 | * Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. |
||
| 533 | * Doesn't clear existing fixtures. |
||
| 534 | * |
||
| 535 | * @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir |
||
| 536 | */ |
||
| 537 | public function loadFixture($fixtureFile) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Clear all fixtures which were previously loaded through |
||
| 545 | * {@link loadFixture()} |
||
| 546 | */ |
||
| 547 | public function clearFixtures() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Useful for writing unit tests without hardcoding folder structures. |
||
| 554 | * |
||
| 555 | * @return String Absolute path to current class. |
||
| 556 | */ |
||
| 557 | protected function getCurrentAbsolutePath() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @return String File path relative to webroot |
||
| 568 | */ |
||
| 569 | protected function getCurrentRelativePath() |
||
| 578 | |||
| 579 | protected function tearDown() |
||
| 622 | |||
| 623 | View Code Duplication | public static function assertContains( |
|
| 636 | |||
| 637 | View Code Duplication | public static function assertNotContains( |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Clear the log of emails sent |
||
| 653 | */ |
||
| 654 | public function clearEmails() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Search for an email that was sent. |
||
| 661 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 662 | * @param $to |
||
| 663 | * @param $from |
||
| 664 | * @param $subject |
||
| 665 | * @param $content |
||
| 666 | * @return array Contains keys: 'type', 'to', 'from', 'subject','content', 'plainContent', 'attachedFiles', |
||
| 667 | * 'customHeaders', 'htmlContent', 'inlineImages' |
||
| 668 | */ |
||
| 669 | public function findEmail($to, $from = null, $subject = null, $content = null) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Assert that the matching email was sent since the last call to clearEmails() |
||
| 676 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 677 | * @param $to |
||
| 678 | * @param $from |
||
| 679 | * @param $subject |
||
| 680 | * @param $content |
||
| 681 | * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
||
| 682 | * 'customHeaders', 'htmlContent', inlineImages' |
||
| 683 | */ |
||
| 684 | public function assertEmailSent($to, $from = null, $subject = null, $content = null) |
||
| 711 | |||
| 712 | |||
| 713 | /** |
||
| 714 | * Assert that the given {@link SS_List} includes DataObjects matching the given key-value |
||
| 715 | * pairs. Each match must correspond to 1 distinct record. |
||
| 716 | * |
||
| 717 | * @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 718 | * either pass a single pattern or an array of patterns. |
||
| 719 | * @param SS_List $dataObjectSet The {@link SS_List} to test. |
||
| 720 | * |
||
| 721 | * Examples |
||
| 722 | * -------- |
||
| 723 | * Check that $members includes an entry with Email = [email protected]: |
||
| 724 | * $this->assertDOSContains(array('Email' => '[email protected]'), $members); |
||
| 725 | * |
||
| 726 | * Check that $members includes entries with Email = [email protected] and with |
||
| 727 | * Email = [email protected]: |
||
| 728 | * $this->assertDOSContains(array( |
||
| 729 | * array('Email' => '[email protected]'), |
||
| 730 | * array('Email' => '[email protected]'), |
||
| 731 | * ), $members); |
||
| 732 | */ |
||
| 733 | public function assertDOSContains($matches, $dataObjectSet) |
||
| 761 | /** |
||
| 762 | * Asserts that no items in a given list appear in the given dataobject list |
||
| 763 | * |
||
| 764 | * @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 765 | * either pass a single pattern or an array of patterns. |
||
| 766 | * @param SS_List $dataObjectSet The {@link SS_List} to test. |
||
| 767 | * |
||
| 768 | * Examples |
||
| 769 | * -------- |
||
| 770 | * Check that $members doesn't have an entry with Email = [email protected]: |
||
| 771 | * $this->assertNotDOSContains(array('Email' => '[email protected]'), $members); |
||
| 772 | * |
||
| 773 | * Check that $members doesn't have entries with Email = [email protected] and with |
||
| 774 | * Email = [email protected]: |
||
| 775 | * $this->assertNotDOSContains(array( |
||
| 776 | * array('Email' => '[email protected]'), |
||
| 777 | * array('Email' => '[email protected]'), |
||
| 778 | * ), $members); |
||
| 779 | */ |
||
| 780 | public function assertNotDOSContains($matches, $dataObjectSet) |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Assert that the given {@link SS_List} includes only DataObjects matching the given |
||
| 808 | * key-value pairs. Each match must correspond to 1 distinct record. |
||
| 809 | * |
||
| 810 | * Example |
||
| 811 | * -------- |
||
| 812 | * Check that *only* the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't |
||
| 813 | * matter: |
||
| 814 | * $this->assertDOSEquals(array( |
||
| 815 | * array('FirstName' =>'Sam', 'Surname' => 'Minnee'), |
||
| 816 | * array('FirstName' => 'Ingo', 'Surname' => 'Schommer'), |
||
| 817 | * ), $members); |
||
| 818 | * |
||
| 819 | * @param mixed $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 820 | * either pass a single pattern or an array of patterns. |
||
| 821 | * @param mixed $dataObjectSet The {@link SS_List} to test. |
||
| 822 | */ |
||
| 823 | public function assertDOSEquals($matches, $dataObjectSet) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Assert that the every record in the given {@link SS_List} matches the given key-value |
||
| 868 | * pairs. |
||
| 869 | * |
||
| 870 | * Example |
||
| 871 | * -------- |
||
| 872 | * Check that every entry in $members has a Status of 'Active': |
||
| 873 | * $this->assertDOSAllMatch(array('Status' => 'Active'), $members); |
||
| 874 | * |
||
| 875 | * @param mixed $match The pattern to match. The pattern is a map of key-value pairs. |
||
| 876 | * @param mixed $dataObjectSet The {@link SS_List} to test. |
||
| 877 | */ |
||
| 878 | public function assertDOSAllMatch($match, $dataObjectSet) |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Removes sequences of repeated whitespace characters from SQL queries |
||
| 897 | * making them suitable for string comparison |
||
| 898 | * |
||
| 899 | * @param string $sql |
||
| 900 | * @return string The cleaned and normalised SQL string |
||
| 901 | */ |
||
| 902 | protected function normaliseSQL($sql) |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Asserts that two SQL queries are equivalent |
||
| 909 | * |
||
| 910 | * @param string $expectedSQL |
||
| 911 | * @param string $actualSQL |
||
| 912 | * @param string $message |
||
| 913 | * @param float|int $delta |
||
| 914 | * @param integer $maxDepth |
||
| 915 | * @param boolean $canonicalize |
||
| 916 | * @param boolean $ignoreCase |
||
| 917 | */ |
||
| 918 | public function assertSQLEquals( |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Asserts that a SQL query contains a SQL fragment |
||
| 936 | * |
||
| 937 | * @param string $needleSQL |
||
| 938 | * @param string $haystackSQL |
||
| 939 | * @param string $message |
||
| 940 | * @param boolean $ignoreCase |
||
| 941 | * @param boolean $checkForObjectIdentity |
||
| 942 | */ |
||
| 943 | View Code Duplication | public function assertSQLContains( |
|
| 955 | |||
| 956 | /** |
||
| 957 | * Asserts that a SQL query contains a SQL fragment |
||
| 958 | * |
||
| 959 | * @param string $needleSQL |
||
| 960 | * @param string $haystackSQL |
||
| 961 | * @param string $message |
||
| 962 | * @param boolean $ignoreCase |
||
| 963 | * @param boolean $checkForObjectIdentity |
||
| 964 | */ |
||
| 965 | View Code Duplication | public function assertSQLNotContains( |
|
| 977 | |||
| 978 | /** |
||
| 979 | * Helper function for the DOS matchers |
||
| 980 | * |
||
| 981 | * @param array $item |
||
| 982 | * @param array $match |
||
| 983 | * @return bool |
||
| 984 | */ |
||
| 985 | private function dataObjectArrayMatch($item, $match) |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Helper function for the DOS matchers |
||
| 997 | * |
||
| 998 | * @param SS_List|array $dataObjectSet |
||
| 999 | * @param array $match |
||
| 1000 | * @return string |
||
| 1001 | */ |
||
| 1002 | private function DOSSummaryForMatch($dataObjectSet, $match) |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Start test environment |
||
| 1013 | */ |
||
| 1014 | public static function start() |
||
| 1022 | |||
| 1023 | /** |
||
| 1024 | * Pushes a class and template manifest instance that include tests onto the |
||
| 1025 | * top of the loader stacks. |
||
| 1026 | */ |
||
| 1027 | protected static function use_test_manifest() |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Returns true if we are currently using a temporary database |
||
| 1057 | */ |
||
| 1058 | public static function using_temp_db() |
||
| 1065 | |||
| 1066 | public static function kill_temp_db() |
||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * Remove all content from the temporary database. |
||
| 1090 | */ |
||
| 1091 | public static function empty_temp_db() |
||
| 1107 | |||
| 1108 | public static function create_temp_db() |
||
| 1133 | |||
| 1134 | public static function delete_all_temp_dbs() |
||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Reset the testing database's schema. |
||
| 1152 | * @param bool $includeExtraDataObjects If true, the extraDataObjects tables will also be included |
||
| 1153 | */ |
||
| 1154 | public static function resetDBSchema($includeExtraDataObjects = false) |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Create a member and group with the given permission code, and log in with it. |
||
| 1197 | * Returns the member ID. |
||
| 1198 | * |
||
| 1199 | * @param string|array $permCode Either a permission, or list of permissions |
||
| 1200 | * @return int Member ID |
||
| 1201 | */ |
||
| 1202 | public function logInWithPermission($permCode = "ADMIN") |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Cache for logInWithPermission() |
||
| 1249 | */ |
||
| 1250 | protected $cache_generatedMembers = array(); |
||
| 1251 | |||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Test against a theme. |
||
| 1255 | * |
||
| 1256 | * @param string $themeBaseDir themes directory |
||
| 1257 | * @param string $theme Theme name |
||
| 1258 | * @param callable $callback |
||
| 1259 | * @throws Exception |
||
| 1260 | */ |
||
| 1261 | protected function useTestTheme($themeBaseDir, $theme, $callback) |
||
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Get fixture paths for this test |
||
| 1288 | * |
||
| 1289 | * @return array List of paths |
||
| 1290 | */ |
||
| 1291 | protected function getFixturePaths() |
||
| 1304 | |||
| 1305 | /** |
||
| 1306 | * Return all extra objects to scaffold for this test |
||
| 1307 | * @return array |
||
| 1308 | */ |
||
| 1309 | protected static function getExtraDataObjects() |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Get additional controller classes to register routes for |
||
| 1316 | * |
||
| 1317 | * @return array |
||
| 1318 | */ |
||
| 1319 | protected static function getExtraControllers() |
||
| 1323 | |||
| 1324 | /** |
||
| 1325 | * Map a fixture path to a physical file |
||
| 1326 | * |
||
| 1327 | * @param string $fixtureFilePath |
||
| 1328 | * @return string |
||
| 1329 | */ |
||
| 1330 | protected function resolveFixturePath($fixtureFilePath) |
||
| 1353 | |||
| 1354 | protected function setUpRoutes() |
||
| 1372 | |||
| 1373 | /** |
||
| 1374 | * Get extra routes to merge into Director.rules |
||
| 1375 | * |
||
| 1376 | * @return array |
||
| 1377 | */ |
||
| 1378 | protected function getExtraRoutes() |
||
| 1389 | } |
||
| 1390 |