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 | /** @config */ |
||
| 56 | private static $dependencies = array( |
||
| 57 | 'fixtureFactory' => '%$FixtureFactory', |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Path to fixture data for this test run. |
||
| 62 | * If passed as an array, multiple fixture files will be loaded. |
||
| 63 | * Please note that you won't be able to refer with "=>" notation |
||
| 64 | * between the fixtures, they act independent of each other. |
||
| 65 | * |
||
| 66 | * @var string|array |
||
| 67 | */ |
||
| 68 | protected static $fixture_file = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var FixtureFactory |
||
| 72 | */ |
||
| 73 | protected $fixtureFactory; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var Boolean If set to TRUE, this will force a test database to be generated |
||
| 77 | * in {@link setUp()}. Note that this flag is overruled by the presence of a |
||
| 78 | * {@link $fixture_file}, which always forces a database build. |
||
| 79 | */ |
||
| 80 | protected $usesDatabase = null; |
||
| 81 | protected $originalMemberPasswordValidator; |
||
| 82 | protected $originalRequirements; |
||
| 83 | protected $originalIsRunningTest; |
||
| 84 | protected $originalNestedURLsState; |
||
| 85 | protected $originalMemoryLimit; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var TestMailer |
||
| 89 | */ |
||
| 90 | protected $mailer; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Pointer to the manifest that isn't a test manifest |
||
| 94 | */ |
||
| 95 | protected static $regular_manifest; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var boolean |
||
| 99 | */ |
||
| 100 | protected static $is_running_test = false; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var ClassManifest |
||
| 104 | */ |
||
| 105 | protected static $test_class_manifest; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * By default, setUp() does not require default records. Pass |
||
| 109 | * class names in here, and the require/augment default records |
||
| 110 | * function will be called on them. |
||
| 111 | */ |
||
| 112 | protected $requireDefaultRecordsFrom = array(); |
||
| 113 | |||
| 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 | protected $illegalExtensions = array( |
||
| 123 | ); |
||
| 124 | |||
| 125 | /** |
||
| 126 | * A list of extensions that must be applied during the execution of this run. If they are |
||
| 127 | * not applied, they will be temporarily added and a database migration called. |
||
| 128 | * |
||
| 129 | * The keys of the are the classes to apply the extensions to, and the values are an array |
||
| 130 | * of required extensions on that class. |
||
| 131 | * |
||
| 132 | * Example: |
||
| 133 | * <code> |
||
| 134 | * array("MyTreeDataObject" => array("Versioned", "Hierarchy")) |
||
| 135 | * </code> |
||
| 136 | */ |
||
| 137 | protected $requiredExtensions = array( |
||
| 138 | ); |
||
| 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 $extraDataObjects = array(); |
||
| 146 | |||
| 147 | /** |
||
| 148 | * We need to disabling backing up of globals to avoid overriding |
||
| 149 | * the few globals SilverStripe relies on, like $lang for the i18n subsystem. |
||
| 150 | * |
||
| 151 | * @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html |
||
| 152 | */ |
||
| 153 | protected $backupGlobals = FALSE; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Helper arrays for illegalExtensions/requiredExtensions code |
||
| 157 | */ |
||
| 158 | private $extensionsToReapply = array(), $extensionsToRemove = array(); |
||
|
|
|||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Determines if unit tests are currently run, flag set during test bootstrap. |
||
| 163 | * This is used as a cheap replacement for fully mockable state |
||
| 164 | * in certain contiditions (e.g. access checks). |
||
| 165 | * Caution: When set to FALSE, certain controllers might bypass |
||
| 166 | * access checks, so this is a very security sensitive setting. |
||
| 167 | * |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | public static function is_running_test() { |
||
| 173 | |||
| 174 | public static function set_is_running_test($bool) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Set the manifest to be used to look up test classes by helper functions |
||
| 180 | * |
||
| 181 | * @param ClassManifest $manifest |
||
| 182 | */ |
||
| 183 | public static function set_test_class_manifest($manifest) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Return the manifest being used to look up test classes by helper functions |
||
| 189 | * |
||
| 190 | * @return ClassManifest |
||
| 191 | */ |
||
| 192 | public static function get_test_class_manifest() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return String |
||
| 198 | */ |
||
| 199 | public static function get_fixture_file() { |
||
| 202 | |||
| 203 | protected $model; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * State of Versioned before this test is run |
||
| 207 | * |
||
| 208 | * @var string |
||
| 209 | */ |
||
| 210 | protected $originalReadingMode = null; |
||
| 211 | |||
| 212 | public function setUp() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Called once per test case ({@link SapphireTest} subclass). |
||
| 312 | * This is different to {@link setUp()}, which gets called once |
||
| 313 | * per method. Useful to initialize expensive operations which |
||
| 314 | * don't change state for any called method inside the test, |
||
| 315 | * e.g. dynamically adding an extension. See {@link tearDownOnce()} |
||
| 316 | * for tearing down the state again. |
||
| 317 | */ |
||
| 318 | public function setUpOnce() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * tearDown method that's called once per test class rather once per test method. |
||
| 370 | */ |
||
| 371 | public function tearDownOnce() { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return FixtureFactory |
||
| 405 | */ |
||
| 406 | public function getFixtureFactory() { |
||
| 410 | |||
| 411 | public function setFixtureFactory(FixtureFactory $factory) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get the ID of an object from the fixture. |
||
| 418 | * |
||
| 419 | * @param string $className The data class, as specified in your fixture file. Parent classes won't work |
||
| 420 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 421 | * @return int |
||
| 422 | */ |
||
| 423 | protected function idFromFixture($className, $identifier) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Return all of the IDs in the fixture of a particular class name. |
||
| 439 | * Will collate all IDs form all fixtures if multiple fixtures are provided. |
||
| 440 | * |
||
| 441 | * @param string $className |
||
| 442 | * @return array A map of fixture-identifier => object-id |
||
| 443 | */ |
||
| 444 | protected function allFixtureIDs($className) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get an object from the fixture. |
||
| 450 | * |
||
| 451 | * @param string $className The data class, as specified in your fixture file. Parent classes won't work |
||
| 452 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 453 | * |
||
| 454 | * @return DataObject |
||
| 455 | */ |
||
| 456 | protected function objFromFixture($className, $identifier) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Load a YAML fixture file into the database. |
||
| 472 | * Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. |
||
| 473 | * Doesn't clear existing fixtures. |
||
| 474 | * |
||
| 475 | * @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir |
||
| 476 | */ |
||
| 477 | public function loadFixture($fixtureFile) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Clear all fixtures which were previously loaded through |
||
| 484 | * {@link loadFixture()} |
||
| 485 | */ |
||
| 486 | public function clearFixtures() { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Useful for writing unit tests without hardcoding folder structures. |
||
| 492 | * |
||
| 493 | * @return String Absolute path to current class. |
||
| 494 | */ |
||
| 495 | protected function getCurrentAbsolutePath() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @return String File path relative to webroot |
||
| 505 | */ |
||
| 506 | protected function getCurrentRelativePath() { |
||
| 512 | |||
| 513 | public function tearDown() { |
||
| 552 | |||
| 553 | public static function assertContains( |
||
| 564 | |||
| 565 | public static function assertNotContains( |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Clear the log of emails sent |
||
| 579 | */ |
||
| 580 | public function clearEmails() { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Search for an email that was sent. |
||
| 586 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 587 | * @param $to |
||
| 588 | * @param $from |
||
| 589 | * @param $subject |
||
| 590 | * @param $content |
||
| 591 | * @return array Contains keys: 'type', 'to', 'from', 'subject','content', 'plainContent', 'attachedFiles', |
||
| 592 | * 'customHeaders', 'htmlContent', 'inlineImages' |
||
| 593 | */ |
||
| 594 | public function findEmail($to, $from = null, $subject = null, $content = null) { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Assert that the matching email was sent since the last call to clearEmails() |
||
| 600 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 601 | * @param $to |
||
| 602 | * @param $from |
||
| 603 | * @param $subject |
||
| 604 | * @param $content |
||
| 605 | * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
||
| 606 | * 'customHeaders', 'htmlContent', inlineImages' |
||
| 607 | */ |
||
| 608 | public function assertEmailSent($to, $from = null, $subject = null, $content = null) { |
||
| 624 | |||
| 625 | |||
| 626 | /** |
||
| 627 | * Assert that the given {@link SS_List} includes DataObjects matching the given key-value |
||
| 628 | * pairs. Each match must correspond to 1 distinct record. |
||
| 629 | * |
||
| 630 | * @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 631 | * either pass a single pattern or an array of patterns. |
||
| 632 | * @param SS_List $dataObjectSet The {@link SS_List} to test. |
||
| 633 | * |
||
| 634 | * Examples |
||
| 635 | * -------- |
||
| 636 | * Check that $members includes an entry with Email = [email protected]: |
||
| 637 | * $this->assertDOSContains(array('Email' => '[email protected]'), $members); |
||
| 638 | * |
||
| 639 | * Check that $members includes entries with Email = [email protected] and with |
||
| 640 | * Email = [email protected]: |
||
| 641 | * $this->assertDOSContains(array( |
||
| 642 | * array('Email' => '[email protected]'), |
||
| 643 | * array('Email' => '[email protected]'), |
||
| 644 | * ), $members); |
||
| 645 | */ |
||
| 646 | public function assertDOSContains($matches, $dataObjectSet) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Assert that the given {@link SS_List} includes only DataObjects matching the given |
||
| 676 | * key-value pairs. Each match must correspond to 1 distinct record. |
||
| 677 | * |
||
| 678 | * Example |
||
| 679 | * -------- |
||
| 680 | * Check that *only* the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't |
||
| 681 | * matter: |
||
| 682 | * $this->assertDOSEquals(array( |
||
| 683 | * array('FirstName' =>'Sam', 'Surname' => 'Minnee'), |
||
| 684 | * array('FirstName' => 'Ingo', 'Surname' => 'Schommer'), |
||
| 685 | * ), $members); |
||
| 686 | * |
||
| 687 | * @param mixed $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 688 | * either pass a single pattern or an array of patterns. |
||
| 689 | * @param mixed $dataObjectSet The {@link SS_List} to test. |
||
| 690 | */ |
||
| 691 | public function assertDOSEquals($matches, $dataObjectSet) { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Assert that the every record in the given {@link SS_List} matches the given key-value |
||
| 735 | * pairs. |
||
| 736 | * |
||
| 737 | * Example |
||
| 738 | * -------- |
||
| 739 | * Check that every entry in $members has a Status of 'Active': |
||
| 740 | * $this->assertDOSAllMatch(array('Status' => 'Active'), $members); |
||
| 741 | * |
||
| 742 | * @param mixed $match The pattern to match. The pattern is a map of key-value pairs. |
||
| 743 | * @param mixed $dataObjectSet The {@link SS_List} to test. |
||
| 744 | */ |
||
| 745 | public function assertDOSAllMatch($match, $dataObjectSet) { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Removes sequences of repeated whitespace characters from SQL queries |
||
| 763 | * making them suitable for string comparison |
||
| 764 | * |
||
| 765 | * @param string $sql |
||
| 766 | * @return string The cleaned and normalised SQL string |
||
| 767 | */ |
||
| 768 | protected function normaliseSQL($sql) { |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Asserts that two SQL queries are equivalent |
||
| 774 | * |
||
| 775 | * @param string $expectedSQL |
||
| 776 | * @param string $actualSQL |
||
| 777 | * @param string $message |
||
| 778 | * @param float|int $delta |
||
| 779 | * @param integer $maxDepth |
||
| 780 | * @param boolean $canonicalize |
||
| 781 | * @param boolean $ignoreCase |
||
| 782 | */ |
||
| 783 | public function assertSQLEquals($expectedSQL, $actualSQL, $message = '', $delta = 0, $maxDepth = 10, |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Asserts that a SQL query contains a SQL fragment |
||
| 795 | * |
||
| 796 | * @param string $needleSQL |
||
| 797 | * @param string $haystackSQL |
||
| 798 | * @param string $message |
||
| 799 | * @param boolean $ignoreCase |
||
| 800 | * @param boolean $checkForObjectIdentity |
||
| 801 | */ |
||
| 802 | public function assertSQLContains($needleSQL, $haystackSQL, $message = '', $ignoreCase = false, |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Asserts that a SQL query contains a SQL fragment |
||
| 813 | * |
||
| 814 | * @param string $needleSQL |
||
| 815 | * @param string $haystackSQL |
||
| 816 | * @param string $message |
||
| 817 | * @param boolean $ignoreCase |
||
| 818 | * @param boolean $checkForObjectIdentity |
||
| 819 | */ |
||
| 820 | public function assertSQLNotContains($needleSQL, $haystackSQL, $message = '', $ignoreCase = false, |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Helper function for the DOS matchers |
||
| 831 | * |
||
| 832 | * @param array $item |
||
| 833 | * @param array $match |
||
| 834 | * @return bool |
||
| 835 | */ |
||
| 836 | private function dataObjectArrayMatch($item, $match) { |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Helper function for the DOS matchers |
||
| 847 | * |
||
| 848 | * @param SS_List|array $dataObjectSet |
||
| 849 | * @param array $match |
||
| 850 | * @return string |
||
| 851 | */ |
||
| 852 | private function DOSSummaryForMatch($dataObjectSet, $match) { |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Pushes a class and template manifest instance that include tests onto the |
||
| 862 | * top of the loader stacks. |
||
| 863 | */ |
||
| 864 | public static function use_test_manifest() { |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Returns true if we are currently using a temporary database |
||
| 892 | */ |
||
| 893 | public static function using_temp_db() { |
||
| 899 | |||
| 900 | public static function kill_temp_db() { |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Remove all content from the temporary database. |
||
| 921 | */ |
||
| 922 | public static function empty_temp_db() { |
||
| 935 | |||
| 936 | public static function create_temp_db() { |
||
| 962 | |||
| 963 | public static function delete_all_temp_dbs() { |
||
| 977 | |||
| 978 | /** |
||
| 979 | * Reset the testing database's schema. |
||
| 980 | * @param bool $includeExtraDataObjects If true, the extraDataObjects tables will also be included |
||
| 981 | */ |
||
| 982 | public function resetDBSchema($includeExtraDataObjects = false) { |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Create a member and group with the given permission code, and log in with it. |
||
| 1020 | * Returns the member ID. |
||
| 1021 | * |
||
| 1022 | * @param string|array $permCode Either a permission, or list of permissions |
||
| 1023 | * @return int Member ID |
||
| 1024 | */ |
||
| 1025 | public function logInWithPermission($permCode = "ADMIN") { |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Cache for logInWithPermission() |
||
| 1071 | */ |
||
| 1072 | protected $cache_generatedMembers = array(); |
||
| 1073 | |||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * Test against a theme. |
||
| 1077 | * |
||
| 1078 | * @param string $themeBaseDir themes directory |
||
| 1079 | * @param string $theme Theme name |
||
| 1080 | * @param callable $callback |
||
| 1081 | * @throws Exception |
||
| 1082 | */ |
||
| 1083 | protected function useTestTheme($themeBaseDir, $theme, $callback) { |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Get fixture paths for this test |
||
| 1106 | * |
||
| 1107 | * @return array List of paths |
||
| 1108 | */ |
||
| 1109 | protected function getFixturePaths() |
||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Map a fixture path to a physical file |
||
| 1125 | * |
||
| 1126 | * @param string $fixtureFilePath |
||
| 1127 | * @return string |
||
| 1128 | */ |
||
| 1129 | protected function resolveFixturePath($fixtureFilePath) |
||
| 1152 | |||
| 1153 | } |
||
| 1154 | |||
| 1156 |
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.