From d05bea4f6530021718b90c5efa07f7b9860413f1 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Thu, 7 May 2026 00:36:43 +0530 Subject: [PATCH] refactor: migrate `cache:info` command as modern command --- system/Commands/Cache/InfoCache.php | 58 +++++-------------- tests/system/Commands/Cache/InfoCacheTest.php | 47 ++++++++++----- 2 files changed, 45 insertions(+), 60 deletions(-) diff --git a/system/Commands/Cache/InfoCache.php b/system/Commands/Cache/InfoCache.php index ff93eaf82287..40a96dace0d7 100644 --- a/system/Commands/Cache/InfoCache.php +++ b/system/Commands/Cache/InfoCache.php @@ -13,52 +13,21 @@ namespace CodeIgniter\Commands\Cache; -use CodeIgniter\Cache\CacheFactory; -use CodeIgniter\CLI\BaseCommand; +use CodeIgniter\CLI\AbstractCommand; +use CodeIgniter\CLI\Attributes\Command; use CodeIgniter\CLI\CLI; use CodeIgniter\I18n\Time; use Config\Cache; /** - * Shows information on the cache. + * Shows file cache information in the current system. */ -class InfoCache extends BaseCommand +#[Command(name: 'cache:info', description: 'Shows file cache information in the current system.', group: 'Cache')] +class InfoCache extends AbstractCommand { - /** - * Command grouping. - * - * @var string - */ - protected $group = 'Cache'; - - /** - * The Command's name - * - * @var string - */ - protected $name = 'cache:info'; - - /** - * the Command's short description - * - * @var string - */ - protected $description = 'Shows file cache information in the current system.'; - - /** - * the Command's usage - * - * @var string - */ - protected $usage = 'cache:info'; - - /** - * Clears the cache - */ - public function run(array $params) + protected function execute(array $arguments, array $options): int { $config = config(Cache::class); - helper('number'); if ($config->handler !== 'file') { CLI::error('This command only supports the file cache handler.'); @@ -66,11 +35,12 @@ public function run(array $params) return EXIT_ERROR; } - $cache = CacheFactory::getHandler($config); - $caches = $cache->getCacheInfo(); - $tbody = []; + $cache = service('cache', $config); + $tbody = []; + + helper('number'); - foreach ($caches as $key => $field) { + foreach ($cache->getCacheInfo() as $key => $field) { $tbody[] = [ $key, clean_path($field['server_path']), @@ -79,14 +49,12 @@ public function run(array $params) ]; } - $thead = [ + CLI::table($tbody, [ CLI::color('Name', 'green'), CLI::color('Server Path', 'green'), CLI::color('Size', 'green'), CLI::color('Date', 'green'), - ]; - - CLI::table($tbody, $thead); + ]); return EXIT_SUCCESS; } diff --git a/tests/system/Commands/Cache/InfoCacheTest.php b/tests/system/Commands/Cache/InfoCacheTest.php index 44aa389338cc..e062252143b4 100644 --- a/tests/system/Commands/Cache/InfoCacheTest.php +++ b/tests/system/Commands/Cache/InfoCacheTest.php @@ -14,6 +14,7 @@ namespace CodeIgniter\Commands\Cache; use CodeIgniter\Cache\CacheFactory; +use CodeIgniter\CLI\CLI; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; use Config\Services; @@ -31,28 +32,39 @@ protected function setUp(): void { parent::setUp(); - // Make sure we are testing with the correct handler (override injections) + CLI::resetLastWrite(); + $this->resetServices(); Services::injectMock('cache', CacheFactory::getHandler(config('Cache'))); } protected function tearDown(): void { - // restore default cache handler - config('Cache')->handler = 'file'; + parent::tearDown(); + + CLI::resetLastWrite(); + $this->resetServices(); + $this->resetFactories(); } - protected function getBuffer(): string + private function getUndecoratedBuffer(): string { - return $this->getStreamFilterBuffer(); + return preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()) ?? ''; } public function testInfoCacheErrorsOnInvalidHandler(): void { config('Cache')->handler = 'redis'; - cache()->save('foo', 'bar'); + command('cache:info'); - $this->assertStringContainsString('This command only supports the file cache handler.', $this->getBuffer()); + $this->assertSame( + <<<'EOT' + + This command only supports the file cache handler. + + EOT, + $this->getUndecoratedBuffer(), + ); } public function testInfoCacheCanSeeFoo(): void @@ -60,24 +72,29 @@ public function testInfoCacheCanSeeFoo(): void cache()->save('foo', 'bar'); command('cache:info'); - $this->assertStringContainsString('foo', $this->getBuffer()); + $this->assertStringContainsString('foo', $this->getStreamFilterBuffer()); } - public function testInfoCacheCanSeeTable(): void + public function testInfoCacheCanSeeTheads(): void { command('cache:info'); - $this->assertStringContainsString('Name', $this->getBuffer()); - $this->assertStringContainsString('Server Path', $this->getBuffer()); - $this->assertStringContainsString('Size', $this->getBuffer()); - $this->assertStringContainsString('Date', $this->getBuffer()); + $this->assertMatchesRegularExpression( + '/\|\sName[[:space:]]+\|\sServer Path[[:space:]]+\|\sSize[[:space:]]+\|\sDate[[:space:]]+\|/', + $this->getUndecoratedBuffer(), + ); } public function testInfoCacheCannotSeeFoo(): void { - cache()->delete('foo'); + cache()->save('foo', 'bar'); command('cache:info'); + $this->assertStringContainsString('foo', $this->getStreamFilterBuffer()); + + $this->resetStreamFilterBuffer(); - $this->assertStringNotContainsString('foo', $this->getBuffer()); + cache()->delete('foo'); + command('cache:info'); + $this->assertStringNotContainsString('foo', $this->getUndecoratedBuffer()); } }