Skip to content

Commit

Permalink
Merge pull request #7867 from kenjis/fix-feature-test-buffering
Browse files Browse the repository at this point in the history
fix: FeatureTest may cause risky tests
  • Loading branch information
kenjis authored Sep 4, 2023
2 parents 37ebc6f + 1e91936 commit 5d40f69
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 5 additions & 1 deletion system/Test/FeatureTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/_support/Config/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
27 changes: 27 additions & 0 deletions tests/_support/Filters/RedirectFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* 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 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
{
}
}
15 changes: 15 additions & 0 deletions tests/system/Test/FeatureTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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([
Expand Down

0 comments on commit 5d40f69

Please sign in to comment.