diff --git a/Binary/Loader/FileSystemLoader.php b/Binary/Loader/FileSystemLoader.php index ef7104b0f..529c26f47 100644 --- a/Binary/Loader/FileSystemLoader.php +++ b/Binary/Loader/FileSystemLoader.php @@ -42,7 +42,7 @@ class FileSystemLoader implements LoaderInterface * * @param MimeTypeGuesserInterface $mimeGuesser * @param ExtensionGuesserInterface $extensionGuesser - * @param LocatorInterface + * @param LocatorInterface $locator */ public function __construct(MimeTypeGuesserInterface $mimeGuesser, ExtensionGuesserInterface $extensionGuesser, $locator) { diff --git a/CHANGELOG.md b/CHANGELOG.md index 529132f9d..3c1d47a3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,11 +10,18 @@ This project adheres to [semantic versioning](http://semver.org/spec/v2.0.0.html ## [Unreleased](https://github.com/liip/LiipImagineBundle/tree/HEAD) *Note: Recent developments can be tracked via the -[latest changelog](https://github.com/liip/LiipImagineBundle/compare/1.9.0...HEAD), the -[active milestone](https://github.com/liip/LiipImagineBundle/milestone/15), as well as all +[latest changelog](https://github.com/liip/LiipImagineBundle/compare/1.9.1...HEAD), the +[active milestone](https://github.com/liip/LiipImagineBundle/milestone/16), as well as all [open milestones](https://github.com/liip/LiipImagineBundle/milestones).* +## [v1.9.1](https://github.com/liip/LiipImagineBundle/tree/1.9.1) + +*Released on* 2017-09-08 *and assigned* [`1.9.1`](https://github.com/liip/LiipImagineBundle/releases/tag/1.9.1) *tag \([view verbose changelog](https://github.com/liip/LiipImagineBundle/compare/1.9.0...1.9.1)\).* + +- __\[Console\]__ __\[BC BREAK\]__ The resolve command's --as-script/-s option/shortcut renamed to --machine-readable/-m \(fixes [\#988](https://github.com/liip/LiipImagineBundle/pull/988)\), its output updated to aligned with the resolve command, and the "--machine-readable/-m" option added. [\#991](https://github.com/liip/LiipImagineBundle/pull/991) *([robfrawley](https://github.com/robfrawley))* + + ## [v1.9.0](https://github.com/liip/LiipImagineBundle/tree/1.9.0) *Released on* 2017-08-30 *and assigned* [`1.9.0`](https://github.com/liip/LiipImagineBundle/releases/tag/1.9.0) *tag \([view verbose changelog](https://github.com/liip/LiipImagineBundle/compare/1.8.0...1.9.0)\).* diff --git a/Command/AbstractCacheCommand.php b/Command/AbstractCacheCommand.php new file mode 100644 index 000000000..557892eb6 --- /dev/null +++ b/Command/AbstractCacheCommand.php @@ -0,0 +1,242 @@ +output = $output; + $this->machineReadable = $input->getOption('machine-readable'); + $this->actionFailures = 0; + } + + /** + * @param InputInterface $input + * + * @return array + */ + protected function resolveFilters(InputInterface $input) + { + $filters = $input->getOption('filter'); + + if (0 !== count($deprecated = $input->getOption('filters'))) { + $filters = array_merge($filters, $deprecated); + @trigger_error('The --filters option was deprecated in 1.9.0 and removed in 2.0.0. Use the --filter option instead.', E_USER_DEPRECATED); + } + + if (0 === count($filters) && 0 === count($filters = array_keys($this->getFilterManager()->getFilterConfiguration()->all()))) { + $this->output->writeln(' [ERROR] You do not have any configured filters available. '); + } + + return $filters; + } + + /** + * @param string $command + */ + protected function writeCommandHeading($command) + { + if ($this->machineReadable) { + return; + } + + $title = sprintf('[liip/imagine-bundle] %s Image Caches', ucfirst($command)); + $this->writeNewline(); + $this->output->writeln(sprintf('%s', $title)); + $this->output->writeln(str_repeat('=', strlen($title))); + $this->writeNewline(); + } + + /** + * @param string[] $filters + * @param string[] $targets + * @param bool $glob + */ + protected function writeResultSummary(array $filters, array $targets, $glob = false) + { + if ($this->machineReadable) { + return; + } + + $targetCount = count($targets); + $filterCount = count($filters); + $actionCount = ($glob ? $filterCount : ($filterCount * $targetCount)) - $this->actionFailures; + + $this->writeNewline(); + $this->output->writeln(vsprintf(' Completed %d %s (%d %s / %s %s) %s', array( + $actionCount, + $this->getPluralized($actionCount, 'operation'), + $filterCount, + $this->getPluralized($filterCount, 'filter'), + $glob ? '?' : $targetCount, + $this->getPluralized($targetCount, 'image'), + $this->getResultSummaryFailureMarkup(), + ))); + $this->writeNewline(); + } + + /** + * @param string $filter + * @param string|null $target + */ + protected function writeActionStart($filter, $target = null) + { + if (!$this->machineReadable) { + $this->output->write(' - '); + } + + $this->output->write(sprintf('%s[%s] ', $target ?: '*', $filter)); + } + + /** + * @param string $result + * @param bool $continued + */ + protected function writeActionResult($result, $continued = true) + { + $this->output->write($continued ? sprintf('%s: ', $result) : $result); + + if (!$continued) { + $this->writeNewline(); + } + } + + /** + * @param string $detail + */ + protected function writeActionDetail($detail) + { + $this->output->write($detail); + $this->writeNewline(); + } + + /** + * @param \Exception $exception + */ + protected function writeActionException(\Exception $exception) + { + $this->writeActionResult('failure'); + $this->writeActionDetail($exception->getMessage()); + ++$this->actionFailures; + } + + /** + * @return int + */ + protected function getReturnCode() + { + return 0 === $this->actionFailures ? 0 : 255; + } + + /** + * @return CacheManager + */ + protected function getCacheManager() + { + static $manager; + + if (null === $manager) { + $manager = $this->getContainer()->get('liip_imagine.cache.manager'); + } + + return $manager; + } + + /** + * @return FilterManager + */ + protected function getFilterManager() + { + static $manager; + + if (null === $manager) { + $manager = $this->getContainer()->get('liip_imagine.filter.manager'); + } + + return $manager; + } + + /** + * @return DataManager + */ + protected function getDataManager() + { + static $manager; + + if (null === $manager) { + $manager = $this->getContainer()->get('liip_imagine.data.manager'); + } + + return $manager; + } + + /** + * @param int $count + */ + private function writeNewline($count = 1) + { + $this->output->write(str_repeat(PHP_EOL, $count)); + } + + /** + * @param int $size + * @param string $word + * + * @return string + */ + private function getPluralized($size, $word) + { + return 1 === $size ? $word : sprintf('%ss', $word); + } + + /** + * @return string + */ + private function getResultSummaryFailureMarkup() + { + if (0 === $this->actionFailures) { + return ''; + } + + return vsprintf(' encountered %s %s ', array( + $this->actionFailures, + $this->getPluralized($this->actionFailures, 'failure'), + )); + } +} diff --git a/Command/RemoveCacheCommand.php b/Command/RemoveCacheCommand.php index aa8d53514..ab6e33376 100644 --- a/Command/RemoveCacheCommand.php +++ b/Command/RemoveCacheCommand.php @@ -11,41 +11,62 @@ namespace Liip\ImagineBundle\Command; -use Liip\ImagineBundle\Imagine\Cache\CacheManager; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -class RemoveCacheCommand extends ContainerAwareCommand +class RemoveCacheCommand extends AbstractCacheCommand { protected function configure() { $this ->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', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, - 'List of filters to remove for passed images (Deprecated, use "filter").') + ->setDescription('Remove asset caches for the passed asset paths(s) and filter name(s)') + ->addArgument('paths', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, + 'Asset paths to remove caches of (passing none will use all paths).') ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, - 'List of filters to remove for passed images.') + 'Filter name to remove caches of (passing none will use all registered filters)') + ->addOption('machine-readable', 'm', InputOption::VALUE_NONE, + 'Enable machine parseable output (removing extraneous output and all text styles)') + ->addOption('filters', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'Deprecated in 1.9.0 and removed in 2.0.0 (use the --filter option instead)') ->setHelp(<<<'EOF' -The %command.name% command removes cache by specified parameters. +The %command.name% command removes asset cache for the passed image(s) and filter(s). + +For an application that has only the two files "foo.ext" and "bar.ext", and only the filter sets +named "thumb_sm" and "thumb_lg", the following examples will behave as shown. + +# bin/console %command.name% foo.ext +Removes caches for foo.ext using all configured filters, outputting: + - foo.ext[thumb_sm] removed + - foo.ext[thumb_lg] removed + +# bin/console %command.name% --filter=thumb_sm --filter=thumb_lg foo.ext bar.ext +Removes caches for both foo.ext and bar.ext using thumb_sm and thumb_lg filters, outputting: + - foo.ext[thumb_sm] removed + - foo.ext[thumb_lg] removed + - bar.ext[thumb_sm] removed + - bar.ext[thumb_lg] removed + +# bin/console %command.name% --filter=thumb_sm +Removes all caches for thumb_sm filter, outputting: + - *[thumb_sm] glob-removal -Paths should be separated by spaces: -php app/console %command.name% path1 path2 -All cache for a given `paths` will be lost. +# bin/console %command.name% +Removes all caches for all configured filters, removing all cached assets, outputting: + - *[thumb_sm] glob-removal + - *[thumb_lg] glob-removal -If you use --filter parameter: -php app/console %command.name% --filter=thumb1 --filter=thumb2 -All cache for a given filters will be lost. +# bin/console %command.name% --force --filter=thumb_sm foo.ext +Removing caches for foo.ext using thumb_sm filter when already removed will caused skipping, outputting: + - foo.ext[thumb_sm] skipped -You can combine these parameters: -php app/console %command.name% path1 path2 --filter=thumb1 --filter=thumb2 +# bin/console %command.name% --filter=does_not_exist --filter=thumb_sm foo.ext +Removes caches for foo.ext for thumb_sm while failing inline on invalid filter (or other errors), outputting: + - foo.ext[does_not_exist] failure: Could not find configuration for a filter: does_not_exist + - foo.ext[thumb_sm] removed -php app/console %command.name% -Cache for all paths and filters will be lost when executing this command without parameters. EOF ); } @@ -58,34 +79,68 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $paths = $input->getArgument('paths'); - $filters = $this->resolveInputFilters($input); + $this->initializeInstState($input, $output); + $this->writeCommandHeading('remove'); - if (empty($filters)) { - $filters = null; - } + $filters = $this->resolveFilters($input); + $targets = $input->getArgument('paths'); - /* @var CacheManager cacheManager */ - $cacheManager = $this->getContainer()->get('liip_imagine.cache.manager'); - $cacheManager->remove($paths, $filters); + if (0 === count($targets)) { + $this->doCacheRemoveAsGlobbedFilterName($filters); + } else { + $this->doCacheRemoveAsFiltersAndTargets($filters, $targets); + } - return 0; + return $this->getReturnCode(); } /** - * @param InputInterface $input - * - * @return array|mixed + * @param string[] $filters */ - private function resolveInputFilters(InputInterface $input) + private function doCacheRemoveAsGlobbedFilterName(array $filters) { - $filters = $input->getOption('filter'); + foreach ($filters as $f) { + $this->doCacheRemove($f); + } + + $this->writeResultSummary($filters, array(), true); + } - 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); + /** + * @param string[] $filters + * @param string[] $targets + */ + private function doCacheRemoveAsFiltersAndTargets(array $filters, array $targets) + { + foreach ($targets as $t) { + foreach ($filters as $f) { + $this->doCacheRemove($f, $t); + } } - return $filters; + $this->writeResultSummary($filters, $targets); + } + + /** + * @param string $filter + * @param string|null $target + */ + private function doCacheRemove($filter, $target = null) + { + $this->writeActionStart($filter, $target); + + try { + if (null === $target) { + $this->getCacheManager()->remove(null, $filter); + $this->writeActionResult('glob-removal', false); + } elseif ($this->getCacheManager()->isStored($target, $filter)) { + $this->getCacheManager()->remove($target, $filter); + $this->writeActionResult('removed', false); + } else { + $this->writeActionResult('skipped', false); + } + } catch (\Exception $e) { + $this->writeActionException($e); + } } } diff --git a/Command/ResolveCacheCommand.php b/Command/ResolveCacheCommand.php index 0b3209cb3..df9d3cdf7 100644 --- a/Command/ResolveCacheCommand.php +++ b/Command/ResolveCacheCommand.php @@ -11,193 +11,113 @@ namespace Liip\ImagineBundle\Command; -use Liip\ImagineBundle\Imagine\Cache\CacheManager; -use Liip\ImagineBundle\Imagine\Data\DataManager; -use Liip\ImagineBundle\Imagine\Filter\FilterManager; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -class ResolveCacheCommand extends ContainerAwareCommand +class ResolveCacheCommand extends AbstractCacheCommand { protected function configure() { $this ->setName('liip:imagine:cache:resolve') - ->setDescription('Resolve cache for given path and set of filters.') + ->setDescription('Resolves asset caches for the passed asset paths(s) and filter set name(s)') ->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").') + 'Asset paths to resolve caches for') ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, - 'List of filters to apply to passed images.') + 'Filter name to resolve caches for (passing none will use all registered filters)') ->addOption('force', 'F', InputOption::VALUE_NONE, - 'Force image resolution regardless of cache.') - ->addOption('as-script', 's', InputOption::VALUE_NONE, - 'Only print machine-parseable results.') + 'Force asset cache resolution (ignoring whether it already cached)') + ->addOption('machine-readable', 'm', InputOption::VALUE_NONE, + 'Enable machine parseable output (removing extraneous output and all text styles)') + ->addOption('filters', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'Deprecated in 1.9.0 and removed in 2.0.0 (use the --filter option instead)') ->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" +The %command.name% command resolves asset cache for the passed image(s) and filter(s). -# 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" +For an application that has only the two files "foo.ext" and "bar.ext", and only the filter sets +named "thumb_sm" and "thumb_lg", the following examples will behave as shown. -# 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% foo.ext bar.ext +Resolves caches for both foo.ext and bar.ext using all configured filters, outputting: + - foo.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/foo.ext + - bar.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/bar.ext + - foo.ext[thumb_lg] resolved: http://localhost/media/cache/thumb_lg/foo.ext + - bar.ext[thumb_lg] resolved: http://localhost/media/cache/thumb_lg/bar.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 = $this->resolveInputFilters($input); - $machine = $input->getOption('as-script'); - $failed = 0; - - $filterManager = $this->getFilterManager(); - $dataManager = $this->getDataManager(); - $cacheManager = $this->getCacheManager(); - - if (0 === count($filters)) { - $filters = array_keys($filterManager->getFilterConfiguration()->all()); - } +# bin/console %command.name% --filter=thumb_sm foo.ext +Resolves caches for foo.ext using only thumb_sm filter, outputting: + - foo.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/foo.ext - $this->outputTitle($output, $machine); - - foreach ($paths as $path) { - foreach ($filters as $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)); - } catch (\Exception $e) { - $output->writeln(sprintf('failed: %s', $e->getMessage())); - ++$failed; - } - } - } +# bin/console %command.name% --filter=thumb_sm --filter=thumb_lg foo.ext +Resolves caches for foo.ext using both thumb_sm and thumb_lg filters, outputting: + - foo.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/foo.ext + - foo.ext[thumb_lg] resolved: http://localhost/media/cache/thumb_lg/foo.ext - $this->outputSummary($output, $machine, count($filters), count($paths), $failed); +# bin/console %command.name% --force --filter=thumb_sm foo.ext +Resolving caches for foo.ext using thumb_sm filter when already cached will caused skipped, outputting: + - foo.ext[thumb_sm] skipped: http://localhost/media/cache/thumb_sm/foo.ext - return 0 === $failed ? 0 : 255; - } +# bin/console %command.name% --force --filter=thumb_sm foo.ext +Resolving caches for foo.ext using thumb_sm filter when already cached with force option re-resolves (ignoring cache), outputting: + - foo.ext[thumb_sm] resolved: http://localhost/media/cache/thumb_sm/foo.ext - /** - * @param OutputInterface $output - * @param bool $machine - */ - private function outputTitle(OutputInterface $output, $machine) - { - if (!$machine) { - $title = '[liip/imagine-bundle] Image Resolver'; +# bin/console %command.name% --filter=does_not_exist --filter=thumb_sm foo.ext +Resolves caches for foo.ext using thumb_sm while failing inline on invalid filter (or other errors), outputting: + - foo.ext[does_not_exist] failed: Could not find configuration for a filter: does_not_exist + - foo.ext[thumb_sm] removed: http://localhost/media/cache/thumb_sm/foo.ext - $output->writeln(sprintf('%s', $title)); - $output->writeln(str_repeat('=', strlen($title))); - $output->writeln(''); - } +EOF + ); } /** + * @param InputInterface $input * @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 + * @return int */ - private function pluralizeWord($count, $singular, $pluralEnding = 's') + protected function execute(InputInterface $input, OutputInterface $output) { - return 1 === $count ? $singular : $singular.$pluralEnding; - } + $this->initializeInstState($input, $output); + $this->writeCommandHeading('resolve'); - /** - * @param InputInterface $input - * - * @return array|mixed - */ - private function resolveInputFilters(InputInterface $input) - { - $filters = $input->getOption('filter'); + $filters = $this->resolveFilters($input); + $targets = $input->getArgument('paths'); + $doForce = $input->getOption('force'); - 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); + foreach ($targets as $t) { + foreach ($filters as $f) { + $this->doCacheResolve($t, $f, $doForce); + } } - return $filters; - } + $this->writeResultSummary($filters, $targets); - /** - * @return FilterManager - */ - private function getFilterManager() - { - return $this->getContainer()->get('liip_imagine.filter.manager'); + return $this->getReturnCode(); } /** - * @return DataManager + * @param string $target + * @param string $filter + * @param bool $forced */ - private function getDataManager() + private function doCacheResolve($target, $filter, $forced) { - return $this->getContainer()->get('liip_imagine.data.manager'); - } + $this->writeActionStart($filter, $target); + + try { + if ($forced || !$this->getCacheManager()->isStored($target, $filter)) { + $this->getCacheManager()->store($this->getFilterManager()->applyFilter($this->getDataManager()->find($filter, $target), $filter), $target, $filter); + $this->writeActionResult('resolved'); + } else { + $this->writeActionResult('skipped'); + } - /** - * @return CacheManager - */ - private function getCacheManager() - { - return $this->getContainer()->get('liip_imagine.cache.manager'); + $this->writeActionDetail($this->getCacheManager()->resolve($target, $filter)); + } catch (\Exception $e) { + $this->writeActionException($e); + } } } diff --git a/Imagine/Filter/Loader/ResampleFilterLoader.php b/Imagine/Filter/Loader/ResampleFilterLoader.php index 0c91d5579..b19fc5f03 100644 --- a/Imagine/Filter/Loader/ResampleFilterLoader.php +++ b/Imagine/Filter/Loader/ResampleFilterLoader.php @@ -128,7 +128,7 @@ private function resolveOptions(array $options) $resolver->setAllowedValues('unit', array( ImageInterface::RESOLUTION_PIXELSPERINCH, - ImageInterface::RESOLUTION_PIXELSPERCENTIMETER + ImageInterface::RESOLUTION_PIXELSPERCENTIMETER, )); $resolver->setNormalizer('filter', function (Options $options, $value) { diff --git a/Tests/DependencyInjection/LiipImagineExtensionTest.php b/Tests/DependencyInjection/LiipImagineExtensionTest.php index 9d7034d1c..f12b8cc2c 100644 --- a/Tests/DependencyInjection/LiipImagineExtensionTest.php +++ b/Tests/DependencyInjection/LiipImagineExtensionTest.php @@ -57,7 +57,7 @@ public function testLoadWithDefaults() new Reference('liip_imagine.cache.manager'), new Reference('liip_imagine.cache.signer'), new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE), - '%liip_imagine.controller.redirect_response_code%' + '%liip_imagine.controller.redirect_response_code%', ) ); } diff --git a/Tests/Functional/Binary/Loader/ChainLoaderTest.php b/Tests/Functional/Binary/Loader/ChainLoaderTest.php index f95611823..2e2813a6f 100644 --- a/Tests/Functional/Binary/Loader/ChainLoaderTest.php +++ b/Tests/Functional/Binary/Loader/ChainLoaderTest.php @@ -33,7 +33,7 @@ public function testFind() { static::createClient(); - $loader = $this->getLoader('baz'); + $loader = $this->getLoader('default'); foreach (array('images/cats.jpeg', 'images/cats2.jpeg', 'file.ext', 'bar-bundle-file.ext', 'foo-bundle-file.ext') as $file) { $this->assertNotNull($loader->find($file)); diff --git a/Tests/Functional/Command/AbstractCacheCommandTestCase.php b/Tests/Functional/Command/AbstractCacheCommandTestCase.php new file mode 100644 index 000000000..5fc824958 --- /dev/null +++ b/Tests/Functional/Command/AbstractCacheCommandTestCase.php @@ -0,0 +1,263 @@ +setApplication(new Application($this->createClient()->getKernel())); + if ($command instanceof ContainerAwareCommand) { + $command->setContainer($this->createClient()->getContainer()); + } + + $arguments = array_replace(array('command' => $command->getName()), $arguments); + + $commandTester = new CommandTester($command); + $return = $commandTester->execute($arguments, array('--env' => 'test')); + + return $commandTester->getDisplay(); + } + + /** + * @param string[] $images + * @param string[] $filters + */ + protected 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 + */ + protected 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 + */ + protected 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 + */ + protected function assertOutputContainsRemovedImages($output, array $images, array $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->assertOutputContainsImageShort($output, $i, $f, 'removed'); + } + } + } + + /** + * @param string $output + * @param array $filters + */ + protected function assertOutputContainsRemovedGlob($output, array $filters) + { + foreach ($filters as $f) { + $this->assertOutputContainsImageShort($output, '*', $f, 'glob-removal'); + } + } + + /** + * @param string $output + * @param array $images + * @param array $filters + */ + protected function assertOutputContainsSkippedImages($output, array $images, array $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->assertOutputContainsImage($output, $i, $f, 'skipped'); + } + } + } + + /** + * @param string $output + * @param array $images + * @param array $filters + */ + protected function assertOutputContainsSkippedImagesShort($output, array $images, array $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->assertOutputContainsImageShort($output, $i, $f, 'skipped'); + } + } + } + + /** + * @param string $output + * @param array $images + * @param array $filters + */ + protected function assertOutputContainsFailedImages($output, array $images, array $filters) + { + foreach ($images as $i) { + foreach ($filters as $f) { + $this->assertContains(sprintf('%s[%s] failure: ', $i, $f), $output); + } + } + } + + /** + * @param string $output + * @param string $image + * @param string $filter + * @param string $type + */ + protected 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 $image + * @param string $filter + * @param string $type + */ + protected function assertOutputContainsImageShort($output, $image, $filter, $type) + { + $expected = vsprintf('%s[%s] %s', array( + $image, + $filter, + $type, + )); + $this->assertContains($expected, $output); + } + + /** + * @param string $output + * @param string[] $images + * @param string[] $filters + * @param int $failures + */ + protected 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[] $filters + * @param int $failures + */ + protected function assertOutputContainsSummaryGlob($output, array $filters, $failures = 0) + { + $this->assertContains(sprintf('Completed %d operation', count($filters) - $failures), $output); + $this->assertContains('? 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 + */ + protected 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 + */ + protected 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 + */ + protected function putResolvedImages(array $images, array $filters, $content = 'anImageContent') + { + $this->delResolvedImages($images, $filters); + + foreach ($images as $i) { + foreach ($filters as $f) { + $this->filesystem->dumpFile(sprintf('%s/%s/%s', $this->cacheRoot, $f, $i), $content); + } + } + } +} diff --git a/Tests/Functional/Command/AbstractCommandTestCase.php b/Tests/Functional/Command/AbstractCommandTestCase.php deleted file mode 100644 index 35e56d8a6..000000000 --- a/Tests/Functional/Command/AbstractCommandTestCase.php +++ /dev/null @@ -1,43 +0,0 @@ -setApplication(new Application($this->createClient()->getKernel())); - if ($command instanceof ContainerAwareCommand) { - $command->setContainer($this->createClient()->getContainer()); - } - - $arguments = array_replace(array('command' => $command->getName()), $arguments); - - $commandTester = new CommandTester($command); - $return = $commandTester->execute($arguments, array('--env' => 'test')); - - return $commandTester->getDisplay(); - } -} diff --git a/Tests/Functional/Command/RemoveCacheCommandTest.php b/Tests/Functional/Command/RemoveCacheCommandTest.php new file mode 100644 index 000000000..73945b35f --- /dev/null +++ b/Tests/Functional/Command/RemoveCacheCommandTest.php @@ -0,0 +1,340 @@ +putResolvedImages($images, $filters); + $this->assertImagesExist($images, $filters); + + $output = $this->executeRemoveCacheCommand(array(), array()); + + $this->assertImagesNotExist($images, $allFilters); + $this->assertOutputContainsRemovedGlob($output, $allFilters); + $this->assertOutputContainsSummaryGlob($output, $allFilters); + + $this->delResolvedImages($images, $allFilters); + } + + /** + * @return array + */ + public static function provideRemovesWhenPassedPathsAndFiltersData() + { + return CacheCommandFixtures::getAvailableFilterAndImageCombinations(); + } + + /** + * @dataProvider provideRemovesWhenPassedPathsAndFiltersData + * + * @param array $images + * @param array $filters + */ + public function testRemovesWhenPassedPathsAndFilters(array $images, array $filters) + { + $this->putResolvedImages($images, $filters); + $this->assertImagesExist($images, $filters); + + $output = $this->executeRemoveCacheCommand($images, $filters); + + $this->assertImagesNotExist($images, $filters); + $this->assertOutputContainsRemovedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); + } + + /** + * @return array + */ + public static function provideRemovesWhenPassedOnlyImagesData() + { + return static::provideRemovesCachesWithoutPathsOrFiltersData(); + } + + /** + * @dataProvider provideRemovesWhenPassedOnlyImagesData + * + * @param array $images + * @param array $filters + * @param array $allFilters + */ + public function testRemovesWhenPassedOnlyImages(array $images, array $filters, array $allFilters) + { + $this->putResolvedImages($images, $filters); + $this->assertImagesExist($images, $filters); + + $output = $this->executeRemoveCacheCommand($images, array()); + + $this->assertImagesNotExist($images, $filters); + $this->assertOutputContainsRemovedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $allFilters); + + if (count($allFilters) !== count($filters)) { + $this->assertOutputContainsSkippedImagesShort($output, $images, array_diff($allFilters, $filters)); + } + + $this->delResolvedImages($images, $allFilters); + } + + /** + * @return array + */ + public static function provideRemovesWhenPassedOnlyFiltersData() + { + return array_map(function (array $entry) { + $entry[] = CacheCommandFixtures::getValidImages(); + + return $entry; + }, CacheCommandFixtures::getAvailableFilterAndImageCombinations()); + } + + /** + * @dataProvider provideRemovesWhenPassedOnlyFiltersData + * + * @param array $images + * @param array $filters + * @param array $allImages + */ + public function testRemovesWhenPassedOnlyFilters(array $images, array $filters, array $allImages) + { + $this->putResolvedImages($allImages, $filters); + $this->assertImagesExist($allImages, $filters); + + $output = $this->executeRemoveCacheCommand(array(), $filters); + + $this->assertImagesNotExist($allImages, $filters); + $this->assertOutputContainsRemovedGlob($output, $filters); + $this->assertOutputContainsSummaryGlob($output, $filters); + + $this->delResolvedImages($allImages, $filters); + } + + /** + * @return array + */ + public static function provideRemoveSkipsWhenCacheDoesNotExistData() + { + $data = array_map(function (array $entry) { + $entry[] = CacheCommandFixtures::getImagesNotInArray($entry[0]); + + return $entry; + }, CacheCommandFixtures::getAvailableFilterAndImageCombinations()); + + return array_filter($data, function (array $entry) { + return null !== $entry[2]; + }); + } + + /** + * @dataProvider provideRemoveSkipsWhenCacheDoesNotExistData + * + * @param array $images + * @param array $filters + * @param array $existingImages + */ + public function testRemoveSkipsWhenCacheDoesNotExist(array $images, array $filters, array $existingImages) + { + $this->putResolvedImages($existingImages, $filters); + $this->assertImagesExist($existingImages, $filters); + $this->assertImagesNotExist($images, $filters); + + $output = $this->executeRemoveCacheCommand($images, $filters); + + $this->assertImagesExist($existingImages, $filters); + $this->assertImagesNotExist($images, $filters); + $this->assertOutputContainsSkippedImagesShort($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages(array_merge($images, $existingImages), $filters); + } + + /** + * @return array + */ + public static function provideRemoveShowsFailureAndContinuesWhenPassedInvalidFiltersData() + { + return array_map(function (array $entry) { + $entry[] = CacheCommandFixtures::getInvalidFilters(); + + return $entry; + }, CacheCommandFixtures::getAvailableFilterAndImageCombinations()); + } + + /** + * @dataProvider provideRemoveShowsFailureAndContinuesWhenPassedInvalidFiltersData + * + * @param array $images + * @param array $filters + * @param array $invalidFilters + */ + public function testRemoveShowsFailureAndContinuesWhenPassedInvalidFilters(array $images, array $filters, array $invalidFilters) + { + $allFilters = array_merge($filters, $invalidFilters); + + $this->putResolvedImages($images, $allFilters); + $this->assertImagesExist($images, $allFilters); + + $return = null; + $output = $this->executeRemoveCacheCommand($images, $allFilters, array(), $return); + + $this->assertSame(255, $return); + $this->assertImagesNotExist($images, $filters); + $this->assertImagesExist($images, $invalidFilters); + $this->assertOutputContainsRemovedImages($output, $images, $filters); + $this->assertOutputContainsFailedImages($output, $images, $invalidFilters); + $this->assertOutputContainsSummary($output, $images, $allFilters, count($images) * count($invalidFilters)); + + $this->delResolvedImages($images, $allFilters); + } + + /** + * @return array + */ + public static function provideRemoveShowsFailureAndContinuesWhenPassedInvalidPathsData() + { + return array_map(function (array $entry) { + $entry[] = CacheCommandFixtures::getInvalidImages(); + + return $entry; + }, CacheCommandFixtures::getAvailableFilterAndImageCombinations()); + } + + /** + * @dataProvider provideRemoveShowsFailureAndContinuesWhenPassedInvalidPathsData + * + * @param array $images + * @param array $filters + * @param array $invalidImages + */ + public function testRemoveShowsFailureAndContinuesWhenPassedInvalidPaths(array $images, array $filters, array $invalidImages) + { + $allImages = array_merge($images, $invalidImages); + + $this->putResolvedImages($images, $filters); + $this->assertImagesExist($images, $filters); + $this->assertImagesNotExist($invalidImages, $filters); + + $output = $this->executeRemoveCacheCommand($allImages, $filters); + + $this->assertImagesNotExist($allImages, $filters); + $this->assertOutputContainsRemovedImages($output, $images, $filters); + $this->assertOutputContainsSkippedImagesShort($output, $invalidImages, $filters); + $this->assertOutputContainsSummary($output, $allImages, $filters); + + $this->delResolvedImages($allImages, $filters); + } + + /** + * @return array + */ + public static function provideRemoveOutputsMachineParseableTextWhenPassedMachineReadableOptionData() + { + return CacheCommandFixtures::getAvailableFilterAndImageCombinations(); + } + + /** + * @dataProvider provideRemoveOutputsMachineParseableTextWhenPassedMachineReadableOptionData + * + * @param array $images + * @param array $filters + */ + public function testRemoveOutputsMachineParseableTextWhenPassedMachineReadableOption(array $images, array $filters) + { + $this->putResolvedImages($images, $filters); + $this->assertImagesExist($images, $filters); + + $output = $this->executeRemoveCacheCommand($images, $filters, array('--machine-readable' => true)); + + $this->assertImagesNotExist($images, $filters); + $this->assertNotContains('[liip/imagine-bundle]', $output); + $this->assertNotContains('=====================', $output); + $this->assertOutputContainsRemovedImages($output, $images, $filters); + $this->assertOutputNotContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); + } + + /** + * @return array + */ + public static function provideRemoveEmitsDeprecationMessageWhenUsingLegacyFiltersOptionData() + { + return CacheCommandFixtures::getAvailableFilterAndImageCombinations(); + } + + /** + * @dataProvider provideRemoveEmitsDeprecationMessageWhenUsingLegacyFiltersOptionData + * + * @group legacy + * @expectedDeprecation The --filters option was deprecated in 1.9.0 and removed in 2.0.0. Use the --filter option instead. + */ + public function testRemoveEmitsDeprecationMessageWhenUsingLegacyFiltersOption(array $images, array $filters) + { + $this->putResolvedImages($images, $filters); + $this->assertImagesExist($images, $filters); + + $output = $this->executeRemoveCacheCommand($images, array(), array('paths' => $images, '--filters' => $filters)); + + $this->assertImagesNotExist($images, $filters); + $this->assertOutputContainsRemovedImages($output, $images, $filters); + + $this->delResolvedImages($images, $filters); + } + + /** + * @param string[] $paths + * @param string[] $filters + * @param string[] $additionalOptions + * @param int $return + * + * @return string + */ + private function executeRemoveCacheCommand(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 RemoveCacheCommand(), $options, $return); + } +} diff --git a/Tests/Functional/Command/RemoveCacheTest.php b/Tests/Functional/Command/RemoveCacheTest.php deleted file mode 100644 index bd98777f2..000000000 --- a/Tests/Functional/Command/RemoveCacheTest.php +++ /dev/null @@ -1,269 +0,0 @@ -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'), - '--filter' => 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('--filter' => 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('--filter' => 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('--filter' => 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'), - '--filter' => 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'), - '--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/ResolveCacheCommandTest.php b/Tests/Functional/Command/ResolveCacheCommandTest.php new file mode 100644 index 000000000..a9b87dfd5 --- /dev/null +++ b/Tests/Functional/Command/ResolveCacheCommandTest.php @@ -0,0 +1,306 @@ +putResolvedImages($images, $filters); + + $output = $this->executeResolveCacheCommand($images, $filters); + + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsSkippedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); + } + + /** + * @return array + */ + public static function provideResolvesWhenPassedOnlyImagesData() + { + return CacheCommandFixtures::getAvailableFilterAndImageCombinations(); + } + + /** + * @param array $images + * @param array $filters + * + * @dataProvider provideResolvedWhenPassedPathsAndFiltersData + */ + public function testResolvesWhenPassedOnlyImages(array $images, array $filters) + { + $this->putResolvedImages($images, $filters); + + $output = $this->executeResolveCacheCommand($images, $filters); + + $this->assertImagesExist($images, $filters); + $this->assertOutputContainsSkippedImages($output, $images, $filters); + $this->assertOutputContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); + } + + /** + * @return array + */ + public static function provideResolvesWhenPassedPathsAndFiltersWithPartialCachesData() + { + $data = array_map(function (array $entry) { + $entry[] = CacheCommandFixtures::getImagesNotInArray($entry[0]); + $entry[] = CacheCommandFixtures::getFiltersNotInArray($entry[1]); + + return $entry; + }, CacheCommandFixtures::getAvailableFilterAndImageCombinations()); + + return array_filter($data, function (array $entry) { + return null !== $entry[2] && null !== $entry[3]; + }); + } + + /** + * @param array $images + * @param array $filters + * @param array $cachedImages + * @param array $cachedFilters + * + * @dataProvider provideResolvesWhenPassedPathsAndFiltersWithPartialCachesData + */ + public function testResolvesWhenPassedPathsAndFiltersWithPartialCaches(array $images, array $filters, array $cachedImages, array $cachedFilters) + { + $allImages = array_merge($images, $cachedImages); + $allFilters = array_merge($filters, $cachedFilters); + + $this->putResolvedImages($cachedImages, $cachedFilters); + + $this->assertImagesNotExist($images, $filters); + $this->assertImagesExist($cachedImages, $cachedFilters); + + $output = $this->executeResolveCacheCommand($allImages, $allFilters); + + $this->assertImagesExist($allImages, $allFilters); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + $this->assertOutputContainsSkippedImages($output, $cachedImages, $cachedFilters); + $this->assertOutputContainsSummary($output, $allImages, $allFilters); + + $this->delResolvedImages($allImages, $allFilters); + } + + /** + * @return array + */ + public static function provideResolvesForcedAllWhenPassedPathsAndFiltersWithPartialCachesData() + { + return static::provideResolvesWhenPassedPathsAndFiltersWithPartialCachesData(); + } + + /** + * @param array $images + * @param array $filters + * @param array $cachedImages + * @param array $cachedFilters + * + * @dataProvider provideResolvesForcedAllWhenPassedPathsAndFiltersWithPartialCachesData + */ + public function testForcedResolveWhenPassedPathsAndFiltersWithPartialCaches(array $images, array $filters, array $cachedImages, array $cachedFilters) + { + $allImages = array_merge($images, $cachedImages); + $allFilters = array_merge($filters, $cachedFilters); + + $this->putResolvedImages($cachedImages, $cachedFilters); + + $this->assertImagesNotExist($images, $filters); + $this->assertImagesExist($cachedImages, $cachedFilters); + + $output = $this->executeResolveCacheCommand($allImages, $allFilters, array('--force' => true)); + + $this->assertImagesExist($allImages, $allFilters); + $this->assertOutputContainsResolvedImages($output, $allImages, $allFilters); + $this->assertOutputContainsSummary($output, $allImages, $allFilters); + + $this->delResolvedImages($allImages, $allFilters); + } + + /** + * @return array + */ + public static function provideResolveShowsFailureAndContinuesWhenPassedInvalidFiltersData() + { + return array_map(function (array $entry) { + $entry[] = CacheCommandFixtures::getInvalidFilters(); + + return $entry; + }, CacheCommandFixtures::getAvailableFilterAndImageCombinations()); + } + + /** + * @param array $images + * @param array $filters + * @param array $invalidFilters + * + * @dataProvider provideResolveShowsFailureAndContinuesWhenPassedInvalidFiltersData + */ + public function testResolveShowsFailureAndContinuesWhenPassedInvalidFilters(array $images, array $filters, array $invalidFilters) + { + $allFilters = array_merge($filters, $invalidFilters); + + $this->assertImagesNotExist($images, $allFilters); + + $return = null; + $output = $this->executeResolveCacheCommand($images, $allFilters, array(), $return); + + $this->assertSame(255, $return); + $this->assertImagesExist($images, $filters); + $this->assertImagesNotExist($images, $invalidFilters); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + $this->assertOutputContainsFailedImages($output, $images, $invalidFilters); + $this->assertOutputContainsSummary($output, $images, $allFilters, count($images) * count($invalidFilters)); + + $this->delResolvedImages($images, $allFilters); + } + + /** + * @return array + */ + public static function provideResolveShowsFailureAndContinuesWhenPassedInvalidPathsData() + { + return array_map(function (array $entry) { + $entry[] = CacheCommandFixtures::getInvalidImages(); + + return $entry; + }, CacheCommandFixtures::getAvailableFilterAndImageCombinations()); + } + + /** + * @param array $images + * @param array $filters + * @param array $invalidImages + * + * @dataProvider provideResolveShowsFailureAndContinuesWhenPassedInvalidPathsData + */ + public function testResolveShowsFailureAndContinuesWhenPassedInvalidPaths(array $images, array $filters, array $invalidImages) + { + $allImages = array_merge($images, $invalidImages); + + $this->assertImagesNotExist($allImages, $filters); + + $return = null; + $output = $this->executeResolveCacheCommand($allImages, $filters, array(), $return); + + $this->assertSame(255, $return); + $this->assertImagesExist($images, $filters); + $this->assertImagesNotExist($invalidImages, $filters); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + $this->assertOutputContainsFailedImages($output, $invalidImages, $filters); + $this->assertOutputContainsSummary($output, $allImages, $filters, count($filters) * count($invalidImages)); + + $this->delResolvedImages($allImages, $filters); + } + + /** + * @return array + */ + public static function provideResolveOutputsMachineParseableTextWhenPassedMachineReadableOptionData() + { + return CacheCommandFixtures::getAvailableFilterAndImageCombinations(); + } + + /** + * @param array $images + * @param array $filters + * + * @dataProvider provideResolveOutputsMachineParseableTextWhenPassedMachineReadableOptionData + */ + public function testResolveOutputsMachineParseableTextWhenPassedMachineReadableOption(array $images, array $filters) + { + $this->assertImagesNotExist($images, $filters); + + $output = $this->executeResolveCacheCommand($images, $filters, array('--machine-readable' => true)); + + $this->assertImagesExist($images, $filters); + $this->assertNotContains('[liip/imagine-bundle]', $output); + $this->assertNotContains('=====================', $output); + $this->assertOutputContainsResolvedImages($output, $images, $filters); + $this->assertOutputNotContainsSummary($output, $images, $filters); + + $this->delResolvedImages($images, $filters); + } + + /** + * @return array + */ + public static function provideResolveEmitsDeprecationMessageWhenUsingLegacyFiltersOptionData() + { + return CacheCommandFixtures::getAvailableFilterAndImageCombinations(); + } + + /** + * @dataProvider provideResolveEmitsDeprecationMessageWhenUsingLegacyFiltersOptionData + * + * @group legacy + * @expectedDeprecation The --filters option was deprecated in 1.9.0 and removed in 2.0.0. Use the --filter option instead. + */ + public function testResolveEmitsDeprecationMessageWhenUsingLegacyFiltersOption(array $images, array $filters) + { + $this->assertImagesNotExist($images, $filters); + + $output = $this->executeResolveCacheCommand($images, array(), 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 + */ + protected 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); + } +} diff --git a/Tests/Functional/Command/ResolveCacheTest.php b/Tests/Functional/Command/ResolveCacheTest.php deleted file mode 100644 index 822944812..000000000 --- a/Tests/Functional/Command/ResolveCacheTest.php +++ /dev/null @@ -1,369 +0,0 @@ -assertImagesNotExist($images, $filters); - - $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() - { - $images = array('images/cats.jpeg'); - $filters = array('thumbnail_web_path'); - - $this->putResolvedImages($images, $filters); - - $output = $this->executeResolveCacheCommand($images, $filters); - - $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() - { - $images = array('images/cats.jpeg', 'images/cats2.jpeg'); - $filters = array('thumbnail_web_path'); - - $output = $this->executeResolveCacheCommand($images, $filters); - - $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() - { - $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); - - $output = $this->executeResolveCacheCommand($images, $filters); - - $this->assertImagesExist($images, $filters); - $this->assertOutputContainsResolvedImages($output, $imagesResolved, $filters); - $this->assertOutputContainsCachedImages($output, $imagesCached, $filters); - $this->assertOutputContainsSummary($output, $images, $filters); - - $this->delResolvedImages($images, $filters); - } - - public function testShouldResolveWithFewPathsAndFewFilters() - { - $images = array('images/cats.jpeg', 'images/cats2.jpeg'); - $filters = array('thumbnail_web_path', 'thumbnail_default'); - - $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() - { - $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); - - $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); - } - } - } -} diff --git a/Tests/Functional/Controller/ImagineControllerTest.php b/Tests/Functional/Controller/ImagineControllerTest.php index 14301a439..426014bab 100644 --- a/Tests/Functional/Controller/ImagineControllerTest.php +++ b/Tests/Functional/Controller/ImagineControllerTest.php @@ -30,44 +30,44 @@ public function testCouldBeGetFromContainer() public function testShouldResolvePopulatingCacheFirst() { //guard - $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileNotExists($this->cacheRoot.'/profile_thumb_sm/images/cats.jpeg'); - $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/cats.jpeg'); + $this->client->request('GET', '/media/cache/resolve/profile_thumb_sm/images/cats.jpeg'); $response = $this->client->getResponse(); $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); $this->assertEquals(302, $response->getStatusCode()); - $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl()); + $this->assertEquals('http://localhost/media/cache/profile_thumb_sm/images/cats.jpeg', $response->getTargetUrl()); - $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileExists($this->cacheRoot.'/profile_thumb_sm/images/cats.jpeg'); } public function testShouldResolveFromCache() { $this->filesystem->dumpFile( - $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', + $this->cacheRoot.'/profile_thumb_sm/images/cats.jpeg', 'anImageContent' ); - $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/cats.jpeg'); + $this->client->request('GET', '/media/cache/resolve/profile_thumb_sm/images/cats.jpeg'); $response = $this->client->getResponse(); $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); $this->assertEquals(302, $response->getStatusCode()); - $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl()); + $this->assertEquals('http://localhost/media/cache/profile_thumb_sm/images/cats.jpeg', $response->getTargetUrl()); - $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); + $this->assertFileExists($this->cacheRoot.'/profile_thumb_sm/images/cats.jpeg'); } /** * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException - * @expectedExceptionMessage Signed url does not pass the sign check for path "images/cats.jpeg" and filter "thumbnail_web_path" and runtime config {"thumbnail":{"size":["50","50"]}} + * @expectedExceptionMessage Signed url does not pass the sign check for path "images/cats.jpeg" and filter "profile_thumb_sm" and runtime config {"thumbnail":{"size":["50","50"]}} */ public function testThrowBadRequestIfSignInvalidWhileUsingCustomFilters() { - $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/rc/invalidHash/images/cats.jpeg?'.http_build_query(array( + $this->client->request('GET', '/media/cache/resolve/profile_thumb_sm/rc/invalidHash/images/cats.jpeg?'.http_build_query(array( 'filters' => array( 'thumbnail' => array('size' => array(50, 50)), ), @@ -81,7 +81,7 @@ public function testThrowBadRequestIfSignInvalidWhileUsingCustomFilters() */ public function testShouldThrowNotFoundHttpExceptionIfFiltersNotArray() { - $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/rc/invalidHash/images/cats.jpeg?'.http_build_query(array( + $this->client->request('GET', '/media/cache/resolve/profile_thumb_sm/rc/invalidHash/images/cats.jpeg?'.http_build_query(array( 'filters' => 'some-string', '_hash' => 'hash', ))); @@ -93,7 +93,7 @@ public function testShouldThrowNotFoundHttpExceptionIfFiltersNotArray() */ public function testShouldThrowNotFoundHttpExceptionIfFileNotExists() { - $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/shrodinger_cats_which_not_exist.jpeg'); + $this->client->request('GET', '/media/cache/resolve/profile_thumb_sm/images/shrodinger_cats_which_not_exist.jpeg'); } /** @@ -119,7 +119,7 @@ public function testShouldResolveWithCustomFiltersPopulatingCacheFirst() $hash = $signer->sign($path, $params['filters']); - $expectedCachePath = 'thumbnail_web_path/rc/'.$hash.'/'.$path; + $expectedCachePath = 'profile_thumb_sm/rc/'.$hash.'/'.$path; $url = 'http://localhost/media/cache/resolve/'.$expectedCachePath.'?'.http_build_query($params); @@ -152,7 +152,7 @@ public function testShouldResolveWithCustomFiltersFromCache() $hash = $signer->sign($path, $params['filters']); - $expectedCachePath = 'thumbnail_web_path/rc/'.$hash.'/'.$path; + $expectedCachePath = 'profile_thumb_sm/rc/'.$hash.'/'.$path; $url = 'http://localhost/media/cache/resolve/'.$expectedCachePath.'?'.http_build_query($params); @@ -175,20 +175,20 @@ public function testShouldResolveWithCustomFiltersFromCache() public function testShouldResolvePathWithSpecialCharactersAndWhiteSpaces() { $this->filesystem->dumpFile( - $this->cacheRoot.'/thumbnail_web_path/images/foo bar.jpeg', + $this->cacheRoot.'/profile_thumb_sm/images/foo bar.jpeg', 'anImageContent' ); // we are calling url with encoded file name as it will be called by browser $urlEncodedFileName = 'foo+bar'; - $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/'.$urlEncodedFileName.'.jpeg'); + $this->client->request('GET', '/media/cache/resolve/profile_thumb_sm/images/'.$urlEncodedFileName.'.jpeg'); $response = $this->client->getResponse(); $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); $this->assertEquals(302, $response->getStatusCode()); - $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/foo bar.jpeg', $response->getTargetUrl()); + $this->assertEquals('http://localhost/media/cache/profile_thumb_sm/images/foo bar.jpeg', $response->getTargetUrl()); - $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/foo bar.jpeg'); + $this->assertFileExists($this->cacheRoot.'/profile_thumb_sm/images/foo bar.jpeg'); } } diff --git a/Tests/Functional/Fixtures/BarBundle/Resources/public/cats-bar-bundle.jpg b/Tests/Functional/Fixtures/BarBundle/Resources/public/cats-bar-bundle.jpg new file mode 100644 index 000000000..bc458e350 Binary files /dev/null and b/Tests/Functional/Fixtures/BarBundle/Resources/public/cats-bar-bundle.jpg differ diff --git a/Tests/Functional/Fixtures/CacheCommandFixtures.php b/Tests/Functional/Fixtures/CacheCommandFixtures.php new file mode 100644 index 000000000..9ff8bdd7a --- /dev/null +++ b/Tests/Functional/Fixtures/CacheCommandFixtures.php @@ -0,0 +1,99 @@ +