Skip to content
Permalink
Browse files

add caches shell

  • Loading branch information...
chris48s committed May 26, 2016
1 parent e97a125 commit 4ce2da2e486d97c62e0ab7c5268fcc955f506d67
@@ -0,0 +1,113 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Shell;
use Cake\Cache\Cache;
use Cake\Console\Shell;
use Cake\Core\Configure;
/**
* Caches Shell.
*
* Provides a CLI interface to clear caches.
* This tool can be used in development or by deployment scripts when changes
* are made that require cached data to be removed.
*/
class CachesShell extends Shell
{
/**
* Get the option parser for this shell.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->addSubcommand('list_prefixes', [
'help' => 'Show a list of all defined cache prefixes.',
]);
$parser->addSubcommand('clear_all', [
'help' => 'Clear all caches.',
]);
$parser->addSubcommand('clear', [
'help' => 'Clear the cache for a specified prefix.',
'parser' => [
'description' => [
'Clear the cache for a particular prefix.',
'For example, `cake cache clear _cake_model_` will clear the model cache',
'Use `cake cache clear list_prefixes` to list available prefixes'
],
'arguments' => [
'prefix' => [
'help' => 'The cache prefix to be cleared.',
'required' => true
]
]
]
]);
return $parser;
}
/**
* Clear metadata.
*
* @param string|null $prefix The cache prefix to be cleared.
* @throws Cake\Console\Exception\StopException
* @return void
*/
public function clear($prefix = null)
{
try {
Cache::clear(false, $prefix);
} catch (\InvalidArgumentException $e) {
$this->abort($e->getMessage());
}
$this->out("<success>Cleared $prefix cache</success>");
}
/**
* Clear metadata.
*
* @return void
*/
public function clearAll()
{
if (version_compare(Configure::version(), '3.2.0', '>=')) {
Cache::clearAll(false);
$this->out("<success>Cleared all caches</success>");
} else {
$prefixes = Cache::configured();
foreach ($prefixes as $prefix) {
$this->clear($prefix);
}
}
}
/**
* Show a list of all defined cache prefixes.
*
* @return void
*/
public function listPrefixes()
{
$prefixes = Cache::configured();
foreach ($prefixes as $prefix) {
$this->out($prefix);
}
}
}
@@ -0,0 +1,111 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Shell;
use Cake\Cache\Cache;
use Cake\Shell\CachesShell;
use Cake\TestSuite\TestCase;
/**
* CachesShell tests.
*/
class CachesShellTest extends TestCase
{
/**
* setup method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->io = $this->getMock('Cake\Console\ConsoleIo');
$this->shell = new CachesShell($this->io);
Cache::config('test', ['engine' => 'File', 'path' => TMP]);
}
/**
* Teardown
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
unset($this->io);
unset($this->shell);
Cache::drop('test');
}
/**
* Test that getOptionParser() returns an instance of \Cake\Console\ConsoleOptionParser
*
* @return void
*/
public function testGetOptionParser()
{
$this->assertInstanceOf('Cake\Console\ConsoleOptionParser', $this->shell->getOptionParser());
}
/**
* Test that clear() throws \Cake\Console\Exception\StopException if cache prefix is invalid
*
* @return void
*/
public function testClearInvalidPrefix()
{
$this->setExpectedException('Cake\Console\Exception\StopException');
$this->shell->clear('foo');
}
/**
* Test that clear() clears the specified cache when a valid prefix is used
*
* @return void
*/
public function testClearValidPrefix()
{
Cache::add('key', 'value', 'test');
$this->shell->clear('test');
$this->assertFalse(Cache::read('key', 'test'));
}
/**
* Test that clear() only clears the specified cache
*
* @return void
*/
public function testClearIgnoresOtherCaches()
{
Cache::add('key', 'value', 'test');
$this->shell->clear('_cake_core_');
$this->assertEquals('value', Cache::read('key', 'test'));
}
/**
* Test that clearAll() clears values from all defined caches
*
* @return void
*/
public function testClearAll()
{
Cache::add('key', 'value1', 'test');
Cache::add('key', 'value3', '_cake_core_');
$this->shell->clearAll();
$this->assertFalse(Cache::read('key', 'test'));
$this->assertFalse(Cache::read('key', '_cake_core_'));
}
}
@@ -81,7 +81,7 @@ public function testMain()
$expected = "/\[.*TestPluginTwo.*\] example, unique, welcome/";
$this->assertRegExp($expected, $output);
$expected = "/\[.*CORE.*\] i18n, orm_cache, plugin, routes, server/";
$expected = "/\[.*CORE.*\] caches, i18n, orm_cache, plugin, routes, server/";
$this->assertRegExp($expected, $output);
$expected = "/\[.*app.*\] i18m, sample/";
@@ -102,7 +102,7 @@ public function testMainAppPriority()
$output = implode("\n", $output);
rename(APP . 'Shell' . DS . 'I18nShell.php', APP . 'Shell' . DS . 'I18mShell.php');
$expected = "/\[.*CORE.*\] orm_cache, plugin, routes, server/";
$expected = "/\[.*CORE.*\] caches, orm_cache, plugin, routes, server/";
$this->assertRegExp($expected, $output);
$expected = "/\[.*app.*\] i18n, sample/";
@@ -120,7 +120,7 @@ public function testCommands()
$output = $this->out->output;
$expected = "TestPlugin.example TestPlugin.sample TestPluginTwo.example unique welcome " .
"i18n orm_cache plugin routes server i18m sample testing_dispatch\n";
"caches i18n orm_cache plugin routes server i18m sample testing_dispatch\n";
$this->assertTextEquals($expected, $output);
}

0 comments on commit 4ce2da2

Please sign in to comment.
You can’t perform that action at this time.