diff --git a/DependencyInjection/Compiler/CreateCacheDirectoriesCompilerPass.php b/DependencyInjection/Compiler/CreateCacheDirectoriesCompilerPass.php deleted file mode 100644 index a7c81f3ea..000000000 --- a/DependencyInjection/Compiler/CreateCacheDirectoriesCompilerPass.php +++ /dev/null @@ -1,34 +0,0 @@ -getParameter('liip_imagine.cache')) { - return; - } - - $webRoot = $container->getParameter('liip_imagine.web_root'); - $cachePrefix = $container->getParameter('liip_imagine.cache_prefix'); - $filters = $container->getParameter('liip_imagine.filters'); - $mode = $container->getParameter('liip_imagine.cache_mkdir_mode'); - - foreach ($filters as $filter => $options) { - $dir = isset($options['path']) - ? $webRoot.$options['path'] - : $webRoot.$cachePrefix.'/'.$filter; - - if (!is_dir($dir) && !mkdir($dir, $mode, true)) { - throw new \RuntimeException(sprintf( - 'Could not create directory for caching processed '. - 'images in "%s"', $dir - )); - } - } - } -} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4c38c5e82..b71d9f3a1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -29,7 +29,6 @@ public function getConfigTreeBuilder() ->end() ->scalarNode('web_root')->defaultValue('%kernel.root_dir%/../web')->end() ->scalarNode('data_root')->defaultValue('%liip_imagine.web_root%')->end() - ->scalarNode('cache_mkdir_mode')->defaultValue(0777)->end() ->scalarNode('cache_prefix')->defaultValue('/media/cache')->end() ->scalarNode('cache')->defaultValue('web_path')->end() ->scalarNode('cache_base_path')->defaultValue('')->end() diff --git a/DependencyInjection/LiipImagineExtension.php b/DependencyInjection/LiipImagineExtension.php index 00d32ddf3..f46d3ea2f 100644 --- a/DependencyInjection/LiipImagineExtension.php +++ b/DependencyInjection/LiipImagineExtension.php @@ -29,7 +29,6 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('liip_imagine.cache_prefix', $cachePrefix); $container->setParameter('liip_imagine.web_root', $config['web_root']); $container->setParameter('liip_imagine.data_root', $config['data_root']); - $container->setParameter('liip_imagine.cache_mkdir_mode', $config['cache_mkdir_mode']); $container->setParameter('liip_imagine.formats', $config['formats']); $container->setParameter('liip_imagine.cache.resolver.default', $config['cache']); diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index 369a5623b..e1de3e285 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -20,11 +20,6 @@ class CacheManager */ protected $router; - /** - * @var string - */ - protected $webRoot; - /** * @var string */ @@ -40,14 +35,12 @@ class CacheManager * * @param FilterConfiguration $filterConfig * @param RouterInterface $router - * @param string $webRoot * @param string $defaultResolver */ - public function __construct(FilterConfiguration $filterConfig, RouterInterface $router, $webRoot, $defaultResolver = null) + public function __construct(FilterConfiguration $filterConfig, RouterInterface $router, $defaultResolver = null) { $this->filterConfig = $filterConfig; $this->router = $router; - $this->webRoot = realpath($webRoot); $this->defaultResolver = $defaultResolver; } @@ -68,16 +61,6 @@ public function addResolver($filter, ResolverInterface $resolver) } } - /** - * Returns the configured web root path. - * - * @return string - */ - public function getWebRoot() - { - return $this->webRoot; - } - /** * Gets a resolver for the given filter. * diff --git a/Imagine/Cache/Resolver/NoCacheResolver.php b/Imagine/Cache/Resolver/NoCacheWebPathResolver.php similarity index 54% rename from Imagine/Cache/Resolver/NoCacheResolver.php rename to Imagine/Cache/Resolver/NoCacheWebPathResolver.php index 85d87f994..7c7195474 100644 --- a/Imagine/Cache/Resolver/NoCacheResolver.php +++ b/Imagine/Cache/Resolver/NoCacheWebPathResolver.php @@ -3,12 +3,18 @@ namespace Liip\ImagineBundle\Imagine\Cache\Resolver; use Liip\ImagineBundle\Binary\BinaryInterface; +use Symfony\Component\Routing\RequestContext; -/** - * NoCacheResolver. - */ -class NoCacheResolver extends WebPathResolver +class NoCacheWebPathResolver implements ResolverInterface { + /** + * @param RequestContext $requestContext + */ + public function __construct(RequestContext $requestContext) + { + $this->requestContext = $requestContext; + } + /** * {@inheritDoc} */ @@ -22,7 +28,11 @@ public function isStored($path, $filter) */ public function resolve($path, $filter) { - return $this->getRequest()->getSchemeAndHttpHost().'/'.$path; + return sprintf('%s://%s/%s', + $this->requestContext->getScheme(), + $this->requestContext->getHost(), + ltrim($path, '/') + ); } /** @@ -39,3 +49,4 @@ public function remove(array $paths, array $filters) { } } + diff --git a/Imagine/Cache/Resolver/ResolverInterface.php b/Imagine/Cache/Resolver/ResolverInterface.php index 283403a5b..98c97faa5 100644 --- a/Imagine/Cache/Resolver/ResolverInterface.php +++ b/Imagine/Cache/Resolver/ResolverInterface.php @@ -23,7 +23,7 @@ function isStored($path, $filter); * @param string $path The path where the original file is expected to be. * @param string $filter The name of the imagine filter in effect. * - * @return string The URL of the cached image. + * @return string The absolute URL of the cached image. * * @throws NotResolvableException */ diff --git a/Imagine/Cache/Resolver/WebPathResolver.php b/Imagine/Cache/Resolver/WebPathResolver.php index 229da2855..7ee901e9a 100644 --- a/Imagine/Cache/Resolver/WebPathResolver.php +++ b/Imagine/Cache/Resolver/WebPathResolver.php @@ -2,47 +2,107 @@ namespace Liip\ImagineBundle\Imagine\Cache\Resolver; -class WebPathResolver extends AbstractFilesystemResolver +use Liip\ImagineBundle\Binary\BinaryInterface; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Routing\RequestContext; + +class WebPathResolver implements ResolverInterface { /** - * If the file has already been cached, we're probably not rewriting - * correctly, hence make a 301 to proper location, so browser remembers. - * - * Strip the base URL of this request from the browserpath to not interfere with the base path. - * + * @var Filesystem + */ + protected $filesystem; + + /** + * @var RequestContext + */ + protected $requestContext; + + /** + * @var string + */ + protected $webRoot; + /** + * @var string + */ + protected $cachePrefix; + + /** + * @param Filesystem $filesystem + * @param RequestContext $requestContext + * @param string $webRootDir + * @param string $cachePrefix + */ + public function __construct( + Filesystem $filesystem, + RequestContext $requestContext, + $webRootDir, + $cachePrefix = 'media/cache' + ) { + $this->filesystem = $filesystem; + $this->requestContext = $requestContext; + + $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); + $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); + $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; + } + + /** * {@inheritDoc} */ public function resolve($path, $filter) { - $browserPath = $this->decodeBrowserPath($this->getBrowserPath($path, $filter)); - $this->basePath = $this->getRequest()->getBaseUrl(); - - if ($this->basePath && 0 === strpos($browserPath, $this->basePath)) { - $browserPath = substr($browserPath, strlen($this->basePath)); - } + return sprintf('%s://%s/%s', + $this->requestContext->getScheme(), + $this->requestContext->getHost(), + $this->getFileUrl($path, $filter) + ); + } - return $this->getRequest()->getBasePath().$browserPath; + /** + * {@inheritDoc} + */ + public function isStored($path, $filter) + { + return $this->filesystem->exists($this->getFilePath($path, $filter)); } /** * {@inheritDoc} */ - public function getBrowserPath($path, $filter, $absolute = false) + public function store(BinaryInterface $binary, $path, $filter) { - return $this->cacheManager->generateUrl($path, $filter, $absolute); + $this->filesystem->dumpFile( + $this->getFilePath($path, $filter), + $binary->getContent() + ); } /** - * Decodes the URL encoded browser path. - * - * @param string $browserPath - * - * @return string + * {@inheritDoc} */ - protected function decodeBrowserPath($browserPath) + public function remove(array $paths, array $filters) { - //TODO: find out why I need double urldecode to get a valid path - return urldecode(urldecode($browserPath)); + if (empty($paths) && empty($filters)) { + return; + } + + if (empty($paths)) { + $filtersCacheDir = array(); + foreach ($filters as $filter) { + $filtersCacheDir[] = $this->cacheRoot.'/'.$filter; + } + + $this->filesystem->remove($filtersCacheDir); + + return; + } + + foreach ($paths as $path) { + foreach ($filters as $filter) { + $this->filesystem->remove($this->getFilePath($path, $filter)); + } + } } /** @@ -50,6 +110,15 @@ protected function decodeBrowserPath($browserPath) */ protected function getFilePath($path, $filter) { - return $this->cacheManager->getWebRoot().$this->resolve($path, $filter); + return $this->webRoot.'/'.$this->getFileUrl($path, $filter); + } + + /** + * {@inheritDoc} + */ + protected function getFileUrl($path, $filter) + { + return $this->cachePrefix.'/'.$filter.'/'.$path; } } + diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index 17188d286..0b3e3dfb4 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -53,7 +53,7 @@ Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver - Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheResolver + Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheWebPathResolver @@ -80,7 +80,6 @@ - %liip_imagine.web_root% %liip_imagine.cache.resolver.default% @@ -186,24 +185,17 @@ - + - - %liip_imagine.cache.resolver.base_path% - - - %liip_imagine.cache_mkdir_mode% - - - - - - - + + %liip_imagine.web_root% + %liip_imagine.cache_prefix% + - + + diff --git a/Resources/doc/configuration.md b/Resources/doc/configuration.md index 57a7c9332..61902e49f 100644 --- a/Resources/doc/configuration.md +++ b/Resources/doc/configuration.md @@ -7,7 +7,6 @@ liip_imagine: driver: gd web_root: %kernel.root_dir%/../web data_root: %liip_imagine.web_root% - cache_mkdir_mode: 0777 cache_prefix: /media/cache cache: web_path cache_clearer: true @@ -45,12 +44,6 @@ There are several configuration options available: default: `%kernel.root_dir%/../web` - - `cache_mkdir_mode` - permissions to set on generated cache directories. - Must be specified as an octal number, which means it should begin with a - leading zero. mode is ignored on Windows. - - default: `0777` - - `cache_prefix` - this is also used in the path for image generation, so as to not clutter your web root with cached images. For example by default, the images would be written to the `web/media/cache/` directory. diff --git a/Tests/Controller/ImagineControllerTest.php b/Tests/Controller/ImagineControllerTest.php index d2d60cb4e..453e7c279 100644 --- a/Tests/Controller/ImagineControllerTest.php +++ b/Tests/Controller/ImagineControllerTest.php @@ -22,6 +22,7 @@ use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser; use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\RequestContext; /** * @covers Liip\ImagineBundle\Controller\ImagineController @@ -75,16 +76,6 @@ protected function setUp() public function testFilterActionLive() { - $router = $this->createRouterMock(); - $router - ->expects($this->any()) - ->method('generate') - ->with('_imagine_thumbnail', array( - 'path' => 'cats.jpeg' - ), false) - ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) - ; - $dataLoader = new FileSystemLoader( MimeTypeGuesser::getInstance(), ExtensionGuesser::getInstance(), @@ -105,23 +96,31 @@ public function testFilterActionLive() $filterManager = new FilterManager($this->configuration, $this->imagine); $filterManager->addLoader('thumbnail', $filterLoader); - $webPathResolver = new WebPathResolver($this->filesystem); + $webPathResolver = new WebPathResolver( + $this->filesystem, + new RequestContext, + $this->webRoot + ); + + $cacheManager = new CacheManager( + $this->configuration, + $this->createRouterMock(), + 'web_path' + ); - $cacheManager = new CacheManager($this->configuration, $router, $this->webRoot, 'web_path'); $cacheManager->addResolver('web_path', $webPathResolver); $controller = new ImagineController($dataManager, $filterManager, $cacheManager, $this->imagine); - $request = Request::create('/media/cache/thumbnail/cats.jpeg'); - - $webPathResolver->setRequest($request); - $response = $controller->filterAction('cats.jpeg', 'thumbnail'); $filePath = realpath($this->webRoot).'/media/cache/thumbnail/cats.jpeg'; - $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); + + $this->assertEquals('http://localhost/media/cache/thumbnail/cats.jpeg', $response->getTargetUrl()); $this->assertEquals(301, $response->getStatusCode()); + $this->assertTrue(file_exists($filePath)); $this->assertNotEmpty(file_get_contents($filePath)); diff --git a/Tests/Functional/Imagine/Cache/Resolver/NoCacheResolverTest.php b/Tests/Functional/Imagine/Cache/Resolver/NoCacheResolverTest.php deleted file mode 100644 index 0673a4fea..000000000 --- a/Tests/Functional/Imagine/Cache/Resolver/NoCacheResolverTest.php +++ /dev/null @@ -1,24 +0,0 @@ -createClient(); - - self::$kernel->getContainer()->enterScope('request'); - self::$kernel->getContainer()->set('request', new Request, 'request'); - - $resolver = self::$kernel->getContainer()->get('liip_imagine.cache.resolver.no_cache'); - - $this->assertInstanceOf('Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheResolver', $resolver); - } -} diff --git a/Tests/Functional/Imagine/Cache/Resolver/NoCacheWebPathResolverTest.php b/Tests/Functional/Imagine/Cache/Resolver/NoCacheWebPathResolverTest.php new file mode 100644 index 000000000..719a739f1 --- /dev/null +++ b/Tests/Functional/Imagine/Cache/Resolver/NoCacheWebPathResolverTest.php @@ -0,0 +1,19 @@ +createClient(); + + $resolver = self::$kernel->getContainer()->get('liip_imagine.cache.resolver.no_cache_web_path'); + + $this->assertInstanceOf('Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheWebPathResolver', $resolver); + } +} diff --git a/Tests/Functional/Imagine/Cache/Resolver/WebPathResolverTest.php b/Tests/Functional/Imagine/Cache/Resolver/WebPathResolverTest.php index f55c90a22..13dd93d3f 100644 --- a/Tests/Functional/Imagine/Cache/Resolver/WebPathResolverTest.php +++ b/Tests/Functional/Imagine/Cache/Resolver/WebPathResolverTest.php @@ -6,7 +6,6 @@ use Symfony\Component\HttpFoundation\Request; /** - * @covers Liip\ImagineBundle\Imagine\Cache\Resolver\AbstractFilesystemResolver * @covers Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver */ class WebPathResolverTest extends WebTestCase @@ -15,9 +14,6 @@ public function testCouldBeGetFromContainer() { $this->createClient(); - self::$kernel->getContainer()->enterScope('request'); - self::$kernel->getContainer()->set('request', new Request, 'request'); - $resolver = self::$kernel->getContainer()->get('liip_imagine.cache.resolver.web_path'); $this->assertInstanceOf('Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver', $resolver); diff --git a/Tests/Imagine/Cache/CacheManagerTest.php b/Tests/Imagine/Cache/CacheManagerTest.php index 0bbcc367f..93686c282 100644 --- a/Tests/Imagine/Cache/CacheManagerTest.php +++ b/Tests/Imagine/Cache/CacheManagerTest.php @@ -14,15 +14,9 @@ class CacheManagerTest extends AbstractTest { protected $resolver; - public function testGetWebRoot() - { - $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), $this->fixturesDir.'/assets'); - $this->assertEquals(str_replace('/', DIRECTORY_SEPARATOR, $this->fixturesDir.'/assets'), $cacheManager->getWebRoot()); - } - public function testAddCacheManagerAwareResolver() { - $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), $this->fixturesDir.'/assets'); + $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock()); $resolver = $this->getMock('Liip\ImagineBundle\Tests\Fixtures\CacheManagerAwareResolver'); $resolver @@ -48,7 +42,7 @@ public function testGetBrowserPathWithoutResolver() ))) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), $this->fixturesDir.'/assets', 'default'); + $cacheManager = new CacheManager($config, $this->createRouterMock(), 'default'); $this->setExpectedException('OutOfBoundsException', 'Could not find resolver for "thumbnail" filter type'); $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail', true); @@ -88,7 +82,7 @@ public function testDefaultResolverUsedIfNoneSetOnGetBrowserPath() ->method('generate') ; - $cacheManager = new CacheManager($config, $router, $this->fixturesDir.'/assets', 'default'); + $cacheManager = new CacheManager($config, $router, 'default'); $cacheManager->addResolver('default', $resolver); $actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail', true); @@ -129,7 +123,7 @@ public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBr ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) ; - $cacheManager = new CacheManager($config, $router, $this->fixturesDir.'/assets', 'default'); + $cacheManager = new CacheManager($config, $router, 'default'); $cacheManager->addResolver('default', $resolver); $actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail', true); @@ -142,7 +136,7 @@ public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBr */ public function testResolveInvalidPath($path) { - $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), $this->fixturesDir.'/assets'); + $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock()); $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); $cacheManager->resolve($path, 'thumbnail'); @@ -150,7 +144,7 @@ public function testResolveInvalidPath($path) public function testThrowsIfConcreteResolverNotExists() { - $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), $this->fixturesDir.'/assets'); + $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock()); $this->setExpectedException('OutOfBoundsException', 'Could not find resolver for "thumbnail" filter type'); $this->assertFalse($cacheManager->resolve('cats.jpeg', 'thumbnail')); @@ -191,7 +185,7 @@ public function testFallbackToDefaultResolver() ))) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), $this->fixturesDir.'/assets', 'default'); + $cacheManager = new CacheManager($config, $this->createRouterMock(), 'default'); $cacheManager->addResolver('default', $resolver); // Resolve fallback to default resolver @@ -248,7 +242,7 @@ public function testGenerateUrl($filterConfig, $path, $expectedPath) ), true) ; - $cacheManager = new CacheManager($config, $router, $this->fixturesDir.'/assets'); + $cacheManager = new CacheManager($config, $router); $cacheManager->generateUrl($path, 'thumbnail', true); } @@ -275,7 +269,7 @@ public function testRemoveCacheForPathAndFilterOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), 'aWebRoot'); + $cacheManager = new CacheManager($config, $this->createRouterMock()); $cacheManager->addResolver($expectedFilter, $resolver); $cacheManager->remove($expectedPath, $expectedFilter); @@ -312,7 +306,7 @@ public function testRemoveCacheForPathAndSomeFiltersOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), 'aWebRoot'); + $cacheManager = new CacheManager($config, $this->createRouterMock()); $cacheManager->addResolver($expectedFilterOne, $resolverOne); $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); @@ -346,7 +340,7 @@ public function testRemoveCacheForSomePathsAndFilterOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), 'aWebRoot'); + $cacheManager = new CacheManager($config, $this->createRouterMock()); $cacheManager->addResolver($expectedFilter, $resolver); $cacheManager->remove(array($expectedPathOne, $expectedPathTwo), $expectedFilter); @@ -384,7 +378,7 @@ public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), 'aWebRoot'); + $cacheManager = new CacheManager($config, $this->createRouterMock()); $cacheManager->addResolver($expectedFilterOne, $resolverOne); $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); @@ -432,7 +426,7 @@ public function testRemoveCacheForAllFiltersOnRemove() ))) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), 'aWebRoot'); + $cacheManager = new CacheManager($config, $this->createRouterMock()); $cacheManager->addResolver($expectedFilterOne, $resolverOne); $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); @@ -478,7 +472,7 @@ public function testRemoveCacheForPathAndAllFiltersOnRemove() ))) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), 'aWebRoot'); + $cacheManager = new CacheManager($config, $this->createRouterMock()); $cacheManager->addResolver($expectedFilterOne, $resolverOne); $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); @@ -508,7 +502,7 @@ public function testAggregateFiltersByResolverOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), 'aWebRoot'); + $cacheManager = new CacheManager($config, $this->createRouterMock()); $cacheManager->addResolver($expectedFilterOne, $resolver); $cacheManager->addResolver($expectedFilterTwo, $resolver); diff --git a/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php b/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php deleted file mode 100644 index d01126c4f..000000000 --- a/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php +++ /dev/null @@ -1,154 +0,0 @@ -markTestSkipped('file_get_contents can not read files with utf-8 file names on windows'); - } - - $image = $this->fixturesDir.'/assets/АГГЗ.jpeg'; - - $data = file_get_contents($image); - - $binary = new Binary($data, 'image/jpeg', 'jpeg'); - - $filePath = $this->tempDir.'/cached/АГГЗ.jpeg'; - - $resolver = $this->getMockAbstractFilesystemResolver(new Filesystem()); - $resolver - ->expects($this->once()) - ->method('getFilePath') - ->will($this->returnValue($filePath)) - ; - - $resolver->store($binary, '/a/path', 'mirror'); - - $this->assertTrue(file_exists($filePath)); - $this->assertEquals($data, file_get_contents($filePath)); - } - - public function testUsePathAndFilterToGetFilePath() - { - if (false !== strpos(strtolower(PHP_OS), 'win')) { - $this->markTestSkipped('mkdir mode is ignored on windows'); - } - - $expectedPath = '/a/path'; - $expectedFilter = 'thumbnail'; - $expectedFilePath = $this->tempDir . '/cats.jpeg'; - - $resolver = $this->getMockAbstractFilesystemResolver(new Filesystem()); - $resolver - ->expects($this->once()) - ->method('getFilePath') - ->with($expectedPath, $expectedFilter) - ->will($this->returnValue($expectedFilePath)) - ; - - $binary = new Binary('theImageContent', 'image/jpeg', 'jpg'); - - $resolver->store($binary, $expectedPath, $expectedFilter); - } - - public function testStoreBinaryContentToFilePath() - { - if (false !== strpos(strtolower(PHP_OS), 'win')) { - $this->markTestSkipped('mkdir mode is ignored on windows'); - } - - $expectedFilePath = $this->tempDir . '/cats.jpeg'; - - $resolver = $this->getMockAbstractFilesystemResolver(new Filesystem()); - $resolver - ->expects($this->once()) - ->method('getFilePath') - ->will($this->returnValue($expectedFilePath)) - ; - - $binary = new Binary('theImageContent', 'image/jpeg', 'jpeg'); - - $resolver->store($binary, '/a/path', 'thumbnail'); - $this->assertFileExists($expectedFilePath); - $this->assertEquals('theImageContent', file_get_contents($expectedFilePath)); - } - - public function testMkdirVerifyPermissionOnLastLevel() - { - if (false !== strpos(strtolower(PHP_OS), 'win')) { - $this->markTestSkipped('mkdir mode is ignored on windows'); - } - - $filePath = $this->tempDir . '/first-level/second-level/cats.jpeg'; - - $resolver = $this->getMockAbstractFilesystemResolver(new Filesystem()); - $resolver - ->expects($this->once()) - ->method('getFilePath') - ->will($this->returnValue($filePath)) - ; - - $resolver->store(new Binary('aContent', 'image/jpeg', 'jpg'), '/a/path', 'thumbnail'); - $this->assertEquals(040777, fileperms($this->tempDir . '/first-level/second-level')); - } - - public function testMkdirVerifyPermissionOnFirstLevel() - { - if (false !== strpos(strtolower(PHP_OS), 'win')) { - $this->markTestSkipped('mkdir mode is ignored on windows'); - } - - $filePath = $this->tempDir . '/first-level/second-level/cats.jpeg'; - - $resolver = $this->getMockAbstractFilesystemResolver(new Filesystem()); - $resolver - ->expects($this->once()) - ->method('getFilePath') - ->will($this->returnValue($filePath)) - ; - - $resolver->store(new Binary('aContent', 'image/jpeg', 'jpg'), '/a/path', 'thumbnail'); - $this->assertEquals(040777, fileperms($this->tempDir . '/first-level')); - } - - public function testStoreInvalidDirectory() - { - if (false !== strpos(strtolower(PHP_OS), 'win')) { - $this->markTestSkipped('mkdir mode is ignored on windows'); - } - - $filePath = $this->tempDir.'/unwriteable/thumbnail/cats.jpeg'; - - $resolver = $this->getMockAbstractFilesystemResolver(new Filesystem()); - $resolver - ->expects($this->once()) - ->method('getFilePath') - ->will($this->returnValue($filePath)) - ; - - $this->filesystem->mkdir($this->tempDir.'/unwriteable', 0555); - - $this->setExpectedException('RuntimeException', 'Could not create directory '.dirname($this->tempDir.'/unwriteable/thumbnail/cats.jpeg')); - $resolver->store(new Binary('aContent', 'image/jpeg', 'jpg'), '/a/path', 'thumbnail'); - } - - /** - * @param Filesystem $filesystem - * - * @return \PHPUnit_Framework_MockObject_MockObject|\Liip\ImagineBundle\Imagine\Cache\Resolver\AbstractFilesystemResolver - */ - protected function getMockAbstractFilesystemResolver(Filesystem $filesystem) - { - return $this->getMock('Liip\ImagineBundle\Imagine\Cache\Resolver\AbstractFilesystemResolver', array('resolve', 'getBrowserPath', 'getFilePath'), array($filesystem)); - } -} diff --git a/Tests/Imagine/Cache/Resolver/NoCacheResolverTest.php b/Tests/Imagine/Cache/Resolver/NoCacheResolverTest.php deleted file mode 100644 index a9187f45d..000000000 --- a/Tests/Imagine/Cache/Resolver/NoCacheResolverTest.php +++ /dev/null @@ -1,77 +0,0 @@ -setRequest($request); - - $this->assertEquals('http://foo.com/a/path', $resolver->resolve('a/path', 'aFilter')); - } - - public function testReturnAbsoluteUrlOfOriginalImageExcludingBaseUrlOnResolve() - { - $request = Request::create('http://foo.com'); - $request->server->set('SCRIPT_FILENAME', 'app.php'); - $request->server->set('SCRIPT_NAME', 'app.php'); - - // guard - $this->assertNotNull($request->getBaseUrl()); - - $resolver = new NoCacheResolver(new Filesystem); - $resolver->setRequest($request); - - $this->assertEquals('http://foo.com/a/path', $resolver->resolve('a/path', 'aFilter')); - } - - public function testDoNothingOnStore() - { - $resolver = new NoCacheResolver(new Filesystem); - $resolver->setRequest(null); - - $this->assertNull($resolver->store( - new Binary('aContent', 'image/jpeg', 'jpg'), - 'a/path', - 'aFilter') - ); - } - - public function testDoNothingForPathAndFilterOnRemove() - { - $resolver = new NoCacheResolver(new Filesystem); - $resolver->setRequest(null); - - $resolver->remove(array('a/path'), array('aFilter')); - } - - public function testDoNothingForSomePathsAndSomeFiltersOnRemove() - { - $resolver = new NoCacheResolver(new Filesystem); - $resolver->setRequest(null); - - $resolver->remove(array('foo', 'bar'), array('foo', 'bar')); - } - - public function testDoNothingForEmptyPathAndEmptyFilterOnRemove() - { - $resolver = new NoCacheResolver(new Filesystem); - $resolver->setRequest(null); - - $resolver->remove(array(), array()); - } -} diff --git a/Tests/Imagine/Cache/Resolver/NoCacheWebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/NoCacheWebPathResolverTest.php new file mode 100644 index 000000000..074354ff3 --- /dev/null +++ b/Tests/Imagine/Cache/Resolver/NoCacheWebPathResolverTest.php @@ -0,0 +1,62 @@ +assertEquals('theschema://theHost/aPath', $resolver->resolve('aPath', 'aFilter')); + } + + public function testDoNothingOnStore() + { + $resolver = new NoCacheWebPathResolver(new RequestContext); + + $this->assertNull($resolver->store( + new Binary('aContent', 'image/jpeg', 'jpg'), + 'a/path', + 'aFilter' + )); + } + + public function testDoNothingForPathAndFilterOnRemove() + { + $resolver = new NoCacheWebPathResolver(new RequestContext); + + $resolver->remove(array('a/path'), array('aFilter')); + } + + public function testDoNothingForSomePathsAndSomeFiltersOnRemove() + { + $resolver = new NoCacheWebPathResolver(new RequestContext); + + $resolver->remove(array('foo', 'bar'), array('foo', 'bar')); + } + + public function testDoNothingForEmptyPathAndEmptyFilterOnRemove() + { + $resolver = new NoCacheWebPathResolver(new RequestContext); + + $resolver->remove(array(), array()); + } +} diff --git a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php index 28aa46a04..ba3463e33 100644 --- a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php @@ -5,390 +5,320 @@ use Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver; use Liip\ImagineBundle\Model\Binary; use Liip\ImagineBundle\Tests\AbstractTest; -use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Routing\RequestContext; /** - * @covers Liip\ImagineBundle\Imagine\Cache\Resolver\AbstractFilesystemResolver * @covers Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver */ -class WebPathResolverTest extends AbstractTest +class WebPathResolverTest extends \PHPUnit_Framework_TestCase { - protected $config; - protected $cacheManager; - - /** - * @var WebPathResolver - */ - protected $resolver; - - protected $webRoot; - protected $dataRoot; - protected $cacheDir; - - protected function setUp() + public function testImplementsResolverInterface() { - parent::setUp(); - - $this->config = $this->createFilterConfigurationMock(); - $this->config - ->expects($this->any()) - ->method('get') - ->with('thumbnail') - ->will($this->returnValue(array( - 'size' => array(180, 180), - 'mode' => 'outbound', - 'cache' => null, - ))) - ; + $rc = new \ReflectionClass('Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver'); - $this->webRoot = $this->tempDir.'/root/web'; - $this->dataRoot = $this->fixturesDir.'/assets'; - $this->cacheDir = $this->webRoot.'/media/cache'; + $this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface')); + } - $this->filesystem->mkdir($this->cacheDir); + public function testCouldBeConstructedWithRequiredArguments() + { + $filesystemMock = $this->createFilesystemMock(); + $requestContext = new RequestContext; + $webRoot = 'theWebRoot'; - $this->cacheManager = $this->getMock('Liip\ImagineBundle\Imagine\Cache\CacheManager', array( - 'generateUrl', - ), array( - $this->config, $this->createRouterMock(), $this->webRoot, 'web_path' - )); + $resolver = new WebPathResolver($filesystemMock, $requestContext, $webRoot); - $this->resolver = new WebPathResolver($this->filesystem); - $this->cacheManager->addResolver('web_path', $this->resolver); + $this->assertAttributeSame($filesystemMock, 'filesystem', $resolver); + $this->assertAttributeSame($requestContext, 'requestContext', $resolver); + $this->assertAttributeSame($webRoot, 'webRoot', $resolver); } - public function testDefaultBehavior() + public function testCouldBeConstructedWithOptionalArguments() { - $this->cacheManager - ->expects($this->atLeastOnce()) - ->method('generateUrl') - ->will($this->returnValue(str_replace('/', DIRECTORY_SEPARATOR, '/media/cache/thumbnail/cats.jpeg'))) - ; + $resolver = new WebPathResolver( + $this->createFilesystemMock(), + new RequestContext, + 'aWebRoot', + 'theCachePrefix' + ); - $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); - $request - ->expects($this->atLeastOnce()) - ->method('getBaseUrl') - ->will($this->returnValue('/app.php')) - ; + $this->assertAttributeSame('theCachePrefix', 'cachePrefix', $resolver); + } - $this->resolver->setRequest($request); + public function testTrimRightSlashFromWebPathOnConstruct() + { + $resolver = new WebPathResolver( + $this->createFilesystemMock(), + new RequestContext, + 'aWebRoot/', + 'theCachePrefix' + ); - $path = 'cats.jpeg'; + $this->assertAttributeSame('aWebRoot', 'webRoot', $resolver); + } - // guard - $this->assertFalse($this->resolver->isStored($path, 'thumbnail')); + public function testRemoveDoubleSlashFromWebRootOnConstruct() + { + $resolver = new WebPathResolver( + $this->createFilesystemMock(), + new RequestContext, + 'aWeb//Root', + '/aCachePrefix' + ); - // Store the cached version of that image. - $content = file_get_contents($this->dataRoot.'/cats.jpeg'); - $binary = new Binary($content, 'image/jpeg', 'jpeg'); - $this->assertNull($this->resolver->store($binary, $path, 'thumbnail')); + $this->assertAttributeSame('aWeb/Root', 'webRoot', $resolver); } - /** - * @depends testDefaultBehavior - */ - public function testMissingRewrite() + public function testTrimRightSlashFromCachePrefixOnConstruct() { - $this->cacheManager - ->expects($this->atLeastOnce()) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) - ; + $resolver = new WebPathResolver( + $this->createFilesystemMock(), + new RequestContext, + 'aWebRoot', + '/aCachePrefix' + ); - $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); - $request - ->expects($this->atLeastOnce()) - ->method('getBaseUrl') - ->will($this->returnValue('')) - ; + $this->assertAttributeSame('aCachePrefix', 'cachePrefix', $resolver); + } - $this->resolver->setRequest($request); + public function testRemoveDoubleSlashFromCachePrefixOnConstruct() + { + $resolver = new WebPathResolver( + $this->createFilesystemMock(), + new RequestContext, + 'aWebRoot', + 'aCache//Prefix' + ); - $path = 'cats.jpeg'; - $webFilePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; + $this->assertAttributeSame('aCache/Prefix', 'cachePrefix', $resolver); + } - // The file has already been cached by this resolver. - $this->filesystem->mkdir(dirname($webFilePath)); - file_put_contents($webFilePath, file_get_contents($this->dataRoot.'/cats.jpeg')); + public function testReturnTrueIfFileExistsOnIsStore() + { + $filesystemMock = $this->createFilesystemMock(); + $filesystemMock + ->expects($this->once()) + ->method('exists') + ->with('/aWebRoot/aCachePrefix/aFilter/aPath') + ->will($this->returnValue(true)) + ; - $this->assertEquals( - '/media/cache/thumbnail/cats.jpeg', - $this->resolver->resolve($path, 'thumbnail'), - '->resolve() returns the expected Location of the cached image.' + $resolver = new WebPathResolver( + $filesystemMock, + new RequestContext, + '/aWebRoot', + 'aCachePrefix' ); + + $this->assertTrue($resolver->isStored('aPath', 'aFilter')); } - /** - * @depends testMissingRewrite - */ - public function testMissingRewriteWithBaseUrl() + public function testReturnFalseIfFileNotExistsOnIsStore() { - $this->cacheManager - ->expects($this->atLeastOnce()) - ->method('generateUrl') - ->will($this->returnValue('/app_dev.php/media/cache/thumbnail/cats.jpeg')) - ; - - $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); - $request - ->expects($this->atLeastOnce()) - ->method('getBaseUrl') - ->will($this->returnValue('/app_dev.php')) + $filesystemMock = $this->createFilesystemMock(); + $filesystemMock + ->expects($this->once()) + ->method('exists') + ->with('/aWebRoot/aCachePrefix/aFilter/aPath') + ->will($this->returnValue(false)) ; - $this->resolver->setRequest($request); + $resolver = new WebPathResolver( + $filesystemMock, + new RequestContext, + '/aWebRoot', + 'aCachePrefix' + ); - $path = 'cats.jpeg'; - $filePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; + $this->assertFalse($resolver->isStored('aPath', 'aFilter')); + } - // The file has already been cached by this resolver. - $this->filesystem->mkdir(dirname($filePath)); - file_put_contents($filePath, file_get_contents($this->dataRoot.'/cats.jpeg')); + public function testComposeSchemaHostAndFileUrlOnResolve() + { + $requestContext = new RequestContext; + $requestContext->setScheme('theSchema'); + $requestContext->setHost('theHost'); + + $resolver = new WebPathResolver( + $this->createFilesystemMock(), + $requestContext, + '/aWebRoot', + 'aCachePrefix' + ); $this->assertEquals( - '/media/cache/thumbnail/cats.jpeg', - $this->resolver->resolve($path, 'thumbnail'), - '->resolve() returns the expected url of the cached image.' + 'theschema://theHost/aCachePrefix/aFilter/aPath', + $resolver->resolve('aPath', 'aFilter') ); } - /** - * @depends testDefaultBehavior - */ - public function testResolveWithBasePath() + public function testDumpBinaryContentOnStore() { - $this->cacheManager - ->expects($this->atLeastOnce()) - ->method('generateUrl') - ->will($this->returnValue(str_replace('/', DIRECTORY_SEPARATOR, '/sandbox/app_dev.php/media/cache/thumbnail/cats.jpeg'))) - ; + $binary = new Binary('theContent', 'aMimeType', 'aFormat'); - $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); - $request - ->expects($this->atLeastOnce()) - ->method('getBaseUrl') - ->will($this->returnValue(str_replace('/', DIRECTORY_SEPARATOR, '/sandbox/app_dev.php'))) + $filesystemMock = $this->createFilesystemMock(); + $filesystemMock + ->expects($this->once()) + ->method('dumpFile') + ->with('/aWebRoot/aCachePrefix/aFilter/aPath', 'theContent') ; - $this->resolver->setRequest($request); - - $path = 'cats.jpeg'; - $filePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; - - // guard - $this->assertFalse($this->resolver->isStored($path, 'thumbnail')); - - // Store the cached version of that image. - $content = file_get_contents($this->dataRoot.'/cats.jpeg'); - $binary = new Binary($content, 'image/jpeg', 'jpeg'); - $this->resolver->store($binary, $path, 'thumbnail'); + $resolver = new WebPathResolver( + $filesystemMock, + new RequestContext, + '/aWebRoot', + 'aCachePrefix' + ); - $this->assertTrue(file_exists($filePath), - '->store() creates the cached image file to be served.'); - $this->assertEquals($content, file_get_contents($filePath), - '->store() writes the content of the original binary into the cache file.'); + $this->assertNull($resolver->store($binary, 'aPath', 'aFilter')); } - /** - * @depends testMissingRewrite - * @depends testResolveWithBasePath - */ - public function testMissingRewriteWithBasePathWithScriptname() + public function testDoNothingIfFiltersAndPathsEmptyOnRemove() { - $this->cacheManager - ->expects($this->atLeastOnce()) - ->method('generateUrl') - ->will($this->returnValue('/sandbox/app_dev.php/media/cache/thumbnail/cats.jpeg')) - ; - - $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); - $request - ->expects($this->atLeastOnce()) - ->method('getBasePath') - ->will($this->returnValue('/sandbox')) - ; - $request - ->expects($this->atLeastOnce()) - ->method('getBaseUrl') - ->will($this->returnValue('/sandbox/app_dev.php')) + $filesystemMock = $this->createFilesystemMock(); + $filesystemMock + ->expects($this->never()) + ->method('remove') ; - $this->resolver->setRequest($request); - - $path = 'cats.jpeg'; - $filePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; - - // guard - $this->assertFalse($this->resolver->isStored($path, 'thumbnail')); - - $this->filesystem->mkdir(dirname($filePath)); - file_put_contents($filePath, file_get_contents($this->dataRoot.'/cats.jpeg')); - - $this->assertEquals( - '/sandbox/media/cache/thumbnail/cats.jpeg', - $this->resolver->resolve($path, 'thumbnail'), - '->resolve() returns the expected Location of the cached image.' + $resolver = new WebPathResolver( + $filesystemMock, + new RequestContext, + '/aWebRoot', + 'aCachePrefix' ); - } - public function testThrowIfRequestNotSetOnResolve() - { - $this->resolver->setRequest(null); - - $this->setExpectedException('LogicException', 'The request was not injected, inject it before using resolver.'); - $this->resolver->resolve('/a/path', 'aFilter'); - } - - public function testDoNothingIfFiltersAndPathsEmptyOnRemove() - { - $this->resolver->remove(array(), array()); + $resolver->remove(array(), array()); } public function testRemoveCacheForPathAndFilterOnRemove() { - $this->cacheManager - ->expects($this->atLeastOnce()) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) + $filesystemMock = $this->createFilesystemMock(); + $filesystemMock + ->expects($this->once()) + ->method('remove') + ->with('/aWebRoot/aCachePrefix/aFilter/aPath') ; - $expectedFilePath = $this->createCache('cats.jpeg', 'thumbnail'); - - $this->resolver->setRequest(Request::create('/')); - - $this->resolver->remove(array('cats.jpeg'), array('thumbnail')); + $resolver = new WebPathResolver( + $filesystemMock, + new RequestContext, + '/aWebRoot', + 'aCachePrefix' + ); - $this->assertFileNotExists($expectedFilePath); + $resolver->remove(array('aPath'), array('aFilter')); } public function testRemoveCacheForSomePathsAndFilterOnRemove() { - $this->cacheManager + $filesystemMock = $this->createFilesystemMock(); + $filesystemMock ->expects($this->at(0)) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) + ->method('remove') + ->with('/aWebRoot/aCachePrefix/aFilter/aPathOne') ; - $this->cacheManager + $filesystemMock ->expects($this->at(1)) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/thumbnail/cats.gif')) + ->method('remove') + ->with('/aWebRoot/aCachePrefix/aFilter/aPathTwo') ; - $expectedFilePathOne = $this->createCache('cats.jpeg', 'thumbnail'); - $expectedFilePathTwo = $this->createCache('dogs.jpeg', 'thumbnail'); - - $this->resolver->setRequest(Request::create('/')); - - $this->resolver->remove(array('cats.jpeg', 'dogs.jpeg'), array('thumbnail')); + $resolver = new WebPathResolver( + $filesystemMock, + new RequestContext, + '/aWebRoot', + 'aCachePrefix' + ); - $this->assertFileNotExists($expectedFilePathOne); - $this->assertFileNotExists($expectedFilePathTwo); + $resolver->remove(array('aPathOne', 'aPathTwo'), array('aFilter')); } public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() { - $this->cacheManager + $filesystemMock = $this->createFilesystemMock(); + $filesystemMock ->expects($this->at(0)) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/filterOne/cats.jpeg')) + ->method('remove') + ->with('/aWebRoot/aCachePrefix/aFilterOne/aPathOne') ; - $this->cacheManager + $filesystemMock ->expects($this->at(1)) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/filterOne/cats.gif')) + ->method('remove') + ->with('/aWebRoot/aCachePrefix/aFilterTwo/aPathOne') ; - $this->cacheManager - ->expects($this->at(3)) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/filterTwo/cats.jpeg')) + $filesystemMock + ->expects($this->at(2)) + ->method('remove') + ->with('/aWebRoot/aCachePrefix/aFilterOne/aPathTwo') ; - $this->cacheManager - ->expects($this->at(4)) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/filterTwo/cats.gif')) + $filesystemMock + ->expects($this->at(3)) + ->method('remove') + ->with('/aWebRoot/aCachePrefix/aFilterTwo/aPathTwo') ; - $expectedFilePathOne = $this->createCache('cats.jpeg', 'filterOne'); - $expectedFilePathTwo = $this->createCache('dogs.jpeg', 'filterOne'); - $expectedFilePathThree = $this->createCache('cats.jpeg', 'filterTwo'); - $expectedFilePathFour = $this->createCache('dogs.jpeg', 'filterTwo'); - - $this->resolver->setRequest(Request::create('/')); - - $this->resolver->remove( - array('cats.jpeg', 'dogs.jpeg'), - array('filterOne', 'filterTwo') + $resolver = new WebPathResolver( + $filesystemMock, + new RequestContext, + '/aWebRoot', + 'aCachePrefix' ); - $this->assertFileNotExists($expectedFilePathOne); - $this->assertFileNotExists($expectedFilePathTwo); - $this->assertFileNotExists($expectedFilePathThree); - $this->assertFileNotExists($expectedFilePathFour); - } - - public function testDoNothingWhenFileNotExistForPathAndFilterOnRemove() - { - $this->cacheManager - ->expects($this->atLeastOnce()) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) - ; - - $this->resolver->setRequest(Request::create('/')); - - $this->resolver->remove(array('cats.jpeg'), array('thumbnail')); + $resolver->remove( + array('aPathOne', 'aPathTwo'), + array('aFilterOne', 'aFilterTwo') + ); } public function testRemoveCacheForFilterOnRemove() { - $this->cacheManager - ->expects($this->atLeastOnce()) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/filter/cats.jpeg')) + $filesystemMock = $this->createFilesystemMock(); + $filesystemMock + ->expects($this->once()) + ->method('remove') + ->with(array( + '/aWebRoot/aCachePrefix/aFilter', + )) ; - $filePathOne = $this->createCache('cats.jpeg', 'filter'); - $filePathTwo = $this->createCache('dogs.jpeg', 'filter'); - - $this->resolver->setRequest(Request::create('/')); - - $this->resolver->remove(array(), array('filter')); + $resolver = new WebPathResolver( + $filesystemMock, + new RequestContext, + '/aWebRoot', + 'aCachePrefix' + ); - $this->assertFileNotExists($filePathOne); - $this->assertFileNotExists($filePathTwo); + $resolver->remove(array(), array('aFilter')); } public function testRemoveCacheForSomeFiltersOnRemove() { - $this->cacheManager - ->expects($this->atLeastOnce()) - ->method('generateUrl') - ->will($this->returnValue('/media/cache/filterOne/cats.jpeg')) + $filesystemMock = $this->createFilesystemMock(); + $filesystemMock + ->expects($this->once()) + ->method('remove') + ->with(array( + '/aWebRoot/aCachePrefix/aFilterOne', + '/aWebRoot/aCachePrefix/aFilterTwo' + )) ; - $filePathOne = $this->createCache('cats.jpeg', 'filterOne'); - $filePathTwo = $this->createCache('dogs.jpeg', 'filterOne'); - $filePathThree = $this->createCache('cats.jpeg', 'filterTwo'); - $filePathFour = $this->createCache('dogs.jpeg', 'filterTwo'); - - $this->resolver->setRequest(Request::create('/')); - - $this->resolver->remove(array(), array('filterOne', 'filterTwo')); + $resolver = new WebPathResolver( + $filesystemMock, + new RequestContext, + '/aWebRoot', + 'aCachePrefix' + ); - $this->assertFileNotExists($filePathOne); - $this->assertFileNotExists($filePathTwo); - $this->assertFileNotExists($filePathThree); - $this->assertFileNotExists($filePathFour); + $resolver->remove(array(), array('aFilterOne', 'aFilterTwo')); } - protected function createCache($path, $filter, $fixtureFile = 'cats.jpeg') + /** + * @return \PHPUnit_Framework_MockObject_MockObject|Filesystem + */ + protected function createFilesystemMock() { - $filePath = $this->webRoot."/media/cache/{$filter}/{$path}"; - - $this->filesystem->mkdir(dirname($filePath)); - file_put_contents($filePath, file_get_contents($this->dataRoot.'/'.$fixtureFile)); - - return $filePath; + return $this->getMock('Symfony\Component\Filesystem\Filesystem'); } } diff --git a/UPGRADE.md b/UPGRADE.md index f8149fe4d..e8bb306dc 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -7,6 +7,7 @@ Upgrade * Required minimum symfony version was updated to 2.3. * [CacheResolver] first argument request was removed from `resolve` method. * [CacheResolver] `CacheManager::resolve` may throw OutOfBoundsException if required resolver does not exist. +* [CacheResolver] `CacheManager::resolve` must always return absolute url. * [CacheResolver] Now resolve method has to return the url of the image. * [CacheResolver] New `isStored` method was added. * [CacheResolver] The method `getBrowserPath` was removed. @@ -19,6 +20,9 @@ Upgrade * [CacheResolver] The method `CacheManager::store` accept `BinaryInterface` as first argument. * [CacheResolver] The method `CacheManager::store` return nothing. * [CacheResolver] The method `CacheManager::clearResolversCache` was removed. +* [CacheResolver] The method `CacheManager::getWebRoot` was removed. +* [CacheResolver] `NoCacheResolver` renamed to `NoCacheWebPathResolver`. +* [CacheResolver] `AbstractFilesystemResolver` is removed. * [DataLoader] `LoaderInterface::find` now can return string or `BinaryInterface` instance. * [DataLoader] `DataManager::find` now can return `BinaryInterface` instance only. * [Filter] `FilterManager::applyFilter` now return instance of `BinaryInterface`.