diff --git a/Command/RemoveCacheCommand.php b/Command/RemoveCacheCommand.php index c08824067..aa8d53514 100644 --- a/Command/RemoveCacheCommand.php +++ b/Command/RemoveCacheCommand.php @@ -26,12 +26,10 @@ protected function configure() ->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' - ) + ->addOption('filters', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'List of filters to remove for passed images (Deprecated, use "filter").') + ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'List of filters to remove for passed images.') ->setHelp(<<<'EOF' The %command.name% command removes cache by specified parameters. @@ -39,12 +37,12 @@ protected function configure() 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 +If you use --filter parameter: +php app/console %command.name% --filter=thumb1 --filter=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% path1 path2 --filter=thumb1 --filter=thumb2 php app/console %command.name% Cache for all paths and filters will be lost when executing this command without parameters. @@ -52,10 +50,16 @@ protected function configure() ); } + /** + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int + */ protected function execute(InputInterface $input, OutputInterface $output) { $paths = $input->getArgument('paths'); - $filters = $input->getOption('filters'); + $filters = $this->resolveInputFilters($input); if (empty($filters)) { $filters = null; @@ -63,7 +67,25 @@ protected function execute(InputInterface $input, OutputInterface $output) /* @var CacheManager cacheManager */ $cacheManager = $this->getContainer()->get('liip_imagine.cache.manager'); - $cacheManager->remove($paths, $filters); + + return 0; + } + + /** + * @param InputInterface $input + * + * @return array|mixed + */ + private function resolveInputFilters(InputInterface $input) + { + $filters = $input->getOption('filter'); + + if (count($filtersDeprecated = $input->getOption('filters'))) { + $filters = array_merge($filters, $filtersDeprecated); + @trigger_error('As of 1.9, use of the "--filters" option has been deprecated in favor of "--filter" and will be removed in 2.0.', E_USER_DEPRECATED); + } + + return $filters; } } diff --git a/Command/ResolveCacheCommand.php b/Command/ResolveCacheCommand.php index 8aa69a8ba..0b3209cb3 100644 --- a/Command/ResolveCacheCommand.php +++ b/Command/ResolveCacheCommand.php @@ -27,67 +27,177 @@ protected function configure() $this ->setName('liip:imagine:cache:resolve') ->setDescription('Resolve cache for given path and set of filters.') - ->addArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Image paths') - ->addOption( - 'filters', - 'f', - InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, - 'Filters list' - )->setHelp(<<<'EOF' -The %command.name% command resolves cache by specified parameters. -It returns list of urls. - -php app/console %command.name% path1 path2 --filters=thumb1 -Cache for this two paths will be resolved with passed filter. -As a result you will get - http://localhost/media/cache/thumb1/path1 - http://localhost/media/cache/thumb1/path2 - -You can pass few filters: -php app/console %command.name% path1 --filters=thumb1 --filters=thumb2 -As a result you will get - http://localhost/media/cache/thumb1/path1 - http://localhost/media/cache/thumb2/path1 - -If you omit --filters parameter then to resolve given paths will be used all configured and available filters in application: -php app/console %command.name% path1 -As a result you will get - http://localhost/media/cache/thumb1/path1 - http://localhost/media/cache/thumb2/path1 + ->addArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY, + 'Any number of image paths to act on.') + ->addOption('filters', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'List of filters to apply to passed images (Deprecated, use "filter").') + ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'List of filters to apply to passed images.') + ->addOption('force', 'F', InputOption::VALUE_NONE, + 'Force image resolution regardless of cache.') + ->addOption('as-script', 's', InputOption::VALUE_NONE, + 'Only print machine-parseable results.') + ->setHelp(<<<'EOF' +The %command.name% command resolves the passed image(s) for the resolved +filter(s), outputting results using the following basic format: + - "image.ext[filter]" (resolved|cached|failed) as "path/to/cached/image.ext" + +# bin/console %command.name% --filter=thumb1 foo.ext bar.ext +Resolve both foo.ext and bar.ext using thumb1 filter, outputting: + - "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext" + - "bar.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/bar.ext" + +# bin/console %command.name% --filter=thumb1 --filter=thumb2 foo.ext +Resolve foo.ext using both thumb1 and thumb2 filters, outputting: + - "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext" + - "foo.ext[thumb2]" resolved as "http://localhost/media/cache/thumb2/foo.ext" + +# bin/console %command.name% foo.ext +Resolve foo.ext using all configured filters (as none are specified), outputting: + - "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext" + - "foo.ext[thumb2]" resolved as "http://localhost/media/cache/thumb2/foo.ext" + +# bin/console %command.name% --force --filter=thumb1 foo.ext +Resolve foo.ext using thumb1 and force creation regardless of cache, outputting: + - "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext" + EOF ); } protected function execute(InputInterface $input, OutputInterface $output) { + $force = $input->getOption('force'); $paths = $input->getArgument('paths'); - $filters = $input->getOption('filters'); + $filters = $this->resolveInputFilters($input); + $machine = $input->getOption('as-script'); + $failed = 0; - /* @var FilterManager filterManager */ - $filterManager = $this->getContainer()->get('liip_imagine.filter.manager'); - /* @var CacheManager cacheManager */ - $cacheManager = $this->getContainer()->get('liip_imagine.cache.manager'); - /* @var DataManager dataManager */ - $dataManager = $this->getContainer()->get('liip_imagine.data.manager'); + $filterManager = $this->getFilterManager(); + $dataManager = $this->getDataManager(); + $cacheManager = $this->getCacheManager(); - if (empty($filters)) { + if (0 === count($filters)) { $filters = array_keys($filterManager->getFilterConfiguration()->all()); } + $this->outputTitle($output, $machine); + foreach ($paths as $path) { foreach ($filters as $filter) { - if (!$cacheManager->isStored($path, $filter)) { - $binary = $dataManager->find($filter, $path); - - $cacheManager->store( - $filterManager->applyFilter($binary, $filter), - $path, - $filter - ); - } + $output->write(sprintf('- %s[%s] ', $path, $filter)); + + try { + if ($force || !$cacheManager->isStored($path, $filter)) { + $cacheManager->store($filterManager->applyFilter($dataManager->find($filter, $path), $filter), $path, $filter); + $output->write('resolved: '); + } else { + $output->write('cached: '); + } - $output->writeln($cacheManager->resolve($path, $filter)); + $output->writeln($cacheManager->resolve($path, $filter)); + } catch (\Exception $e) { + $output->writeln(sprintf('failed: %s', $e->getMessage())); + ++$failed; + } } } + + $this->outputSummary($output, $machine, count($filters), count($paths), $failed); + + return 0 === $failed ? 0 : 255; + } + + /** + * @param OutputInterface $output + * @param bool $machine + */ + private function outputTitle(OutputInterface $output, $machine) + { + if (!$machine) { + $title = '[liip/imagine-bundle] Image Resolver'; + + $output->writeln(sprintf('%s', $title)); + $output->writeln(str_repeat('=', strlen($title))); + $output->writeln(''); + } + } + + /** + * @param OutputInterface $output + * @param bool $machine + * @param int $filters + * @param int $paths + * @param int $failed + */ + private function outputSummary(OutputInterface $output, $machine, $filters, $paths, $failed) + { + if (!$machine) { + $operations = ($filters * $paths) - $failed; + + $output->writeln(''); + $output->writeln(vsprintf('Completed %d %s (%d %s on %d %s) %s', array( + $operations, + $this->pluralizeWord($operations, 'operation'), + $filters, + $this->pluralizeWord($filters, 'filter'), + $paths, + $this->pluralizeWord($paths, 'image'), + 0 === $failed ? '' : sprintf('[encountered %d %s]', $failed, $this->pluralizeWord($failed, 'failure')), + ))); + } + } + + /** + * @param int $count + * @param string $singular + * @param string $pluralEnding + * + * @return string + */ + private function pluralizeWord($count, $singular, $pluralEnding = 's') + { + return 1 === $count ? $singular : $singular.$pluralEnding; + } + + /** + * @param InputInterface $input + * + * @return array|mixed + */ + private function resolveInputFilters(InputInterface $input) + { + $filters = $input->getOption('filter'); + + if (count($filtersDeprecated = $input->getOption('filters'))) { + $filters = array_merge($filters, $filtersDeprecated); + @trigger_error('As of 1.9, use of the "--filters" option has been deprecated in favor of "--filter" and will be removed in 2.0.', E_USER_DEPRECATED); + } + + return $filters; + } + + /** + * @return FilterManager + */ + private function getFilterManager() + { + return $this->getContainer()->get('liip_imagine.filter.manager'); + } + + /** + * @return DataManager + */ + private function getDataManager() + { + return $this->getContainer()->get('liip_imagine.data.manager'); + } + + /** + * @return CacheManager + */ + private function getCacheManager() + { + return $this->getContainer()->get('liip_imagine.cache.manager'); } } diff --git a/Tests/Functional/Command/AbstractCommandTestCase.php b/Tests/Functional/Command/AbstractCommandTestCase.php index c620f9df5..35e56d8a6 100644 --- a/Tests/Functional/Command/AbstractCommandTestCase.php +++ b/Tests/Functional/Command/AbstractCommandTestCase.php @@ -22,11 +22,11 @@ class AbstractCommandTestCase extends AbstractSetupWebTestCase /** * @param Command $command * @param array $arguments - * @param array $options + * @param int $return * * @return string */ - protected function executeConsole(Command $command, array $arguments = array(), array $options = array()) + protected function executeConsole(Command $command, array $arguments = array(), &$return = null) { $command->setApplication(new Application($this->createClient()->getKernel())); if ($command instanceof ContainerAwareCommand) { @@ -34,10 +34,9 @@ protected function executeConsole(Command $command, array $arguments = array(), } $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->execute($arguments, array('--env' => 'test')); return $commandTester->getDisplay(); } diff --git a/Tests/Functional/Command/RemoveCacheTest.php b/Tests/Functional/Command/RemoveCacheTest.php index ccf00bbf0..bd98777f2 100644 --- a/Tests/Functional/Command/RemoveCacheTest.php +++ b/Tests/Functional/Command/RemoveCacheTest.php @@ -33,7 +33,7 @@ public function testExecuteSuccessfullyWithEmptyCacheAndOnePathAndOneFilter() new RemoveCacheCommand(), array( 'paths' => array('images/cats.jpeg'), - '--filters' => array('thumbnail_web_path'), + '--filter' => array('thumbnail_web_path'), )); } @@ -53,7 +53,7 @@ public function testExecuteSuccessfullyWithEmptyCacheAndMultipleFilters() $this->executeConsole( new RemoveCacheCommand(), - array('--filters' => array('thumbnail_web_path', 'thumbnail_default')) + array('--filter' => array('thumbnail_web_path', 'thumbnail_default')) ); } @@ -160,7 +160,7 @@ public function testShouldRemoveCacheBySingleFilter() $this->executeConsole( new RemoveCacheCommand(), - array('--filters' => array('thumbnail_default')) + array('--filter' => array('thumbnail_default')) ); $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); @@ -190,7 +190,7 @@ public function testShouldRemoveCacheByMultipleFilters() $this->executeConsole( new RemoveCacheCommand(), - array('--filters' => array('thumbnail_default', 'thumbnail_web_path')) + array('--filter' => array('thumbnail_default', 'thumbnail_web_path')) ); $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); @@ -218,7 +218,7 @@ public function testShouldRemoveCacheByOnePathAndMultipleFilters() new RemoveCacheCommand(), array( 'paths' => array('images/cats.jpeg'), - '--filters' => array('thumbnail_default', 'thumbnail_web_path'), ) + '--filter' => array('thumbnail_default', 'thumbnail_web_path'), ) ); $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); @@ -245,11 +245,25 @@ public function testShouldRemoveCacheByMultiplePathsAndSingleFilter() new RemoveCacheCommand(), array( 'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'), - '--filters' => array('thumbnail_web_path'), ) + '--filter' => 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'); } + + /** + * @group legacy + * @expectedDeprecation As of 1.9, use of the "--filters" option has been deprecated in favor of "--filter" and will be removed in 2.0. + */ + public function testDeprecatedFiltersOption() + { + $this->executeConsole( + new RemoveCacheCommand(), + array( + 'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'), + '--filters' => array('thumbnail_web_path'), ) + ); + } } diff --git a/Tests/Functional/Command/ResolveCacheTest.php b/Tests/Functional/Command/ResolveCacheTest.php index b0437e002..822944812 100644 --- a/Tests/Functional/Command/ResolveCacheTest.php +++ b/Tests/Functional/Command/ResolveCacheTest.php @@ -20,100 +20,350 @@ class ResolveCacheTest extends AbstractCommandTestCase { public function testShouldResolveWithEmptyCache() { - $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $images = array('images/cats.jpeg', 'images/cats2.jpeg'); + $filters = array('thumbnail_web_path'); - $output = $this->executeConsole( - new ResolveCacheCommand(), - array( - 'paths' => array('images/cats.jpeg'), - '--filters' => array('thumbnail_web_path'), ) - ); + $this->assertImagesNotExist($images, $filters); - $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); - $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); + $return = null; + $output = $this->executeResolveCacheCommand($images, $filters, array(), $return); + + $this->assertSame(0, $return); + $this->assertImagesExist($images, $filters); + $this->assertImagesNotExist($images, array('thumbnail_default')); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); } public function testShouldResolveWithCacheExists() { - $this->filesystem->dumpFile( - $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', - 'anImageContent' - ); + $images = array('images/cats.jpeg'); + $filters = array('thumbnail_web_path'); + + $this->putResolvedImages($images, $filters); - $output = $this->executeConsole( - new ResolveCacheCommand(), - array( - 'paths' => array('images/cats.jpeg'), - '--filters' => array('thumbnail_web_path'), ) - ); + $output = $this->executeResolveCacheCommand($images, $filters); - $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); - $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); + $this->assertImagesExist($images, $filters); + $this->assertImagesNotExist($images, array('thumbnail_default')); + $this->assertOutputContainsCachedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); } public function testShouldResolveWithFewPathsAndSingleFilter() { - $output = $this->executeConsole( - new ResolveCacheCommand(), - array( - 'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'), - '--filters' => array('thumbnail_web_path'), ) - ); + $images = array('images/cats.jpeg', 'images/cats2.jpeg'); + $filters = array('thumbnail_web_path'); + + $output = $this->executeResolveCacheCommand($images, $filters); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output); + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + + $output = $this->executeResolveCacheCommand($images, $filters); + + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsCachedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); } public function testShouldResolveWithFewPathsSingleFilterAndPartiallyFullCache() { - $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $imagesResolved = array('images/cats.jpeg'); + $imagesCached = array('images/cats2.jpeg'); + $images = array_merge($imagesResolved, $imagesCached); + $filters = array('thumbnail_web_path'); + + $this->putResolvedImages($imagesCached, $filters); + + $this->assertImagesNotExist($imagesResolved, $filters); + $this->assertImagesExist($imagesCached, $filters); - $this->filesystem->dumpFile( - $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg', - 'anImageContent' - ); + $output = $this->executeResolveCacheCommand($images, $filters); - $output = $this->executeConsole( - new ResolveCacheCommand(), - array( - 'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'), - '--filters' => array('thumbnail_web_path'), ) - ); + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsResolvedImages($output, $imagesResolved, $filters); + $this->assertOutputContainsCachedImages($output, $imagesCached, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); - $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); - $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); - $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg'); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output); + $this->delResolvedImages($images, $filters); } public function testShouldResolveWithFewPathsAndFewFilters() { - $output = $this->executeConsole( - new ResolveCacheCommand(), - array( - 'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'), - '--filters' => array('thumbnail_web_path', 'thumbnail_default'), ) - ); + $images = array('images/cats.jpeg', 'images/cats2.jpeg'); + $filters = array('thumbnail_web_path', 'thumbnail_default'); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output); - $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats.jpeg', $output); - $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats2.jpeg', $output); + $output = $this->executeResolveCacheCommand($images, $filters); + + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); } public function testShouldResolveWithFewPathsAndWithoutFilters() { - $output = $this->executeConsole( - new ResolveCacheCommand(), - array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg')) - ); + $images = array('images/cats.jpeg', 'images/cats2.jpeg'); + $filters = array('thumbnail_web_path', 'thumbnail_default'); + + $output = $this->executeResolveCacheCommand($images); + + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); + } + + public function testCachedAndForceResolve() + { + $images = array('images/cats.jpeg', 'images/cats2.jpeg'); + $filters = array('thumbnail_web_path', 'thumbnail_default'); + + $this->assertImagesNotExist($images, $filters); + $this->putResolvedImages($images, $filters); + $this->assertImagesExist($images, $filters); + + $output = $this->executeResolveCacheCommand($images, $filters); + + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsCachedImages($output, $images, $filters); + + $output = $this->executeResolveCacheCommand($images, $filters, array('--force' => true)); + + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); + } + + public function testFailedResolve() + { + $images = array('images/cats.jpeg', 'images/cats2.jpeg'); + $filters = array('does_not_exist'); + + $this->assertImagesNotExist($images, $filters); + + $return = null; + $output = $this->executeResolveCacheCommand($images, $filters, array(), $return); + + $this->assertSame(255, $return); + $this->assertImagesNotExist($images, $filters); + $this->assertOutputContainsFailedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters, 2); + + $this->delResolvedImages($images, $filters); + } + + public function testMachineOption() + { + $images = array('images/cats.jpeg', 'images/cats2.jpeg'); + $filters = array('does_not_exist'); + + $this->assertImagesNotExist($images, $filters); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); - $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output); - $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats.jpeg', $output); - $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats2.jpeg', $output); + $output = $this->executeResolveCacheCommand($images, $filters, array('--as-script' => true)); + + $this->assertImagesNotExist($images, $filters); + $this->assertNotContains('liip/imagine-bundle (cache resolver)', $output); + $this->assertNotContains('====================================', $output); + $this->assertOutputContainsFailedImages($output, $images, $filters); + $this->assertOutputNotContainsSummary($output, $images, $filters, 2); + + $this->delResolvedImages($images, $filters); + } + + /** + * @group legacy + * @expectedDeprecation As of 1.9, use of the "--filters" option has been deprecated in favor of "--filter" and will be removed in 2.0. + */ + public function testDeprecatedFiltersOption() + { + $images = array('images/cats.jpeg'); + $filters = array('thumbnail_web_path'); + + $this->assertImagesNotExist($images, $filters); + + $output = $this->executeConsole(new ResolveCacheCommand(), array('paths' => $images, '--filters' => $filters)); + + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + + $this->delResolvedImages($images, $filters); + } + + /** + * @param string[] $paths + * @param string[] $filters + * @param string[] $additionalOptions + * @param int $return + * + * @return string + */ + private function executeResolveCacheCommand(array $paths, array $filters = array(), array $additionalOptions = array(), &$return = null) + { + $options = array_merge(array('paths' => $paths), $additionalOptions); + + if (0 < count($filters)) { + $options['--filter'] = $filters; + } + + return $this->executeConsole(new ResolveCacheCommand(), $options, $return); + } + + /** + * @param string[] $images + * @param string[] $filters + */ + private function assertImagesNotExist($images, $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->assertFileNotExists(sprintf('%s/%s/%s', $this->cacheRoot, $f, $i)); + } + } + } + + /** + * @param string[] $images + * @param string[] $filters + */ + private function assertImagesExist($images, $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->assertFileExists(sprintf('%s/%s/%s', $this->cacheRoot, $f, $i)); + } + } + } + + /** + * @param string $output + * @param array $images + * @param array $filters + */ + private function assertOutputContainsResolvedImages($output, array $images, array $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->assertOutputContainsImage($output, $i, $f, 'resolved'); + } + } + } + + /** + * @param string $output + * @param array $images + * @param array $filters + */ + private function assertOutputContainsCachedImages($output, array $images, array $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->assertOutputContainsImage($output, $i, $f, 'cached'); + } + } + } + + /** + * @param string $output + * @param array $images + * @param array $filters + */ + private function assertOutputContainsFailedImages($output, array $images, array $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->assertContains(sprintf('%s[%s] failed: ', $i, $f), $output); + } + } + } + + /** + * @param string $output + * @param string $image + * @param string $filter + * @param string $type + */ + private function assertOutputContainsImage($output, $image, $filter, $type) + { + $expected = vsprintf('%s[%s] %s: http://localhost/media/cache/%s/%s', array( + $image, + $filter, + $type, + $filter, + $image, + )); + $this->assertContains($expected, $output); + } + + /** + * @param string $output + * @param string[] $images + * @param string[] $filters + * @param int $failures + */ + private function assertOutputContainsSummary($output, array $images, array $filters, $failures = 0) + { + $this->assertContains(sprintf('Completed %d operation', (count($images) * count($filters)) - $failures), $output); + $this->assertContains(sprintf('%d image', count($images)), $output); + $this->assertContains(sprintf('%d filter', count($filters)), $output); + if (0 !== $failures) { + $this->assertContains(sprintf('%d failure', $failures), $output); + } + } + + /** + * @param string $output + * @param string[] $images + * @param string[] $filters + * @param int $failures + */ + private function assertOutputNotContainsSummary($output, array $images, array $filters, $failures = 0) + { + $this->assertNotContains(sprintf('Completed %d operation', (count($images) * count($filters)) - $failures), $output); + $this->assertNotContains(sprintf('%d image', count($images)), $output); + $this->assertNotContains(sprintf('%d filter', count($filters)), $output); + if (0 !== $failures) { + $this->assertNotContains(sprintf('%d failure', $failures), $output); + } + } + + /** + * @param string[] $images + * @param string[] $filters + */ + private function delResolvedImages(array $images, array $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + if (file_exists($f = sprintf('%s/%s/%s', $this->cacheRoot, $f, $i))) { + @unlink($f); + } + } + } + } + + /** + * @param string[] $images + * @param string[] $filters + * @param string $content + */ + private function putResolvedImages(array $images, array $filters, $content = 'anImageContent') + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->filesystem->dumpFile(sprintf('%s/%s/%s', $this->cacheRoot, $f, $i), $content); + } + } } }