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 |
||
| 53 | class SapphireTest extends PHPUnit_Framework_TestCase |
||
| 54 | { |
||
| 55 | |||
| 56 | /** @config */ |
||
| 57 | private static $dependencies = array( |
||
| 58 | 'fixtureFactory' => '%$FixtureFactory', |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Path to fixture data for this test run. |
||
| 63 | * If passed as an array, multiple fixture files will be loaded. |
||
| 64 | * Please note that you won't be able to refer with "=>" notation |
||
| 65 | * between the fixtures, they act independent of each other. |
||
| 66 | * |
||
| 67 | * @var string|array |
||
| 68 | */ |
||
| 69 | protected static $fixture_file = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var FixtureFactory |
||
| 73 | */ |
||
| 74 | protected $fixtureFactory; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var Boolean If set to TRUE, this will force a test database to be generated |
||
| 78 | * in {@link setUp()}. Note that this flag is overruled by the presence of a |
||
| 79 | * {@link $fixture_file}, which always forces a database build. |
||
| 80 | */ |
||
| 81 | protected $usesDatabase = null; |
||
| 82 | protected $originalMemberPasswordValidator; |
||
| 83 | protected $originalRequirements; |
||
| 84 | protected $originalIsRunningTest; |
||
| 85 | protected $originalNestedURLsState; |
||
| 86 | protected $originalMemoryLimit; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var TestMailer |
||
| 90 | */ |
||
| 91 | protected $mailer; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Pointer to the manifest that isn't a test manifest |
||
| 95 | */ |
||
| 96 | protected static $regular_manifest; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var boolean |
||
| 100 | */ |
||
| 101 | protected static $is_running_test = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var ClassManifest |
||
| 105 | */ |
||
| 106 | protected static $test_class_manifest; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * By default, setUp() does not require default records. Pass |
||
| 110 | * class names in here, and the require/augment default records |
||
| 111 | * function will be called on them. |
||
| 112 | */ |
||
| 113 | protected $requireDefaultRecordsFrom = array(); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * A list of extensions that can't be applied during the execution of this run. If they are |
||
| 117 | * applied, they will be temporarily removed and a database migration called. |
||
| 118 | * |
||
| 119 | * The keys of the are the classes that the extensions can't be applied the extensions to, and |
||
| 120 | * the values are an array of illegal extensions on that class. |
||
| 121 | * |
||
| 122 | * Set a class to `*` to remove all extensions (unadvised) |
||
| 123 | */ |
||
| 124 | protected static $illegal_extensions = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * A list of extensions that must be applied during the execution of this run. If they are |
||
| 128 | * not applied, they will be temporarily added and a database migration called. |
||
| 129 | * |
||
| 130 | * The keys of the are the classes to apply the extensions to, and the values are an array |
||
| 131 | * of required extensions on that class. |
||
| 132 | * |
||
| 133 | * Example: |
||
| 134 | * <code> |
||
| 135 | * array("MyTreeDataObject" => array("Versioned", "Hierarchy")) |
||
| 136 | * </code> |
||
| 137 | */ |
||
| 138 | protected static $required_extensions = []; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * By default, the test database won't contain any DataObjects that have the interface TestOnly. |
||
| 142 | * This variable lets you define additional TestOnly DataObjects to set up for this test. |
||
| 143 | * Set it to an array of DataObject subclass names. |
||
| 144 | */ |
||
| 145 | protected static $extra_dataobjects = []; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * List of class names of {@see Controller} objects to register routes for |
||
| 149 | * Controllers must implement Link() method |
||
| 150 | * |
||
| 151 | * @var array |
||
| 152 | */ |
||
| 153 | protected static $extra_controllers = []; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * We need to disabling backing up of globals to avoid overriding |
||
| 157 | * the few globals SilverStripe relies on, like $lang for the i18n subsystem. |
||
| 158 | * |
||
| 159 | * @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html |
||
| 160 | */ |
||
| 161 | protected $backupGlobals = false; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Helper arrays for illegal_extensions/required_extensions code |
||
| 165 | */ |
||
| 166 | private static $extensions_to_reapply = []; |
||
| 167 | |||
| 168 | private static $extensions_to_remove = []; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Check flushables on setupBeforeClass() |
||
| 172 | * |
||
| 173 | * @var bool |
||
| 174 | */ |
||
| 175 | protected static $flushedFlushables = false; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Determines if unit tests are currently run, flag set during test bootstrap. |
||
| 179 | * This is used as a cheap replacement for fully mockable state |
||
| 180 | * in certain contiditions (e.g. access checks). |
||
| 181 | * Caution: When set to FALSE, certain controllers might bypass |
||
| 182 | * access checks, so this is a very security sensitive setting. |
||
| 183 | * |
||
| 184 | * @return boolean |
||
| 185 | */ |
||
| 186 | public static function is_running_test() |
||
| 190 | |||
| 191 | public static function set_is_running_test($bool) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set the manifest to be used to look up test classes by helper functions |
||
| 198 | * |
||
| 199 | * @param ClassManifest $manifest |
||
| 200 | */ |
||
| 201 | public static function set_test_class_manifest($manifest) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Return the manifest being used to look up test classes by helper functions |
||
| 208 | * |
||
| 209 | * @return ClassManifest |
||
| 210 | */ |
||
| 211 | public static function get_test_class_manifest() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return String |
||
| 218 | */ |
||
| 219 | public static function get_fixture_file() |
||
| 223 | |||
| 224 | protected $model; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * State of Versioned before this test is run |
||
| 228 | * |
||
| 229 | * @var string |
||
| 230 | */ |
||
| 231 | protected $originalReadingMode = null; |
||
| 232 | |||
| 233 | protected $originalEnv = null; |
||
| 234 | |||
| 235 | protected function setUp() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Called once per test case ({@link SapphireTest} subclass). |
||
| 338 | * This is different to {@link setUp()}, which gets called once |
||
| 339 | * per method. Useful to initialize expensive operations which |
||
| 340 | * don't change state for any called method inside the test, |
||
| 341 | * e.g. dynamically adding an extension. See {@link teardownAfterClass()} |
||
| 342 | * for tearing down the state again. |
||
| 343 | */ |
||
| 344 | public static function setUpBeforeClass() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * tearDown method that's called once per test class rather once per test method. |
||
| 429 | */ |
||
| 430 | public static function tearDownAfterClass() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return FixtureFactory |
||
| 466 | */ |
||
| 467 | public function getFixtureFactory() |
||
| 474 | |||
| 475 | public function setFixtureFactory(FixtureFactory $factory) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get the ID of an object from the fixture. |
||
| 483 | * |
||
| 484 | * @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work |
||
| 485 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 486 | * @return int |
||
| 487 | */ |
||
| 488 | View Code Duplication | protected function idFromFixture($className, $identifier) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Return all of the IDs in the fixture of a particular class name. |
||
| 505 | * Will collate all IDs form all fixtures if multiple fixtures are provided. |
||
| 506 | * |
||
| 507 | * @param string $className The data class or table name, as specified in your fixture file |
||
| 508 | * @return array A map of fixture-identifier => object-id |
||
| 509 | */ |
||
| 510 | protected function allFixtureIDs($className) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Get an object from the fixture. |
||
| 517 | * |
||
| 518 | * @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work |
||
| 519 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 520 | * |
||
| 521 | * @return DataObject |
||
| 522 | */ |
||
| 523 | View Code Duplication | protected function objFromFixture($className, $identifier) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Load a YAML fixture file into the database. |
||
| 540 | * Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. |
||
| 541 | * Doesn't clear existing fixtures. |
||
| 542 | * |
||
| 543 | * @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir |
||
| 544 | */ |
||
| 545 | public function loadFixture($fixtureFile) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Clear all fixtures which were previously loaded through |
||
| 553 | * {@link loadFixture()} |
||
| 554 | */ |
||
| 555 | public function clearFixtures() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Useful for writing unit tests without hardcoding folder structures. |
||
| 562 | * |
||
| 563 | * @return String Absolute path to current class. |
||
| 564 | */ |
||
| 565 | protected function getCurrentAbsolutePath() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @return String File path relative to webroot |
||
| 576 | */ |
||
| 577 | protected function getCurrentRelativePath() |
||
| 586 | |||
| 587 | protected function tearDown() |
||
| 630 | |||
| 631 | View Code Duplication | public static function assertContains( |
|
| 644 | |||
| 645 | View Code Duplication | public static function assertNotContains( |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Clear the log of emails sent |
||
| 661 | */ |
||
| 662 | public function clearEmails() |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Search for an email that was sent. |
||
| 669 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 670 | * @param $to |
||
| 671 | * @param $from |
||
| 672 | * @param $subject |
||
| 673 | * @param $content |
||
| 674 | * @return array Contains keys: 'type', 'to', 'from', 'subject','content', 'plainContent', 'attachedFiles', |
||
| 675 | * 'customHeaders', 'htmlContent', 'inlineImages' |
||
| 676 | */ |
||
| 677 | public function findEmail($to, $from = null, $subject = null, $content = null) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Assert that the matching email was sent since the last call to clearEmails() |
||
| 684 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 685 | * @param $to |
||
| 686 | * @param $from |
||
| 687 | * @param $subject |
||
| 688 | * @param $content |
||
| 689 | * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
||
| 690 | * 'customHeaders', 'htmlContent', inlineImages' |
||
| 691 | */ |
||
| 692 | public function assertEmailSent($to, $from = null, $subject = null, $content = null) |
||
| 719 | |||
| 720 | |||
| 721 | /** |
||
| 722 | * Assert that the given {@link SS_List} includes DataObjects matching the given key-value |
||
| 723 | * pairs. Each match must correspond to 1 distinct record. |
||
| 724 | * |
||
| 725 | * @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 726 | * either pass a single pattern or an array of patterns. |
||
| 727 | * @param SS_List $dataObjectSet The {@link SS_List} to test. |
||
| 728 | * |
||
| 729 | * Examples |
||
| 730 | * -------- |
||
| 731 | * Check that $members includes an entry with Email = [email protected]: |
||
| 732 | * $this->assertDOSContains(array('Email' => '[email protected]'), $members); |
||
| 733 | * |
||
| 734 | * Check that $members includes entries with Email = [email protected] and with |
||
| 735 | * Email = [email protected]: |
||
| 736 | * $this->assertDOSContains(array( |
||
| 737 | * array('Email' => '[email protected]'), |
||
| 738 | * array('Email' => '[email protected]'), |
||
| 739 | * ), $members); |
||
| 740 | */ |
||
| 741 | public function assertDOSContains($matches, $dataObjectSet) |
||
| 769 | /** |
||
| 770 | * Asserts that no items in a given list appear in the given dataobject list |
||
| 771 | * |
||
| 772 | * @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 773 | * either pass a single pattern or an array of patterns. |
||
| 774 | * @param SS_List $dataObjectSet The {@link SS_List} to test. |
||
| 775 | * |
||
| 776 | * Examples |
||
| 777 | * -------- |
||
| 778 | * Check that $members doesn't have an entry with Email = [email protected]: |
||
| 779 | * $this->assertNotDOSContains(array('Email' => '[email protected]'), $members); |
||
| 780 | * |
||
| 781 | * Check that $members doesn't have entries with Email = [email protected] and with |
||
| 782 | * Email = [email protected]: |
||
| 783 | * $this->assertNotDOSContains(array( |
||
| 784 | * array('Email' => '[email protected]'), |
||
| 785 | * array('Email' => '[email protected]'), |
||
| 786 | * ), $members); |
||
| 787 | */ |
||
| 788 | public function assertNotDOSContains($matches, $dataObjectSet) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Assert that the given {@link SS_List} includes only DataObjects matching the given |
||
| 816 | * key-value pairs. Each match must correspond to 1 distinct record. |
||
| 817 | * |
||
| 818 | * Example |
||
| 819 | * -------- |
||
| 820 | * Check that *only* the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't |
||
| 821 | * matter: |
||
| 822 | * $this->assertDOSEquals(array( |
||
| 823 | * array('FirstName' =>'Sam', 'Surname' => 'Minnee'), |
||
| 824 | * array('FirstName' => 'Ingo', 'Surname' => 'Schommer'), |
||
| 825 | * ), $members); |
||
| 826 | * |
||
| 827 | * @param mixed $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 828 | * either pass a single pattern or an array of patterns. |
||
| 829 | * @param mixed $dataObjectSet The {@link SS_List} to test. |
||
| 830 | */ |
||
| 831 | public function assertDOSEquals($matches, $dataObjectSet) |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Assert that the every record in the given {@link SS_List} matches the given key-value |
||
| 876 | * pairs. |
||
| 877 | * |
||
| 878 | * Example |
||
| 879 | * -------- |
||
| 880 | * Check that every entry in $members has a Status of 'Active': |
||
| 881 | * $this->assertDOSAllMatch(array('Status' => 'Active'), $members); |
||
| 882 | * |
||
| 883 | * @param mixed $match The pattern to match. The pattern is a map of key-value pairs. |
||
| 884 | * @param mixed $dataObjectSet The {@link SS_List} to test. |
||
| 885 | */ |
||
| 886 | public function assertDOSAllMatch($match, $dataObjectSet) |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Removes sequences of repeated whitespace characters from SQL queries |
||
| 905 | * making them suitable for string comparison |
||
| 906 | * |
||
| 907 | * @param string $sql |
||
| 908 | * @return string The cleaned and normalised SQL string |
||
| 909 | */ |
||
| 910 | protected function normaliseSQL($sql) |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Asserts that two SQL queries are equivalent |
||
| 917 | * |
||
| 918 | * @param string $expectedSQL |
||
| 919 | * @param string $actualSQL |
||
| 920 | * @param string $message |
||
| 921 | * @param float|int $delta |
||
| 922 | * @param integer $maxDepth |
||
| 923 | * @param boolean $canonicalize |
||
| 924 | * @param boolean $ignoreCase |
||
| 925 | */ |
||
| 926 | public function assertSQLEquals( |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Asserts that a SQL query contains a SQL fragment |
||
| 944 | * |
||
| 945 | * @param string $needleSQL |
||
| 946 | * @param string $haystackSQL |
||
| 947 | * @param string $message |
||
| 948 | * @param boolean $ignoreCase |
||
| 949 | * @param boolean $checkForObjectIdentity |
||
| 950 | */ |
||
| 951 | View Code Duplication | public function assertSQLContains( |
|
| 963 | |||
| 964 | /** |
||
| 965 | * Asserts that a SQL query contains a SQL fragment |
||
| 966 | * |
||
| 967 | * @param string $needleSQL |
||
| 968 | * @param string $haystackSQL |
||
| 969 | * @param string $message |
||
| 970 | * @param boolean $ignoreCase |
||
| 971 | * @param boolean $checkForObjectIdentity |
||
| 972 | */ |
||
| 973 | View Code Duplication | public function assertSQLNotContains( |
|
| 985 | |||
| 986 | /** |
||
| 987 | * Helper function for the DOS matchers |
||
| 988 | * |
||
| 989 | * @param array $item |
||
| 990 | * @param array $match |
||
| 991 | * @return bool |
||
| 992 | */ |
||
| 993 | private function dataObjectArrayMatch($item, $match) |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Helper function for the DOS matchers |
||
| 1005 | * |
||
| 1006 | * @param SS_List|array $dataObjectSet |
||
| 1007 | * @param array $match |
||
| 1008 | * @return string |
||
| 1009 | */ |
||
| 1010 | private function DOSSummaryForMatch($dataObjectSet, $match) |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Start test environment |
||
| 1021 | */ |
||
| 1022 | public static function start() |
||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Pushes a class and template manifest instance that include tests onto the |
||
| 1033 | * top of the loader stacks. |
||
| 1034 | */ |
||
| 1035 | protected static function use_test_manifest() |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Returns true if we are currently using a temporary database |
||
| 1065 | */ |
||
| 1066 | public static function using_temp_db() |
||
| 1072 | |||
| 1073 | public static function kill_temp_db() |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Remove all content from the temporary database. |
||
| 1097 | */ |
||
| 1098 | public static function empty_temp_db() |
||
| 1114 | |||
| 1115 | public static function create_temp_db() |
||
| 1144 | |||
| 1145 | public static function delete_all_temp_dbs() |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Reset the testing database's schema. |
||
| 1163 | * @param bool $includeExtraDataObjects If true, the extraDataObjects tables will also be included |
||
| 1164 | */ |
||
| 1165 | public static function resetDBSchema($includeExtraDataObjects = false) |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Create a member and group with the given permission code, and log in with it. |
||
| 1208 | * Returns the member ID. |
||
| 1209 | * |
||
| 1210 | * @param string|array $permCode Either a permission, or list of permissions |
||
| 1211 | * @return int Member ID |
||
| 1212 | */ |
||
| 1213 | public function logInWithPermission($permCode = "ADMIN") |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * Log in as the given member |
||
| 1260 | * |
||
| 1261 | * @param Member|int|string $member The ID, fixture codename, or Member object of the member that you want to log in |
||
| 1262 | */ |
||
| 1263 | public function logInAs($member) |
||
| 1272 | |||
| 1273 | /** |
||
| 1274 | * Log out the current user |
||
| 1275 | */ |
||
| 1276 | public function logOut() |
||
| 1280 | |||
| 1281 | /** |
||
| 1282 | * Cache for logInWithPermission() |
||
| 1283 | */ |
||
| 1284 | protected $cache_generatedMembers = array(); |
||
| 1285 | |||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Test against a theme. |
||
| 1289 | * |
||
| 1290 | * @param string $themeBaseDir themes directory |
||
| 1291 | * @param string $theme Theme name |
||
| 1292 | * @param callable $callback |
||
| 1293 | * @throws Exception |
||
| 1294 | */ |
||
| 1295 | protected function useTestTheme($themeBaseDir, $theme, $callback) |
||
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Get fixture paths for this test |
||
| 1322 | * |
||
| 1323 | * @return array List of paths |
||
| 1324 | */ |
||
| 1325 | protected function getFixturePaths() |
||
| 1338 | |||
| 1339 | /** |
||
| 1340 | * Return all extra objects to scaffold for this test |
||
| 1341 | * @return array |
||
| 1342 | */ |
||
| 1343 | protected static function getExtraDataObjects() |
||
| 1347 | |||
| 1348 | /** |
||
| 1349 | * Get additional controller classes to register routes for |
||
| 1350 | * |
||
| 1351 | * @return array |
||
| 1352 | */ |
||
| 1353 | protected static function getExtraControllers() |
||
| 1357 | |||
| 1358 | /** |
||
| 1359 | * Map a fixture path to a physical file |
||
| 1360 | * |
||
| 1361 | * @param string $fixtureFilePath |
||
| 1362 | * @return string |
||
| 1363 | */ |
||
| 1364 | protected function resolveFixturePath($fixtureFilePath) |
||
| 1387 | |||
| 1388 | protected function setUpRoutes() |
||
| 1406 | |||
| 1407 | /** |
||
| 1408 | * Get extra routes to merge into Director.rules |
||
| 1409 | * |
||
| 1410 | * @return array |
||
| 1411 | */ |
||
| 1412 | protected function getExtraRoutes() |
||
| 1423 | } |
||
| 1424 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: