Skip to content

Commit f373ac3

Browse files
committed
refactor: add full testing for cache:clear command
1 parent 7fa1101 commit f373ac3

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

system/Commands/Cache/ClearCache.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CodeIgniter\Commands\Cache;
1515

16-
use CodeIgniter\Cache\CacheFactory;
1716
use CodeIgniter\CLI\BaseCommand;
1817
use CodeIgniter\CLI\CLI;
1918
use Config\Cache;
@@ -69,22 +68,21 @@ public function run(array $params)
6968
$handler = $params[0] ?? $config->handler;
7069

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

74-
return;
73+
return EXIT_ERROR;
7574
}
7675

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

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

84-
return;
85-
// @codeCoverageIgnoreEnd
81+
return EXIT_ERROR;
8682
}
8783

8884
CLI::write(CLI::color('Cache cleared.', 'green'));
85+
86+
return EXIT_SUCCESS;
8987
}
9088
}

system/Language/en/Cache.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// Cache language settings
1515
return [
1616
'unableToWrite' => 'Cache unable to write to "{0}".',
17+
'invalidHandler' => 'Cache driver "{0}" is not a valid cache handler.',
1718
'invalidHandlers' => 'Cache config must have an array of $validHandlers.',
1819
'noBackup' => 'Cache config must have a handler and backupHandler set.',
1920
'handlerNotFound' => 'Cache config has an invalid handler or backup handler specified.',

tests/system/Commands/ClearCacheTest.php

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

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

35+
$this->resetServices();
36+
3437
// Make sure we are testing with the correct handler (override injections)
3538
Services::injectMock('cache', CacheFactory::getHandler(config('Cache')));
3639
}
3740

41+
protected function tearDown(): void
42+
{
43+
parent::tearDown();
44+
45+
$this->resetServices();
46+
}
47+
3848
public function testClearCacheInvalidHandler(): void
3949
{
4050
command('cache:clear junk');
4151

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

4558
public function testClearCacheWorks(): void
@@ -52,4 +65,22 @@ public function testClearCacheWorks(): void
5265
$this->assertNull(cache('foo'));
5366
$this->assertStringContainsString('Cache cleared.', $this->getStreamFilterBuffer());
5467
}
68+
69+
public function testClearCacheFails(): void
70+
{
71+
$cache = $this->getMockBuilder(FileHandler::class)
72+
->setConstructorArgs([config('Cache')])
73+
->onlyMethods(['clean'])
74+
->getMock();
75+
$cache->expects($this->once())->method('clean')->willReturn(false);
76+
77+
Services::injectMock('cache', $cache);
78+
79+
command('cache:clear');
80+
81+
$this->assertSame(
82+
"Error while clearing the cache.\n",
83+
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
84+
);
85+
}
5586
}

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ BREAKING
1818
Message Changes
1919
***************
2020

21+
- The ``Cache.invalidHandler`` message string was added.
22+
2123
*******
2224
Changes
2325
*******

0 commit comments

Comments
 (0)