Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli command to resolve cache #379

Merged
merged 7 commits into from
Apr 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Command/ResolveCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Liip\ImagineBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ResolveCacheCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('liip:imagine:cache:resolve')
->setDescription('Resolve cache for given path and set of filters.')
->addArgument('path', InputArgument::REQUIRED, 'Image path')
->addArgument('filters', InputArgument::OPTIONAL|InputArgument::IS_ARRAY, 'Filters list');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$path = $input->getArgument('path');
$filters = $input->getArgument('filters');

/** @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');

if (empty($filters)) {
$filters = array_keys($filterManager->getFilterConfiguration()->all());
}

foreach ($filters as $filter) {
if (!$cacheManager->isStored($path, $filter)) {
$binary = $dataManager->find($filter, $path);

$cacheManager->store(
$filterManager->applyFilter($binary, $filter),
$path,
$filter
);
}

$output->writeln($cacheManager->resolve($path, $filter));
}
}
}
109 changes: 109 additions & 0 deletions Tests/Functional/Command/ResolveCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Liip\ImagineBundle\Tests\Functional\Command;

use Liip\ImagineBundle\Tests\Functional\WebTestCase;
use Liip\ImagineBundle\Command\ResolveCacheCommand;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class ResolveCacheTest extends WebTestCase
{
protected $client;

protected $webRoot;

protected $filesystem;

protected $cacheRoot;

public function setUp()
{
parent::setUp();

$this->client = $this->createClient();

$this->webRoot = self::$kernel->getContainer()->getParameter('kernel.root_dir').'/web';
$this->cacheRoot = $this->webRoot.'/'.self::$kernel->getContainer()->getParameter('liip_imagine.cache_prefix');

$this->filesystem = new Filesystem;
$this->filesystem->remove($this->cacheRoot);
}


public function testShouldResolveWithEmptyCache()
{
$this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');

$output = $this->executeConsole(new ResolveCacheCommand(), array(
'path' => 'images/cats.jpeg',
'filters' => array('thumbnail_web_path')
));

$this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
$this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output);
}

public function testShouldResolveWithCacheExists()
{
$this->filesystem->dumpFile(
$this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
'anImageContent'
);

$output = $this->executeConsole(new ResolveCacheCommand(), array(
'path' => 'images/cats.jpeg',
'filters' => array('thumbnail_web_path')
));

$this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output);
}

public function testShouldResolveWithFewFiltersInParams()
{
$output = $this->executeConsole(new ResolveCacheCommand(), array(
'path' => 'images/cats.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_default/images/cats.jpeg', $output);
}

public function testShouldResolveWithoutFiltersInParams()
{
$output = $this->executeConsole(new ResolveCacheCommand(), array(
'path' => 'images/cats.jpeg',
));

$this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output);
$this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats.jpeg', $output);
}

/**
* Helper function return the result of command execution.
*
* @param Command $command
* @param array $arguments
* @param array $options
* @return string
*/
protected function executeConsole(Command $command, array $arguments = array(), array $options = array())
{
$command->setApplication(new Application($this->createClient()->getKernel()));
if ($command instanceof ContainerAwareCommand) {
$command->setContainer($this->createClient()->getContainer());
}

$arguments = array_replace(array('command' => $command->getName()), $arguments);
$options = array_replace(array('--env' => 'test'), $options);

$commandTester = new CommandTester($command);
$commandTester->execute($arguments, $options);

return $commandTester->getDisplay();
}
}
3 changes: 3 additions & 0 deletions Tests/Functional/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ liip_imagine:
thumbnail_web_path:
filters:
thumbnail: { size: [223, 223], mode: inset }
thumbnail_default:
filters:
thumbnail: { size: [223, 223], mode: inset }
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"aws/aws-sdk-php": "~2.4",
"amazonwebservices/aws-sdk-for-php": "~1.0",
"psr/log": "~1.0",
"ext-gd": "*"
"ext-gd": "*",
"symfony/console": "~2.3"
},

"suggest": {
Expand Down