Browse Source

add caches shell

chris48s 9 years ago
parent
commit
4ce2da2e48

+ 113 - 0
src/Shell/CachesShell.php

@@ -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);
+        }
+    }
+}

+ 111 - 0
tests/TestCase/Shell/CachesShellTest.php

@@ -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_'));
+    }
+}

+ 2 - 2
tests/TestCase/Shell/CommandListShellTest.php

@@ -81,7 +81,7 @@ class CommandListShellTest extends TestCase
         $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 @@ class CommandListShellTest extends TestCase
         $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/";

+ 1 - 1
tests/TestCase/Shell/CompletionShellTest.php

@@ -120,7 +120,7 @@ class CompletionShellTest extends TestCase
         $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);
     }