Skip to content

Commit dd4e542

Browse files
authored
Merge pull request #7297 from kenjis/refactor-CodeIgniter-check-cache
refactor: use config(Cache::class) in CodeIgniter
2 parents 6988e74 + a54ee52 commit dd4e542

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

system/CodeIgniter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class CodeIgniter
122122
/**
123123
* Cache expiration time
124124
*
125-
* @var int
125+
* @var int seconds
126126
*/
127127
protected static $cacheTTL = 0;
128128

@@ -351,7 +351,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
351351

352352
// Check for a cached page. Execution will stop
353353
// if the page has been cached.
354-
$cacheConfig = new Cache();
354+
$cacheConfig = config(Cache::class);
355355
$response = $this->displayCache($cacheConfig);
356356
if ($response instanceof ResponseInterface) {
357357
if ($returnResponse) {
@@ -734,7 +734,7 @@ public static function cache(int $time)
734734
* Caches the full response from the current request. Used for
735735
* full-page caching for very high performance.
736736
*
737-
* @return mixed
737+
* @return bool
738738
*/
739739
public function cachePage(Cache $config)
740740
{
@@ -920,7 +920,7 @@ protected function createController()
920920
* 2. PHP CLI: accessed by CLI via php public/index.php, arguments become URI segments,
921921
* sent to Controllers via Routes, output varies
922922
*
923-
* @param mixed $class
923+
* @param Controller $class
924924
*
925925
* @return false|ResponseInterface|string|void
926926
*/
@@ -965,7 +965,7 @@ protected function display404errors(PageNotFoundException $e)
965965

966966
unset($override);
967967

968-
$cacheConfig = new Cache();
968+
$cacheConfig = config(Cache::class);
969969
$this->gatherOutput($cacheConfig, $returned);
970970
if ($this->returnResponse) {
971971
return $this->response;

tests/system/CodeIgniterTest.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function tearDown(): void
5353
{
5454
parent::tearDown();
5555

56-
if (count(ob_list_handlers()) > 1) {
56+
if (ob_get_level() > 1) {
5757
ob_end_clean();
5858
}
5959

@@ -761,7 +761,7 @@ public function testPageCacheWithCacheQueryString(
761761
CITestStreamFilter::addErrorFilter();
762762

763763
// Create cache config with cacheQueryString value from the dataProvider
764-
$cacheConfig = new Cache();
764+
$cacheConfig = config(Cache::class);
765765
$cacheConfig->cacheQueryString = $cacheQueryStringValue;
766766

767767
// Clear cache before starting the test
@@ -773,15 +773,16 @@ public function testPageCacheWithCacheQueryString(
773773

774774
// Generate request to each URL from the testing array
775775
foreach ($testingUrls as $testingUrl) {
776+
$this->resetServices();
776777
$_SERVER['REQUEST_URI'] = '/' . $testingUrl;
777-
$routes = Services::routes(true);
778-
$routes->add($testingUrl, static function () {
779-
// Don't cache the page in the run() function because CodeIgniter
780-
// class will create default $cacheConfig and overwrite settings
781-
// from the dataProvider
782-
CodeIgniter::cache(0);
778+
$this->codeigniter = new MockCodeIgniter(new App());
779+
780+
$routes = Services::routes(true);
781+
$routePath = explode('?', $testingUrl)[0];
782+
$string = 'This is a test page, to check cache configuration';
783+
$routes->add($routePath, static function () use ($string) {
784+
CodeIgniter::cache(60);
783785
$response = Services::response();
784-
$string = 'This is a test page, to check cache configuration';
785786

786787
return $response->setBody($string);
787788
});
@@ -792,9 +793,15 @@ public function testPageCacheWithCacheQueryString(
792793

793794
// Cache the page output using default caching function and $cacheConfig
794795
// with value from the data provider
796+
ob_start();
795797
$this->codeigniter->run();
796-
// Cache the page using our own $cacheConfig confugration
797-
$this->codeigniter->cachePage($cacheConfig);
798+
$output = ob_get_clean();
799+
800+
$this->assertSame($string, $output);
801+
802+
if (ob_get_level() > 1) {
803+
ob_end_clean();
804+
}
798805
}
799806

800807
// Calculate how much cached items exist in the cache after the test requests

0 commit comments

Comments
 (0)