Skip to content

Commit 9dd7cb0

Browse files
committed
test: add test when filter returns response
1 parent 98239ad commit 9dd7cb0

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

system/Test/FeatureTestTrait.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ protected function withRoutes(?array $routes = null)
5151
$collection->resetRoutes();
5252

5353
foreach ($routes as $route) {
54-
$collection->{$route[0]}($route[1], $route[2]);
54+
if (isset($route[3])) {
55+
$collection->{$route[0]}($route[1], $route[2], $route[3]);
56+
} else {
57+
$collection->{$route[0]}($route[1], $route[2]);
58+
}
5559
}
5660
}
5761

tests/_support/Config/Filters.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111

1212
namespace Tests\Support\Config\Filters;
1313

14-
$filters->aliases['test-customfilter'] = \Tests\Support\Filters\Customfilter::class;
14+
$filters->aliases['test-customfilter'] = \Tests\Support\Filters\Customfilter::class;
15+
$filters->aliases['test-redirectfilter'] = \Tests\Support\Filters\RedirectFilter::class;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Filters;
13+
14+
use CodeIgniter\HTTP\RequestInterface;
15+
use CodeIgniter\HTTP\ResponseInterface;
16+
17+
class RedirectFilter implements \CodeIgniter\Filters\FilterInterface
18+
{
19+
public function before(RequestInterface $request, $arguments = null)
20+
{
21+
return redirect()->to('login');
22+
}
23+
24+
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
25+
{
26+
}
27+
}

tests/system/Test/FeatureTestTraitTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ public function testCallGetAndUriString(): void
7777
$this->assertSame('http://example.com/index.php/foo/bar/1/2/3', current_url());
7878
}
7979

80+
public function testCallGetAndFilterReturnsResponse(): void
81+
{
82+
$this->withRoutes([
83+
[
84+
'get',
85+
'admin',
86+
static fn () => 'Admin Area',
87+
['filter' => 'test-redirectfilter'],
88+
],
89+
]);
90+
$response = $this->get('admin');
91+
92+
$response->assertRedirectTo('login');
93+
}
94+
8095
public function testClosureWithEcho()
8196
{
8297
$this->withRoutes([

0 commit comments

Comments
 (0)