Skip to content

Commit

Permalink
AbstractFilesystemResolverTest + CacheManagerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
havvg committed Feb 8, 2013
1 parent c00512c commit 3c8c825
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 34 deletions.
6 changes: 1 addition & 5 deletions DependencyInjection/LiipImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
namespace Liip\ImagineBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Reference;

use Liip\ImagineBundle\LiipImagineBundle;
use Symfony\Component\HttpKernel\Kernel;

class LiipImagineExtension extends Extension
{
Expand Down
16 changes: 8 additions & 8 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ class CacheManager
/**
* @var FilterConfiguration
*/
private $filterConfig;
protected $filterConfig;

/**
* @var RouterInterface
*/
private $router;
protected $router;

/**
* @var string
*/
private $webRoot;
protected $webRoot;

/**
* @var string
*/
private $defaultResolver;
protected $defaultResolver;

/**
* @var array
* @var ResolverInterface[]
*/
private $resolvers = array();
protected $resolvers = array();

/**
* Constructs the cache manager to handle Resolvers based on the provided FilterConfiguration.
Expand Down Expand Up @@ -80,7 +80,7 @@ public function getWebRoot()
* @param string $filter
* @return ResolverInterface
*/
private function getResolver($filter)
protected function getResolver($filter)
{
$config = $this->filterConfig->get($filter);

Expand Down Expand Up @@ -154,7 +154,7 @@ public function generateUrl($targetPath, $filter, $absolute = false)
public function resolve(Request $request, $path, $filter)
{
if (false !== strpos($path, '/../') || 0 === strpos($path, '../')) {
throw new NotFoundHttpException(sprintf("Source image was searched with '%s' out side of the defined root path", $path));
throw new NotFoundHttpException(sprintf("Source image was searched with '%s' outside of the defined root path", $path));
}

try {
Expand Down
61 changes: 61 additions & 0 deletions Tests/AbstractTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Liip\ImagineBundle\Tests;

use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Symfony\Component\Filesystem\Filesystem;

abstract class AbstractTest extends \PHPUnit_Framework_TestCase
{
protected $fixturesDir;
protected $tempDir;

protected function setUp()
{
$this->fixturesDir = __DIR__.'/Fixtures';

$this->tempDir = sys_get_temp_dir().'/liip_imagine_test';

$fs = new Filesystem();

if ($fs->exists($this->tempDir)) {
$fs->remove($this->tempDir);
}

$fs->mkdir($this->tempDir);
}

protected function createFilterConfiguration()
{
$config = new FilterConfiguration();
$config->set('thumbnail', array(
'size' => array(180, 180),
'mode' => 'outbound',
));

return $config;
}

protected function getMockFilterConfiguration()
{
return $this->getMock('Liip\ImagineBundle\Imagine\Filter\FilterConfiguration');
}

protected function getMockRouter()
{
return $this->getMock('Symfony\Component\Routing\RouterInterface');
}

protected function getMockResolver()
{
return $this->getMock('Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface');
}

protected function tearDown()
{
$fs = new Filesystem();
if ($fs->exists($this->tempDir)) {
$fs->remove($this->tempDir);
}
}
}
28 changes: 10 additions & 18 deletions Tests/DependencyInjection/LiipImagineExtensionTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\Tests\DependencyInjection;

use Liip\ImagineBundle\Tests\AbstractTest;
use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Kernel;
use Liip\ImagineBundle\LiipImagineBundle;
use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\DependencyInjection\Reference;

class LiipImagineExtensionTest extends \PHPUnit_Framework_TestCase
class LiipImagineExtensionTest extends AbstractTest
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
Expand Down Expand Up @@ -69,18 +61,18 @@ public function testCacheClearerIsNotRegistered()

$this->assertFalse($this->containerBuilder->hasDefinition('liip_imagine.cache.clearer'));
}

public function testCustomRouteRequirements()
{
$this->createFullConfiguration();
$param = $this->containerBuilder->getParameter('liip_imagine.filter_sets');

$this->assertTrue(isset($param['small']['filters']['route']['requirements']));

$variable1 = $param['small']['filters']['route']['requirements']['variable1'];
$this->assertEquals('value1', $variable1, sprintf('%s parameter is correct', $variable1));
}

/**
* @return ContainerBuilder
*/
Expand All @@ -91,7 +83,7 @@ protected function createEmptyConfiguration()
$loader->load(array(array()), $this->containerBuilder);
$this->assertTrue($this->containerBuilder instanceof ContainerBuilder);
}

/**
* @return ContainerBuilder
*/
Expand Down
11 changes: 11 additions & 0 deletions Tests/Fixtures/CacheManagerAwareResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Liip\ImagineBundle\Tests\Fixtures;

use Liip\ImagineBundle\Imagine\Cache\CacheManagerAwareInterface;
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;

abstract class CacheManagerAwareResolver implements ResolverInterface, CacheManagerAwareInterface
{

}
Binary file added Tests/Fixtures/assets/cats.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/Fixtures/assets/АГГЗ.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
174 changes: 174 additions & 0 deletions Tests/Imagine/Cache/CacheManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

namespace Liip\ImagineBundle\Tests\Imagine\Cache;

use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Tests\AbstractTest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @covers Liip\ImagineBundle\Imagine\Cache\CacheManager
*/
class CacheManagerTest extends AbstractTest
{
protected $resolver;

public function testAddCacheManagerAwareResolver()
{
$cacheManager = new CacheManager($this->getMockFilterConfiguration(), $this->getMockRouter(), $this->fixturesDir.'/assets');

$resolver = $this->getMock('Liip\ImagineBundle\Tests\Fixtures\CacheManagerAwareResolver');
$resolver
->expects($this->once())
->method('setCacheManager')
->with($cacheManager)
;

$cacheManager->addResolver('thumbnail', $resolver);
}

public function testGetBrowserPathWithoutResolver()
{
$config = $this->getMockFilterConfiguration();
$config
->expects($this->once())
->method('get')
->with('thumbnail')
->will($this->returnValue(array(
'size' => array(180, 180),
'mode' => 'outbound',
'cache' => null,
)))
;

$cacheManager = new CacheManager($config, $this->getMockRouter(), $this->fixturesDir.'/assets', 'default');

$this->setExpectedException('InvalidArgumentException', 'Could not find resolver for "thumbnail" filter type');
$cacheManager->getBrowserPath('cats.jpeg', 'thumbnail', true);
}

public function testDefaultResolverUsedIfNoneSet()
{
$resolver = $this->getMockResolver();
$resolver
->expects($this->once())
->method('getBrowserPath')
->with('cats.jpeg', 'thumbnail', true)
;

$config = $this->getMockFilterConfiguration();
$config
->expects($this->once())
->method('get')
->with('thumbnail')
->will($this->returnValue(array(
'size' => array(180, 180),
'mode' => 'outbound',
'cache' => null,
)))
;

$cacheManager = new CacheManager($config, $this->getMockRouter(), $this->fixturesDir.'/assets', 'default');
$cacheManager->addResolver('default', $resolver);

$cacheManager->getBrowserPath('cats.jpeg', 'thumbnail', true);
}

public function invalidPathProvider()
{
return array(
array($this->fixturesDir.'/assets/../../foobar.png'),
array($this->fixturesDir.'/assets/some_folder/../foobar.png'),
array('../../outside/foobar.jpg'),
);
}

/**
* @dataProvider invalidPathProvider
*/
public function testResolveInvalidPath($path)
{
$cacheManager = new CacheManager($this->getMockFilterConfiguration(), $this->getMockRouter(), $this->fixturesDir.'/assets');

$this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
$cacheManager->resolve(new Request(), $path, 'thumbnail');
}

public function testResolveWithoutResolver()
{
$cacheManager = new CacheManager($this->getMockFilterConfiguration(), $this->getMockRouter(), $this->fixturesDir.'/assets');

$this->assertFalse($cacheManager->resolve(new Request(), 'cats.jpeg', 'thumbnail'));
}

public function testFallbackToDefaultResolver()
{
$response = new Response('', 200);
$request = new Request();

$resolver = $this->getMockResolver();
$resolver
->expects($this->once())
->method('resolve')
->with($request, 'cats.jpeg', 'thumbnail')
->will($this->returnValue('/thumbs/cats.jpeg'))
;
$resolver
->expects($this->once())
->method('store')
->with($response, '/thumbs/cats.jpeg', 'thumbnail')
->will($this->returnValue($response))
;
$resolver
->expects($this->once())
->method('remove')
->with('/thumbs/cats.jpeg', 'thumbnail')
->will($this->returnValue(true))
;

$config = $this->getMockFilterConfiguration();
$config
->expects($this->exactly(3))
->method('get')
->with('thumbnail')
->will($this->returnValue(array(
'size' => array(180, 180),
'mode' => 'outbound',
'cache' => null,
)))
;

$cacheManager = new CacheManager($config, $this->getMockRouter(), $this->fixturesDir.'/assets', 'default');
$cacheManager->addResolver('default', $resolver);

// Resolve fallback to default resolver
$this->assertEquals('/thumbs/cats.jpeg', $cacheManager->resolve($request, 'cats.jpeg', 'thumbnail'));

// Store fallback to default resolver
$this->assertEquals($response, $cacheManager->store($response, '/thumbs/cats.jpeg', 'thumbnail'));

// Remove fallback to default resolver
$this->assertTrue($cacheManager->remove('/thumbs/cats.jpeg', 'thumbnail'));
}

public function testClearResolversCacheClearsAll()
{
$resolver = $this->getMockResolver();
$resolver
->expects($this->exactly(5))
->method('clear')
->with('imagine_cache')
;

$cacheManager = new CacheManager($this->getMockFilterConfiguration(), $this->getMockRouter(), $this->fixturesDir.'/assets', 'default');

$cacheManager->addResolver('default', $resolver);
$cacheManager->addResolver('thumbnail1', $resolver);
$cacheManager->addResolver('thumbnail2', $resolver);
$cacheManager->addResolver('thumbnail3', $resolver);
$cacheManager->addResolver('thumbnail4', $resolver);

$cacheManager->clearResolversCache('imagine_cache');
}
}
Loading

0 comments on commit 3c8c825

Please sign in to comment.