Skip to content

Commit 26ad933

Browse files
authored
refactor: migrate cache:info command as modern command (#10163)
1 parent 3a10470 commit 26ad933

2 files changed

Lines changed: 45 additions & 60 deletions

File tree

‎system/Commands/Cache/InfoCache.php‎

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,34 @@
1313

1414
namespace CodeIgniter\Commands\Cache;
1515

16-
use CodeIgniter\Cache\CacheFactory;
17-
use CodeIgniter\CLI\BaseCommand;
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
1818
use CodeIgniter\CLI\CLI;
1919
use CodeIgniter\I18n\Time;
2020
use Config\Cache;
2121

2222
/**
23-
* Shows information on the cache.
23+
* Shows file cache information in the current system.
2424
*/
25-
class InfoCache extends BaseCommand
25+
#[Command(name: 'cache:info', description: 'Shows file cache information in the current system.', group: 'Cache')]
26+
class InfoCache extends AbstractCommand
2627
{
27-
/**
28-
* Command grouping.
29-
*
30-
* @var string
31-
*/
32-
protected $group = 'Cache';
33-
34-
/**
35-
* The Command's name
36-
*
37-
* @var string
38-
*/
39-
protected $name = 'cache:info';
40-
41-
/**
42-
* the Command's short description
43-
*
44-
* @var string
45-
*/
46-
protected $description = 'Shows file cache information in the current system.';
47-
48-
/**
49-
* the Command's usage
50-
*
51-
* @var string
52-
*/
53-
protected $usage = 'cache:info';
54-
55-
/**
56-
* Clears the cache
57-
*/
58-
public function run(array $params)
28+
protected function execute(array $arguments, array $options): int
5929
{
6030
$config = config(Cache::class);
61-
helper('number');
6231

6332
if ($config->handler !== 'file') {
6433
CLI::error('This command only supports the file cache handler.');
6534

6635
return EXIT_ERROR;
6736
}
6837

69-
$cache = CacheFactory::getHandler($config);
70-
$caches = $cache->getCacheInfo();
71-
$tbody = [];
38+
$cache = service('cache', $config);
39+
$tbody = [];
40+
41+
helper('number');
7242

73-
foreach ($caches as $key => $field) {
43+
foreach ($cache->getCacheInfo() as $key => $field) {
7444
$tbody[] = [
7545
$key,
7646
clean_path($field['server_path']),
@@ -79,14 +49,12 @@ public function run(array $params)
7949
];
8050
}
8151

82-
$thead = [
52+
CLI::table($tbody, [
8353
CLI::color('Name', 'green'),
8454
CLI::color('Server Path', 'green'),
8555
CLI::color('Size', 'green'),
8656
CLI::color('Date', 'green'),
87-
];
88-
89-
CLI::table($tbody, $thead);
57+
]);
9058

9159
return EXIT_SUCCESS;
9260
}

‎tests/system/Commands/Cache/InfoCacheTest.php‎

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Commands\Cache;
1515

1616
use CodeIgniter\Cache\CacheFactory;
17+
use CodeIgniter\CLI\CLI;
1718
use CodeIgniter\Test\CIUnitTestCase;
1819
use CodeIgniter\Test\StreamFilterTrait;
1920
use Config\Services;
@@ -31,53 +32,69 @@ protected function setUp(): void
3132
{
3233
parent::setUp();
3334

34-
// Make sure we are testing with the correct handler (override injections)
35+
CLI::resetLastWrite();
36+
$this->resetServices();
3537
Services::injectMock('cache', CacheFactory::getHandler(config('Cache')));
3638
}
3739

3840
protected function tearDown(): void
3941
{
40-
// restore default cache handler
41-
config('Cache')->handler = 'file';
42+
parent::tearDown();
43+
44+
CLI::resetLastWrite();
45+
$this->resetServices();
46+
$this->resetFactories();
4247
}
4348

44-
protected function getBuffer(): string
49+
private function getUndecoratedBuffer(): string
4550
{
46-
return $this->getStreamFilterBuffer();
51+
return preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()) ?? '';
4752
}
4853

4954
public function testInfoCacheErrorsOnInvalidHandler(): void
5055
{
5156
config('Cache')->handler = 'redis';
52-
cache()->save('foo', 'bar');
57+
5358
command('cache:info');
5459

55-
$this->assertStringContainsString('This command only supports the file cache handler.', $this->getBuffer());
60+
$this->assertSame(
61+
<<<'EOT'
62+
63+
This command only supports the file cache handler.
64+
65+
EOT,
66+
$this->getUndecoratedBuffer(),
67+
);
5668
}
5769

5870
public function testInfoCacheCanSeeFoo(): void
5971
{
6072
cache()->save('foo', 'bar');
6173
command('cache:info');
6274

63-
$this->assertStringContainsString('foo', $this->getBuffer());
75+
$this->assertStringContainsString('foo', $this->getStreamFilterBuffer());
6476
}
6577

66-
public function testInfoCacheCanSeeTable(): void
78+
public function testInfoCacheCanSeeTheads(): void
6779
{
6880
command('cache:info');
6981

70-
$this->assertStringContainsString('Name', $this->getBuffer());
71-
$this->assertStringContainsString('Server Path', $this->getBuffer());
72-
$this->assertStringContainsString('Size', $this->getBuffer());
73-
$this->assertStringContainsString('Date', $this->getBuffer());
82+
$this->assertMatchesRegularExpression(
83+
'/\|\sName[[:space:]]+\|\sServer Path[[:space:]]+\|\sSize[[:space:]]+\|\sDate[[:space:]]+\|/',
84+
$this->getUndecoratedBuffer(),
85+
);
7486
}
7587

7688
public function testInfoCacheCannotSeeFoo(): void
7789
{
78-
cache()->delete('foo');
90+
cache()->save('foo', 'bar');
7991
command('cache:info');
92+
$this->assertStringContainsString('foo', $this->getStreamFilterBuffer());
93+
94+
$this->resetStreamFilterBuffer();
8095

81-
$this->assertStringNotContainsString('foo', $this->getBuffer());
96+
cache()->delete('foo');
97+
command('cache:info');
98+
$this->assertStringNotContainsString('foo', $this->getUndecoratedBuffer());
8299
}
83100
}

0 commit comments

Comments
 (0)