Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 13 additions & 45 deletions system/Commands/Cache/InfoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,34 @@

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.');

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']),
Expand All @@ -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;
}
Expand Down
47 changes: 32 additions & 15 deletions tests/system/Commands/Cache/InfoCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,53 +32,69 @@ 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
{
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());
}
}
Loading