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 getStrictRequestErrorCheck() |
||
63 | |||
64 | public function __get($name) |
||
74 | |||
75 | public static function setUpBeforeClass() |
||
87 | |||
88 | public static function tearDownAfterClass() |
||
92 | |||
93 | /** |
||
94 | * Reset CodeIgniter instance and assign new CodeIgniter instance as $this->CI |
||
95 | */ |
||
96 | public function resetInstance() |
||
102 | |||
103 | protected function tearDown() |
||
139 | |||
140 | /** |
||
141 | * Request to Controller |
||
142 | * |
||
143 | * @param string $http_method HTTP method |
||
144 | * @param array|string $argv array of controller,method,arg|uri |
||
145 | * @param array $params POST parameters/Query string |
||
146 | */ |
||
147 | public function request($http_method, $argv, $params = []) |
||
151 | |||
152 | /** |
||
153 | * Disable strict error check |
||
154 | */ |
||
155 | public function disableStrictErrorCheck() |
||
162 | |||
163 | /** |
||
164 | * Enable strict error check |
||
165 | */ |
||
166 | public function enableStrictErrorCheck() |
||
180 | |||
181 | /** |
||
182 | * Request to Controller using ajax request |
||
183 | * |
||
184 | * @param string $http_method HTTP method |
||
185 | * @param array|string $argv array of controller,method,arg|uri |
||
186 | * @param array $params POST parameters/Query string |
||
187 | */ |
||
188 | public function ajaxRequest($http_method, $argv, $params = []) |
||
193 | |||
194 | /** |
||
195 | * Get Mock Object |
||
196 | * |
||
197 | * $email = $this->getMockBuilder('CI_Email') |
||
198 | * ->setMethods(['send']) |
||
199 | * ->getMock(); |
||
200 | * $email->method('send')->willReturn(TRUE); |
||
201 | * |
||
202 | * will be |
||
203 | * |
||
204 | * $email = $this->getDouble('CI_Email', ['send' => TRUE]); |
||
205 | * |
||
206 | * @param string $classname |
||
207 | * @param array $params [method_name => return_value] |
||
208 | * @param bool $enable_constructor enable constructor or not |
||
209 | * @return object PHPUnit mock object |
||
210 | */ |
||
211 | public function getDouble($classname, $params, $enable_constructor = false) |
||
215 | |||
216 | /** |
||
217 | * Verifies that method was called exactly $times times |
||
218 | * |
||
219 | * $loader->expects($this->exactly(2)) |
||
220 | * ->method('view') |
||
221 | * ->withConsecutive( |
||
222 | * ['shop_confirm', $this->anything(), TRUE], |
||
223 | * ['shop_tmpl_checkout', $this->anything()] |
||
224 | * ); |
||
225 | * |
||
226 | * will be |
||
227 | * |
||
228 | * $this->verifyInvokedMultipleTimes( |
||
229 | * $loader, |
||
230 | * 'view', |
||
231 | * 2, |
||
232 | * [ |
||
233 | * ['shop_confirm', $this->anything(), TRUE], |
||
234 | * ['shop_tmpl_checkout', $this->anything()] |
||
235 | * ] |
||
236 | * ); |
||
237 | * |
||
238 | * @param object $mock PHPUnit mock object |
||
239 | * @param string $method |
||
240 | * @param int $times |
||
241 | * @param array $params arguments |
||
242 | */ |
||
243 | public function verifyInvokedMultipleTimes($mock, $method, $times, $params = null) |
||
249 | |||
250 | /** |
||
251 | * Verifies a method was invoked at least once |
||
252 | * |
||
253 | * @param object $mock PHPUnit mock object |
||
254 | * @param string $method |
||
255 | * @param array $params arguments |
||
256 | */ |
||
257 | public function verifyInvoked($mock, $method, $params = null) |
||
261 | |||
262 | /** |
||
263 | * Verifies that method was invoked only once |
||
264 | * |
||
265 | * @param object $mock PHPUnit mock object |
||
266 | * @param string $method |
||
267 | * @param array $params arguments |
||
268 | */ |
||
269 | public function verifyInvokedOnce($mock, $method, $params = null) |
||
273 | |||
274 | /** |
||
275 | * Verifies that method was not called |
||
276 | * |
||
277 | * @param object $mock PHPUnit mock object |
||
278 | * @param string $method |
||
279 | * @param array $params arguments |
||
280 | */ |
||
281 | public function verifyNeverInvoked($mock, $method, $params = null) |
||
285 | |||
286 | public function warningOff() |
||
292 | |||
293 | public function warningOn() |
||
297 | |||
298 | /** |
||
299 | * Asserts HTTP response code |
||
300 | * |
||
301 | * @param int $code |
||
302 | */ |
||
303 | public function assertResponseCode($code) |
||
314 | |||
315 | /** |
||
316 | * Asserts HTTP response header |
||
317 | * |
||
318 | * @param string $name header name |
||
319 | * @param string $value header value |
||
320 | */ |
||
321 | public function assertResponseHeader($name, $value) |
||
337 | |||
338 | /** |
||
339 | * Asserts HTTP response cookie |
||
340 | * |
||
341 | * @param string $name cookie name |
||
342 | * @param string|array $value cookie value|array of cookie params |
||
343 | * @param bool $allow_duplicate whether to allow duplicated cookies |
||
344 | */ |
||
345 | public function assertResponseCookie($name, $value, $allow_duplicate = false) |
||
399 | |||
400 | /** |
||
401 | * Asserts Redirect |
||
402 | * |
||
403 | * @param string $uri URI to redirect |
||
404 | * @param int $code response code |
||
405 | */ |
||
406 | public function assertRedirect($uri, $code = null) |
||
443 | } |
||
444 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.