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; } 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..5cb70bef1676 100644 --- a/tests/_support/Config/Filters.php +++ b/tests/_support/Config/Filters.php @@ -11,4 +11,8 @@ namespace Tests\Support\Config\Filters; -$filters->aliases['test-customfilter'] = \Tests\Support\Filters\Customfilter::class; +/** + * @psalm-suppress UndefinedGlobalVariable + */ +$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([