diff --git a/src/Illuminate/Cache/Console/ClearCommand.php b/src/Illuminate/Cache/Console/ClearCommand.php index 41875dd9989d..e009165b8556 100755 --- a/src/Illuminate/Cache/Console/ClearCommand.php +++ b/src/Illuminate/Cache/Console/ClearCommand.php @@ -2,6 +2,7 @@ use Illuminate\Console\Command; use Illuminate\Cache\CacheManager; +use Symfony\Component\Console\Input\InputArgument; class ClearCommand extends Command { @@ -46,13 +47,33 @@ public function __construct(CacheManager $cache) */ public function fire() { - $this->laravel['events']->fire('cache:clearing'); + $storeName = $this->argument('store'); - $this->cache->flush(); + try { + $cacheStore = $this->cache->store($storeName); + } catch (\InvalidArgumentException $e) { + return $this->error("Cache store '{$storeName}' does not exist."); + } - $this->laravel['events']->fire('cache:cleared'); + $this->laravel['events']->fire('cache:clearing', [$storeName]); + + $cacheStore->flush(); + + $this->laravel['events']->fire('cache:cleared', [$storeName]); $this->info('Application cache cleared!'); } + /** + * Get the console command arguments. + * + * @return array + */ + protected function getArguments() + { + return [ + ['store', InputArgument::OPTIONAL, 'The name of the store you would like to clear.'], + ]; + } + } diff --git a/tests/Cache/ClearCommandTest.php b/tests/Cache/ClearCommandTest.php new file mode 100644 index 000000000000..10e5b7638c78 --- /dev/null +++ b/tests/Cache/ClearCommandTest.php @@ -0,0 +1,83 @@ +setLaravel($app); + + $cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository); + $cacheRepository->shouldReceive('flush')->once(); + + $this->runCommand($command); + } + + + public function testClearWithStoreOption() + { + $command = new ClearCommandTestStub( + $cacheManager = m::mock('Illuminate\Cache\CacheManager') + ); + + $cacheRepository = m::mock('\Illuminate\Contracts\Cache\Repository'); + + $app = new Application(); + $command->setLaravel($app); + + $cacheManager->shouldReceive('store')->once()->with('foo')->andReturn($cacheRepository); + $cacheRepository->shouldReceive('flush')->once(); + + $this->runCommand($command, ['store' => 'foo']); + } + + + public function testClearWithInvalidStoreOption() + { + $command = new ClearCommandTestStub( + $cacheManager = m::mock('Illuminate\Cache\CacheManager') + ); + + $cacheRepository = m::mock('\Illuminate\Contracts\Cache\Repository'); + + $app = new Application(); + $command->setLaravel($app); + + $cacheManager->shouldReceive('store')->once()->with('bar')->andThrow('\InvalidArgumentException'); + $cacheRepository->shouldReceive('flush')->never(); + + $this->runCommand($command, ['store' => 'bar']); + } + + + protected function runCommand($command, $input = array()) + { + return $command->run(new Symfony\Component\Console\Input\ArrayInput($input), new Symfony\Component\Console\Output\NullOutput); + } + +} + +class ClearCommandTestStub extends ClearCommand { + + public function call($command, array $arguments = array()) + { + // + } + +}