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 |
||
22 | class CIPHPUnitTestCase extends PHPUnit_Framework_TestCase |
||
23 | { |
||
24 | protected $_error_reporting = -1; |
||
25 | |||
26 | /** |
||
27 | * If you have a route with closure, PHPUnit can't serialize global variables. |
||
28 | * You would see `Exception: Serialization of 'Closure' is not allowed`. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $backupGlobalsBlacklist = ['RTR']; |
||
33 | |||
34 | /** |
||
35 | * Detect warnings and notices in a request output |
||
36 | * |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $strictRequestErrorCheck = true; |
||
40 | |||
41 | protected $restoreErrorHandler = false; |
||
42 | |||
43 | /** |
||
44 | * @var CI_Controller CodeIgniter instance |
||
45 | */ |
||
46 | protected $CI; |
||
47 | |||
48 | protected $class_map = [ |
||
49 | 'request' => 'CIPHPUnitTestRequest', |
||
50 | 'double' => 'CIPHPUnitTestDouble', |
||
51 | 'reflection' => 'CIPHPUnitTestReflection', |
||
52 | ]; |
||
53 | |||
54 | public function setCI(CI_Controller $CI) |
||
58 | |||
59 | public function __get($name) |
||
69 | |||
70 | public static function setUpBeforeClass() |
||
82 | |||
83 | /** |
||
84 | * Reset CodeIgniter instance and assign new CodeIgniter instance as $this->CI |
||
85 | */ |
||
86 | public function resetInstance() |
||
92 | |||
93 | protected function tearDown() |
||
129 | |||
130 | /** |
||
131 | * Request to Controller |
||
132 | * |
||
133 | * @param string $http_method HTTP method |
||
134 | * @param array|string $argv array of controller,method,arg|uri |
||
135 | * @param array $params POST parameters/Query string |
||
136 | */ |
||
137 | public function request($http_method, $argv, $params = []) |
||
145 | |||
146 | /** |
||
147 | * Disable strict error check |
||
148 | */ |
||
149 | public function disableStrictErrorCheck() |
||
156 | |||
157 | /** |
||
158 | * Enable strict error check |
||
159 | */ |
||
160 | public function enableStrictErrorCheck() |
||
174 | |||
175 | /** |
||
176 | * Request to Controller using ajax request |
||
177 | * |
||
178 | * @param string $http_method HTTP method |
||
179 | * @param array|string $argv array of controller,method,arg|uri |
||
180 | * @param array $params POST parameters/Query string |
||
181 | */ |
||
182 | public function ajaxRequest($http_method, $argv, $params = []) |
||
187 | |||
188 | /** |
||
189 | * Get Mock Object |
||
190 | * |
||
191 | * $email = $this->getMockBuilder('CI_Email') |
||
192 | * ->setMethods(['send']) |
||
193 | * ->getMock(); |
||
194 | * $email->method('send')->willReturn(TRUE); |
||
195 | * |
||
196 | * will be |
||
197 | * |
||
198 | * $email = $this->getDouble('CI_Email', ['send' => TRUE]); |
||
199 | * |
||
200 | * @param string $classname |
||
201 | * @param array $params [method_name => return_value] |
||
202 | * @param bool $enable_constructor enable constructor or not |
||
203 | * @return object PHPUnit mock object |
||
204 | */ |
||
205 | public function getDouble($classname, $params, $enable_constructor = false) |
||
209 | |||
210 | /** |
||
211 | * Verifies that method was called exactly $times times |
||
212 | * |
||
213 | * $loader->expects($this->exactly(2)) |
||
214 | * ->method('view') |
||
215 | * ->withConsecutive( |
||
216 | * ['shop_confirm', $this->anything(), TRUE], |
||
217 | * ['shop_tmpl_checkout', $this->anything()] |
||
218 | * ); |
||
219 | * |
||
220 | * will be |
||
221 | * |
||
222 | * $this->verifyInvokedMultipleTimes( |
||
223 | * $loader, |
||
224 | * 'view', |
||
225 | * 2, |
||
226 | * [ |
||
227 | * ['shop_confirm', $this->anything(), TRUE], |
||
228 | * ['shop_tmpl_checkout', $this->anything()] |
||
229 | * ] |
||
230 | * ); |
||
231 | * |
||
232 | * @param object $mock PHPUnit mock object |
||
233 | * @param string $method |
||
234 | * @param int $times |
||
235 | * @param array $params arguments |
||
236 | */ |
||
237 | public function verifyInvokedMultipleTimes($mock, $method, $times, $params = null) |
||
243 | |||
244 | /** |
||
245 | * Verifies a method was invoked at least once |
||
246 | * |
||
247 | * @param object $mock PHPUnit mock object |
||
248 | * @param string $method |
||
249 | * @param array $params arguments |
||
250 | */ |
||
251 | public function verifyInvoked($mock, $method, $params = null) |
||
255 | |||
256 | /** |
||
257 | * Verifies that method was invoked only once |
||
258 | * |
||
259 | * @param object $mock PHPUnit mock object |
||
260 | * @param string $method |
||
261 | * @param array $params arguments |
||
262 | */ |
||
263 | public function verifyInvokedOnce($mock, $method, $params = null) |
||
267 | |||
268 | /** |
||
269 | * Verifies that method was not called |
||
270 | * |
||
271 | * @param object $mock PHPUnit mock object |
||
272 | * @param string $method |
||
273 | * @param array $params arguments |
||
274 | */ |
||
275 | public function verifyNeverInvoked($mock, $method, $params = null) |
||
279 | |||
280 | public function warningOff() |
||
286 | |||
287 | public function warningOn() |
||
291 | |||
292 | /** |
||
293 | * Asserts HTTP response code |
||
294 | * |
||
295 | * @param int $code |
||
296 | */ |
||
297 | public function assertResponseCode($code) |
||
308 | |||
309 | /** |
||
310 | * Asserts HTTP response header |
||
311 | * |
||
312 | * @param string $name header name |
||
313 | * @param string $value header value |
||
314 | */ |
||
315 | public function assertResponseHeader($name, $value) |
||
331 | |||
332 | /** |
||
333 | * Asserts HTTP response cookie |
||
334 | * |
||
335 | * @param string $name cookie name |
||
336 | * @param string|array $value cookie value|array of cookie params |
||
337 | * @param bool $allow_duplicate whether to allow duplicated cookies |
||
338 | */ |
||
339 | public function assertResponseCookie($name, $value, $allow_duplicate = false) |
||
393 | |||
394 | /** |
||
395 | * Asserts Redirect |
||
396 | * |
||
397 | * @param string $uri URI to redirect |
||
398 | * @param int $code response code |
||
399 | */ |
||
400 | public function assertRedirect($uri, $code = null) |
||
437 | } |
||
438 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.