Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions system/Test/FeatureTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Test\Mock\MockFilters;
use Config\App;
use Config\Services;
use Exception;
Expand Down Expand Up @@ -147,8 +148,12 @@ public function call(string $method, string $path, array $params = null)
// instance get the right one.
Services::injectMock('request', $request);

// Make sure filters are reset between tests
Services::injectMock('filters', Services::filters(null, false));
// Make sure filters do not output during testing
Services::injectMock('filters', new MockFilters(
config('Filters'),
Services::request(),
Services::response()
));

$response = $this->app
->setRequest($request)
Expand Down
45 changes: 45 additions & 0 deletions system/Test/Mock/MockFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the 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 CodeIgniter\Test\Mock;

use CodeIgniter\Filters\Filters;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

/**
* Mock Filters handler to prevent output during
* unit testing (especially Feature Tests).
*/
class MockFilters extends Filters
{
/**
* Runs through all of the filters for the specified
* uri and position.
*
* @param string $uri
* @param string $position
*
* @return RequestInterface|ResponseInterface|mixed
*/
public function run(string $uri, string $position = 'before')
{
$result = parent::run($uri, $position);

if ($result instanceof ResponseInterface && ! $result instanceof RedirectResponse)
{
\ob_start();
}

return $result;
}
}