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 3 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
48 changes: 48 additions & 0 deletions Command/ResolveCommand.php
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResolveCacheCommand

{
protected function configure()
{
$this
->setName('liip_imagine:cache:resolve')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name it liip:imagine:cache:resolve to be compatible with other liips bundle, for example doctrine cache bundle has a commandliip:doctrine-cache:clear`.

->setDescription('Resolve url for image')
Copy link
Collaborator

Choose a reason for hiding this comment

The 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');
Copy link
Collaborator

Choose a reason for hiding this comment

The 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');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add /** @var FilterManager filterManager */. apply this rule for other places too.

$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));
}
}
}
118 changes: 118 additions & 0 deletions Tests/Functional/Command/ResolveTest.php
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
}
}
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