diff --git a/DependencyInjection/LiipImagineExtension.php b/DependencyInjection/LiipImagineExtension.php index f834a8f5d..405ffe31f 100644 --- a/DependencyInjection/LiipImagineExtension.php +++ b/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 { diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index 3145f8f41..292ee5b1f 100644 --- a/Imagine/Cache/CacheManager.php +++ b/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 { diff --git a/Tests/AbstractTest.php b/Tests/AbstractTest.php new file mode 100644 index 000000000..d5a7b3611 --- /dev/null +++ b/Tests/AbstractTest.php @@ -0,0 +1,61 @@ +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); + } + } +} diff --git a/Tests/DependencyInjection/LiipImagineExtensionTest.php b/Tests/DependencyInjection/LiipImagineExtensionTest.php index c3981bfb1..56daef825 100644 --- a/Tests/DependencyInjection/LiipImagineExtensionTest.php +++ b/Tests/DependencyInjection/LiipImagineExtensionTest.php @@ -1,24 +1,16 @@ - * - * 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 */ diff --git a/Tests/Fixtures/CacheManagerAwareResolver.php b/Tests/Fixtures/CacheManagerAwareResolver.php new file mode 100644 index 000000000..aa028f497 --- /dev/null +++ b/Tests/Fixtures/CacheManagerAwareResolver.php @@ -0,0 +1,11 @@ +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'); + } +} diff --git a/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php b/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php new file mode 100644 index 000000000..6823182c0 --- /dev/null +++ b/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php @@ -0,0 +1,35 @@ +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)); + } +} diff --git a/Tests/Imagine/Cache/Resolver/AmazonS3ResolverTest.php b/Tests/Imagine/Cache/Resolver/AmazonS3ResolverTest.php index 9accb15cf..16a768798 100644 --- a/Tests/Imagine/Cache/Resolver/AmazonS3ResolverTest.php +++ b/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'); } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a5fdc0200..85d1871a4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,6 @@ - +