forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterTestTrait.php
More file actions
323 lines (270 loc) · 9.19 KB
/
FilterTestTrait.php
File metadata and controls
323 lines (270 loc) · 9.19 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Test;
use Closure;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\Exceptions\RuntimeException;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\Filters\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Router\RouteCollection;
use Config\Filters as FiltersConfig;
/**
* Filter Test Trait
*
* Provides functionality for testing
* filters and their route associations.
*
* @mixin CIUnitTestCase
*/
trait FilterTestTrait
{
/**
* Have the one-time classes been instantiated?
*
* @var bool
*/
private $doneFilterSetUp = false;
/**
* The active IncomingRequest or CLIRequest
*
* @var RequestInterface
*/
protected $request;
/**
* The active Response instance
*
* @var ResponseInterface
*/
protected $response;
/**
* The Filters configuration to use.
* Extracted for access to aliases
* during Filters::discoverFilters().
*
* @var FiltersConfig|null
*/
protected $filtersConfig;
/**
* The prepared Filters library.
*
* @var Filters|null
*/
protected $filters;
/**
* The default App and discovered
* routes to check for filters.
*
* @var RouteCollection|null
*/
protected $collection;
// --------------------------------------------------------------------
// Staging
// --------------------------------------------------------------------
/**
* Initializes dependencies once.
*/
protected function setUpFilterTestTrait(): void
{
if ($this->doneFilterSetUp === true) {
return;
}
// Create our own Request and Response so we can
// use the same ones for Filters and FilterInterface
// yet isolate them from outside influence
$this->request ??= clone service('request');
$this->response ??= clone service('response');
// Create our config and Filters instance to reuse for performance
$this->filtersConfig ??= config(FiltersConfig::class);
$this->filters ??= new Filters($this->filtersConfig, $this->request, $this->response);
if ($this->collection === null) {
$this->collection = service('routes')->loadRoutes();
}
$this->doneFilterSetUp = true;
}
// --------------------------------------------------------------------
// Utility
// --------------------------------------------------------------------
/**
* Returns a callable method for a filter position
* using the local HTTP instances.
*
* @param FilterInterface|string $filter The filter instance, class, or alias
* @param string $position "before" or "after"
*
* @return Closure(list<string>|null=): mixed
*/
protected function getFilterCaller($filter, string $position): Closure
{
if (! in_array($position, ['before', 'after'], true)) {
throw new InvalidArgumentException('Invalid filter position passed: ' . $position);
}
if ($filter instanceof FilterInterface) {
$filterInstances = [$filter];
}
if (is_string($filter)) {
// Check for an alias (no namespace)
if (! str_contains($filter, '\\')) {
if (! isset($this->filtersConfig->aliases[$filter])) {
throw new RuntimeException("No filter found with alias '{$filter}'");
}
$filterClasses = (array) $this->filtersConfig->aliases[$filter];
} else {
// FQCN
$filterClasses = [$filter];
}
$filterInstances = [];
foreach ($filterClasses as $class) {
// Get an instance
$filter = new $class();
if (! $filter instanceof FilterInterface) {
throw FilterException::forIncorrectInterface($filter::class);
}
$filterInstances[] = $filter;
}
}
$request = clone $this->request;
if ($position === 'before') {
return static function (?array $params = null) use ($filterInstances, $request) {
$result = null;
foreach ($filterInstances as $filter) {
$result = $filter->before($request, $params);
// @TODO The following logic is in Filters class.
// Should use Filters class.
if ($result instanceof RequestInterface) {
$request = $result;
continue;
}
if ($result instanceof ResponseInterface) {
return $result;
}
if (empty($result)) {
continue;
}
}
return $result;
};
}
$response = clone $this->response;
return static function (?array $params = null) use ($filterInstances, $request, $response) {
$result = null;
foreach ($filterInstances as $filter) {
$result = $filter->after($request, $response, $params);
// @TODO The following logic is in Filters class.
// Should use Filters class.
if ($result instanceof ResponseInterface) {
$response = $result;
continue;
}
}
return $result;
};
}
/**
* Gets an array of filter aliases enabled
* for the given route at position.
*
* @param string $route The route to test
* @param string $position "before" or "after"
*
* @return list<string> The filter aliases
*/
protected function getFiltersForRoute(string $route, string $position): array
{
if (! in_array($position, ['before', 'after'], true)) {
throw new InvalidArgumentException('Invalid filter position passed:' . $position);
}
$this->filters->reset();
$routeFilters = $this->collection->getFiltersForRoute($route);
if ($routeFilters !== []) {
$this->filters->enableFilters($routeFilters, $position);
}
$aliases = $this->filters->initialize($route)->getFilters();
$this->filters->reset();
return $aliases[$position];
}
// --------------------------------------------------------------------
// Assertions
// --------------------------------------------------------------------
/**
* Asserts that the given route at position uses
* the filter (by its alias).
*
* @param string $route The route to test
* @param string $position "before" or "after"
* @param string $alias Alias for the anticipated filter
*/
protected function assertFilter(string $route, string $position, string $alias): void
{
$filters = $this->getFiltersForRoute($route, $position);
$this->assertContains(
$alias,
$filters,
"Filter '{$alias}' does not apply to '{$route}'.",
);
}
/**
* Asserts that the given route at position does not
* use the filter (by its alias).
*
* @param string $route The route to test
* @param string $position "before" or "after"
* @param string $alias Alias for the anticipated filter
*
* @return void
*/
protected function assertNotFilter(string $route, string $position, string $alias)
{
$filters = $this->getFiltersForRoute($route, $position);
$this->assertNotContains(
$alias,
$filters,
"Filter '{$alias}' applies to '{$route}' when it should not.",
);
}
/**
* Asserts that the given route at position has
* at least one filter set.
*
* @param string $route The route to test
* @param string $position "before" or "after"
*
* @return void
*/
protected function assertHasFilters(string $route, string $position)
{
$filters = $this->getFiltersForRoute($route, $position);
$this->assertNotEmpty(
$filters,
"No filters found for '{$route}' when at least one was expected.",
);
}
/**
* Asserts that the given route at position has
* no filters set.
*
* @param string $route The route to test
* @param string $position "before" or "after"
*
* @return void
*/
protected function assertNotHasFilters(string $route, string $position)
{
$filters = $this->getFiltersForRoute($route, $position);
$this->assertSame(
[],
$filters,
"Found filters for '{$route}' when none were expected: " . implode(', ', $filters) . '.',
);
}
}