-
Notifications
You must be signed in to change notification settings - Fork 379
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
Changes from 3 commits
586fba5
789d10c
553bc9a
bfc4830
45bb0c5
310382a
9d663cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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 ResolveCommand extends ContainerAwareCommand | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('liip_imagine:cache:resolve') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name it |
||
->setDescription('Resolve url for image') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dont align |
||
$filters = $input->getArgument('filters'); | ||
|
||
$filterManager = $this->getContainer()->get('liip_imagine.filter.manager'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
$cacheManager = $this->getContainer()->get('liip_imagine.cache.manager'); | ||
$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)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Tests\Functional\Command; | ||
|
||
use Liip\ImagineBundle\Tests\Functional\WebTestCase; | ||
use Liip\ImagineBundle\Command\ResolveCommand; | ||
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 ResolveTest 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 ResolveCommand(), 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 ResolveCommand(), array( | ||
'path' => 'images/cats.jpeg', | ||
'filters' => array('thumbnail_web_path') | ||
)); | ||
|
||
$this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); | ||
} | ||
|
||
/** | ||
* @dataProvider providerFiltersSet | ||
*/ | ||
public function testShouldResolveAgainstFiltersSet(array $filters, array $contains) | ||
{ | ||
$output = $this->executeConsole(new ResolveCommand(), array( | ||
'path' => 'images/cats.jpeg', | ||
'filters' => $filters | ||
)); | ||
|
||
foreach ($contains as $phrase) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid usage of loops and conditionals in tests as much as possible. |
||
$this->assertContains($phrase, $output); | ||
} | ||
} | ||
|
||
/** | ||
* Data provider for filters test | ||
*/ | ||
public static function providerFiltersSet() | ||
{ | ||
return array( | ||
array(array('thumbnail_web_path', 'thumbnail_default'), array( | ||
'http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', | ||
'http://localhost/media/cache/thumbnail_default/images/cats.jpeg')), | ||
array(array(), array( | ||
'http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', | ||
'http://localhost/media/cache/thumbnail_default/images/cats.jpeg')) | ||
); | ||
} | ||
|
||
/** | ||
* Helper function return the result of command execution. | ||
* | ||
* @param Command $command | ||
* @param array $arguments | ||
* @param array $options | ||
* @return string | ||
*/ | ||
protected function executeConsole(Command $command, array $arguments = array(), array $options = array()) | ||
{ | ||
$command->setApplication(new Application($this->createClient()->getKernel())); | ||
if ($command instanceof ContainerAwareCommand) { | ||
$command->setContainer($this->createClient()->getContainer()); | ||
} | ||
|
||
$arguments = array_replace(array('command' => $command->getName()), $arguments); | ||
$options = array_replace(array('--env' => 'test'), $options); | ||
|
||
$commandTester = new CommandTester($command); | ||
$commandTester->execute($arguments, $options); | ||
|
||
return $commandTester->getDisplay(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ResolveCacheCommand