diff --git a/Command/RemoveCacheCommand.php b/Command/RemoveCacheCommand.php new file mode 100644 index 000000000..bdf562e44 --- /dev/null +++ b/Command/RemoveCacheCommand.php @@ -0,0 +1,59 @@ +setName('liip:imagine:cache:remove') + ->setDescription('Remove cache for given paths and set of filters.') + ->addArgument('paths', InputArgument::OPTIONAL|InputArgument::IS_ARRAY, 'Image paths') + ->addOption( + 'filters', + 'f', + InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, + 'Filters list' + ) + ->setHelp(<<%command.name% command removes cache by specified parameters. + +Paths should be separated by spaces: +php app/console %command.name% path1 path2 +All cache for a given `paths` will be lost. + +If you use --filters parameter: +php app/console %command.name% --filters=thumb1 --filters=thumb2 +All cache for a given filters will be lost. + +You can combine these parameters: +php app/console %command.name% path1 path2 --filters=thumb1 --filters=thumb2 + +php app/console %command.name% +Cache for all paths and filters will be lost when executing this command without parameters. +EOF + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $paths = $input->getArgument('paths'); + $filters = $input->getOption('filters'); + + if (empty($filters)) { + $filters = null; + } + + /** @var CacheManager cacheManager */ + $cacheManager = $this->getContainer()->get('liip_imagine.cache.manager'); + + $cacheManager->remove($paths, $filters); + } +} diff --git a/Tests/Functional/Command/RemoveCacheTest.php b/Tests/Functional/Command/RemoveCacheTest.php new file mode 100644 index 000000000..a0499161c --- /dev/null +++ b/Tests/Functional/Command/RemoveCacheTest.php @@ -0,0 +1,296 @@ +client = $this->createClient(); + + $this->webRoot = self::$kernel->getContainer()->getParameter('kernel.root_dir').'/web'; + $this->cacheRoot = $this->webRoot.'/'.self::$kernel->getContainer()->getParameter('liip_imagine.cache_prefix'); + + $this->filesystem = new Filesystem; + $this->filesystem->remove($this->cacheRoot); + } + + public function testExecuteSuccessfullyWithEmptyCacheAndWithoutParameters() + { + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + + $this->executeConsole(new RemoveCacheCommand()); + } + + public function testExecuteSuccessfullyWithEmptyCacheAndOnePathAndOneFilter() + { + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + + $this->executeConsole( + new RemoveCacheCommand(), + array( + 'paths' => array('images/cats.jpeg'), + '--filters' => array('thumbnail_web_path') + )); + } + + public function testExecuteSuccessfullyWithEmptyCacheAndMultiplePaths() + { + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + + $this->executeConsole( + new RemoveCacheCommand(), + array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg')) + ); + } + + public function testExecuteSuccessfullyWithEmptyCacheAndMultipleFilters() + { + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + + $this->executeConsole( + new RemoveCacheCommand(), + array('--filters' => array('thumbnail_web_path', 'thumbnail_default')) + ); + } + + public function testShouldRemoveAllCacheIfParametersDoesNotPassed() + { + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg', + 'anImageContent2' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats.jpeg', + 'anImageContent' + ); + + $this->executeConsole(new RemoveCacheCommand()); + + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); + } + + public function testShouldRemoveCacheBySinglePath() + { + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg', + 'anImageContent2' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats2.jpeg', + 'anImageContent2' + ); + + $this->executeConsole( + new RemoveCacheCommand(), + array('paths' => array('images/cats.jpeg')) + ); + + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); + $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg'); + $this->assertFileExists($this->cacheRoot.'/thumbnail_default/images/cats2.jpeg'); + } + + public function testShouldRemoveCacheByMultiplePaths() + { + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg', + 'anImageContent2' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats2.jpeg', + 'anImageContent2' + ); + + $this->executeConsole( + new RemoveCacheCommand(), + array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg')) + ); + + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats2.jpeg'); + } + + public function testShouldRemoveCacheBySingleFilter() + { + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg', + 'anImageContent2' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats2.jpeg', + 'anImageContent2' + ); + + $this->executeConsole( + new RemoveCacheCommand(), + array('--filters' => array('thumbnail_default')) + ); + + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats2.jpeg'); + $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg'); + } + + public function testShouldRemoveCacheByMultipleFilters() + { + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg', + 'anImageContent2' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats2.jpeg', + 'anImageContent2' + ); + + $this->executeConsole( + new RemoveCacheCommand(), + array('--filters' => array('thumbnail_default', 'thumbnail_web_path')) + ); + + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats2.jpeg'); + } + + public function testShouldRemoveCacheByOnePathAndMultipleFilters() + { + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg', + 'anImageContent2' + ); + + $this->executeConsole( + new RemoveCacheCommand(), + array( + 'paths' => array('images/cats.jpeg'), + '--filters' => array('thumbnail_default', 'thumbnail_web_path')) + ); + + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); + $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg'); + } + + public function testShouldRemoveCacheByMultiplePathsAndSingleFilter() + { + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_default/images/cats.jpeg', + 'anImageContent' + ); + $this->filesystem->dumpFile( + $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg', + 'anImageContent2' + ); + + $this->executeConsole( + new RemoveCacheCommand(), + array( + 'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'), + '--filters' => array('thumbnail_web_path')) + ); + + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg'); + $this->assertFileExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); + } + + /** + * Helper function return the result of command execution. + * + * @param Command $command + * @param array $arguments + * @param array $options + * @return string + */ + protected function executeConsole(Command $command, array $arguments = array(), array $options = array()) + { + $command->setApplication(new Application($this->createClient()->getKernel())); + if ($command instanceof ContainerAwareCommand) { + $command->setContainer($this->createClient()->getContainer()); + } + + $arguments = array_replace(array('command' => $command->getName()), $arguments); + $options = array_replace(array('--env' => 'test'), $options); + + $commandTester = new CommandTester($command); + $commandTester->execute($arguments, $options); + + return $commandTester->getDisplay(); + } +} + +