diff --git a/Controller/ImagineController.php b/Controller/ImagineController.php index 11a2cd5e3..ba5dc3d71 100644 --- a/Controller/ImagineController.php +++ b/Controller/ImagineController.php @@ -51,19 +51,14 @@ public function __construct(DataManager $dataManager, FilterManager $filterManag */ public function filterAction(Request $request, $path, $filter) { - $originalImagePath = $path; - $filteredImagePath = $this->cacheManager->resolve($originalImagePath, $filter); - if ($filteredImagePath instanceof Response) { - return $filteredImagePath; + if ($response = $this->cacheManager->resolve($path, $filter)) { + return $response; } - $originalImage = $this->dataManager->find($filter, $originalImagePath); - $response = $this->filterManager->get($request, $filter, $originalImage, $originalImagePath); + $image = $this->dataManager->find($filter, $path); - if ($filteredImagePath) { - $response = $this->cacheManager->store($response, $filteredImagePath, $filter); - } + $response = $this->filterManager->get($request, $filter, $image, $path); - return $response; + return $this->cacheManager->store($response, $path, $filter); } } diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index e494c9180..e762b1fb6 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -164,12 +164,10 @@ public function generateUrl($path, $filter, $absolute = false) /** * Resolves filtered path for rendering in the browser. * - * @param Request $request * @param string $path * @param string $filter * - * @return string|boolean|Response target path or false if filter has no - * resolver or a Response object from the resolver + * @return Response|boolean The response of the respective Resolver or false. * * @throws NotFoundHttpException if the path can not be resolved */ @@ -194,15 +192,15 @@ public function resolve($path, $filter) * @see ResolverInterface::store * * @param Response $response - * @param string $targetPath + * @param string $path * @param string $filter * * @return Response */ - public function store(Response $response, $targetPath, $filter) + public function store(Response $response, $path, $filter) { if ($response->isSuccessful()) { - $response = $this->getResolver($filter)->store($response, $targetPath, $filter); + $response = $this->getResolver($filter)->store($response, $path, $filter); } return $response; @@ -213,14 +211,14 @@ public function store(Response $response, $targetPath, $filter) * * @see ResolverInterface::remove * - * @param string $targetPath + * @param string $path * @param string $filter * * @return bool */ - public function remove($targetPath, $filter) + public function remove($path, $filter) { - return $this->getResolver($filter)->remove($targetPath, $filter); + return $this->getResolver($filter)->remove($path, $filter); } /** diff --git a/Imagine/Cache/Resolver/AbstractFilesystemResolver.php b/Imagine/Cache/Resolver/AbstractFilesystemResolver.php index f556337e6..65d9c5f54 100644 --- a/Imagine/Cache/Resolver/AbstractFilesystemResolver.php +++ b/Imagine/Cache/Resolver/AbstractFilesystemResolver.php @@ -9,7 +9,6 @@ use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Kernel; abstract class AbstractFilesystemResolver implements ResolverInterface, CacheManagerAwareInterface { @@ -83,23 +82,17 @@ public function setFolderPermissions ($folderPermissions) } /** - * Stores the content into a static file. - * - * @param Response $response - * @param string $targetPath - * @param string $filter - * - * @return Response - * - * @throws \RuntimeException + * {@inheritDoc} */ - public function store(Response $response, $targetPath, $filter) + public function store(Response $response, $path, $filter) { - $dir = pathinfo($targetPath, PATHINFO_DIRNAME); + $filePath = $this->getFilePath($path, $filter); + + $dir = pathinfo($filePath, PATHINFO_DIRNAME); $this->makeFolder($dir); - file_put_contents($targetPath, $response->getContent()); + file_put_contents($filePath, $response->getContent()); $response->setStatusCode(201); @@ -109,17 +102,19 @@ public function store(Response $response, $targetPath, $filter) /** * Removes a stored image resource. * - * @param string $targetPath The target path provided by the resolve method. + * @param string $path The target path provided by the resolve method. * @param string $filter The name of the imagine filter in effect. * * @return bool Whether the file has been removed successfully. */ - public function remove($targetPath, $filter) + public function remove($path, $filter) { - $filename = $this->getFilePath($targetPath, $filter); - $this->filesystem->remove($filename); + $this->basePath = $this->getRequest()->getBaseUrl(); + $filePath = $this->getFilePath($path, $filter); + + $this->filesystem->remove($filePath); - return !file_exists($filename); + return !file_exists($filePath); } /** @@ -140,7 +135,7 @@ protected function getRequest() * @param string $dir * @throws \RuntimeException */ - protected function makeFolder ($dir) + protected function makeFolder($dir) { if (!is_dir($dir)) { $parent = dirname($dir); diff --git a/Imagine/Cache/Resolver/AmazonS3Resolver.php b/Imagine/Cache/Resolver/AmazonS3Resolver.php index a6ad3b10f..7b2227135 100644 --- a/Imagine/Cache/Resolver/AmazonS3Resolver.php +++ b/Imagine/Cache/Resolver/AmazonS3Resolver.php @@ -87,16 +87,16 @@ public function resolve($path, $filter) if ($this->objectExists($objectPath)) { return new RedirectResponse($this->getObjectUrl($objectPath), 301); } - - return $objectPath; } /** * {@inheritDoc} */ - public function store(Response $response, $targetPath, $filter) + public function store(Response $response, $path, $filter) { - $storageResponse = $this->storage->create_object($this->bucket, $targetPath, array( + $objectPath = $this->getObjectPath($path, $filter); + + $storageResponse = $this->storage->create_object($this->bucket, $objectPath, array( 'body' => $response->getContent(), 'contentType' => $response->headers->get('Content-Type'), 'length' => strlen($response->getContent()), @@ -105,11 +105,11 @@ public function store(Response $response, $targetPath, $filter) if ($storageResponse->isOK()) { $response->setStatusCode(301); - $response->headers->set('Location', $this->getObjectUrl($targetPath)); + $response->headers->set('Location', $this->getObjectUrl($objectPath)); } else { if ($this->logger) { $this->logger->warning('The object could not be created on Amazon S3.', array( - 'targetPath' => $targetPath, + 'objectPath' => $objectPath, 'filter' => $filter, 's3_response' => $storageResponse, )); @@ -135,14 +135,16 @@ public function getBrowserPath($path, $filter, $absolute = false) /** * {@inheritDoc} */ - public function remove($targetPath, $filter) + public function remove($path, $filter) { - if (!$this->objectExists($targetPath)) { - // A non-existing object to delete: done! - return true; + $objectPath = $this->getObjectPath($path, $filter); + + if ($this->objectExists($objectPath)) { + return $this->storage->delete_object($this->bucket, $objectPath)->isOK(); } - return $this->storage->delete_object($this->bucket, $targetPath)->isOK(); + // A non-existing object to delete: done! + return true; } /** @@ -188,13 +190,13 @@ protected function getObjectPath($path, $filter) /** * Returns the URL for an object saved on Amazon S3. * - * @param string $targetPath + * @param string $path * * @return string */ - protected function getObjectUrl($targetPath) + protected function getObjectUrl($path) { - return $this->storage->get_object_url($this->bucket, $targetPath, 0, $this->objUrlOptions); + return $this->storage->get_object_url($this->bucket, $path, 0, $this->objUrlOptions); } /** diff --git a/Imagine/Cache/Resolver/AwsS3Resolver.php b/Imagine/Cache/Resolver/AwsS3Resolver.php index eb1128994..4392da6ee 100644 --- a/Imagine/Cache/Resolver/AwsS3Resolver.php +++ b/Imagine/Cache/Resolver/AwsS3Resolver.php @@ -87,27 +87,27 @@ public function resolve($path, $filter) if ($this->objectExists($objectPath)) { return new RedirectResponse($this->getObjectUrl($objectPath), 301); } - - return $objectPath; } /** * {@inheritDoc} */ - public function store(Response $response, $targetPath, $filter) + public function store(Response $response, $path, $filter) { + $objectPath = $this->getObjectPath($path, $filter); + try { $storageResponse = $this->storage->putObject(array( 'ACL' => $this->acl, 'Bucket' => $this->bucket, - 'Key' => $targetPath, + 'Key' => $objectPath, 'Body' => $response->getContent(), 'ContentType' => $response->headers->get('Content-Type') )); } catch (\Exception $e) { if ($this->logger) { $this->logger->warning('The object could not be created on Amazon S3.', array( - 'targetPath' => $targetPath, + 'objectPath' => $objectPath, 'filter' => $filter, )); } @@ -137,17 +137,19 @@ public function getBrowserPath($path, $filter, $absolute = false) /** * {@inheritDoc} */ - public function remove($targetPath, $filter) + public function remove($path, $filter) { - if (!$this->objectExists($targetPath)) { + $objectPath = $this->getObjectPath($path, $filter); + + if (!$this->objectExists($objectPath)) { // A non-existing object to delete: done! return true; } try { - $response = $this->storage->deleteObject(array( + $this->storage->deleteObject(array( 'Bucket' => $this->bucket, - 'Key' => $targetPath, + 'Key' => $objectPath, )); return true; @@ -199,13 +201,13 @@ protected function getObjectPath($path, $filter) /** * Returns the URL for an object saved on Amazon S3. * - * @param string $targetPath + * @param string $path * * @return string */ - protected function getObjectUrl($targetPath) + protected function getObjectUrl($path) { - return $this->storage->getObjectUrl($this->bucket, $targetPath, 0, $this->objUrlOptions); + return $this->storage->getObjectUrl($this->bucket, $path, 0, $this->objUrlOptions); } /** diff --git a/Imagine/Cache/Resolver/CacheResolver.php b/Imagine/Cache/Resolver/CacheResolver.php index d9a9957f0..6a3e9aa26 100644 --- a/Imagine/Cache/Resolver/CacheResolver.php +++ b/Imagine/Cache/Resolver/CacheResolver.php @@ -61,29 +61,18 @@ public function resolve($path, $filter) return $this->cache->fetch($key); } - $targetPath = $this->resolver->resolve($path, $filter); - $this->saveToCache($key, $targetPath); + $resolved = $this->resolver->resolve($path, $filter); + $this->saveToCache($key, $resolved); - /* - * The targetPath being a string will be forwarded to the ResolverInterface::store method. - * As there is no way to reverse this operation by the interface, we store this information manually. - * - * If it's not a string, it's a Response it will be returned as it without calling the store method. - */ - if (is_string($targetPath)) { - $reverseKey = $this->generateCacheKey('reverse', $targetPath, $filter); - $this->saveToCache($reverseKey, $path); - } - - return $targetPath; + return $resolved; } /** * {@inheritDoc} */ - public function store(Response $response, $targetPath, $filter) + public function store(Response $response, $path, $filter) { - return $this->resolver->store($response, $targetPath, $filter); + return $this->resolver->store($response, $path, $filter); } /** @@ -108,18 +97,16 @@ public function getBrowserPath($path, $filter, $absolute = false) /** * {@inheritDoc} */ - public function remove($targetPath, $filter) + public function remove($path, $filter) { - $removed = $this->resolver->remove($targetPath, $filter); + $removed = $this->resolver->remove($path, $filter); // If the resolver did not remove the content, we can leave the cache. if ($removed) { - $reverseKey = $this->generateCacheKey('reverse', $targetPath, $filter); - if ($this->cache->contains($reverseKey)) { - $path = $this->cache->fetch($reverseKey); - + $key = $this->generateCacheKey('resolve', $path, $filter); + if ($this->cache->contains($key)) { // The indexKey is not utilizing the method so the value is not important. - $indexKey = $this->generateIndexKey($this->generateCacheKey(null, $path, $filter)); + $indexKey = $this->generateIndexKey($key); // Retrieve the index and remove the content from the cache. $index = $this->cache->fetch($indexKey); @@ -129,7 +116,6 @@ public function remove($targetPath, $filter) // Remove the auxiliary keys. $this->cache->delete($indexKey); - $this->cache->delete($reverseKey); } } diff --git a/Imagine/Cache/Resolver/NoCacheResolver.php b/Imagine/Cache/Resolver/NoCacheResolver.php index d28c38593..e0448a136 100644 --- a/Imagine/Cache/Resolver/NoCacheResolver.php +++ b/Imagine/Cache/Resolver/NoCacheResolver.php @@ -14,15 +14,12 @@ class NoCacheResolver extends WebPathResolver */ public function resolve($path, $filter) { - $this->setBasePath($this->getRequest()->getBaseUrl()); - - return $this->getFilePath($path, $filter); } /** * {@inheritDoc} */ - public function store(Response $response, $targetPath, $filter) + public function store(Response $response, $path, $filter) { return $response; } @@ -30,7 +27,7 @@ public function store(Response $response, $targetPath, $filter) /** * {@inheritDoc} */ - public function remove($targetPath, $filter) + public function remove($path, $filter) { return true; } diff --git a/Imagine/Cache/Resolver/ResolverInterface.php b/Imagine/Cache/Resolver/ResolverInterface.php index f873b88ec..a16f3ad92 100644 --- a/Imagine/Cache/Resolver/ResolverInterface.php +++ b/Imagine/Cache/Resolver/ResolverInterface.php @@ -9,11 +9,10 @@ interface ResolverInterface /** * Resolves filtered path for rendering in the browser. * - * @param string $path The path where the resolved file is expected. + * @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|Response The target path to be used in other methods of this Resolver, - * a Response may be returned to avoid calling store upon resolution. + * @return Response An HTTP response that either contains image content or redirects to a URL to load the image from. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException In case the path can not be resolved. */ @@ -23,17 +22,17 @@ function resolve($path, $filter); * Stores the content of the given Response. * * @param Response $response The response provided by the _imagine_* filter route. - * @param string $targetPath The target path provided by the resolve method. - * @param string $filter The name of the imagine filter in effect. + * @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 Response The (modified) response to be sent to the browser. */ - function store(Response $response, $targetPath, $filter); + function store(Response $response, $path, $filter); /** * Returns a web accessible URL. * - * @param string $path The path where the resolved file is expected. + * @param string $path The path where the original file is expected to be. * @param string $filter The name of the imagine filter in effect. * @param bool $absolute Whether to generate an absolute URL or a relative path is accepted. * In case the resolver does not support relative paths, it may ignore this flag. @@ -43,14 +42,14 @@ function store(Response $response, $targetPath, $filter); function getBrowserPath($path, $filter, $absolute = false); /** - * Removes a stored image resource. + * Removes a cached image resource. * - * @param string $targetPath The target path provided by the resolve method. + * @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 bool Whether the file has been removed successfully. */ - function remove($targetPath, $filter); + function remove($path, $filter); /** * Clear the CacheResolver cache. diff --git a/Imagine/Cache/Resolver/WebPathResolver.php b/Imagine/Cache/Resolver/WebPathResolver.php index 7c261be3b..c508de419 100644 --- a/Imagine/Cache/Resolver/WebPathResolver.php +++ b/Imagine/Cache/Resolver/WebPathResolver.php @@ -14,11 +14,11 @@ public function resolve($path, $filter) { $browserPath = $this->decodeBrowserPath($this->getBrowserPath($path, $filter)); $this->basePath = $this->getRequest()->getBaseUrl(); - $targetPath = $this->getFilePath($path, $filter); + $filePath = $this->getFilePath($path, $filter); // if the file has already been cached, we're probably not rewriting // correctly, hence make a 301 to proper location, so browser remembers - if (file_exists($targetPath)) { + if (file_exists($filePath)) { // Strip the base URL of this request from the browserpath to not interfere with the base path. $baseUrl = $this->getRequest()->getBaseUrl(); if ($baseUrl && 0 === strpos($browserPath, $baseUrl)) { @@ -27,16 +27,14 @@ public function resolve($path, $filter) return new RedirectResponse($this->getRequest()->getBasePath().$browserPath); } - - return $targetPath; } /** * {@inheritDoc} */ - public function getBrowserPath($targetPath, $filter, $absolute = false) + public function getBrowserPath($path, $filter, $absolute = false) { - return $this->cacheManager->generateUrl($targetPath, $filter, $absolute); + return $this->cacheManager->generateUrl($path, $filter, $absolute); } /** diff --git a/Resources/doc/filters.md b/Resources/doc/filters.md index b896485f9..6764aa33a 100644 --- a/Resources/doc/filters.md +++ b/Resources/doc/filters.md @@ -172,9 +172,8 @@ but it illustrates the core idea. ``` php public function filterAction(Request $request, $path, $filter) { - $targetPath = $this->cacheManager->resolve($path, $filter); - if ($targetPath instanceof Response) { - return $targetPath; + if ($response = $this->cacheManager->resolve($path, $filter)) { + return $response; } $image = $this->dataManager->find($filter, $path); @@ -186,9 +185,7 @@ public function filterAction(Request $request, $path, $filter) $response = $this->filterManager->get($request, $filter, $image, $path); - if ($targetPath) { - $response = $this->cacheManager->store($response, $targetPath, $filter); - } + $this->cacheManager->store($response, $path, $filter); return $response; } diff --git a/Resources/doc/introduction.md b/Resources/doc/introduction.md index 757417b55..7737e3eb6 100644 --- a/Resources/doc/introduction.md +++ b/Resources/doc/introduction.md @@ -47,15 +47,12 @@ The images will be created upon first request and will remain in their static ca A `CacheResolver` implements the `Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface`. -It knows about two different, important paths: - -1. The so-called `path`, which is the identifier you use, when addressing the original image, e.g. in your template. This path relates to the path used in the `DataLoader`. -2. The `targetPath`, which is a representation of the image filepath on the resolver itself. +It handles the so-called `path`, which is the identifier you use, when addressing the original image, e.g. in your template. This path relates to the path used in the `DataLoader`. The responsibilities of the `CacheResolver` are: -1. to resolve a given `path` into a `targetPath`, -2. store a cached image under the resolved `targetPath`, +1. to resolve a given `path` into a `Response`, if possible, +2. store given content under a given `path` to be resolved later, 3. generate an URI to address the cached image directly, 4. remove a cached image. diff --git a/Tests/Controller/ImagineControllerTest.php b/Tests/Controller/ImagineControllerTest.php index d74ac3c46..d7989c461 100644 --- a/Tests/Controller/ImagineControllerTest.php +++ b/Tests/Controller/ImagineControllerTest.php @@ -105,12 +105,12 @@ public function testFilterActionLive() $response = $controller->filterAction($request, 'cats.jpeg', 'thumbnail'); - $targetPath = realpath($this->webRoot).'/media/cache/thumbnail/cats.jpeg'; + $filePath = realpath($this->webRoot).'/media/cache/thumbnail/cats.jpeg'; $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); $this->assertEquals(201, $response->getStatusCode()); - $this->assertTrue(file_exists($targetPath)); - $this->assertNotEmpty(file_get_contents($targetPath)); + $this->assertTrue(file_exists($filePath)); + $this->assertNotEmpty(file_get_contents($filePath)); return $controller; } diff --git a/Tests/Imagine/Cache/CacheManagerTest.php b/Tests/Imagine/Cache/CacheManagerTest.php index 3e4ce39a2..56e8e460f 100644 --- a/Tests/Imagine/Cache/CacheManagerTest.php +++ b/Tests/Imagine/Cache/CacheManagerTest.php @@ -194,7 +194,7 @@ public function generateUrlProvider() /** * @dataProvider generateUrlProvider */ - public function testGenerateUrl($filterConfig, $targetPath, $expectedPath) + public function testGenerateUrl($filterConfig, $path, $expectedPath) { $config = $this->getMockFilterConfiguration(); $config @@ -214,6 +214,6 @@ public function testGenerateUrl($filterConfig, $targetPath, $expectedPath) ; $cacheManager = new CacheManager($config, $router, $this->fixturesDir.'/assets'); - $cacheManager->generateUrl($targetPath, 'thumbnail', true); + $cacheManager->generateUrl($path, 'thumbnail', true); } } diff --git a/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php b/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php index 4c5adcc88..d24dc57fa 100644 --- a/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php @@ -25,35 +25,97 @@ public function testStoreCyrillicFilename() 'content-type' => 'image/jpeg', )); - $targetPath = $this->tempDir.'/cached/АГГЗ.jpeg'; + $filePath = $this->tempDir.'/cached/АГГЗ.jpeg'; $resolver = $this->getMockAbstractFilesystemResolver(new Filesystem()); - $resolver->store($response, $targetPath, 'mirror'); + $resolver + ->expects($this->once()) + ->method('getFilePath') + ->will($this->returnValue($filePath)) + ; - $this->assertTrue(file_exists($targetPath)); - $this->assertEquals($data, file_get_contents($targetPath)); + $resolver->store($response, '/a/path', 'mirror'); + + $this->assertTrue(file_exists($filePath)); + $this->assertEquals($data, file_get_contents($filePath)); } - public function testMkdirVerifyPermissionOnLastLevel () { + 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)) + ; + + $resolver->store(new Response('theImageContent'), $expectedPath, $expectedFilter); + } + + public function testStoreResponseContentToFilePath() + { + 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)) + ; + + $resolver->store(new Response('theImageContent'), '/a/path', 'thumbnail'); + $this->assertFileExists($expectedFilePath); + $this->assertEquals('theImageContent', file_get_contents($expectedFilePath)); + } - $resolver->store(new Response(''), $this->tempDir . '/first-level/second-level/cats.jpeg', 'thumbnail'); + 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 Response(''), '/a/path', 'thumbnail'); $this->assertEquals(040777, fileperms($this->tempDir . '/first-level/second-level')); } - public function testMkdirVerifyPermissionOnFirstLevel () { + 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 Response(''), $this->tempDir . '/first-level/second-level/cats.jpeg', 'thumbnail'); + $resolver->store(new Response(''), '/a/path', 'thumbnail'); $this->assertEquals(040777, fileperms($this->tempDir . '/first-level')); } @@ -63,15 +125,27 @@ public function testStoreInvalidDirectory() $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 Response(''), $this->tempDir.'/unwriteable/thumbnail/cats.jpeg', 'thumbnail'); + $resolver->store(new Response(''), '/a/path', 'thumbnail'); } - protected function getMockAbstractFilesystemResolver($filesystem) + /** + * @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', 'clear', 'getBrowserPath', 'getFilePath'), array($filesystem)); } diff --git a/Tests/Imagine/Cache/Resolver/AmazonS3ResolverTest.php b/Tests/Imagine/Cache/Resolver/AmazonS3ResolverTest.php index 0d82fde6c..16c3c947d 100644 --- a/Tests/Imagine/Cache/Resolver/AmazonS3ResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/AmazonS3ResolverTest.php @@ -19,17 +19,17 @@ public function testNoDoubleSlashesInObjectUrl() $s3 ->expects($this->once()) ->method('if_object_exists') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ->will($this->returnValue(true)) ; $s3 ->expects($this->once()) ->method('get_object_url') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ; $resolver = new AmazonS3Resolver($s3, 'images.example.com'); - $resolver->getBrowserPath('/some-folder/targetpath.jpg', 'thumb'); + $resolver->getBrowserPath('/some-folder/path.jpg', 'thumb'); } public function testObjUrlOptions() @@ -43,12 +43,12 @@ public function testObjUrlOptions() $s3 ->expects($this->once()) ->method('get_object_url') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg', 0, array('torrent' => true)) + ->with('images.example.com', 'thumb/some-folder/path.jpg', 0, array('torrent' => true)) ; $resolver = new AmazonS3Resolver($s3, 'images.example.com'); $resolver->setObjectUrlOption('torrent', true); - $resolver->getBrowserPath('/some-folder/targetpath.jpg', 'thumb'); + $resolver->getBrowserPath('/some-folder/path.jpg', 'thumb'); } public function testBrowserPathNotExisting() @@ -57,7 +57,7 @@ public function testBrowserPathNotExisting() $s3 ->expects($this->once()) ->method('if_object_exists') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ->will($this->returnValue(false)) ; $s3 @@ -69,14 +69,14 @@ public function testBrowserPathNotExisting() $cacheManager ->expects($this->once()) ->method('generateUrl') - ->with('/some-folder/targetpath.jpg', 'thumb', false) - ->will($this->returnValue('/media/cache/thumb/some-folder/targetpath.jpg')) + ->with('/some-folder/path.jpg', 'thumb', false) + ->will($this->returnValue('/media/cache/thumb/some-folder/path.jpg')) ; $resolver = new AmazonS3Resolver($s3, 'images.example.com'); $resolver->setCacheManager($cacheManager); - $this->assertEquals('/media/cache/thumb/some-folder/targetpath.jpg', $resolver->getBrowserPath('/some-folder/targetpath.jpg', 'thumb')); + $this->assertEquals('/media/cache/thumb/some-folder/path.jpg', $resolver->getBrowserPath('/some-folder/path.jpg', 'thumb')); } public function testLogNotCreatedObjects() @@ -125,7 +125,7 @@ public function testCreatedObjectRedirects() $resolver = new AmazonS3Resolver($s3, 'images.example.com'); - $this->assertSame($response, $resolver->store($response, 'thumb/foobar.jpg', 'thumb')); + $this->assertSame($response, $resolver->store($response, 'foobar.jpg', 'thumb')); $this->assertEquals(301, $response->getStatusCode()); $this->assertEquals('http://images.example.com/thumb/foobar.jpg', $response->headers->get('Location')); } @@ -140,9 +140,8 @@ public function testResolveNewObject() ; $resolver = new AmazonS3Resolver($s3, 'images.example.com'); - $targetPath = $resolver->resolve('/some-folder/targetpath.jpg', 'thumb'); - $this->assertEquals('thumb/some-folder/targetpath.jpg', $targetPath); + $this->assertNull($resolver->resolve('/some-folder/path.jpg', 'thumb')); } public function testResolveRedirectsOnExisting() @@ -156,45 +155,45 @@ public function testResolveRedirectsOnExisting() $s3 ->expects($this->once()) ->method('get_object_url') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg', 0, array()) - ->will($this->returnValue('http://images.example.com/some-folder/targetpath.jpg')) + ->with('images.example.com', 'thumb/some-folder/path.jpg', 0, array()) + ->will($this->returnValue('http://images.example.com/some-folder/path.jpg')) ; $resolver = new AmazonS3Resolver($s3, 'images.example.com'); - $response = $resolver->resolve('/some-folder/targetpath.jpg', 'thumb'); + $response = $resolver->resolve('/some-folder/path.jpg', 'thumb'); $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); $this->assertEquals(301, $response->getStatusCode()); - $this->assertEquals('http://images.example.com/some-folder/targetpath.jpg', $response->headers->get('Location')); + $this->assertEquals('http://images.example.com/some-folder/path.jpg', $response->headers->get('Location')); } - public function testRemove() + public function testDeleteObjectIfObjectExistOnAmazonOnRemove() { $s3 = $this->getAmazonS3Mock(); $s3 ->expects($this->once()) ->method('if_object_exists') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ->will($this->returnValue(true)) ; $s3 ->expects($this->once()) ->method('delete_object') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ->will($this->returnValue($this->getCFResponseMock(true))) ; $resolver = new AmazonS3Resolver($s3, 'images.example.com'); - $this->assertTrue($resolver->remove('thumb/some-folder/targetpath.jpg', 'thumb')); + $this->assertTrue($resolver->remove('some-folder/path.jpg', 'thumb')); } - public function testRemoveNotExisting() + public function testDoNothingIfObjectNotExistOnAmazonOnRemove() { $s3 = $this->getAmazonS3Mock(); $s3 ->expects($this->once()) ->method('if_object_exists') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ->will($this->returnValue(false)) ; $s3 @@ -203,7 +202,7 @@ public function testRemoveNotExisting() ; $resolver = new AmazonS3Resolver($s3, 'images.example.com'); - $this->assertTrue($resolver->remove('thumb/some-folder/targetpath.jpg', 'thumb')); + $this->assertTrue($resolver->remove('some-folder/path.jpg', 'thumb')); } public function testClearIsDisabled() diff --git a/Tests/Imagine/Cache/Resolver/AwsS3ResolverTest.php b/Tests/Imagine/Cache/Resolver/AwsS3ResolverTest.php index cf4e1bb90..94abb9ee6 100644 --- a/Tests/Imagine/Cache/Resolver/AwsS3ResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/AwsS3ResolverTest.php @@ -19,17 +19,17 @@ public function testNoDoubleSlashesInObjectUrl() $s3 ->expects($this->once()) ->method('doesObjectExist') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ->will($this->returnValue(true)) ; $s3 ->expects($this->once()) ->method('getObjectUrl') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ; $resolver = new AwsS3Resolver($s3, 'images.example.com'); - $resolver->getBrowserPath('/some-folder/targetpath.jpg', 'thumb'); + $resolver->getBrowserPath('/some-folder/path.jpg', 'thumb'); } public function testObjUrlOptions() @@ -43,12 +43,12 @@ public function testObjUrlOptions() $s3 ->expects($this->once()) ->method('getObjectUrl') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg', 0, array('torrent' => true)) + ->with('images.example.com', 'thumb/some-folder/path.jpg', 0, array('torrent' => true)) ; $resolver = new AwsS3Resolver($s3, 'images.example.com'); $resolver->setObjectUrlOption('torrent', true); - $resolver->getBrowserPath('/some-folder/targetpath.jpg', 'thumb'); + $resolver->getBrowserPath('/some-folder/path.jpg', 'thumb'); } public function testBrowserPathNotExisting() @@ -57,7 +57,7 @@ public function testBrowserPathNotExisting() $s3 ->expects($this->once()) ->method('doesObjectExist') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ->will($this->returnValue(false)) ; $s3 @@ -69,14 +69,14 @@ public function testBrowserPathNotExisting() $cacheManager ->expects($this->once()) ->method('generateUrl') - ->with('/some-folder/targetpath.jpg', 'thumb', false) - ->will($this->returnValue('/media/cache/thumb/some-folder/targetpath.jpg')) + ->with('/some-folder/path.jpg', 'thumb', false) + ->will($this->returnValue('/media/cache/thumb/some-folder/path.jpg')) ; $resolver = new AwsS3Resolver($s3, 'images.example.com'); $resolver->setCacheManager($cacheManager); - $this->assertEquals('/media/cache/thumb/some-folder/targetpath.jpg', $resolver->getBrowserPath('/some-folder/targetpath.jpg', 'thumb')); + $this->assertEquals('/media/cache/thumb/some-folder/path.jpg', $resolver->getBrowserPath('/some-folder/path.jpg', 'thumb')); } public function testLogNotCreatedObjects() @@ -143,9 +143,8 @@ public function testResolveNewObject() ; $resolver = new AwsS3Resolver($s3, 'images.example.com'); - $targetPath = $resolver->resolve('/some-folder/targetpath.jpg', 'thumb'); - $this->assertEquals('thumb/some-folder/targetpath.jpg', $targetPath); + $this->assertNull($resolver->resolve('/some-folder/path.jpg', 'thumb')); } public function testResolveRedirectsOnExisting() @@ -159,25 +158,25 @@ public function testResolveRedirectsOnExisting() $s3 ->expects($this->once()) ->method('getObjectUrl') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg', 0, array()) - ->will($this->returnValue('http://images.example.com/some-folder/targetpath.jpg')) + ->with('images.example.com', 'thumb/some-folder/path.jpg', 0, array()) + ->will($this->returnValue('http://images.example.com/some-folder/path.jpg')) ; $resolver = new AwsS3Resolver($s3, 'images.example.com'); - $response = $resolver->resolve('/some-folder/targetpath.jpg', 'thumb'); + $response = $resolver->resolve('/some-folder/path.jpg', 'thumb'); $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); $this->assertEquals(301, $response->getStatusCode()); - $this->assertEquals('http://images.example.com/some-folder/targetpath.jpg', $response->headers->get('Location')); + $this->assertEquals('http://images.example.com/some-folder/path.jpg', $response->headers->get('Location')); } - public function testRemove() + public function testDeleteObjectIfObjectExistOnAmazonOnRemove() { $s3 = $this->getS3ClientMock(); $s3 ->expects($this->once()) ->method('doesObjectExist') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ->will($this->returnValue(true)) ; $s3 @@ -185,22 +184,22 @@ public function testRemove() ->method('deleteObject') ->with(array( 'Bucket' => 'images.example.com', - 'Key' => 'thumb/some-folder/targetpath.jpg', + 'Key' => 'thumb/some-folder/path.jpg', )) ->will($this->returnValue($this->getS3ResponseMock(true))) ; $resolver = new AwsS3Resolver($s3, 'images.example.com'); - $this->assertTrue($resolver->remove('thumb/some-folder/targetpath.jpg', 'thumb')); + $this->assertTrue($resolver->remove('some-folder/path.jpg', 'thumb')); } - public function testRemoveNotExisting() + public function testDoNothingIfObjectNotExistOnAmazonOnRemove() { $s3 = $this->getS3ClientMock(); $s3 ->expects($this->once()) ->method('doesObjectExist') - ->with('images.example.com', 'thumb/some-folder/targetpath.jpg') + ->with('images.example.com', 'thumb/some-folder/path.jpg') ->will($this->returnValue(false)) ; $s3 @@ -209,7 +208,7 @@ public function testRemoveNotExisting() ; $resolver = new AwsS3Resolver($s3, 'images.example.com'); - $this->assertTrue($resolver->remove('thumb/some-folder/targetpath.jpg', 'thumb')); + $this->assertTrue($resolver->remove('some-folder/path.jpg', 'thumb')); } public function testClearIsDisabled() diff --git a/Tests/Imagine/Cache/Resolver/CacheResolverTest.php b/Tests/Imagine/Cache/Resolver/CacheResolverTest.php index 212b7fa8b..d30cbfe83 100644 --- a/Tests/Imagine/Cache/Resolver/CacheResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/CacheResolverTest.php @@ -16,7 +16,7 @@ class CacheResolverTest extends AbstractTest { protected $filter = 'thumbnail'; protected $path = 'MadCat2.jpeg'; - protected $targetPath = '/media/cache/thumbnail/MadCat2.jpeg'; + protected $webPath = '/media/cache/thumbnail/MadCat2.jpeg'; public function testResolveIsSavedToCache() { @@ -25,16 +25,16 @@ public function testResolveIsSavedToCache() ->expects($this->once()) ->method('resolve') ->with($this->path, $this->filter) - ->will($this->returnValue($this->targetPath)) + ->will($this->returnValue($this->webPath)) ; $cacheResolver = new CacheResolver(new MemoryCache(), $resolver); - $this->assertEquals($this->targetPath, $cacheResolver->resolve($this->path, $this->filter)); + $this->assertEquals($this->webPath, $cacheResolver->resolve($this->path, $this->filter)); // Call multiple times to verify the cache is used. - $this->assertEquals($this->targetPath, $cacheResolver->resolve($this->path, $this->filter)); - $this->assertEquals($this->targetPath, $cacheResolver->resolve($this->path, $this->filter)); + $this->assertEquals($this->webPath, $cacheResolver->resolve($this->path, $this->filter)); + $this->assertEquals($this->webPath, $cacheResolver->resolve($this->path, $this->filter)); } public function testStoreIsForwardedToResolver() @@ -45,21 +45,21 @@ public function testStoreIsForwardedToResolver() $resolver ->expects($this->exactly(2)) ->method('store') - ->with($response, $this->targetPath, $this->filter) + ->with($response, $this->webPath, $this->filter) ->will($this->returnValue($response)) ; $cacheResolver = new CacheResolver(new MemoryCache(), $resolver); // Call twice, as this method should not be cached. - $this->assertSame($response, $cacheResolver->store($response, $this->targetPath, $this->filter)); - $this->assertSame($response, $cacheResolver->store($response, $this->targetPath, $this->filter)); + $this->assertSame($response, $cacheResolver->store($response, $this->webPath, $this->filter)); + $this->assertSame($response, $cacheResolver->store($response, $this->webPath, $this->filter)); } public function testGetBrowserPath() { - $absolute = 'http://example.com' . $this->targetPath; - $relative = $this->targetPath; + $absolute = 'http://example.com' . $this->webPath; + $relative = $this->webPath; $resolver = $this->getMockResolver(); $resolver @@ -94,7 +94,7 @@ public function testRemoveUsesIndex() ->expects($this->once()) ->method('resolve') ->with($this->path, $this->filter) - ->will($this->returnValue($this->targetPath)) + ->will($this->returnValue($this->webPath)) ; $resolver ->expects($this->once()) @@ -110,12 +110,12 @@ public function testRemoveUsesIndex() /* * Three items: * * The result of resolve. - * * The result of reverse for the targetPath. + * * The result of reverse for the filePath. * * The index of both entries. */ - $this->assertCount(3, $cache->data); + $this->assertCount(2, $cache->data); - $this->assertTrue($cacheResolver->remove($this->targetPath, $this->filter)); + $this->assertTrue($cacheResolver->remove($this->path, $this->filter)); // Cache including index has been removed. $this->assertCount(0, $cache->data); diff --git a/Tests/Imagine/Cache/Resolver/NoCacheResolverTest.php b/Tests/Imagine/Cache/Resolver/NoCacheResolverTest.php index 2fcad008d..cd1a612bc 100644 --- a/Tests/Imagine/Cache/Resolver/NoCacheResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/NoCacheResolverTest.php @@ -11,12 +11,11 @@ */ class NoCacheResolverTest extends \Phpunit_Framework_TestCase { - public function testThrowIfRequestNotSetOnResolve() + public function testDoNothingOnResolve() { $resolver = new NoCacheResolver(new Filesystem); $resolver->setRequest(null); - $this->setExpectedException('LogicException', 'The request was not injected, inject it before using resolver.'); - $resolver->resolve('/a/path', 'aFilter'); + $this->assertNull($resolver->resolve('/a/path', 'aFilter')); } } diff --git a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php index 69d99ef79..f71e9c0db 100644 --- a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php @@ -4,6 +4,7 @@ use Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver; use Liip\ImagineBundle\Tests\AbstractTest; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; /** @@ -73,30 +74,18 @@ public function testDefaultBehavior() $this->resolver->setRequest($request); + $path = 'cats.jpeg'; + // Resolve the requested image for the given filter. - $targetPath = $this->resolver->resolve('cats.jpeg', 'thumbnail'); - // The realpath() is important for filesystems that are virtual in some way (encrypted, different mount options, ..) - $this->assertEquals(str_replace('/', DIRECTORY_SEPARATOR, realpath($this->cacheDir).'/thumbnail/cats.jpeg'), $targetPath, - '->resolve() correctly converts the requested file into target path within webRoot.'); - $this->assertFalse(file_exists($targetPath), - '->resolve() does not create the file within the target path.'); + $this->assertNull($this->resolver->resolve($path, 'thumbnail')); // Store the cached version of that image. $content = file_get_contents($this->dataRoot.'/cats.jpeg'); $response = new Response($content); - $this->resolver->store($response, $targetPath, 'thumbnail'); + $this->resolver->store($response, $path, 'thumbnail'); $this->assertEquals(201, $response->getStatusCode(), '->store() alters the HTTP response code to "201 - Created".'); - $this->assertTrue(file_exists($targetPath), - '->store() creates the cached image file to be served.'); - $this->assertEquals($content, file_get_contents($targetPath), - '->store() writes the content of the original Response into the cache file.'); - - // Remove the cached image. - $this->assertTrue($this->resolver->remove($targetPath, 'thumbnail'), - '->remove() reports removal of cached image file correctly.'); - $this->assertFalse(file_exists($targetPath), - '->remove() actually removes the cached file from the filesystem.'); + $this->assertEquals($content, $response->getContent()); } /** @@ -119,12 +108,14 @@ public function testMissingRewrite() $this->resolver->setRequest($request); + $path = 'cats.jpeg'; + $webFilePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; + // The file has already been cached by this resolver. - $targetPath = $this->resolver->resolve('cats.jpeg', 'thumbnail'); - $this->filesystem->mkdir(dirname($targetPath)); - file_put_contents($targetPath, file_get_contents($this->dataRoot.'/cats.jpeg')); + $this->filesystem->mkdir(dirname($webFilePath)); + file_put_contents($webFilePath, file_get_contents($this->dataRoot.'/cats.jpeg')); - $response = $this->resolver->resolve('cats.jpeg', 'thumbnail'); + $response = $this->resolver->resolve($path, 'thumbnail'); $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response, '->resolve() returns a Response instance if the target file already exists.'); $this->assertEquals(302, $response->getStatusCode(), @@ -153,12 +144,14 @@ public function testMissingRewriteWithBaseUrl() $this->resolver->setRequest($request); + $path = 'cats.jpeg'; + $filePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; + // The file has already been cached by this resolver. - $targetPath = $this->resolver->resolve('cats.jpeg', 'thumbnail'); - $this->filesystem->mkdir(dirname($targetPath)); - file_put_contents($targetPath, file_get_contents($this->dataRoot.'/cats.jpeg')); + $this->filesystem->mkdir(dirname($filePath)); + file_put_contents($filePath, file_get_contents($this->dataRoot.'/cats.jpeg')); - $response = $this->resolver->resolve('cats.jpeg', 'thumbnail'); + $response = $this->resolver->resolve($path, 'thumbnail'); $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response, '->resolve() returns a Response instance if the target file already exists.'); $this->assertEquals(302, $response->getStatusCode(), @@ -187,30 +180,22 @@ public function testResolveWithBasePath() $this->resolver->setRequest($request); + $path = 'cats.jpeg'; + $filePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; + // Resolve the requested image for the given filter. - $targetPath = $this->resolver->resolve('cats.jpeg', 'thumbnail'); - // The realpath() is important for filesystems that are virtual in some way (encrypted, different mount options, ..) - $this->assertEquals(str_replace('/', DIRECTORY_SEPARATOR, realpath($this->cacheDir).'/thumbnail/cats.jpeg'), $targetPath, - '->resolve() correctly converts the requested file into target path within webRoot.'); - $this->assertFalse(file_exists($targetPath), - '->resolve() does not create the file within the target path.'); + $this->assertNull($this->resolver->resolve($path, 'thumbnail')); // Store the cached version of that image. $content = file_get_contents($this->dataRoot.'/cats.jpeg'); $response = new Response($content); - $this->resolver->store($response, $targetPath, 'thumbnail'); + $this->resolver->store($response, $path, 'thumbnail'); $this->assertEquals(201, $response->getStatusCode(), '->store() alters the HTTP response code to "201 - Created".'); - $this->assertTrue(file_exists($targetPath), + $this->assertTrue(file_exists($filePath), '->store() creates the cached image file to be served.'); - $this->assertEquals($content, file_get_contents($targetPath), + $this->assertEquals($content, file_get_contents($filePath), '->store() writes the content of the original Response into the cache file.'); - - // Remove the cached image. - $this->assertTrue($this->resolver->remove($targetPath, 'thumbnail'), - '->remove() reports removal of cached image file correctly.'); - $this->assertFalse(file_exists($targetPath), - '->remove() actually removes the cached file from the filesystem.'); } /** @@ -239,12 +224,16 @@ public function testMissingRewriteWithBasePathWithScriptname() $this->resolver->setRequest($request); - // The file has already been cached by this resolver. - $targetPath = $this->resolver->resolve('cats.jpeg', 'thumbnail'); - $this->filesystem->mkdir(dirname($targetPath)); - file_put_contents($targetPath, file_get_contents($this->dataRoot.'/cats.jpeg')); + $path = 'cats.jpeg'; + $filePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; - $response = $this->resolver->resolve('cats.jpeg', 'thumbnail'); + // Resolve the requested image for the given filter. + $this->assertNull($this->resolver->resolve($path, 'thumbnail')); + + $this->filesystem->mkdir(dirname($filePath)); + file_put_contents($filePath, file_get_contents($this->dataRoot.'/cats.jpeg')); + + $response = $this->resolver->resolve($path, 'thumbnail'); $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response, '->resolve() returns a Response instance if the target file already exists.'); $this->assertEquals(302, $response->getStatusCode(), @@ -289,4 +278,47 @@ public function testThrowIfRequestNotSetOnResolve() $this->setExpectedException('LogicException', 'The request was not injected, inject it before using resolver.'); $this->resolver->resolve('/a/path', 'aFilter'); } + + public function testRemoveCachedImageWhenExistOnRemove() + { + $this->cacheManager + ->expects($this->atLeastOnce()) + ->method('generateUrl') + ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) + ; + + $path = 'cats.jpeg'; + $filePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; + + $this->filesystem->mkdir(dirname($filePath)); + file_put_contents($filePath, file_get_contents($this->dataRoot.'/cats.jpeg')); + + $this->resolver->setRequest(Request::create('/')); + + // guard + $this->assertNotNull($this->resolver->resolve($path, 'thumbnail')); + + $this->assertTrue($this->resolver->remove($path, 'thumbnail')); + $this->assertFalse(file_exists($filePath)); + } + + public function testDoNothingIfCachedImageNotExistOnRemove() + { + $this->cacheManager + ->expects($this->atLeastOnce()) + ->method('generateUrl') + ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) + ; + + $path = 'cats.jpeg'; + $filePath = $this->webRoot.'/media/cache/thumbnail/cats.jpeg'; + + // guard + $this->assertFalse(file_exists($filePath)); + + $this->resolver->setRequest(Request::create('/')); + + $this->assertTrue($this->resolver->remove($path, 'thumbnail')); + $this->assertFalse(file_exists($filePath)); + } } diff --git a/UPGRADE.md b/UPGRADE.md index 6cc1816e0..f631f9fd8 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -6,4 +6,5 @@ Upgrade * Required minimum symfony version was updated to 2.3. * [CacheResolver] first argument request was removed from `resolve` method. +* [CacheResolver] Now `resolve` method can return NULL or instance of `Response`. * [Logger] Symfony `LoggerInterface` was replaced with PSR-3 one.