From a012833e12f520190f0ba5b7cc2ecdea0da7e5fd Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Wed, 24 Jun 2020 23:20:07 -0500 Subject: [PATCH 1/3] wip --- system/Commands/Cache/ClearCache.php | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 system/Commands/Cache/ClearCache.php diff --git a/system/Commands/Cache/ClearCache.php b/system/Commands/Cache/ClearCache.php new file mode 100644 index 000000000000..c521658aeae2 --- /dev/null +++ b/system/Commands/Cache/ClearCache.php @@ -0,0 +1,54 @@ + 'The cache driver to use', + ]; + + /** + * Creates a new migration file with the current timestamp. + * + * @param array $params + */ + public function run(array $params = []) + { + dd($params); + } +} From e5de8479d6d6c89f7983ecd6ae959e5ba365ad0e Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Mon, 6 Jul 2020 22:45:40 -0500 Subject: [PATCH 2/3] wip --- system/Commands/Cache/ClearCache.php | 21 +++++++- tests/system/Commands/ClearCacheTest.php | 69 ++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/system/Commands/ClearCacheTest.php diff --git a/system/Commands/Cache/ClearCache.php b/system/Commands/Cache/ClearCache.php index c521658aeae2..35c65815bc6b 100644 --- a/system/Commands/Cache/ClearCache.php +++ b/system/Commands/Cache/ClearCache.php @@ -1,5 +1,6 @@ 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')); } } diff --git a/tests/system/Commands/ClearCacheTest.php b/tests/system/Commands/ClearCacheTest.php new file mode 100644 index 000000000000..d52450f0d698 --- /dev/null +++ b/tests/system/Commands/ClearCacheTest.php @@ -0,0 +1,69 @@ +streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); + + $this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH); + $this->env->load(); + + // Set environment values that would otherwise stop the framework from functioning during tests. + if (! isset($_SERVER['app.baseURL'])) + { + $_SERVER['app.baseURL'] = 'http://example.com'; + } + + $_SERVER['argv'] = [ + 'spark', + 'list', + ]; + $_SERVER['argc'] = 2; + CLI::init(); + + $this->config = new MockAppConfig(); + $this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent()); + $this->response = new \CodeIgniter\HTTP\Response($this->config); + $this->logger = Services::logger(); + $this->runner = new CommandRunner(); + $this->runner->initController($this->request, $this->response, $this->logger); + } + + public function tearDown(): void + { + if (! $this->result) + { + return; + } + + stream_filter_remove($this->streamFilter); + } + + public function testClearCacheInvalidHandler() + { + $config = config('Cache'); + $config->handler = 'junk'; + Config::injectMock('Cache', $config); + + $this->runner->index(['cache:clear']); + $result = CITestStreamFilter::$buffer; + + $this->assertStringContainsString('junk is not a valid cache handler.', $result); + } +} From 8e680c76adf828696ea90e47711582a94be8b1d2 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Mon, 13 Jul 2020 23:32:12 -0500 Subject: [PATCH 3/3] Finished up tests --- tests/system/Commands/ClearCacheTest.php | 44 +++++++++--------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/tests/system/Commands/ClearCacheTest.php b/tests/system/Commands/ClearCacheTest.php index d52450f0d698..63e9dabf1f12 100644 --- a/tests/system/Commands/ClearCacheTest.php +++ b/tests/system/Commands/ClearCacheTest.php @@ -20,29 +20,7 @@ protected function setUp(): void CITestStreamFilter::$buffer = ''; $this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); - - $this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH); - $this->env->load(); - - // Set environment values that would otherwise stop the framework from functioning during tests. - if (! isset($_SERVER['app.baseURL'])) - { - $_SERVER['app.baseURL'] = 'http://example.com'; - } - - $_SERVER['argv'] = [ - 'spark', - 'list', - ]; - $_SERVER['argc'] = 2; - CLI::init(); - - $this->config = new MockAppConfig(); - $this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent()); - $this->response = new \CodeIgniter\HTTP\Response($this->config); - $this->logger = Services::logger(); - $this->runner = new CommandRunner(); - $this->runner->initController($this->request, $this->response, $this->logger); + $this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter'); } public function tearDown(): void @@ -57,13 +35,23 @@ public function tearDown(): void public function testClearCacheInvalidHandler() { - $config = config('Cache'); - $config->handler = 'junk'; - Config::injectMock('Cache', $config); - - $this->runner->index(['cache:clear']); + 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); + } }