Skip to content

Commit

Permalink
fixed #15
Browse files Browse the repository at this point in the history
  • Loading branch information
ojhaujjwal committed Aug 24, 2014
1 parent 6f071c8 commit 656e673
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/View/Helper/Factory/ImgUrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}
}
36 changes: 14 additions & 22 deletions src/View/Helper/ImgUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

/**
Expand All @@ -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');

Expand Down
99 changes: 44 additions & 55 deletions tests/HtImgModuleTest/View/Helper/ImgUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit 656e673

Please sign in to comment.