From 54a95395b39d4baa5d3a7784e337eaa9400f7d7d Mon Sep 17 00:00:00 2001 From: MGatner Date: Fri, 6 Nov 2020 20:03:59 +0000 Subject: [PATCH 1/2] Add MockFilters for feature testing --- system/Test/FeatureTestTrait.php | 9 +++++-- system/Test/Mock/MockFilters.php | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 system/Test/Mock/MockFilters.php diff --git a/system/Test/FeatureTestTrait.php b/system/Test/FeatureTestTrait.php index 92d2d1159ce4..71bfffcb03f3 100644 --- a/system/Test/FeatureTestTrait.php +++ b/system/Test/FeatureTestTrait.php @@ -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; @@ -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) diff --git a/system/Test/Mock/MockFilters.php b/system/Test/Mock/MockFilters.php new file mode 100644 index 000000000000..c05749c7d6d0 --- /dev/null +++ b/system/Test/Mock/MockFilters.php @@ -0,0 +1,45 @@ + + * + * 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\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 + * @throws FilterException + */ + public function run(string $uri, string $position = 'before') + { + $result = parent::run($uri, $position); + + if ($result instanceof ResponseInterface && ! $result instanceof RedirectResponse) + { + \ob_start(); + } + + return $result; + } +} From ff65da7678b2276f4834bd1f438941784344fa87 Mon Sep 17 00:00:00 2001 From: MGatner Date: Fri, 6 Nov 2020 20:13:23 +0000 Subject: [PATCH 2/2] Fix docblock types --- system/Test/Mock/MockFilters.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Test/Mock/MockFilters.php b/system/Test/Mock/MockFilters.php index c05749c7d6d0..f96f957bba92 100644 --- a/system/Test/Mock/MockFilters.php +++ b/system/Test/Mock/MockFilters.php @@ -13,6 +13,7 @@ use CodeIgniter\Filters\Filters; use CodeIgniter\HTTP\RedirectResponse; +use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; /** @@ -29,7 +30,6 @@ class MockFilters extends Filters * @param string $position * * @return RequestInterface|ResponseInterface|mixed - * @throws FilterException */ public function run(string $uri, string $position = 'before') {