Complex classes like TestSuite 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 TestSuite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class TestSuite implements Test, SelfDescribing, IteratorAggregate |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Last count of tests in this suite. |
||
| 31 | * |
||
| 32 | * @var int|null |
||
| 33 | */ |
||
| 34 | private $cachedNumTests; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Enable or disable the backup and restoration of the $GLOBALS array. |
||
| 38 | * |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | protected $backupGlobals; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Enable or disable the backup and restoration of static attributes. |
||
| 45 | * |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $backupStaticAttributes; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | private $beStrictAboutChangesToGlobalState; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $runTestInSeparateProcess = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The name of the test suite. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $name = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The test groups of the test suite. |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $groups = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The tests in the test suite. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $tests = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The number of tests in the test suite. |
||
| 83 | * |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | protected $numTests = -1; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $testCase = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $foundClasses = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var Factory |
||
| 100 | */ |
||
| 101 | private $iteratorFilter; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Constructs a new TestSuite: |
||
| 105 | * |
||
| 106 | * - PHPUnit_Framework_TestSuite() constructs an empty TestSuite. |
||
| 107 | * |
||
| 108 | * - PHPUnit_Framework_TestSuite(ReflectionClass) constructs a |
||
| 109 | * TestSuite from the given class. |
||
| 110 | * |
||
| 111 | * - PHPUnit_Framework_TestSuite(ReflectionClass, String) |
||
| 112 | * constructs a TestSuite from the given class with the given |
||
| 113 | * name. |
||
| 114 | * |
||
| 115 | * - PHPUnit_Framework_TestSuite(String) either constructs a |
||
| 116 | * TestSuite from the given class (if the passed string is the |
||
| 117 | * name of an existing class) or constructs an empty TestSuite |
||
| 118 | * with the given name. |
||
| 119 | * |
||
| 120 | * @param mixed $theClass |
||
| 121 | * @param string $name |
||
| 122 | * |
||
| 123 | * @throws Exception |
||
| 124 | */ |
||
| 125 | public function __construct($theClass = '', $name = '') |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns a string representation of the test suite. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function toString() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Adds a test to the suite. |
||
| 210 | * |
||
| 211 | * @param Test $test |
||
| 212 | * @param array $groups |
||
| 213 | */ |
||
| 214 | public function addTest(Test $test, $groups = []) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Adds the tests from the given class to the suite. |
||
| 246 | * |
||
| 247 | * @param mixed $testClass |
||
| 248 | * |
||
| 249 | * @throws Exception |
||
| 250 | */ |
||
| 251 | public function addTestSuite($testClass) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Wraps both <code>addTest()</code> and <code>addTestSuite</code> |
||
| 295 | * as well as the separate import statements for the user's convenience. |
||
| 296 | * |
||
| 297 | * If the named file cannot be read or there are no new tests that can be |
||
| 298 | * added, a <code>PHPUnit_Framework_WarningTestCase</code> will be created instead, |
||
| 299 | * leaving the current test run untouched. |
||
| 300 | * |
||
| 301 | * @param string $filename |
||
| 302 | * |
||
| 303 | * @throws Exception |
||
| 304 | */ |
||
| 305 | public function addTestFile($filename) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Wrapper for addTestFile() that adds multiple test files. |
||
| 378 | * |
||
| 379 | * @param array|Iterator $filenames |
||
| 380 | * |
||
| 381 | * @throws Exception |
||
| 382 | */ |
||
| 383 | public function addTestFiles($filenames) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Counts the number of test cases that will be run by this test. |
||
| 400 | * |
||
| 401 | * @param bool $preferCache Indicates if cache is preferred. |
||
| 402 | * |
||
| 403 | * @return int |
||
| 404 | */ |
||
| 405 | public function count($preferCache = false) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param ReflectionClass $theClass |
||
| 424 | * @param string $name |
||
| 425 | * |
||
| 426 | * @return Test |
||
| 427 | * |
||
| 428 | * @throws Exception |
||
| 429 | */ |
||
| 430 | public static function createTest(ReflectionClass $theClass, $name) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Creates a default TestResult object. |
||
| 605 | * |
||
| 606 | * @return TestResult |
||
| 607 | */ |
||
| 608 | protected function createResult() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Returns the name of the suite. |
||
| 615 | * |
||
| 616 | * @return string |
||
| 617 | */ |
||
| 618 | public function getName() |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Returns the test groups of the suite. |
||
| 625 | * |
||
| 626 | * @return array |
||
| 627 | */ |
||
| 628 | public function getGroups() |
||
| 632 | |||
| 633 | public function getGroupDetails() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Set tests groups of the test case |
||
| 640 | * |
||
| 641 | * @param array $groups |
||
| 642 | */ |
||
| 643 | public function setGroupDetails(array $groups) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Runs the tests and collects their result in a TestResult. |
||
| 650 | * |
||
| 651 | * @param TestResult $result |
||
| 652 | * |
||
| 653 | * @return TestResult |
||
| 654 | */ |
||
| 655 | public function run(TestResult $result = null) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * @param bool $runTestInSeparateProcess |
||
| 751 | * |
||
| 752 | * @throws Exception |
||
| 753 | */ |
||
| 754 | public function setRunTestInSeparateProcess($runTestInSeparateProcess) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Runs a test. |
||
| 765 | * |
||
| 766 | * @deprecated |
||
| 767 | * |
||
| 768 | * @param Test $test |
||
| 769 | * @param TestResult $result |
||
| 770 | */ |
||
| 771 | public function runTest(Test $test, TestResult $result) |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Sets the name of the suite. |
||
| 778 | * |
||
| 779 | * @param string |
||
| 780 | */ |
||
| 781 | public function setName($name) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Returns the test at the given index. |
||
| 788 | * |
||
| 789 | * @param int|false |
||
| 790 | * |
||
| 791 | * @return Test |
||
| 792 | */ |
||
| 793 | public function testAt($index) |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Returns the tests as an enumeration. |
||
| 804 | * |
||
| 805 | * @return array |
||
| 806 | */ |
||
| 807 | public function tests() |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Set tests of the test suite |
||
| 814 | * |
||
| 815 | * @param array $tests |
||
| 816 | */ |
||
| 817 | public function setTests(array $tests) |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Mark the test suite as skipped. |
||
| 824 | * |
||
| 825 | * @param string $message |
||
| 826 | * |
||
| 827 | * @throws SkippedTestSuiteError |
||
| 828 | */ |
||
| 829 | public function markTestSuiteSkipped($message = '') |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @param ReflectionClass $class |
||
| 836 | * @param ReflectionMethod $method |
||
| 837 | */ |
||
| 838 | protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method) |
||
| 873 | |||
| 874 | /** |
||
| 875 | * @param ReflectionMethod $method |
||
| 876 | * |
||
| 877 | * @return bool |
||
| 878 | */ |
||
| 879 | public static function isTestMethod(ReflectionMethod $method) |
||
| 892 | |||
| 893 | /** |
||
| 894 | * @param string $message |
||
| 895 | * |
||
| 896 | * @return WarningTestCase |
||
| 897 | */ |
||
| 898 | protected static function warning($message) |
||
| 902 | |||
| 903 | /** |
||
| 904 | * @param string $class |
||
| 905 | * @param string $methodName |
||
| 906 | * @param string $message |
||
| 907 | * |
||
| 908 | * @return SkippedTestCase |
||
| 909 | */ |
||
| 910 | protected static function skipTest($class, $methodName, $message) |
||
| 914 | |||
| 915 | /** |
||
| 916 | * @param string $class |
||
| 917 | * @param string $methodName |
||
| 918 | * @param string $message |
||
| 919 | * |
||
| 920 | * @return IncompleteTestCase |
||
| 921 | */ |
||
| 922 | protected static function incompleteTest($class, $methodName, $message) |
||
| 926 | |||
| 927 | /** |
||
| 928 | * @param bool $beStrictAboutChangesToGlobalState |
||
| 929 | */ |
||
| 930 | public function setbeStrictAboutChangesToGlobalState($beStrictAboutChangesToGlobalState) |
||
| 936 | |||
| 937 | /** |
||
| 938 | * @param bool $backupGlobals |
||
| 939 | */ |
||
| 940 | public function setBackupGlobals($backupGlobals) |
||
| 946 | |||
| 947 | /** |
||
| 948 | * @param bool $backupStaticAttributes |
||
| 949 | */ |
||
| 950 | public function setBackupStaticAttributes($backupStaticAttributes) |
||
| 956 | |||
| 957 | /** |
||
| 958 | * Returns an iterator for this test suite. |
||
| 959 | * |
||
| 960 | * @return RecursiveIteratorIterator |
||
| 961 | */ |
||
| 962 | public function getIterator() |
||
| 972 | |||
| 973 | public function injectFilter(Factory $filter) |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Template Method that is called before the tests |
||
| 985 | * of this test suite are run. |
||
| 986 | */ |
||
| 987 | protected function setUp() |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Template Method that is called after the tests |
||
| 993 | * of this test suite have finished running. |
||
| 994 | */ |
||
| 995 | protected function tearDown() |
||
| 998 | } |
||
| 999 |