Browse Source

adds test coverage

chris cnizzardini 2 years ago
parent
commit
7bef557ef2

+ 12 - 1
src/Command/CacheClearGroupCommand.php

@@ -53,6 +53,10 @@ class CacheClearGroupCommand extends Command
                 'all caches belonging to group "mygroup".',
             'required' => true,
         ]);
+        $parser->addArgument('config', [
+            'help' => 'Name of the configuration to use. Defaults to "default"',
+            'default' => 'default'
+        ]);
 
         return $parser;
     }
@@ -75,7 +79,14 @@ class CacheClearGroupCommand extends Command
             return static::CODE_ERROR;
         }
 
-        if (!Cache::clearGroup($group)) {
+        $config = $args->getArgument('config');
+        if ($config !== null && Cache::getConfig($config) === null) {
+            $io->error(sprintf('Cache config "%s" not found', $config));
+
+            return static::CODE_ERROR;
+        }
+
+        if (!Cache::clearGroup($group, $args->getArgument('config'))) {
             $io->error(sprintf('Error encountered clearing group "%s"', $group));
 
             return static::CODE_ERROR;

+ 26 - 1
tests/TestCase/Command/CacheCommandsTest.php

@@ -34,7 +34,7 @@ class CacheCommandsTest extends TestCase
     public function setUp(): void
     {
         parent::setUp();
-        Cache::setConfig('test', ['engine' => 'File', 'path' => CACHE]);
+        Cache::setConfig('test', ['engine' => 'File', 'path' => CACHE, 'groups' => ['test_group']]);
         $this->setAppNamespace();
         $this->useCommandRunner();
     }
@@ -141,4 +141,29 @@ class CacheCommandsTest extends TestCase
         $this->assertNull(Cache::read('key', 'test'));
         $this->assertNull(Cache::read('key', '_cake_core_'));
     }
+
+    public function testClearGroup(): void
+    {
+        Cache::add('key', 'value1', 'test');
+        $this->exec('cache clear_group test_group test');
+
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertNull(Cache::read('key', 'test'));
+    }
+
+    public function testClearGroupInvalidConfig(): void
+    {
+        $this->exec('cache clear_group test_group does_not_exist');
+
+        $this->assertExitCode(Shell::CODE_ERROR);
+        $this->assertErrorContains('Cache config "does_not_exist" not found');
+    }
+
+    public function testClearInvalidGroup(): void
+    {
+        $this->exec('cache clear_group does_not_exist');
+
+        $this->assertExitCode(Shell::CODE_ERROR);
+        $this->assertErrorContains('Cache group "does_not_exist" not found');
+    }
 }