-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
FeatureTestTrait.php
431 lines (369 loc) · 11.3 KB
/
FeatureTestTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Test;
use CodeIgniter\Events\Events;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\URI;
use Config\App;
use Config\Services;
use Exception;
use ReflectionException;
/**
* Trait FeatureTestTrait
*
* Provides additional utilities for doing full HTTP testing
* against your application in trait format.
*/
trait FeatureTestTrait
{
/**
* Sets a RouteCollection that will override
* the application's route collection.
*
* Example routes:
* [
* ['GET', 'home', 'Home::index'],
* ]
*
* @param array|null $routes Array to set routes
*
* @return $this
*/
protected function withRoutes(?array $routes = null)
{
$collection = service('routes');
if ($routes !== null) {
$collection->resetRoutes();
foreach ($routes as $route) {
if ($route[0] === strtolower($route[0])) {
@trigger_error(
'Passing lowercase HTTP method "' . $route[0] . '" is deprecated.'
. ' Use uppercase HTTP method like "' . strtoupper($route[0]) . '".',
E_USER_DEPRECATED
);
}
/**
* @TODO For backward compatibility. Remove strtolower() in the future.
* @deprecated 4.5.0
*/
$method = strtolower($route[0]);
if (isset($route[3])) {
$collection->{$method}($route[1], $route[2], $route[3]);
} else {
$collection->{$method}($route[1], $route[2]);
}
}
}
$this->routes = $collection;
return $this;
}
/**
* Sets any values that should exist during this session.
*
* @param array|null $values Array of values, or null to use the current $_SESSION
*
* @return $this
*/
public function withSession(?array $values = null)
{
$this->session = $values ?? $_SESSION;
return $this;
}
/**
* Set request's headers
*
* Example of use
* withHeaders([
* 'Authorization' => 'Token'
* ])
*
* @param array $headers Array of headers
*
* @return $this
*/
public function withHeaders(array $headers = [])
{
$this->headers = $headers;
return $this;
}
/**
* Set the format the request's body should have.
*
* @param string $format The desired format. Currently supported formats: xml, json
*
* @return $this
*/
public function withBodyFormat(string $format)
{
$this->bodyFormat = $format;
return $this;
}
/**
* Set the raw body for the request
*
* @param string $body
*
* @return $this
*/
public function withBody($body)
{
$this->requestBody = $body;
return $this;
}
/**
* Don't run any events while running this test.
*
* @return $this
*/
public function skipEvents()
{
Events::simulate(true);
return $this;
}
/**
* Calls a single URI, executes it, and returns a TestResponse
* instance that can be used to run many assertions against.
*
* @param string $method HTTP verb
*
* @return TestResponse
*/
public function call(string $method, string $path, ?array $params = null)
{
if ($method === strtolower($method)) {
@trigger_error(
'Passing lowercase HTTP method "' . $method . '" is deprecated.'
. ' Use uppercase HTTP method like "' . strtoupper($method) . '".',
E_USER_DEPRECATED
);
}
/**
* @deprecated 4.5.0
* @TODO remove this in the future.
*/
$method = strtoupper($method);
// Simulate having a blank session
$_SESSION = [];
service('superglobals')->setServer('REQUEST_METHOD', $method);
$request = $this->setupRequest($method, $path);
$request = $this->setupHeaders($request);
$name = strtolower($method);
$request = $this->populateGlobals($name, $request, $params);
$request = $this->setRequestBody($request, $params);
// Initialize the RouteCollection
$routes = $this->routes;
if ($routes !== []) {
$routes = service('routes')->loadRoutes();
}
$routes->setHTTPVerb($method);
// Make sure any other classes that might call the request
// instance get the right one.
Services::injectMock('request', $request);
// Make sure filters are reset between tests
Services::injectMock('filters', service('filters', null, false));
// Make sure validation is reset between tests
Services::injectMock('validation', service('validation', null, false));
$response = $this->app
->setContext('web')
->setRequest($request)
->run($routes, true);
// Reset directory if it has been set
service('router')->setDirectory(null);
return new TestResponse($response);
}
/**
* Performs a GET request.
*
* @param string $path URI path relative to baseURL. May include query.
*
* @return TestResponse
*
* @throws RedirectException
* @throws Exception
*/
public function get(string $path, ?array $params = null)
{
return $this->call(Method::GET, $path, $params);
}
/**
* Performs a POST request.
*
* @return TestResponse
*
* @throws RedirectException
* @throws Exception
*/
public function post(string $path, ?array $params = null)
{
return $this->call(Method::POST, $path, $params);
}
/**
* Performs a PUT request
*
* @return TestResponse
*
* @throws RedirectException
* @throws Exception
*/
public function put(string $path, ?array $params = null)
{
return $this->call(Method::PUT, $path, $params);
}
/**
* Performss a PATCH request
*
* @return TestResponse
*
* @throws RedirectException
* @throws Exception
*/
public function patch(string $path, ?array $params = null)
{
return $this->call(Method::PATCH, $path, $params);
}
/**
* Performs a DELETE request.
*
* @return TestResponse
*
* @throws RedirectException
* @throws Exception
*/
public function delete(string $path, ?array $params = null)
{
return $this->call(Method::DELETE, $path, $params);
}
/**
* Performs an OPTIONS request.
*
* @return TestResponse
*
* @throws RedirectException
* @throws Exception
*/
public function options(string $path, ?array $params = null)
{
return $this->call(Method::OPTIONS, $path, $params);
}
/**
* Setup a Request object to use so that CodeIgniter
* won't try to auto-populate some of the items.
*
* @param string $method HTTP verb
*/
protected function setupRequest(string $method, ?string $path = null): IncomingRequest
{
$config = config(App::class);
$uri = new SiteURI($config);
// $path may have a query in it
$path = URI::removeDotSegments($path);
$parts = explode('?', $path);
$path = $parts[0];
$query = $parts[1] ?? '';
$superglobals = service('superglobals');
$superglobals->setServer('QUERY_STRING', $query);
$uri->setPath($path);
$uri->setQuery($query);
Services::injectMock('uri', $uri);
$request = service('incomingrequest', $config, false);
$request->setMethod($method);
$request->setProtocolVersion('1.1');
if ($config->forceGlobalSecureRequests) {
$_SERVER['HTTPS'] = 'test';
$server = $request->getServer();
$server['HTTPS'] = 'test';
$request->setGlobal('server', $server);
}
return $request;
}
/**
* Setup the custom request's headers
*
* @return IncomingRequest
*/
protected function setupHeaders(IncomingRequest $request)
{
if (! empty($this->headers)) {
foreach ($this->headers as $name => $value) {
$request->setHeader($name, $value);
}
}
return $request;
}
/**
* Populates the data of our Request with "global" data
* relevant to the request, like $_POST data.
*
* Always populate the GET vars based on the URI.
*
* @param string $name Superglobal name (lowercase)
* @param non-empty-array|null $params
*
* @return Request
*
* @throws ReflectionException
*/
protected function populateGlobals(string $name, Request $request, ?array $params = null)
{
// $params should set the query vars if present,
// otherwise set it from the URL.
$get = ($params !== null && $params !== [] && $name === 'get')
? $params
: $this->getPrivateProperty($request->getUri(), 'query');
$request->setGlobal('get', $get);
if ($name === 'get') {
$request->setGlobal('request', $request->fetchGlobal('get'));
}
if ($name === 'post') {
$request->setGlobal($name, $params);
$request->setGlobal(
'request',
$request->fetchGlobal('post') + $request->fetchGlobal('get')
);
}
$_SESSION = $this->session ?? [];
return $request;
}
/**
* Set the request's body formatted according to the value in $this->bodyFormat.
* This allows the body to be formatted in a way that the controller is going to
* expect as in the case of testing a JSON or XML API.
*
* @param array|null $params The parameters to be formatted and put in the body.
*/
protected function setRequestBody(Request $request, ?array $params = null): Request
{
if ($this->requestBody !== '') {
$request->setBody($this->requestBody);
}
if ($this->bodyFormat !== '') {
$formatMime = '';
if ($this->bodyFormat === 'json') {
$formatMime = 'application/json';
} elseif ($this->bodyFormat === 'xml') {
$formatMime = 'application/xml';
}
if ($formatMime !== '') {
$request->setHeader('Content-Type', $formatMime);
}
if ($params !== null && $formatMime !== '') {
$formatted = service('format')->getFormatter($formatMime)->format($params);
// "withBodyFormat() and $params of call()" has higher priority than withBody().
$request->setBody($formatted);
}
}
return $request;
}
}