Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

AbstractFilesystemResolverTest + CacheManagerTest

  • Loading branch information...
commit 3c8c8255fea9f9b0e897a2b05667f4018ba60021 1 parent c00512c
@havvg havvg authored
View
6 DependencyInjection/LiipImagineExtension.php
@@ -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
{
View
16 Imagine/Cache/CacheManager.php
@@ -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.
@@ -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);
@@ -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 {
View
61 Tests/AbstractTest.php
@@ -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);
+ }
+ }
+}
View
28 Tests/DependencyInjection/LiipImagineExtensionTest.php
@@ -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
@@ -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
*/
@@ -91,7 +83,7 @@ protected function createEmptyConfiguration()
$loader->load(array(array()), $this->containerBuilder);
$this->assertTrue($this->containerBuilder instanceof ContainerBuilder);
}
-
+
/**
* @return ContainerBuilder
*/
View
11 Tests/Fixtures/CacheManagerAwareResolver.php
@@ -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
+{
+
+}
View
BIN  Tests/Fixtures/assets/cats.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  Tests/Fixtures/assets/АГГЗ.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
174 Tests/Imagine/Cache/CacheManagerTest.php
@@ -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');
+ }
+}
View
35 Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Liip\ImagineBundle\Tests\Imagine\Cache\Resolver;
+
+use Liip\ImagineBundle\Tests\AbstractTest;
+use Symfony\Component\Filesystem\Filesystem;
+use Symfony\Component\HttpFoundation\Response;
+
+/**
+ * @covers Liip\ImagineBundle\Imagine\Cache\Resolver\AbstractFilesystemResolver
+ */
+class AbstractFilesystemResolverTest extends AbstractTest
+{
+ public function testStoreCyrillicFilename()
+ {
+ $image = $this->fixturesDir.'/assets/АГГЗ.jpeg';
+ $data = file_get_contents($image);
+ $response = new Response($data, 200, array(
+ 'content-type' => 'image/jpeg',
+ ));
+
+ $targetPath = $this->tempDir.'/cached/АГГЗ.jpeg';
+
+ $resolver = $this->getMockAbstractFilesystemResolver(new Filesystem());
+ $resolver->store($response, $targetPath, 'mirror');
+
+ $this->assertTrue(file_exists($targetPath));
+ $this->assertEquals($data, file_get_contents($targetPath));
+ }
+
+ protected function getMockAbstractFilesystemResolver($filesystem)
+ {
+ return $this->getMock('Liip\ImagineBundle\Imagine\Cache\Resolver\AbstractFilesystemResolver', array('resolve', 'clear', 'getBrowserPath', 'getFilePath'), array($filesystem));
+ }
+}
View
7 Tests/Imagine/Cache/Resolver/AmazonS3ResolverTest.php
@@ -3,15 +3,18 @@
namespace Liip\ImagineBundle\Tests\Imagine\Cache\Resolver;
use Liip\ImagineBundle\Imagine\Cache\Resolver\AmazonS3Resolver;
+use Liip\ImagineBundle\Tests\AbstractTest;
use Symfony\Component\HttpFoundation\Response;
-class AmazonS3ResolverTest extends \PHPUnit_Framework_TestCase
+class AmazonS3ResolverTest extends AbstractTest
{
protected function setUp()
{
+ parent::setUp();
+
if (!class_exists('AmazonS3')) {
- require_once(__DIR__.'/../../../Fixtures/AmazonS3.php');
+ require_once($this->fixturesDir.'/AmazonS3.php');
}
}
View
2  phpunit.xml.dist
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
-<phpunit bootstrap="./Tests/bootstrap.php" colors="true">
+<phpunit bootstrap="./Tests/bootstrap.php" colors="true" forceCoversAnnotation="true">
<testsuites>
<testsuite name="LiipImagineBundle test suite">
Please sign in to comment.
Something went wrong with that request. Please try again.