diff --git a/src/View/Helper/Factory/ImgUrlFactory.php b/src/View/Helper/Factory/ImgUrlFactory.php index 47827c4..39bf304 100644 --- a/src/View/Helper/Factory/ImgUrlFactory.php +++ b/src/View/Helper/Factory/ImgUrlFactory.php @@ -15,7 +15,7 @@ public function createService(ServiceLocatorInterface $helpers) $serviceLocator->get('HtImgModule\Service\CacheManager'), $serviceLocator->get('HtImg\ModuleOptions'), $serviceLocator->get('HtImgModule\Imagine\Filter\FilterManager'), - $serviceLocator->get('HtImg\RelativePathResolver') + $serviceLocator->get('HtImgModule\Imagine\Loader\LoaderManager') ); } } diff --git a/src/View/Helper/ImgUrl.php b/src/View/Helper/ImgUrl.php index 9754ed5..cd513c5 100644 --- a/src/View/Helper/ImgUrl.php +++ b/src/View/Helper/ImgUrl.php @@ -3,11 +3,11 @@ namespace HtImgModule\View\Helper; use Zend\View\Helper\AbstractHelper; -use Zend\View\Resolver\ResolverInterface; use HtImgModule\Options\CacheOptionsInterface; use HtImgModule\Service\CacheManagerInterface; -use HtImgModule\Imagine\Filter\FilterManager; +use HtImgModule\Imagine\Filter\FilterManagerInterface; use HtImgModule\Exception; +use HtImgModule\Imagine\Loader\LoaderManagerInterface; class ImgUrl extends AbstractHelper { @@ -22,33 +22,34 @@ class ImgUrl extends AbstractHelper protected $cacheManager; /** - * @var FilterManager + * @var FilterManagerInterface */ protected $filterManager; /** - * @var ResolverInterface + * @var LoaderManagerInterface */ - protected $relativePathResolver; + protected $loaderManager; /** * Constructor * - * @param CacheManagerInterface $cacheManager - * @param CacheOptionsInterface $cacheOptions - * @param FilterManager $filterManager + * @param CacheManagerInterface $cacheManager + * @param CacheOptionsInterface $cacheOptions + * @param FilterManagerInterface $filterManager + * @param LoaderManagerInterface $loaderManager */ public function __construct( CacheManagerInterface $cacheManager, CacheOptionsInterface $cacheOptions, - FilterManager $filterManager, - ResolverInterface $relativePathResolver + FilterManagerInterface $filterManager, + LoaderManagerInterface $loaderManager ) { $this->cacheManager = $cacheManager; $this->cacheOptions = $cacheOptions; $this->filterManager = $filterManager; - $this->relativePathResolver = $relativePathResolver; + $this->loaderManager = $loaderManager; } /** @@ -64,23 +65,14 @@ public function __invoke($relativeName, $filter) if (isset($filterOptions['format'])) { $format = $filterOptions['format']; } else { - $imagePath = $this->relativePathResolver->resolve($relativeName); - $format = pathinfo($imagePath, PATHINFO_EXTENSION); - $format = $format ?: 'png'; + $binary = $this->loaderManager->loadBinary($relativeName, $filter); + $format = $binary->getFormat() ?: 'png'; } if ($this->cacheOptions->getEnableCache() && $this->cacheManager->cacheExists($relativeName, $filter, $format)) { $basePathHelper = $this->getView()->plugin('basePath'); return $basePathHelper() . '/'. $this->cacheManager->getCacheUrl($relativeName, $filter, $format); } - if (!isset($imagePath)) { - $imagePath = $this->relativePathResolver->resolve($relativeName); - } - if (!$imagePath) { - throw new Exception\ImageNotFoundException( - sprintf('Unable to resolve %s', $relativeName) - ); - } $urlHelper = $this->getView()->plugin('url'); diff --git a/tests/HtImgModuleTest/View/Helper/ImgUrlTest.php b/tests/HtImgModuleTest/View/Helper/ImgUrlTest.php index 85f1ecb..28a5a0f 100644 --- a/tests/HtImgModuleTest/View/Helper/ImgUrlTest.php +++ b/tests/HtImgModuleTest/View/Helper/ImgUrlTest.php @@ -2,90 +2,79 @@ namespace HtImgModuleTest\View\Helper; use HtImgModule\View\Helper\ImgUrl; -use HtImgModule\Options\ModuleOptions; -use HtImgModule\Service\CacheManager; -use HtImgModule\Imagine\Filter\FilterManager; class ImgUrlTest extends \PHPUnit_Framework_TestCase { - public function testGetExceptionWhenResolverCannotResolve() - { - $options = new ModuleOptions; - $cacheManager = new CacheManager($options); - $resolver = $this->getMock('Zend\View\Resolver\AggregateResolver'); - $resolver->expects($this->any()) - ->method('resolve') - ->will($this->returnValue(false)); - $filterManager = new FilterManager($options, $this->getMock('HtImgModule\Imagine\Filter\Loader\FilterLoaderPluginManager')); - $filterManager->addFilter('foo_view_filter', ['type' => 'foo_view_filter_thumbnail', 'options' => ['format' => 'gif']]); - $helper = new ImgUrl( - $cacheManager, - $options, - $filterManager, - $resolver - ); - $this->setExpectedException('HtImgModule\Exception\ImageNotFoundException'); - $helper('path/to/some/random/image/', 'foo_view_filter'); - } - public function testGetNewImageNotFromCache() { - $options = new ModuleOptions; - $cacheManager = $this->getMockBuilder('HtImgModule\Service\CacheManager') - ->disableOriginalConstructor() - ->getMock(); - $cacheManager->expects($this->once()) - ->method('cacheExists') - ->will($this->returnValue(true)); - $cacheManager->expects($this->once()) - ->method('getCacheUrl') - ->will($this->returnValue(RESOURCES_DIR . '/flowers.jpg')); - $resolver = $this->getMock('Zend\View\Resolver\AggregateResolver'); - $resolver->expects($this->any()) - ->method('resolve') - ->will($this->returnValue(true)); - $filterManager = new FilterManager($options, $this->getMock('HtImgModule\Imagine\Filter\Loader\FilterLoaderPluginManager')); - $filterManager->addFilter('foo_view_filter', ['type' => 'foo_view_filter_thumbnail', 'options' => []]); + $options = $this->getMock('HtImgModule\Options\CacheOptionsInterface'); + $options->expects($this->once()) + ->method('getEnableCache') + ->will($this->returnValue(false)); + $cacheManager = $this->getMock('HtImgModule\Service\CacheManagerInterface'); + $binary = $this->getMock('HtImgModule\Binary\BinaryInterface'); + $loaderManager = $this->getMock('HtImgModule\Imagine\Loader\LoaderManagerInterface'); + $loaderManager->expects($this->once()) + ->method('loadBinary') + ->with('path/to/some/random/image/', 'foo_view_filter') + ->will($this->returnValue($binary)); + $filterManager = $this->getMock('HtImgModule\Imagine\Filter\FilterManagerInterface'); + $filterManager->expects($this->once()) + ->method('getFilterOptions') + ->with('foo_view_filter') + ->will($this->returnValue([])); $helper = new ImgUrl( $cacheManager, $options, $filterManager, - $resolver + $loaderManager ); + $urlHelper = $this->getMock('Zend\View\Helper\Url'); $renderer = $this->getMock('Zend\View\Renderer\PhpRenderer'); $renderer->expects($this->once()) ->method('plugin') - ->will($this->returnValue(function () {return 'app';})); + ->with('url') + ->will($this->returnValue($urlHelper)); + $urlHelper->expects($this->once()) + ->method('__invoke') + ->will($this->returnValue('url/to/some/random/image')); $helper->setView($renderer); - $helper('path/to/some/random/image/', 'foo_view_filter'); + $this->assertEquals('url/to/some/random/image', $helper('path/to/some/random/image/', 'foo_view_filter')); } public function testGetImageFromCache() { - $options = new ModuleOptions; - $cacheManager = $this->getMockBuilder('HtImgModule\Service\CacheManager') - ->disableOriginalConstructor() - ->getMock(); + $options = $this->getMock('HtImgModule\Options\CacheOptionsInterface'); + $options->expects($this->once()) + ->method('getEnableCache') + ->will($this->returnValue(true)); + $cacheManager = $this->getMock('HtImgModule\Service\CacheManagerInterface'); $cacheManager->expects($this->once()) ->method('cacheExists') - ->will($this->returnValue(false)); - $resolver = $this->getMock('Zend\View\Resolver\AggregateResolver'); - $resolver->expects($this->any()) - ->method('resolve') + ->with('path/to/some/random/image/', 'foo_view_filter', 'jpeg') ->will($this->returnValue(true)); - $filterManager = new FilterManager($options, $this->getMock('HtImgModule\Imagine\Filter\Loader\FilterLoaderPluginManager')); - $filterManager->addFilter('foo_view_filter', ['type' => 'foo_view_filter_thumbnail', 'options' => []]); + $cacheManager->expects($this->once()) + ->method('getCacheUrl') + ->with('path/to/some/random/image/', 'foo_view_filter', 'jpeg') + ->will($this->returnValue('flowers.jpg')); + $loaderManager = $this->getMock('HtImgModule\Imagine\Loader\LoaderManagerInterface'); + $filterManager = $this->getMock('HtImgModule\Imagine\Filter\FilterManagerInterface'); + $filterManager->expects($this->once()) + ->method('getFilterOptions') + ->with('foo_view_filter') + ->will($this->returnValue(['format' => 'jpeg'])); $helper = new ImgUrl( $cacheManager, $options, $filterManager, - $resolver + $loaderManager ); $renderer = $this->getMock('Zend\View\Renderer\PhpRenderer'); $renderer->expects($this->once()) ->method('plugin') - ->will($this->returnValue(function () {return 'app';})); + ->with('basePath') + ->will($this->returnValue(function () {return '/app';})); $helper->setView($renderer); - $helper('path/to/some/random/image/', 'foo_view_filter'); + $this->assertEquals('/app/flowers.jpg', $helper('path/to/some/random/image/', 'foo_view_filter')); } }