Complex classes like CIPHPUnitTestCase 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 CIPHPUnitTestCase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class CIPHPUnitTestCase extends PHPUnit_Framework_TestCase |
||
|
|
|||
| 17 | { |
||
| 18 | protected $_error_reporting = -1; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * If you have a route with closure, PHPUnit can't serialize global variables. |
||
| 22 | * You would see `Exception: Serialization of 'Closure' is not allowed`. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $backupGlobalsBlacklist = ['RTR']; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var CI_Controller CodeIgniter instance |
||
| 30 | */ |
||
| 31 | protected $CI; |
||
| 32 | |||
| 33 | protected $class_map = [ |
||
| 34 | 'request' => 'CIPHPUnitTestRequest', |
||
| 35 | 'double' => 'CIPHPUnitTestDouble', |
||
| 36 | 'reflection' => 'CIPHPUnitTestReflection', |
||
| 37 | ]; |
||
| 38 | |||
| 39 | public function setCI(CI_Controller $CI) |
||
| 43 | |||
| 44 | public function __get($name) |
||
| 54 | |||
| 55 | public static function setUpBeforeClass() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Reset CodeIgniter instance and assign new CodeIgniter instance as $this->CI |
||
| 70 | */ |
||
| 71 | public function resetInstance() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Create a controller instance |
||
| 80 | * |
||
| 81 | * @param string $classname |
||
| 82 | * @return CI_Controller |
||
| 83 | */ |
||
| 84 | public function newController($classname) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Create a model instance |
||
| 94 | * |
||
| 95 | * @param string $classname |
||
| 96 | * @return CI_Model |
||
| 97 | */ |
||
| 98 | public function newModel($classname) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Create a library instance |
||
| 107 | * |
||
| 108 | * @param string $classname |
||
| 109 | * @return object |
||
| 110 | */ |
||
| 111 | public function newLibrary($classname) |
||
| 119 | |||
| 120 | protected function tearDown() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Request to Controller |
||
| 157 | * |
||
| 158 | * @param string $http_method HTTP method |
||
| 159 | * @param array|string $argv array of controller,method,arg|uri |
||
| 160 | * @param array $params POST parameters/Query string |
||
| 161 | */ |
||
| 162 | public function request($http_method, $argv, $params = []) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Request to Controller using ajax request |
||
| 169 | * |
||
| 170 | * @param string $http_method HTTP method |
||
| 171 | * @param array|string $argv array of controller,method,arg|uri |
||
| 172 | * @param array $params POST parameters/Query string |
||
| 173 | */ |
||
| 174 | public function ajaxRequest($http_method, $argv, $params = []) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get Mock Object |
||
| 182 | * |
||
| 183 | * $email = $this->getMockBuilder('CI_Email') |
||
| 184 | * ->setMethods(['send']) |
||
| 185 | * ->getMock(); |
||
| 186 | * $email->method('send')->willReturn(TRUE); |
||
| 187 | * |
||
| 188 | * will be |
||
| 189 | * |
||
| 190 | * $email = $this->getDouble('CI_Email', ['send' => TRUE]); |
||
| 191 | * |
||
| 192 | * @param string $classname |
||
| 193 | * @param array $params [method_name => return_value] |
||
| 194 | * @param bool $enable_constructor enable constructor or not |
||
| 195 | * @return object PHPUnit mock object |
||
| 196 | */ |
||
| 197 | public function getDouble($classname, $params, $enable_constructor = false) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Verifies that method was called exactly $times times |
||
| 204 | * |
||
| 205 | * $loader->expects($this->exactly(2)) |
||
| 206 | * ->method('view') |
||
| 207 | * ->withConsecutive( |
||
| 208 | * ['shop_confirm', $this->anything(), TRUE], |
||
| 209 | * ['shop_tmpl_checkout', $this->anything()] |
||
| 210 | * ); |
||
| 211 | * |
||
| 212 | * will be |
||
| 213 | * |
||
| 214 | * $this->verifyInvokedMultipleTimes( |
||
| 215 | * $loader, |
||
| 216 | * 'view', |
||
| 217 | * 2, |
||
| 218 | * [ |
||
| 219 | * ['shop_confirm', $this->anything(), TRUE], |
||
| 220 | * ['shop_tmpl_checkout', $this->anything()] |
||
| 221 | * ] |
||
| 222 | * ); |
||
| 223 | * |
||
| 224 | * @param object $mock PHPUnit mock object |
||
| 225 | * @param string $method |
||
| 226 | * @param int $times |
||
| 227 | * @param array $params arguments |
||
| 228 | */ |
||
| 229 | public function verifyInvokedMultipleTimes($mock, $method, $times, $params = null) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Verifies a method was invoked at least once |
||
| 238 | * |
||
| 239 | * @param object $mock PHPUnit mock object |
||
| 240 | * @param string $method |
||
| 241 | * @param array $params arguments |
||
| 242 | */ |
||
| 243 | public function verifyInvoked($mock, $method, $params = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Verifies that method was invoked only once |
||
| 250 | * |
||
| 251 | * @param object $mock PHPUnit mock object |
||
| 252 | * @param string $method |
||
| 253 | * @param array $params arguments |
||
| 254 | */ |
||
| 255 | public function verifyInvokedOnce($mock, $method, $params = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Verifies that method was not called |
||
| 262 | * |
||
| 263 | * @param object $mock PHPUnit mock object |
||
| 264 | * @param string $method |
||
| 265 | * @param array $params arguments |
||
| 266 | */ |
||
| 267 | public function verifyNeverInvoked($mock, $method, $params = null) |
||
| 271 | |||
| 272 | public function warningOff() |
||
| 278 | |||
| 279 | public function warningOn() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Asserts HTTP response code |
||
| 286 | * |
||
| 287 | * @param int $code |
||
| 288 | */ |
||
| 289 | public function assertResponseCode($code) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Asserts HTTP response header |
||
| 303 | * |
||
| 304 | * @param string $name header name |
||
| 305 | * @param string $value header value |
||
| 306 | */ |
||
| 307 | public function assertResponseHeader($name, $value) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Asserts HTTP response cookie |
||
| 326 | * |
||
| 327 | * @param string $name cookie name |
||
| 328 | * @param string|array $value cookie value|array of cookie params |
||
| 329 | * @param bool $allow_duplicate whether to allow duplicated cookies |
||
| 330 | */ |
||
| 331 | public function assertResponseCookie($name, $value, $allow_duplicate = false) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Asserts Redirect |
||
| 378 | * |
||
| 379 | * @param string $uri URI to redirect |
||
| 380 | * @param int $code response code |
||
| 381 | */ |
||
| 382 | public function assertRedirect($uri, $code = null) |
||
| 419 | } |
||
| 420 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.