Skip to content

Commit

Permalink
Merge pull request #7297 from kenjis/refactor-CodeIgniter-check-cache
Browse files Browse the repository at this point in the history
refactor: use config(Cache::class) in CodeIgniter
  • Loading branch information
kenjis authored Feb 26, 2023
2 parents 6988e74 + a54ee52 commit dd4e542
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
10 changes: 5 additions & 5 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class CodeIgniter
/**
* Cache expiration time
*
* @var int
* @var int seconds
*/
protected static $cacheTTL = 0;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 18 additions & 11 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function tearDown(): void
{
parent::tearDown();

if (count(ob_list_handlers()) > 1) {
if (ob_get_level() > 1) {
ob_end_clean();
}

Expand Down Expand Up @@ -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
Expand All @@ -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);
});
Expand All @@ -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 (ob_get_level() > 1) {
ob_end_clean();
}
}

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

0 comments on commit dd4e542

Please sign in to comment.