From a628969f3e941d9dcd51ee9a27d82f4408122ff4 Mon Sep 17 00:00:00 2001 From: serdyuka Date: Tue, 8 Apr 2014 18:44:53 +0300 Subject: [PATCH 01/12] Added resolve events to cache manager --- Events/PostResolveEvent.php | 44 ++++++++++++++++++++++++++++++++++ Events/PreResolveEvent.php | 35 +++++++++++++++++++++++++++ Imagine/Cache/CacheManager.php | 27 ++++++++++++++++++--- ImagineEvents.php | 14 +++++++++++ Resources/config/imagine.xml | 1 + 5 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 Events/PostResolveEvent.php create mode 100644 Events/PreResolveEvent.php create mode 100644 ImagineEvents.php diff --git a/Events/PostResolveEvent.php b/Events/PostResolveEvent.php new file mode 100644 index 000000000..9dff8f429 --- /dev/null +++ b/Events/PostResolveEvent.php @@ -0,0 +1,44 @@ +resolver = $resolver; + $this->path = $path; + $this->filter = $filter; + $this->url = $url; + } + + public function getResolver() + { + return $this->path; + } + + public function getPath() + { + return $this->path; + } + + public function getFilter() + { + return $this->filter; + } + + public function getUrl() + { + return $this->url; + } +} \ No newline at end of file diff --git a/Events/PreResolveEvent.php b/Events/PreResolveEvent.php new file mode 100644 index 000000000..3ad2305ea --- /dev/null +++ b/Events/PreResolveEvent.php @@ -0,0 +1,35 @@ +path = $path; + $this->filter = $filter; + } + + public function getResolver() + { + return $this->path; + } + + public function getPath() + { + return $this->path; + } + + public function getFilter() + { + return $this->filter; + } +} \ No newline at end of file diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index e9c6f6810..a500242fe 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -8,6 +8,11 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\UriSigner; use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\Event; +use Liip\ImagineBundle\ImagineEvents; +use Liip\ImagineBundle\Events\PreResolveEvent; +use Liip\ImagineBundle\Events\PostResolveEvent; class CacheManager { @@ -31,6 +36,8 @@ class CacheManager */ protected $uriSigner; + protected $dispatcher; + /** * @var string */ @@ -44,11 +51,17 @@ class CacheManager * @param UriSigner $uriSigner * @param string $defaultResolver */ - public function __construct(FilterConfiguration $filterConfig, RouterInterface $router, UriSigner $uriSigner, $defaultResolver = null) - { + public function __construct( + FilterConfiguration $filterConfig, + RouterInterface $router, + UriSigner $uriSigner, + EventDispatcherInterface $dispatcher, + $defaultResolver = null + ) { $this->filterConfig = $filterConfig; $this->router = $router; $this->uriSigner = $uriSigner; + $this->dispatcher = $dispatcher; $this->defaultResolver = $defaultResolver ?: 'default'; } @@ -176,7 +189,15 @@ public function resolve($path, $filter) throw new NotFoundHttpException(sprintf("Source image was searched with '%s' outside of the defined root path", $path)); } - return $this->getResolver($filter)->resolve($path, $filter); + $resolver = $this->getResolver($filter); + + $this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, new PreResolveEvent($resolver, $path, $filter)); + + $url = $resolver->resolve($path, $filter); + + $this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, new PostResolveEvent($resolver, $path, $filter, $url)); + + return $url; } /** diff --git a/ImagineEvents.php b/ImagineEvents.php new file mode 100644 index 000000000..09a2196e8 --- /dev/null +++ b/ImagineEvents.php @@ -0,0 +1,14 @@ + + %liip_imagine.cache.resolver.default% From 258bf9f36c0db9bc15064a94c708c73a76f069d9 Mon Sep 17 00:00:00 2001 From: serdyuka Date: Wed, 9 Apr 2014 10:13:02 +0300 Subject: [PATCH 02/12] Improve cache resolver event system --- Events/CacheResolveEvent.php | 82 ++++++++++++++++++++++++++++++++++ Events/PostResolveEvent.php | 44 ------------------ Events/PreResolveEvent.php | 35 --------------- Imagine/Cache/CacheManager.php | 23 ++++++---- ImagineEvents.php | 4 +- 5 files changed, 99 insertions(+), 89 deletions(-) create mode 100644 Events/CacheResolveEvent.php delete mode 100644 Events/PostResolveEvent.php delete mode 100644 Events/PreResolveEvent.php diff --git a/Events/CacheResolveEvent.php b/Events/CacheResolveEvent.php new file mode 100644 index 000000000..04fd00071 --- /dev/null +++ b/Events/CacheResolveEvent.php @@ -0,0 +1,82 @@ +resolver = $resolver; + $this->path = $path; + $this->filter = $filter; + $this->url = $url; + } + + /** + * @param ResolverInterface $resolver + */ + public function setResolver(ResolverInterface $resolver) + { + $this->resolver = $resolver; + } + + /** + * @return ResolverInterface + */ + public function getResolver() + { + return $this->resolver; + } + + public function setPath($path) + { + $this->path = $path; + } + + public function getPath() + { + return $this->path; + } + + public function setFilter($filter) + { + $this->filter = $filter; + } + + public function getFilter() + { + return $this->filter; + } + + public function setUrl($url) + { + $this->url = $url; + } + + public function getUrl() + { + return $this->url; + } +} diff --git a/Events/PostResolveEvent.php b/Events/PostResolveEvent.php deleted file mode 100644 index 9dff8f429..000000000 --- a/Events/PostResolveEvent.php +++ /dev/null @@ -1,44 +0,0 @@ -resolver = $resolver; - $this->path = $path; - $this->filter = $filter; - $this->url = $url; - } - - public function getResolver() - { - return $this->path; - } - - public function getPath() - { - return $this->path; - } - - public function getFilter() - { - return $this->filter; - } - - public function getUrl() - { - return $this->url; - } -} \ No newline at end of file diff --git a/Events/PreResolveEvent.php b/Events/PreResolveEvent.php deleted file mode 100644 index 3ad2305ea..000000000 --- a/Events/PreResolveEvent.php +++ /dev/null @@ -1,35 +0,0 @@ -path = $path; - $this->filter = $filter; - } - - public function getResolver() - { - return $this->path; - } - - public function getPath() - { - return $this->path; - } - - public function getFilter() - { - return $this->filter; - } -} \ No newline at end of file diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index a500242fe..1948cae56 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -11,8 +11,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\Event; use Liip\ImagineBundle\ImagineEvents; -use Liip\ImagineBundle\Events\PreResolveEvent; -use Liip\ImagineBundle\Events\PostResolveEvent; +use Liip\ImagineBundle\Events\CacheResolveEvent; class CacheManager { @@ -189,15 +188,23 @@ public function resolve($path, $filter) throw new NotFoundHttpException(sprintf("Source image was searched with '%s' outside of the defined root path", $path)); } - $resolver = $this->getResolver($filter); + /** @var CacheResolveEvent $event */ + $event = $this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent( + $this->getResolver($filter), + $path, + $filter + )); - $this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, new PreResolveEvent($resolver, $path, $filter)); + $url = $event->getResolver()->resolve($event->getPath(), $event->getFilter()); - $url = $resolver->resolve($path, $filter); + $event = $this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, new CacheResolveEvent( + $event->getResolver(), + $event->getPath(), + $event->getFilter(), + $url + )); - $this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, new PostResolveEvent($resolver, $path, $filter, $url)); - - return $url; + return $event->getUrl(); } /** diff --git a/ImagineEvents.php b/ImagineEvents.php index 09a2196e8..844025b74 100644 --- a/ImagineEvents.php +++ b/ImagineEvents.php @@ -8,7 +8,7 @@ final class ImagineEvents const POST_RESOLVE = 'imagine.post_resolve'; - protected function __construct() + private function __construct() { } -} \ No newline at end of file +} From a913fd3cdc0090137be4e6377df0f6baab43bdbb Mon Sep 17 00:00:00 2001 From: serdyuka Date: Wed, 9 Apr 2014 10:49:54 +0300 Subject: [PATCH 03/12] Rename events. Remove dock blocks. Remove resolver from event. --- Events/CacheResolveEvent.php | 34 +--------------------------------- Imagine/Cache/CacheManager.php | 4 +--- ImagineEvents.php | 4 ++-- 3 files changed, 4 insertions(+), 38 deletions(-) diff --git a/Events/CacheResolveEvent.php b/Events/CacheResolveEvent.php index 04fd00071..915570f7c 100644 --- a/Events/CacheResolveEvent.php +++ b/Events/CacheResolveEvent.php @@ -2,54 +2,22 @@ namespace Liip\ImagineBundle\Events; use Symfony\Component\EventDispatcher\Event; -use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface; class CacheResolveEvent extends Event { - /** - * @var \Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface - */ - protected $resolver; - - /** - * @var string - */ protected $path; - /** - * @var string - */ protected $filter; - /** - * @var string - */ protected $url; - public function __construct(ResolverInterface $resolver, $path, $filter, $url = null) + public function __construct($path, $filter, $url = null) { - $this->resolver = $resolver; $this->path = $path; $this->filter = $filter; $this->url = $url; } - /** - * @param ResolverInterface $resolver - */ - public function setResolver(ResolverInterface $resolver) - { - $this->resolver = $resolver; - } - - /** - * @return ResolverInterface - */ - public function getResolver() - { - return $this->resolver; - } - public function setPath($path) { $this->path = $path; diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index 1948cae56..249572010 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -190,15 +190,13 @@ public function resolve($path, $filter) /** @var CacheResolveEvent $event */ $event = $this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent( - $this->getResolver($filter), $path, $filter )); - $url = $event->getResolver()->resolve($event->getPath(), $event->getFilter()); + $url = $this->getResolver($filter)->resolve($event->getPath(), $event->getFilter()); $event = $this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, new CacheResolveEvent( - $event->getResolver(), $event->getPath(), $event->getFilter(), $url diff --git a/ImagineEvents.php b/ImagineEvents.php index 844025b74..cce7bb1cf 100644 --- a/ImagineEvents.php +++ b/ImagineEvents.php @@ -4,9 +4,9 @@ final class ImagineEvents { - const PRE_RESOLVE = 'imagine.pre_resolve'; + const PRE_RESOLVE = 'liip_imagine.pre_resolve'; - const POST_RESOLVE = 'imagine.post_resolve'; + const POST_RESOLVE = 'liip_imagine.post_resolve'; private function __construct() { From 5542b0f620f4830507e4aec9ca90b8c4e4bde9f6 Mon Sep 17 00:00:00 2001 From: serdyuka Date: Wed, 9 Apr 2014 11:06:07 +0300 Subject: [PATCH 04/12] Added comments --- Events/CacheResolveEvent.php | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Events/CacheResolveEvent.php b/Events/CacheResolveEvent.php index 915570f7c..e66fd422b 100644 --- a/Events/CacheResolveEvent.php +++ b/Events/CacheResolveEvent.php @@ -5,10 +5,22 @@ class CacheResolveEvent extends Event { + /** + * Resource path + * @var string + */ protected $path; + /** + * Filter name + * @var string + */ protected $filter; + /** + * Resource url + * @var null + */ protected $url; public function __construct($path, $filter, $url = null) @@ -18,31 +30,61 @@ public function __construct($path, $filter, $url = null) $this->url = $url; } + /** + * Sets resource path + * + * @param $path + */ public function setPath($path) { $this->path = $path; } + /** + * Returns resource path + * + * @return string + */ public function getPath() { return $this->path; } + /** + * Sets filter name + * + * @param $filter + */ public function setFilter($filter) { $this->filter = $filter; } + /** + * Returns filter name + * + * @return string + */ public function getFilter() { return $this->filter; } + /** + * Sets resource url + * + * @param $url + */ public function setUrl($url) { $this->url = $url; } + /** + * Returns resource url + * + * @return null + */ public function getUrl() { return $this->url; From d3046a223f1a579621bdf1cf0127647215d287df Mon Sep 17 00:00:00 2001 From: serdyuka Date: Wed, 9 Apr 2014 14:19:11 +0300 Subject: [PATCH 05/12] fix tests for cache manager --- Tests/AbstractTest.php | 6 +++ Tests/Imagine/Cache/CacheManagerTest.php | 65 ++++++++++++++++++------ 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/Tests/AbstractTest.php b/Tests/AbstractTest.php index 9e5ea46fd..ea355f251 100644 --- a/Tests/AbstractTest.php +++ b/Tests/AbstractTest.php @@ -6,6 +6,7 @@ use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; abstract class AbstractTest extends \PHPUnit_Framework_TestCase { @@ -81,6 +82,11 @@ protected function createResolverMock() return $this->getMock('Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface'); } + protected function createEventDispatcherMock() + { + return $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + } + protected function getMockImage() { return $this->getMock('Imagine\Image\ImageInterface'); diff --git a/Tests/Imagine/Cache/CacheManagerTest.php b/Tests/Imagine/Cache/CacheManagerTest.php index 31f72debe..2a3cc0886 100644 --- a/Tests/Imagine/Cache/CacheManagerTest.php +++ b/Tests/Imagine/Cache/CacheManagerTest.php @@ -7,6 +7,8 @@ use Liip\ImagineBundle\Tests\AbstractTest; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\UriSigner; +use Liip\ImagineBundle\ImagineEvents; +use Liip\ImagineBundle\Events\CacheResolveEvent; /** * @covers Liip\ImagineBundle\Imagine\Cache\CacheManager @@ -17,7 +19,7 @@ class CacheManagerTest extends AbstractTest public function testAddCacheManagerAwareResolver() { - $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $resolver = $this->getMock('Liip\ImagineBundle\Tests\Fixtures\CacheManagerAwareResolver'); $resolver @@ -43,7 +45,7 @@ public function testGetBrowserPathWithoutResolver() ))) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $this->setExpectedException('OutOfBoundsException', 'Could not find resolver "default" for "thumbnail" filter type'); $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail'); @@ -83,7 +85,19 @@ public function testDefaultResolverUsedIfNoneSetOnGetBrowserPath() ->method('generate') ; - $cacheManager = new CacheManager($config, $router, new UriSigner('secret')); + $dispatcher = $this->createEventDispatcherMock(); + $dispatcher + ->expects($this->at(0)) + ->method('dispatch') + ->with(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail')) + ->will($this->returnArgument(1)); + $dispatcher + ->expects($this->at(1)) + ->method('dispatch') + ->with(ImagineEvents::POST_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail', 'http://a/path/to/an/image.png')) + ->will($this->returnArgument(1)); + + $cacheManager = new CacheManager($config, $router, new UriSigner('secret'), $dispatcher); $cacheManager->addResolver('default', $resolver); $actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail'); @@ -124,7 +138,7 @@ public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBr ->will($this->returnValue('/media/cache/thumbnail/cats.jpeg')) ; - $cacheManager = new CacheManager($config, $router, new UriSigner('secret')); + $cacheManager = new CacheManager($config, $router, new UriSigner('secret'), $this->createEventDispatcherMock()); $cacheManager->addResolver('default', $resolver); $actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail'); @@ -137,7 +151,7 @@ public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBr */ public function testResolveInvalidPath($path) { - $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); $cacheManager->resolve($path, 'thumbnail'); @@ -145,7 +159,14 @@ public function testResolveInvalidPath($path) public function testThrowsIfConcreteResolverNotExists() { - $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), new UriSigner('secret')); + $dispatcher = $this->createEventDispatcherMock(); + $dispatcher + ->expects($this->at(0)) + ->method('dispatch') + ->with(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail')) + ->will($this->returnArgument(1)); + + $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), new UriSigner('secret'), $dispatcher); $this->setExpectedException('OutOfBoundsException', 'Could not find resolver "default" for "thumbnail" filter type'); $this->assertFalse($cacheManager->resolve('cats.jpeg', 'thumbnail')); @@ -186,10 +207,23 @@ public function testFallbackToDefaultResolver() ))) ; + $dispatcher = $this->createEventDispatcherMock(); + $dispatcher + ->expects($this->at(0)) + ->method('dispatch') + ->with(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail')) + ->will($this->returnArgument(1)); + $dispatcher + ->expects($this->at(1)) + ->method('dispatch') + ->with(ImagineEvents::POST_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail', '/thumbs/cats.jpeg')) + ->will($this->returnArgument(1)); + $cacheManager = new CacheManager( $config, $this->createRouterMock(), - new UriSigner('secret') + new UriSigner('secret'), + $dispatcher ); $cacheManager->addResolver('default', $resolver); @@ -224,7 +258,8 @@ public function testGenerateUrl() $cacheManager = new CacheManager( $this->createFilterConfigurationMock(), $routerMock, - new UriSigner('secret') + new UriSigner('secret'), + $this->createEventDispatcherMock() ); $this->assertEquals( @@ -256,7 +291,7 @@ public function testRemoveCacheForPathAndFilterOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $cacheManager->addResolver($expectedFilter, $resolver); $cacheManager->remove($expectedPath, $expectedFilter); @@ -293,7 +328,7 @@ public function testRemoveCacheForPathAndSomeFiltersOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $cacheManager->addResolver($expectedFilterOne, $resolverOne); $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); @@ -327,7 +362,7 @@ public function testRemoveCacheForSomePathsAndFilterOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $cacheManager->addResolver($expectedFilter, $resolver); $cacheManager->remove(array($expectedPathOne, $expectedPathTwo), $expectedFilter); @@ -365,7 +400,7 @@ public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $cacheManager->addResolver($expectedFilterOne, $resolverOne); $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); @@ -413,7 +448,7 @@ public function testRemoveCacheForAllFiltersOnRemove() ))) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $cacheManager->addResolver($expectedFilterOne, $resolverOne); $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); @@ -459,7 +494,7 @@ public function testRemoveCacheForPathAndAllFiltersOnRemove() ))) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $cacheManager->addResolver($expectedFilterOne, $resolverOne); $cacheManager->addResolver($expectedFilterTwo, $resolverTwo); @@ -489,7 +524,7 @@ public function testAggregateFiltersByResolverOnRemove() })) ; - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret')); + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); $cacheManager->addResolver($expectedFilterOne, $resolver); $cacheManager->addResolver($expectedFilterTwo, $resolver); From 1c86ebe1702732b91d0978457df5e2f25ea26831 Mon Sep 17 00:00:00 2001 From: serdyuka Date: Wed, 9 Apr 2014 15:19:10 +0300 Subject: [PATCH 06/12] Added tests for PreResolve and PostResolve events --- Tests/Imagine/Cache/CacheManagerTest.php | 68 ++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Tests/Imagine/Cache/CacheManagerTest.php b/Tests/Imagine/Cache/CacheManagerTest.php index 2a3cc0886..3d9231005 100644 --- a/Tests/Imagine/Cache/CacheManagerTest.php +++ b/Tests/Imagine/Cache/CacheManagerTest.php @@ -530,4 +530,72 @@ public function testAggregateFiltersByResolverOnRemove() $cacheManager->remove(null, array($expectedFilterOne, $expectedFilterTwo)); } + + public function testPreResolveEvent() + { + $config = $this->createFilterConfigurationMock(); + $config + ->expects($this->once()) + ->method('get') + ->with('thumbnail') + ->will($this->returnValue(array( + 'size' => array(180, 180), + 'mode' => 'outbound', + 'cache' => null, + ))) + ; + + $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); + $dispatcher->addListener(ImagineEvents::PRE_RESOLVE, function ($event) { + $event->setPath('cats.jpg'); + }); + + $resolver = $this->createResolverMock(); + $resolver + ->expects($this->once()) + ->method('resolve') + ->with($this->equalTo('cats.jpg'), $this->equalTo('thumbnail')) + ->will($this->returnValue('/thumbnail/cats.jpg')); + + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $dispatcher); + $cacheManager->addResolver('default', $resolver); + + $resolvedUrl = $cacheManager->resolve('invalid_path', 'thumbnail'); + + $this->assertEquals($resolvedUrl, '/thumbnail/cats.jpg'); + } + + public function testPostResolveEvent() + { + $config = $this->createFilterConfigurationMock(); + $config + ->expects($this->once()) + ->method('get') + ->with('thumbnail') + ->will($this->returnValue(array( + 'size' => array(180, 180), + 'mode' => 'outbound', + 'cache' => null, + ))) + ; + + $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); + $dispatcher->addListener(ImagineEvents::POST_RESOLVE, function ($event) { + $event->setUrl('/thumbnail/cats.jpg'); + }); + + $resolver = $this->createResolverMock(); + $resolver + ->expects($this->once()) + ->method('resolve') + ->with($this->equalTo('invalid_path'), $this->equalTo('thumbnail')) + ->will($this->returnValue('/invalid_path/cats.jpg')); + + $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $dispatcher); + $cacheManager->addResolver('default', $resolver); + + $resolvedUrl = $cacheManager->resolve('invalid_path', 'thumbnail'); + + $this->assertEquals($resolvedUrl, '/thumbnail/cats.jpg'); + } } From ee09bc40175965d52e6f64efb457b4e8e71f49f5 Mon Sep 17 00:00:00 2001 From: serdyuka Date: Thu, 10 Apr 2014 11:00:52 +0300 Subject: [PATCH 07/12] Improved CacheManager. Fixed tests for CacheManager. Added tests for CacheResolverEvent. --- Events/CacheResolveEvent.php | 5 + Imagine/Cache/CacheManager.php | 18 +-- Tests/Events/CacheResolverEventTest.php | 66 +++++++++++ Tests/Imagine/Cache/CacheManagerTest.php | 133 +++++++---------------- 4 files changed, 119 insertions(+), 103 deletions(-) create mode 100644 Tests/Events/CacheResolverEventTest.php diff --git a/Events/CacheResolveEvent.php b/Events/CacheResolveEvent.php index e66fd422b..fa1712eb1 100644 --- a/Events/CacheResolveEvent.php +++ b/Events/CacheResolveEvent.php @@ -3,6 +3,11 @@ use Symfony\Component\EventDispatcher\Event; +/** + * Default event class for PreResolve and PostResolve events + * + * @package Liip\ImagineBundle\Events + */ class CacheResolveEvent extends Event { /** diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index 249572010..2f6ddbce3 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -188,21 +188,15 @@ public function resolve($path, $filter) throw new NotFoundHttpException(sprintf("Source image was searched with '%s' outside of the defined root path", $path)); } - /** @var CacheResolveEvent $event */ - $event = $this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent( - $path, - $filter - )); + $preEvent = new CacheResolveEvent($path, $filter); + $this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, $preEvent); - $url = $this->getResolver($filter)->resolve($event->getPath(), $event->getFilter()); + $url = $this->getResolver($filter)->resolve($preEvent->getPath(), $preEvent->getFilter()); - $event = $this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, new CacheResolveEvent( - $event->getPath(), - $event->getFilter(), - $url - )); + $postEvent = new CacheResolveEvent($preEvent->getPath(), $preEvent->getFilter(), $url); + $this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, $postEvent); - return $event->getUrl(); + return $postEvent->getUrl(); } /** diff --git a/Tests/Events/CacheResolverEventTest.php b/Tests/Events/CacheResolverEventTest.php new file mode 100644 index 000000000..22d04d23e --- /dev/null +++ b/Tests/Events/CacheResolverEventTest.php @@ -0,0 +1,66 @@ +event = new CacheResolveEvent('default_path', 'default_filter'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + $this->event = null; + } + + public function testGetPath() + { + $this->assertEquals('default_path', $this->event->getPath()); + } + + public function testSetPath() + { + $this->event->setPath('new_path'); + $this->assertEquals('new_path', $this->event->getPath()); + } + + public function testGetFilter() + { + $this->assertEquals('default_filter', $this->event->getFilter()); + } + + public function testSetFilter() + { + $this->event->setFilter('new_filter'); + $this->assertEquals('new_filter', $this->event->getFilter()); + } + + public function testGetUrl() + { + $this->assertNull($this->event->getUrl()); + } + + public function testSetUrl() + { + $this->event->setUrl('new_url'); + $this->assertEquals('new_url', $this->event->getUrl()); + } +} diff --git a/Tests/Imagine/Cache/CacheManagerTest.php b/Tests/Imagine/Cache/CacheManagerTest.php index 3d9231005..326da6197 100644 --- a/Tests/Imagine/Cache/CacheManagerTest.php +++ b/Tests/Imagine/Cache/CacheManagerTest.php @@ -85,19 +85,7 @@ public function testDefaultResolverUsedIfNoneSetOnGetBrowserPath() ->method('generate') ; - $dispatcher = $this->createEventDispatcherMock(); - $dispatcher - ->expects($this->at(0)) - ->method('dispatch') - ->with(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail')) - ->will($this->returnArgument(1)); - $dispatcher - ->expects($this->at(1)) - ->method('dispatch') - ->with(ImagineEvents::POST_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail', 'http://a/path/to/an/image.png')) - ->will($this->returnArgument(1)); - - $cacheManager = new CacheManager($config, $router, new UriSigner('secret'), $dispatcher); + $cacheManager = new CacheManager($config, $router, new UriSigner('secret'), $this->createEventDispatcherMock()); $cacheManager->addResolver('default', $resolver); $actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail'); @@ -151,7 +139,12 @@ public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBr */ public function testResolveInvalidPath($path) { - $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), new UriSigner('secret'), $this->createEventDispatcherMock()); + $cacheManager = new CacheManager( + $this->createFilterConfigurationMock(), + $this->createRouterMock(), + new UriSigner('secret'), + $this->createEventDispatcherMock() + ); $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); $cacheManager->resolve($path, 'thumbnail'); @@ -159,14 +152,12 @@ public function testResolveInvalidPath($path) public function testThrowsIfConcreteResolverNotExists() { - $dispatcher = $this->createEventDispatcherMock(); - $dispatcher - ->expects($this->at(0)) - ->method('dispatch') - ->with(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail')) - ->will($this->returnArgument(1)); - - $cacheManager = new CacheManager($this->createFilterConfigurationMock(), $this->createRouterMock(), new UriSigner('secret'), $dispatcher); + $cacheManager = new CacheManager( + $this->createFilterConfigurationMock(), + $this->createRouterMock(), + new UriSigner('secret'), + $this->createEventDispatcherMock() + ); $this->setExpectedException('OutOfBoundsException', 'Could not find resolver "default" for "thumbnail" filter type'); $this->assertFalse($cacheManager->resolve('cats.jpeg', 'thumbnail')); @@ -207,23 +198,11 @@ public function testFallbackToDefaultResolver() ))) ; - $dispatcher = $this->createEventDispatcherMock(); - $dispatcher - ->expects($this->at(0)) - ->method('dispatch') - ->with(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail')) - ->will($this->returnArgument(1)); - $dispatcher - ->expects($this->at(1)) - ->method('dispatch') - ->with(ImagineEvents::POST_RESOLVE, new CacheResolveEvent('cats.jpeg', 'thumbnail', '/thumbs/cats.jpeg')) - ->will($this->returnArgument(1)); - $cacheManager = new CacheManager( $config, $this->createRouterMock(), new UriSigner('secret'), - $dispatcher + $this->createEventDispatcherMock() ); $cacheManager->addResolver('default', $resolver); @@ -531,71 +510,43 @@ public function testAggregateFiltersByResolverOnRemove() $cacheManager->remove(null, array($expectedFilterOne, $expectedFilterTwo)); } - public function testPreResolveEvent() + public function testShouldBeDispatchedCachePreResolveEvent() { - $config = $this->createFilterConfigurationMock(); - $config - ->expects($this->once()) - ->method('get') - ->with('thumbnail') - ->will($this->returnValue(array( - 'size' => array(180, 180), - 'mode' => 'outbound', - 'cache' => null, - ))) + $dispatcher = $this->createEventDispatcherMock(); + $dispatcher + ->expects($this->at(0)) + ->method('dispatch') + ->with(ImagineEvents::PRE_RESOLVE, new CacheResolveEvent('cats.jpg', 'thumbnail')) ; - $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); - $dispatcher->addListener(ImagineEvents::PRE_RESOLVE, function ($event) { - $event->setPath('cats.jpg'); - }); - - $resolver = $this->createResolverMock(); - $resolver - ->expects($this->once()) - ->method('resolve') - ->with($this->equalTo('cats.jpg'), $this->equalTo('thumbnail')) - ->will($this->returnValue('/thumbnail/cats.jpg')); - - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $dispatcher); - $cacheManager->addResolver('default', $resolver); - - $resolvedUrl = $cacheManager->resolve('invalid_path', 'thumbnail'); + $cacheManager = new CacheManager( + $this->createFilterConfigurationMock(), + $this->createRouterMock(), + new UriSigner('secret'), + $dispatcher + ); + $cacheManager->addResolver('default', $this->createResolverMock()); - $this->assertEquals($resolvedUrl, '/thumbnail/cats.jpg'); + $cacheManager->resolve('cats.jpg', 'thumbnail'); } - public function testPostResolveEvent() + public function testShouldBeDispatchedCachePostResolveEvent() { - $config = $this->createFilterConfigurationMock(); - $config - ->expects($this->once()) - ->method('get') - ->with('thumbnail') - ->will($this->returnValue(array( - 'size' => array(180, 180), - 'mode' => 'outbound', - 'cache' => null, - ))) + $dispatcher = $this->createEventDispatcherMock(); + $dispatcher + ->expects($this->at(1)) + ->method('dispatch') + ->with(ImagineEvents::POST_RESOLVE, new CacheResolveEvent('cats.jpg', 'thumbnail')) ; - $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); - $dispatcher->addListener(ImagineEvents::POST_RESOLVE, function ($event) { - $event->setUrl('/thumbnail/cats.jpg'); - }); - - $resolver = $this->createResolverMock(); - $resolver - ->expects($this->once()) - ->method('resolve') - ->with($this->equalTo('invalid_path'), $this->equalTo('thumbnail')) - ->will($this->returnValue('/invalid_path/cats.jpg')); - - $cacheManager = new CacheManager($config, $this->createRouterMock(), new UriSigner('secret'), $dispatcher); - $cacheManager->addResolver('default', $resolver); - - $resolvedUrl = $cacheManager->resolve('invalid_path', 'thumbnail'); + $cacheManager = new CacheManager( + $this->createFilterConfigurationMock(), + $this->createRouterMock(), + new UriSigner('secret'), + $dispatcher + ); + $cacheManager->addResolver('default', $this->createResolverMock()); - $this->assertEquals($resolvedUrl, '/thumbnail/cats.jpg'); + $cacheManager->resolve('cats.jpg', 'thumbnail'); } } From 1a01770a598c421fa24de0775b06062dd866c566 Mon Sep 17 00:00:00 2001 From: serdyuka Date: Thu, 10 Apr 2014 11:17:14 +0300 Subject: [PATCH 08/12] changed comments --- Events/CacheResolveEvent.php | 12 +++++++----- Imagine/Cache/CacheManager.php | 3 +++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Events/CacheResolveEvent.php b/Events/CacheResolveEvent.php index fa1712eb1..325d0ff7e 100644 --- a/Events/CacheResolveEvent.php +++ b/Events/CacheResolveEvent.php @@ -3,11 +3,6 @@ use Symfony\Component\EventDispatcher\Event; -/** - * Default event class for PreResolve and PostResolve events - * - * @package Liip\ImagineBundle\Events - */ class CacheResolveEvent extends Event { /** @@ -28,6 +23,13 @@ class CacheResolveEvent extends Event */ protected $url; + /** + * Init default event state + * + * @param string $path + * @param string $filter + * @param null|string $url + */ public function __construct($path, $filter, $url = null) { $this->path = $path; diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index 2f6ddbce3..ee1a70c0e 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -35,6 +35,9 @@ class CacheManager */ protected $uriSigner; + /** + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ protected $dispatcher; /** From 6c7a4f53bb8c22cbf828052e7056b0718f628497 Mon Sep 17 00:00:00 2001 From: serdyuka Date: Thu, 10 Apr 2014 12:15:05 +0300 Subject: [PATCH 09/12] changed tests CacheResolveEvent --- Tests/Events/CacheResolveEventTest.php | 105 ++++++++++++++++++++++++ Tests/Events/CacheResolverEventTest.php | 66 --------------- 2 files changed, 105 insertions(+), 66 deletions(-) create mode 100644 Tests/Events/CacheResolveEventTest.php delete mode 100644 Tests/Events/CacheResolverEventTest.php diff --git a/Tests/Events/CacheResolveEventTest.php b/Tests/Events/CacheResolveEventTest.php new file mode 100644 index 000000000..41e826345 --- /dev/null +++ b/Tests/Events/CacheResolveEventTest.php @@ -0,0 +1,105 @@ +assertAttributeEquals('default_path', 'path', $event); + } + + public function testShouldAllowSetPathByMethod() + { + $event = new CacheResolveEvent('default_path', 'default_filter'); + $event->setPath('new_path'); + + $this->assertAttributeEquals('new_path', 'path', $event); + } + + public function testShouldAllowGetPathWhichWasSetInConstruct() + { + $event = new CacheResolveEvent('default_path', 'default_filter'); + + $this->assertEquals('default_path', $event->getPath()); + } + + public function testShouldAllowGetPathWhichWasSetByMethod() + { + $event = new CacheResolveEvent('default_path', 'default_filter'); + $event->setPath('new_path'); + + $this->assertEquals('new_path', $event->getPath()); + } + + public function testShouldAllowSetFilterInConstruct() + { + $event = new CacheResolveEvent('default_path', 'default_filter'); + + $this->assertAttributeEquals('default_filter', 'filter', $event); + } + + public function testShouldAllowSetFilterByMethod() + { + $event = new CacheResolveEvent('default_path', 'default_filter'); + $event->setFilter('new_filter'); + + $this->assertAttributeEquals('new_filter', 'filter', $event); + } + + public function testShouldAllowGetFilterWhichWasSetInConstruct() + { + $event = new CacheResolveEvent('default_path', 'default_filter'); + + $this->assertEquals('default_filter', $event->getFilter()); + } + + public function testShouldAllowGetFilterWhichWasSetByMethod() + { + $event = new CacheResolveEvent('default_path', 'default_filter'); + $event->setFilter('new_filter'); + + $this->assertEquals('new_filter', $event->getFilter()); + } + + public function testShouldAllowSetUrlInConstruct() + { + $event = new CacheResolveEvent('default_path', 'default_filter', 'default_url'); + + $this->assertAttributeEquals('default_url', 'url', $event); + } + + public function testShouldAllowSetUrlByMethod() + { + $event = new CacheResolveEvent('default_path', 'default_filter'); + $event->setUrl('new_url'); + + $this->assertAttributeEquals('new_url', 'url', $event); + } + + public function testShouldAllowGetUrlWhichWasSetInConstruct() + { + $event = new CacheResolveEvent('default_path', 'default_filter', 'default_url'); + + $this->assertEquals('default_url', $event->getUrl()); + } + + public function testShouldAllowGetUrlWhichWasSetByMethod() + { + $event = new CacheResolveEvent('default_path', 'default_filter'); + $event->setUrl('new_url'); + + $this->assertEquals('new_url', $event->getUrl()); + } +} diff --git a/Tests/Events/CacheResolverEventTest.php b/Tests/Events/CacheResolverEventTest.php deleted file mode 100644 index 22d04d23e..000000000 --- a/Tests/Events/CacheResolverEventTest.php +++ /dev/null @@ -1,66 +0,0 @@ -event = new CacheResolveEvent('default_path', 'default_filter'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - $this->event = null; - } - - public function testGetPath() - { - $this->assertEquals('default_path', $this->event->getPath()); - } - - public function testSetPath() - { - $this->event->setPath('new_path'); - $this->assertEquals('new_path', $this->event->getPath()); - } - - public function testGetFilter() - { - $this->assertEquals('default_filter', $this->event->getFilter()); - } - - public function testSetFilter() - { - $this->event->setFilter('new_filter'); - $this->assertEquals('new_filter', $this->event->getFilter()); - } - - public function testGetUrl() - { - $this->assertNull($this->event->getUrl()); - } - - public function testSetUrl() - { - $this->event->setUrl('new_url'); - $this->assertEquals('new_url', $this->event->getUrl()); - } -} From 25effc006a6613b63da3fdf664ae4d717d33ca9e Mon Sep 17 00:00:00 2001 From: serdyuka Date: Thu, 10 Apr 2014 14:16:58 +0300 Subject: [PATCH 10/12] added tests for cache manager --- Tests/Imagine/Cache/CacheManagerTest.php | 96 +++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/Tests/Imagine/Cache/CacheManagerTest.php b/Tests/Imagine/Cache/CacheManagerTest.php index 326da6197..8148fd042 100644 --- a/Tests/Imagine/Cache/CacheManagerTest.php +++ b/Tests/Imagine/Cache/CacheManagerTest.php @@ -510,7 +510,7 @@ public function testAggregateFiltersByResolverOnRemove() $cacheManager->remove(null, array($expectedFilterOne, $expectedFilterTwo)); } - public function testShouldBeDispatchedCachePreResolveEvent() + public function testShouldDispatchCachePreResolveEvent() { $dispatcher = $this->createEventDispatcherMock(); $dispatcher @@ -530,7 +530,7 @@ public function testShouldBeDispatchedCachePreResolveEvent() $cacheManager->resolve('cats.jpg', 'thumbnail'); } - public function testShouldBeDispatchedCachePostResolveEvent() + public function testShouldDispatchCachePostResolveEvent() { $dispatcher = $this->createEventDispatcherMock(); $dispatcher @@ -549,4 +549,96 @@ public function testShouldBeDispatchedCachePostResolveEvent() $cacheManager->resolve('cats.jpg', 'thumbnail'); } + + public function testShouldAllowToPassChangedDataFromPreResolveEventToResolver() + { + $dispatcher = $this->createEventDispatcherMock(); + $dispatcher + ->expects($this->at(0)) + ->method('dispatch') + ->with(ImagineEvents::PRE_RESOLVE, $this->isInstanceOf('Liip\ImagineBundle\Events\CacheResolveEvent')) + ->will($this->returnCallback(function ($name, $event) { + $event->setPath('changed_path'); + $event->setFilter('changed_filter'); + })) + ; + + $resolver = $this->createResolverMock(); + $resolver + ->expects($this->once()) + ->method('resolve') + ->with('changed_path', 'changed_filter') + ; + + $cacheManager = new CacheManager( + $this->createFilterConfigurationMock(), + $this->createRouterMock(), + new UriSigner('secret'), + $dispatcher + ); + $cacheManager->addResolver('default', $resolver); + + $cacheManager->resolve('cats.jpg', 'thumbnail'); + } + + public function testShouldAllowToPassChangedDataFromPreResolveEventToPostResolveEvent() + { + $dispatcher = $this->createEventDispatcherMock(); + $dispatcher + ->expects($this->at(0)) + ->method('dispatch') + ->with(ImagineEvents::PRE_RESOLVE, $this->isInstanceOf('Liip\ImagineBundle\Events\CacheResolveEvent')) + ->will($this->returnCallback(function ($name, $event) { + $event->setPath('changed_path'); + $event->setFilter('changed_filter'); + })) + ; + + $dispatcher + ->expects($this->at(1)) + ->method('dispatch') + ->with( + ImagineEvents::POST_RESOLVE, + $this->logicalAnd( + $this->isInstanceOf('Liip\ImagineBundle\Events\CacheResolveEvent'), + $this->attributeEqualTo('path', 'changed_path'), + $this->attributeEqualTo('filter', 'changed_filter') + )) + ; + + $cacheManager = new CacheManager( + $this->createFilterConfigurationMock(), + $this->createRouterMock(), + new UriSigner('secret'), + $dispatcher + ); + $cacheManager->addResolver('default', $this->createResolverMock()); + + $cacheManager->resolve('cats.jpg', 'thumbnail'); + } + + public function testShouldReturnUrlChangedInPostResolveEvent() + { + $dispatcher = $this->createEventDispatcherMock(); + $dispatcher + ->expects($this->at(1)) + ->method('dispatch') + ->with(ImagineEvents::POST_RESOLVE, $this->isInstanceOf('Liip\ImagineBundle\Events\CacheResolveEvent')) + ->will($this->returnCallback(function ($name, $event) { + $event->setUrl('changed_url'); + })) + ; + + $cacheManager = new CacheManager( + $this->createFilterConfigurationMock(), + $this->createRouterMock(), + new UriSigner('secret'), + $dispatcher + ); + $cacheManager->addResolver('default', $this->createResolverMock()); + + $url = $cacheManager->resolve('cats.jpg', 'thumbnail'); + + $this->assertEquals('changed_url', $url); + } } From 4606a93dc90166bb7d197d44aab12e1158fca0c6 Mon Sep 17 00:00:00 2001 From: serdyuka Date: Thu, 10 Apr 2014 15:09:10 +0300 Subject: [PATCH 11/12] fixed filter name --- Imagine/Cache/CacheManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index ee1a70c0e..d0c270b64 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -194,7 +194,7 @@ public function resolve($path, $filter) $preEvent = new CacheResolveEvent($path, $filter); $this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, $preEvent); - $url = $this->getResolver($filter)->resolve($preEvent->getPath(), $preEvent->getFilter()); + $url = $this->getResolver($preEvent->getFilter())->resolve($preEvent->getPath(), $preEvent->getFilter()); $postEvent = new CacheResolveEvent($preEvent->getPath(), $preEvent->getFilter(), $url); $this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, $postEvent); From e5f94fc8b0c479c27a86ad7600daf1d99cd8c159 Mon Sep 17 00:00:00 2001 From: serdyuka Date: Thu, 10 Apr 2014 15:10:20 +0300 Subject: [PATCH 12/12] added test for filter name passed from pre resolve event --- Tests/Imagine/Cache/CacheManagerTest.php | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Tests/Imagine/Cache/CacheManagerTest.php b/Tests/Imagine/Cache/CacheManagerTest.php index 8148fd042..1ccbefeee 100644 --- a/Tests/Imagine/Cache/CacheManagerTest.php +++ b/Tests/Imagine/Cache/CacheManagerTest.php @@ -581,6 +581,34 @@ public function testShouldAllowToPassChangedDataFromPreResolveEventToResolver() $cacheManager->resolve('cats.jpg', 'thumbnail'); } + public function testShouldAllowToGetResolverByFilterChangedInPreResolveEvent() + { + $dispatcher = $this->createEventDispatcherMock(); + $dispatcher + ->expects($this->at(0)) + ->method('dispatch') + ->will($this->returnCallback(function ($name, $event) { + $event->setFilter('thumbnail'); + })) + ; + + $cacheManager = $this->getMock('Liip\ImagineBundle\Imagine\Cache\CacheManager', array('getResolver'), array( + $this->createFilterConfigurationMock(), + $this->createRouterMock(), + new UriSigner('secret'), + $dispatcher + )); + + $cacheManager + ->expects($this->once()) + ->method('getResolver') + ->with('thumbnail') + ->will($this->returnValue($this->createResolverMock())) + ; + + $cacheManager->resolve('cats.jpg', 'default'); + } + public function testShouldAllowToPassChangedDataFromPreResolveEventToPostResolveEvent() { $dispatcher = $this->createEventDispatcherMock();