From 7cc02e824669c8adaaa7225f63e84623f861b3d7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 23 Feb 2023 15:43:08 +0900 Subject: [PATCH 1/6] docs: add PHPDoc --- system/CodeIgniter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index d370e34ca22e..85e43e4c2a1b 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -122,7 +122,7 @@ class CodeIgniter /** * Cache expiration time * - * @var int + * @var int seconds */ protected static $cacheTTL = 0; From 2e80f8364c26310e105f31d1fb0add9656e52bcd Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 23 Feb 2023 15:44:20 +0900 Subject: [PATCH 2/6] refactor: use config() instead of new keyword --- system/CodeIgniter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 85e43e4c2a1b..544fcb2b813e 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -351,7 +351,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon // Check for a cached page. Execution will stop // if the page has been cached. - $cacheConfig = new Cache(); + $cacheConfig = config(Cache::class); $response = $this->displayCache($cacheConfig); if ($response instanceof ResponseInterface) { if ($returnResponse) { From a17a2a09c8a1fd835c87848fa065b9577e716e7c Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 23 Feb 2023 15:45:31 +0900 Subject: [PATCH 3/6] test: change to something more realistic A CodeIgniter instance handle one request. Use page caching in the run() method, instead of forcing to call cachePage(). Do not register invalid routes with query string. --- tests/system/CodeIgniterTest.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index 4c6ec5ce8689..f5c1b3e8cfc5 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -761,7 +761,7 @@ public function testPageCacheWithCacheQueryString( CITestStreamFilter::addErrorFilter(); // Create cache config with cacheQueryString value from the dataProvider - $cacheConfig = new Cache(); + $cacheConfig = config(Cache::class); $cacheConfig->cacheQueryString = $cacheQueryStringValue; // Clear cache before starting the test @@ -773,15 +773,16 @@ public function testPageCacheWithCacheQueryString( // Generate request to each URL from the testing array foreach ($testingUrls as $testingUrl) { + $this->resetServices(); $_SERVER['REQUEST_URI'] = '/' . $testingUrl; - $routes = Services::routes(true); - $routes->add($testingUrl, static function () { - // Don't cache the page in the run() function because CodeIgniter - // class will create default $cacheConfig and overwrite settings - // from the dataProvider - CodeIgniter::cache(0); + $this->codeigniter = new MockCodeIgniter(new App()); + + $routes = Services::routes(true); + $routePath = explode('?', $testingUrl)[0]; + $string = 'This is a test page, to check cache configuration'; + $routes->add($routePath, static function () use ($string) { + CodeIgniter::cache(60); $response = Services::response(); - $string = 'This is a test page, to check cache configuration'; return $response->setBody($string); }); @@ -792,9 +793,15 @@ public function testPageCacheWithCacheQueryString( // Cache the page output using default caching function and $cacheConfig // with value from the data provider + ob_start(); $this->codeigniter->run(); - // Cache the page using our own $cacheConfig confugration - $this->codeigniter->cachePage($cacheConfig); + $output = ob_get_clean(); + + $this->assertSame($string, $output); + + if (count(ob_list_handlers()) > 1) { + ob_end_clean(); + } } // Calculate how much cached items exist in the cache after the test requests From 08493fb33f7b9ed01c917b6de4538762a9c77bf4 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 23 Feb 2023 15:50:53 +0900 Subject: [PATCH 4/6] docs: fix PHPDoc types --- system/CodeIgniter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 544fcb2b813e..2ad67b50e4a6 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -734,7 +734,7 @@ public static function cache(int $time) * Caches the full response from the current request. Used for * full-page caching for very high performance. * - * @return mixed + * @return bool */ public function cachePage(Cache $config) { @@ -920,7 +920,7 @@ protected function createController() * 2. PHP CLI: accessed by CLI via php public/index.php, arguments become URI segments, * sent to Controllers via Routes, output varies * - * @param mixed $class + * @param Controller $class * * @return false|ResponseInterface|string|void */ From 1dfa4fefc65b67319bff33f6eecb2f7ae2841788 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 24 Feb 2023 08:48:46 +0900 Subject: [PATCH 5/6] refactor: use config() in display404errors() --- system/CodeIgniter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 2ad67b50e4a6..32db30f90591 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -965,7 +965,7 @@ protected function display404errors(PageNotFoundException $e) unset($override); - $cacheConfig = new Cache(); + $cacheConfig = config(Cache::class); $this->gatherOutput($cacheConfig, $returned); if ($this->returnResponse) { return $this->response; From a54ee527e1f4d369ee8c1e2ec65b697cc1bef2a9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 24 Feb 2023 21:51:26 +0900 Subject: [PATCH 6/6] test: use ob_get_level() --- tests/system/CodeIgniterTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index f5c1b3e8cfc5..a7f5b9bd1908 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -53,7 +53,7 @@ protected function tearDown(): void { parent::tearDown(); - if (count(ob_list_handlers()) > 1) { + if (ob_get_level() > 1) { ob_end_clean(); } @@ -799,7 +799,7 @@ public function testPageCacheWithCacheQueryString( $this->assertSame($string, $output); - if (count(ob_list_handlers()) > 1) { + if (ob_get_level() > 1) { ob_end_clean(); } }