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 | * @var CI_Controller CodeIgniter instance |
||
36 | */ |
||
37 | protected $CI; |
||
38 | |||
39 | protected $class_map = [ |
||
40 | 'request' => 'CIPHPUnitTestRequest', |
||
41 | 'double' => 'CIPHPUnitTestDouble', |
||
42 | 'reflection' => 'CIPHPUnitTestReflection', |
||
43 | ]; |
||
44 | |||
45 | public function setCI(CI_Controller $CI) |
||
49 | |||
50 | public function __get($name) |
||
60 | |||
61 | public static function setUpBeforeClass() |
||
73 | |||
74 | /** |
||
75 | * Reset CodeIgniter instance and assign new CodeIgniter instance as $this->CI |
||
76 | */ |
||
77 | public function resetInstance() |
||
83 | |||
84 | protected function tearDown() |
||
118 | |||
119 | /** |
||
120 | * Request to Controller |
||
121 | * |
||
122 | * @param string $http_method HTTP method |
||
123 | * @param array|string $argv array of controller,method,arg|uri |
||
124 | * @param array $params POST parameters/Query string |
||
125 | */ |
||
126 | public function request($http_method, $argv, $params = []) |
||
130 | |||
131 | /** |
||
132 | * Request to Controller using ajax request |
||
133 | * |
||
134 | * @param string $http_method HTTP method |
||
135 | * @param array|string $argv array of controller,method,arg|uri |
||
136 | * @param array $params POST parameters/Query string |
||
137 | */ |
||
138 | public function ajaxRequest($http_method, $argv, $params = []) |
||
143 | |||
144 | /** |
||
145 | * Get Mock Object |
||
146 | * |
||
147 | * $email = $this->getMockBuilder('CI_Email') |
||
148 | * ->setMethods(['send']) |
||
149 | * ->getMock(); |
||
150 | * $email->method('send')->willReturn(TRUE); |
||
151 | * |
||
152 | * will be |
||
153 | * |
||
154 | * $email = $this->getDouble('CI_Email', ['send' => TRUE]); |
||
155 | * |
||
156 | * @param string $classname |
||
157 | * @param array $params [method_name => return_value] |
||
158 | * @param bool $enable_constructor enable constructor or not |
||
159 | * @return object PHPUnit mock object |
||
160 | */ |
||
161 | public function getDouble($classname, $params, $enable_constructor = false) |
||
165 | |||
166 | /** |
||
167 | * Verifies that method was called exactly $times times |
||
168 | * |
||
169 | * $loader->expects($this->exactly(2)) |
||
170 | * ->method('view') |
||
171 | * ->withConsecutive( |
||
172 | * ['shop_confirm', $this->anything(), TRUE], |
||
173 | * ['shop_tmpl_checkout', $this->anything()] |
||
174 | * ); |
||
175 | * |
||
176 | * will be |
||
177 | * |
||
178 | * $this->verifyInvokedMultipleTimes( |
||
179 | * $loader, |
||
180 | * 'view', |
||
181 | * 2, |
||
182 | * [ |
||
183 | * ['shop_confirm', $this->anything(), TRUE], |
||
184 | * ['shop_tmpl_checkout', $this->anything()] |
||
185 | * ] |
||
186 | * ); |
||
187 | * |
||
188 | * @param object $mock PHPUnit mock object |
||
189 | * @param string $method |
||
190 | * @param int $times |
||
191 | * @param array $params arguments |
||
192 | */ |
||
193 | public function verifyInvokedMultipleTimes($mock, $method, $times, $params = null) |
||
199 | |||
200 | /** |
||
201 | * Verifies a method was invoked at least once |
||
202 | * |
||
203 | * @param object $mock PHPUnit mock object |
||
204 | * @param string $method |
||
205 | * @param array $params arguments |
||
206 | */ |
||
207 | public function verifyInvoked($mock, $method, $params = null) |
||
211 | |||
212 | /** |
||
213 | * Verifies that method was invoked only once |
||
214 | * |
||
215 | * @param object $mock PHPUnit mock object |
||
216 | * @param string $method |
||
217 | * @param array $params arguments |
||
218 | */ |
||
219 | public function verifyInvokedOnce($mock, $method, $params = null) |
||
223 | |||
224 | /** |
||
225 | * Verifies that method was not called |
||
226 | * |
||
227 | * @param object $mock PHPUnit mock object |
||
228 | * @param string $method |
||
229 | * @param array $params arguments |
||
230 | */ |
||
231 | public function verifyNeverInvoked($mock, $method, $params = null) |
||
235 | |||
236 | public function warningOff() |
||
242 | |||
243 | public function warningOn() |
||
247 | |||
248 | /** |
||
249 | * Asserts HTTP response code |
||
250 | * |
||
251 | * @param int $code |
||
252 | */ |
||
253 | public function assertResponseCode($code) |
||
264 | |||
265 | /** |
||
266 | * Asserts HTTP response header |
||
267 | * |
||
268 | * @param string $name header name |
||
269 | * @param string $value header value |
||
270 | */ |
||
271 | public function assertResponseHeader($name, $value) |
||
287 | |||
288 | /** |
||
289 | * Asserts HTTP response cookie |
||
290 | * |
||
291 | * @param string $name cookie name |
||
292 | * @param string|array $value cookie value|array of cookie params |
||
293 | * @param bool $allow_duplicate whether to allow duplicated cookies |
||
294 | */ |
||
295 | public function assertResponseCookie($name, $value, $allow_duplicate = false) |
||
349 | |||
350 | /** |
||
351 | * Asserts Redirect |
||
352 | * |
||
353 | * @param string $uri URI to redirect |
||
354 | * @param int $code response code |
||
355 | */ |
||
356 | public function assertRedirect($uri, $code = null) |
||
393 | } |
||
394 |
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.