Skip to content

Commit 5d40f69

Browse files
authored
Merge pull request #7867 from kenjis/fix-feature-test-buffering
fix: FeatureTest may cause risky tests
2 parents 37ebc6f + 1e91936 commit 5d40f69

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

system/CodeIgniter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
468468

469469
// If a ResponseInterface instance is returned then send it back to the client and stop
470470
if ($possibleResponse instanceof ResponseInterface) {
471+
$this->outputBufferingEnd();
472+
471473
return $possibleResponse;
472474
}
473475

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111

1212
namespace Tests\Support\Config\Filters;
1313

14-
$filters->aliases['test-customfilter'] = \Tests\Support\Filters\Customfilter::class;
14+
/**
15+
* @psalm-suppress UndefinedGlobalVariable
16+
*/
17+
$filters->aliases['test-customfilter'] = \Tests\Support\Filters\Customfilter::class;
18+
$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 <[email protected]>
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
@@ -79,6 +79,21 @@ public function testCallGetAndUriString(): void
7979
$this->assertSame('http://example.com/index.php/foo/bar/1/2/3', current_url());
8080
}
8181

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

0 commit comments

Comments
 (0)