Skip to content
Merged
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
14 changes: 6 additions & 8 deletions system/Commands/Cache/ClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace CodeIgniter\Commands\Cache;

use CodeIgniter\Cache\CacheFactory;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use Config\Cache;
Expand Down Expand Up @@ -69,22 +68,21 @@ public function run(array $params)
$handler = $params[0] ?? $config->handler;

if (! array_key_exists($handler, $config->validHandlers)) {
CLI::error($handler . ' is not a valid cache handler.');
CLI::error(lang('Cache.invalidHandler', [$handler]));

return;
return EXIT_ERROR;
}

$config->handler = $handler;
$cache = CacheFactory::getHandler($config);

if (! $cache->clean()) {
// @codeCoverageIgnoreStart
if (! service('cache', $config)->clean()) {
CLI::error('Error while clearing the cache.');

return;
// @codeCoverageIgnoreEnd
return EXIT_ERROR;
}

CLI::write(CLI::color('Cache cleared.', 'green'));

return EXIT_SUCCESS;
}
}
1 change: 1 addition & 0 deletions system/Language/en/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// Cache language settings
return [
'unableToWrite' => 'Cache unable to write to "{0}".',
'invalidHandler' => 'Cache driver "{0}" is not a valid cache handler.',
'invalidHandlers' => 'Cache config must have an array of $validHandlers.',
'noBackup' => 'Cache config must have a handler and backupHandler set.',
'handlerNotFound' => 'Cache config has an invalid handler or backup handler specified.',
Expand Down
33 changes: 32 additions & 1 deletion tests/system/Commands/ClearCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CodeIgniter\Commands;

use CodeIgniter\Cache\CacheFactory;
use CodeIgniter\Cache\Handlers\FileHandler;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use Config\Services;
Expand All @@ -31,15 +32,27 @@ protected function setUp(): void
{
parent::setUp();

$this->resetServices();

// Make sure we are testing with the correct handler (override injections)
Services::injectMock('cache', CacheFactory::getHandler(config('Cache')));
}

protected function tearDown(): void
{
parent::tearDown();

$this->resetServices();
}

public function testClearCacheInvalidHandler(): void
{
command('cache:clear junk');

$this->assertStringContainsString('junk is not a valid cache handler.', $this->getStreamFilterBuffer());
$this->assertSame(
"Cache driver \"junk\" is not a valid cache handler.\n",
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
);
}

public function testClearCacheWorks(): void
Expand All @@ -52,4 +65,22 @@ public function testClearCacheWorks(): void
$this->assertNull(cache('foo'));
$this->assertStringContainsString('Cache cleared.', $this->getStreamFilterBuffer());
}

public function testClearCacheFails(): void
{
$cache = $this->getMockBuilder(FileHandler::class)
->setConstructorArgs([config('Cache')])
->onlyMethods(['clean'])
->getMock();
$cache->expects($this->once())->method('clean')->willReturn(false);

Services::injectMock('cache', $cache);

command('cache:clear');

$this->assertSame(
"Error while clearing the cache.\n",
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
);
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.7.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ BREAKING
Message Changes
***************

- The ``Cache.invalidHandler`` message string was added.

*******
Changes
*******
Expand Down
Loading