From 9dd7cb040a46ef522ac53989213f6c4656396dde Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 27 Aug 2023 09:31:47 +0900 Subject: [PATCH 1/3] test: add test when filter returns response --- system/Test/FeatureTestTrait.php | 6 ++++- tests/_support/Config/Filters.php | 3 ++- tests/_support/Filters/RedirectFilter.php | 27 ++++++++++++++++++++++ tests/system/Test/FeatureTestTraitTest.php | 15 ++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/_support/Filters/RedirectFilter.php diff --git a/system/Test/FeatureTestTrait.php b/system/Test/FeatureTestTrait.php index b117a1d890ae..c59c801d4f4a 100644 --- a/system/Test/FeatureTestTrait.php +++ b/system/Test/FeatureTestTrait.php @@ -51,7 +51,11 @@ protected function withRoutes(?array $routes = null) $collection->resetRoutes(); foreach ($routes as $route) { - $collection->{$route[0]}($route[1], $route[2]); + if (isset($route[3])) { + $collection->{$route[0]}($route[1], $route[2], $route[3]); + } else { + $collection->{$route[0]}($route[1], $route[2]); + } } } diff --git a/tests/_support/Config/Filters.php b/tests/_support/Config/Filters.php index 48936397449e..dda1549a7154 100644 --- a/tests/_support/Config/Filters.php +++ b/tests/_support/Config/Filters.php @@ -11,4 +11,5 @@ namespace Tests\Support\Config\Filters; -$filters->aliases['test-customfilter'] = \Tests\Support\Filters\Customfilter::class; +$filters->aliases['test-customfilter'] = \Tests\Support\Filters\Customfilter::class; +$filters->aliases['test-redirectfilter'] = \Tests\Support\Filters\RedirectFilter::class; diff --git a/tests/_support/Filters/RedirectFilter.php b/tests/_support/Filters/RedirectFilter.php new file mode 100644 index 000000000000..b04b0ea18706 --- /dev/null +++ b/tests/_support/Filters/RedirectFilter.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace Tests\Support\Filters; + +use CodeIgniter\HTTP\RequestInterface; +use CodeIgniter\HTTP\ResponseInterface; + +class RedirectFilter implements \CodeIgniter\Filters\FilterInterface +{ + public function before(RequestInterface $request, $arguments = null) + { + return redirect()->to('login'); + } + + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void + { + } +} diff --git a/tests/system/Test/FeatureTestTraitTest.php b/tests/system/Test/FeatureTestTraitTest.php index 0f5bb7ce33d5..c1a341c0aa2b 100644 --- a/tests/system/Test/FeatureTestTraitTest.php +++ b/tests/system/Test/FeatureTestTraitTest.php @@ -77,6 +77,21 @@ public function testCallGetAndUriString(): void $this->assertSame('http://example.com/index.php/foo/bar/1/2/3', current_url()); } + public function testCallGetAndFilterReturnsResponse(): void + { + $this->withRoutes([ + [ + 'get', + 'admin', + static fn () => 'Admin Area', + ['filter' => 'test-redirectfilter'], + ], + ]); + $response = $this->get('admin'); + + $response->assertRedirectTo('login'); + } + public function testClosureWithEcho() { $this->withRoutes([ From e28b9d79c6f7b7804cef54a6c6aea8b10f9cf3ea Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 27 Aug 2023 09:34:31 +0900 Subject: [PATCH 2/3] fix: add missing outputBufferingEnd() --- system/CodeIgniter.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 8308dab116cb..e991af8de121 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -468,6 +468,8 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache // If a ResponseInterface instance is returned then send it back to the client and stop if ($possibleResponse instanceof ResponseInterface) { + $this->outputBufferingEnd(); + return $possibleResponse; } From 1e91936c00dd73d5b260ce6718124dc2230e9ce8 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 27 Aug 2023 09:50:03 +0900 Subject: [PATCH 3/3] docs: suppress paslm error --- tests/_support/Config/Filters.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/_support/Config/Filters.php b/tests/_support/Config/Filters.php index dda1549a7154..5cb70bef1676 100644 --- a/tests/_support/Config/Filters.php +++ b/tests/_support/Config/Filters.php @@ -11,5 +11,8 @@ namespace Tests\Support\Config\Filters; +/** + * @psalm-suppress UndefinedGlobalVariable + */ $filters->aliases['test-customfilter'] = \Tests\Support\Filters\Customfilter::class; $filters->aliases['test-redirectfilter'] = \Tests\Support\Filters\RedirectFilter::class;