Browse Source

clear groups across cache configs if no config name is provided

chris cnizzardini 2 years ago
parent
commit
5f7a4d9f71

+ 11 - 6
src/Command/CacheClearGroupCommand.php

@@ -71,7 +71,7 @@ class CacheClearGroupCommand extends Command
     {
         $group = (string)$args->getArgument('group');
         try {
-            Cache::groupConfigs($group);
+            $groupConfigs = Cache::groupConfigs($group);
         } catch (InvalidArgumentException $e) {
             $io->error(sprintf('Cache group "%s" not found', $group));
 
@@ -85,14 +85,19 @@ class CacheClearGroupCommand extends Command
             return static::CODE_ERROR;
         }
 
-        if (!Cache::clearGroup($group, $args->getArgument('config'))) {
-            $io->error(sprintf('Error encountered clearing group "%s"', $group));
+        foreach ($groupConfigs[$group] as $groupConfig) {
+            if ($config !== null && $config !== $groupConfig) {
+                continue;
+            }
 
-            return static::CODE_ERROR;
+            if (!Cache::clearGroup($group, $groupConfig)) {
+                $io->error(sprintf('Error encountered clearing group "%s"', $group));
+                $this->abort();
+            } else {
+                $io->success(sprintf('Group "%s" was cleared', $group));
+            }
         }
 
-        $io->success(sprintf('Group "%s" was cleared', $group));
-
         return static::CODE_SUCCESS;
     }
 }

+ 1 - 1
src/Console/BaseCommand.php

@@ -239,7 +239,7 @@ abstract class BaseCommand implements CommandInterface
     abstract public function execute(Arguments $args, ConsoleIo $io);
 
     /**
-     * Halt the the current process with a StopException.
+     * Halt the current process with a StopException.
      *
      * @param int $code The exit code to use.
      * @throws \Cake\Console\Exception\StopException

+ 13 - 0
tests/TestCase/Command/CacheCommandsTest.php

@@ -35,6 +35,7 @@ class CacheCommandsTest extends TestCase
     {
         parent::setUp();
         Cache::setConfig('test', ['engine' => 'File', 'path' => CACHE, 'groups' => ['test_group']]);
+        Cache::setConfig('test2', ['engine' => 'File', 'path' => CACHE, 'groups' => ['test_group']]);
         $this->setAppNamespace();
         $this->useCommandRunner();
     }
@@ -46,6 +47,7 @@ class CacheCommandsTest extends TestCase
     {
         parent::tearDown();
         Cache::drop('test');
+        Cache::drop('test2');
     }
 
     /**
@@ -145,6 +147,17 @@ class CacheCommandsTest extends TestCase
     public function testClearGroup(): void
     {
         Cache::add('key', 'value1', 'test');
+        Cache::add('key', 'value1', 'test2');
+        $this->exec('cache clear_group test_group');
+
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertNull(Cache::read('key', 'test'));
+        $this->assertNull(Cache::read('key', 'test2'));
+    }
+
+    public function testClearGroupWithConfig(): void
+    {
+        Cache::add('key', 'value1', 'test');
         $this->exec('cache clear_group test_group test');
 
         $this->assertExitCode(Shell::CODE_SUCCESS);