Skip to content

Commit e5de847

Browse files
committed
wip
1 parent a012833 commit e5de847

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

system/Commands/Cache/ClearCache.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace CodeIgniter\Commands\Cache;
22

3+
use CodeIgniter\Cache\CacheFactory;
34
use CodeIgniter\CLI\BaseCommand;
45
use CodeIgniter\CLI\CLI;
56

@@ -49,6 +50,24 @@ class ClearCache extends BaseCommand
4950
*/
5051
public function run(array $params = [])
5152
{
52-
dd($params);
53+
$config = config('Cache');
54+
55+
$handler = $params[0] ?? $config->handler;
56+
if (! array_key_exists($handler, $config->validHandlers))
57+
{
58+
CLI::error($handler . ' is not a valid cache handler.');
59+
return;
60+
}
61+
62+
$config->handler = $handler;
63+
$cache = CacheFactory::getHandler($config);
64+
65+
if (! $cache->clean())
66+
{
67+
CLI::error('Error while clearing the cache.');
68+
return;
69+
}
70+
71+
CLI::write(CLI::color('Done', 'green'));
5372
}
5473
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php namespace CodeIgniter\Commands;
2+
3+
use CodeIgniter\CLI\CLI;
4+
use CodeIgniter\CLI\CommandRunner;
5+
use CodeIgniter\Config\Config;
6+
use CodeIgniter\HTTP\UserAgent;
7+
use CodeIgniter\Test\CIUnitTestCase;
8+
use CodeIgniter\Test\Filters\CITestStreamFilter;
9+
use CodeIgniter\Test\Mock\MockAppConfig;
10+
use Config\Services;
11+
12+
class ClearCacheTest extends CIUnitTestCase
13+
{
14+
protected $streamFilter;
15+
protected $result;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
CITestStreamFilter::$buffer = '';
22+
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
23+
24+
$this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH);
25+
$this->env->load();
26+
27+
// Set environment values that would otherwise stop the framework from functioning during tests.
28+
if (! isset($_SERVER['app.baseURL']))
29+
{
30+
$_SERVER['app.baseURL'] = 'http://example.com';
31+
}
32+
33+
$_SERVER['argv'] = [
34+
'spark',
35+
'list',
36+
];
37+
$_SERVER['argc'] = 2;
38+
CLI::init();
39+
40+
$this->config = new MockAppConfig();
41+
$this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent());
42+
$this->response = new \CodeIgniter\HTTP\Response($this->config);
43+
$this->logger = Services::logger();
44+
$this->runner = new CommandRunner();
45+
$this->runner->initController($this->request, $this->response, $this->logger);
46+
}
47+
48+
public function tearDown(): void
49+
{
50+
if (! $this->result)
51+
{
52+
return;
53+
}
54+
55+
stream_filter_remove($this->streamFilter);
56+
}
57+
58+
public function testClearCacheInvalidHandler()
59+
{
60+
$config = config('Cache');
61+
$config->handler = 'junk';
62+
Config::injectMock('Cache', $config);
63+
64+
$this->runner->index(['cache:clear']);
65+
$result = CITestStreamFilter::$buffer;
66+
67+
$this->assertStringContainsString('junk is not a valid cache handler.', $result);
68+
}
69+
}

0 commit comments

Comments
 (0)