Skip to content

Commit

Permalink
Merge pull request #387 from formapro-forks/remove-cli-command
Browse files Browse the repository at this point in the history
Remove cli command
  • Loading branch information
makasim committed Apr 7, 2014
2 parents f6d5fb8 + 96f7058 commit e396534
Show file tree
Hide file tree
Showing 2 changed files with 355 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Command/RemoveCacheCommand.php
@@ -0,0 +1,59 @@
<?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\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class RemoveCacheCommand extends ContainerAwareCommand
{
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',
'f',
InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY,
'Filters list'
)
->setHelp(<<<EOF
The <info>%command.name%</info> command removes cache by specified parameters.
Paths should be separated by spaces:
<info>php app/console %command.name% path1 path2</info>
All cache for a given `paths` will be lost.
If you use --filters parameter:
<info>php app/console %command.name% --filters=thumb1 --filters=thumb2</info>
All cache for a given filters will be lost.
You can combine these parameters:
<info>php app/console %command.name% path1 path2 --filters=thumb1 --filters=thumb2</info>
<info>php app/console %command.name%</info>
Cache for all paths and filters will be lost when executing this command without parameters.
EOF
);
}

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

if (empty($filters)) {
$filters = null;
}

/** @var CacheManager cacheManager */
$cacheManager = $this->getContainer()->get('liip_imagine.cache.manager');

$cacheManager->remove($paths, $filters);
}
}
296 changes: 296 additions & 0 deletions Tests/Functional/Command/RemoveCacheTest.php
@@ -0,0 +1,296 @@
<?php

namespace Liip\ImagineBundle\Tests\Functional\Command;

use Liip\ImagineBundle\Tests\Functional\WebTestCase;
use Liip\ImagineBundle\Command\RemoveCacheCommand;
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 RemoveCacheTest 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 testExecuteSuccessfullyWithEmptyCacheAndWithoutParameters()
{
$this->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'),
'--filters' => 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('--filters' => 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('--filters' => 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('--filters' => 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'),
'--filters' => 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'),
'--filters' => 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');
}

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


0 comments on commit e396534

Please sign in to comment.