Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New command: cache:clear #3304

Merged
merged 3 commits into from
Jul 15, 2020
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
73 changes: 73 additions & 0 deletions system/Commands/Cache/ClearCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php namespace CodeIgniter\Commands\Cache;

use CodeIgniter\Cache\CacheFactory;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class ClearCache extends BaseCommand
{
/**
* Command grouping.
*
* @var string
*/
protected $group = 'Cache';

/**
* The Command's name
*
* @var string
*/
protected $name = 'cache:clear';

/**
* the Command's short description
*
* @var string
*/
protected $description = 'Clears the current system caches.';

/**
* the Command's usage
*
* @var string
*/
protected $usage = 'cache:clear [driver]';

/**
* the Command's Arguments
*
* @var array
*/
protected $arguments = [
'driver' => 'The cache driver to use',
];

/**
* Creates a new migration file with the current timestamp.
*
* @param array $params
*/
public function run(array $params = [])
{
$config = config('Cache');

$handler = $params[0] ?? $config->handler;
if (! array_key_exists($handler, $config->validHandlers))
{
CLI::error($handler . ' is not a valid cache handler.');
return;
}

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

if (! $cache->clean())
{
CLI::error('Error while clearing the cache.');
return;
}

CLI::write(CLI::color('Done', 'green'));
}
}
57 changes: 57 additions & 0 deletions tests/system/Commands/ClearCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php namespace CodeIgniter\Commands;

use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\CommandRunner;
use CodeIgniter\Config\Config;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\Test\Mock\MockAppConfig;
use Config\Services;

class ClearCacheTest extends CIUnitTestCase
{
protected $streamFilter;
protected $result;

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

CITestStreamFilter::$buffer = '';
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
}

public function tearDown(): void
{
if (! $this->result)
{
return;
}

stream_filter_remove($this->streamFilter);
}

public function testClearCacheInvalidHandler()
{
command('cache:clear junk');
$result = CITestStreamFilter::$buffer;

$this->assertStringContainsString('junk is not a valid cache handler.', $result);
}

public function testClearCacheWorks()
{
cache()->save('foo', 'bar');

$this->assertEquals('bar', cache('foo'));

command('cache:clear');
$result = CITestStreamFilter::$buffer;

$this->assertNull(cache('foo'));

$this->assertStringContainsString('Done', $result);
}
}