From e55a4a6a4bffa34ba45b61898d45b432c2ca600c Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Wed, 14 Feb 2018 17:04:38 +0200 Subject: [PATCH 01/13] Alternative web path resolver added --- .../Resolver/WebPathResolverFactory.php | 9 +- .../Resolver/AbstractWebPathResolver.php | 125 ++++++++++++++++++ .../Resolver/AlternativeWebPathResolver.php | 14 ++ Imagine/Cache/Resolver/WebPathResolver.php | 118 +---------------- Resources/config/imagine.xml | 7 +- 5 files changed, 154 insertions(+), 119 deletions(-) create mode 100644 Imagine/Cache/Resolver/AbstractWebPathResolver.php create mode 100644 Imagine/Cache/Resolver/AlternativeWebPathResolver.php diff --git a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php index 2a08e6f09..39b9e44a6 100644 --- a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php @@ -22,7 +22,11 @@ class WebPathResolverFactory extends AbstractResolverFactory */ public function create(ContainerBuilder $container, $resolverName, array $config) { - $resolverDefinition = $this->getChildResolverDefinition(); + if (!array_key_exists("alt_url", $config)) { + $resolverDefinition = $this->getChildResolverDefinition(); + } else { + $resolverDefinition = $this->getChildResolverDefinition(sprintf("alt_%s", $this->getName())); + } $resolverDefinition->replaceArgument(2, $config['web_root']); $resolverDefinition->replaceArgument(3, $config['cache_prefix']); $resolverDefinition->addTag('liip_imagine.cache.resolver', [ @@ -58,6 +62,9 @@ public function addConfiguration(ArrayNodeDefinition $builder) ->defaultValue('media/cache') ->cannotBeEmpty() ->end() + ->booleanNode('alt_url') + ->defaultFalse() + ->end() ->end(); } } diff --git a/Imagine/Cache/Resolver/AbstractWebPathResolver.php b/Imagine/Cache/Resolver/AbstractWebPathResolver.php new file mode 100644 index 000000000..3992d7aa7 --- /dev/null +++ b/Imagine/Cache/Resolver/AbstractWebPathResolver.php @@ -0,0 +1,125 @@ +filesystem = $filesystem; + $this->requestContext = $requestContext; + + $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); + $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); + $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; + } + + /** + * Checks whether the given path is stored within this Resolver. + * + * @param string $path + * @param string $filter + * + * @return bool + */ + public function isStored($path, $filter) + { + return is_file($this->getFilePath($path, $filter)); + } + + /** + * {@inheritdoc} + */ + public function store(BinaryInterface $binary, $path, $filter) + { + $this->filesystem->dumpFile( + $this->getFilePath($path, $filter), + $binary->getContent() + ); + } + + /** + * {@inheritdoc} + */ + public function remove(array $paths, array $filters) + { + if (empty($paths) && empty($filters)) { + return; + } + + if (empty($paths)) { + $filtersCacheDir = []; + foreach ($filters as $filter) { + $filtersCacheDir[] = $this->cacheRoot.'/'.$filter; + } + + $this->filesystem->remove($filtersCacheDir); + + return; + } + + foreach ($paths as $path) { + foreach ($filters as $filter) { + $this->filesystem->remove($this->getFilePath($path, $filter)); + } + } + } + + /** + * {@inheritdoc} + */ + protected function getFilePath($path, $filter) + { + return $this->webRoot.'/'.$this->getFileUrl($path, $filter); + } + + /** + * {@inheritdoc} + */ + protected function getFileUrl($path, $filter) + { + // crude way of sanitizing URL scheme ("protocol") part + $path = str_replace('://', '---', $path); + + return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); + } +} diff --git a/Imagine/Cache/Resolver/AlternativeWebPathResolver.php b/Imagine/Cache/Resolver/AlternativeWebPathResolver.php new file mode 100644 index 000000000..52895156e --- /dev/null +++ b/Imagine/Cache/Resolver/AlternativeWebPathResolver.php @@ -0,0 +1,14 @@ +getFileUrl($path, $filter)); + } +} diff --git a/Imagine/Cache/Resolver/WebPathResolver.php b/Imagine/Cache/Resolver/WebPathResolver.php index 08120fbd5..b42cc1a37 100644 --- a/Imagine/Cache/Resolver/WebPathResolver.php +++ b/Imagine/Cache/Resolver/WebPathResolver.php @@ -11,57 +11,8 @@ namespace Liip\ImagineBundle\Imagine\Cache\Resolver; -use Liip\ImagineBundle\Binary\BinaryInterface; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Routing\RequestContext; - -class WebPathResolver implements ResolverInterface +class WebPathResolver extends AbstractWebPathResolver { - /** - * @var Filesystem - */ - protected $filesystem; - - /** - * @var RequestContext - */ - protected $requestContext; - - /** - * @var string - */ - protected $webRoot; - - /** - * @var string - */ - protected $cachePrefix; - - /** - * @var string - */ - protected $cacheRoot; - - /** - * @param Filesystem $filesystem - * @param RequestContext $requestContext - * @param string $webRootDir - * @param string $cachePrefix - */ - public function __construct( - Filesystem $filesystem, - RequestContext $requestContext, - $webRootDir, - $cachePrefix = 'media/cache' - ) { - $this->filesystem = $filesystem; - $this->requestContext = $requestContext; - - $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); - $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); - $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; - } - /** * {@inheritdoc} */ @@ -72,72 +23,7 @@ public function resolve($path, $filter) $this->getFileUrl($path, $filter) ); } - - /** - * {@inheritdoc} - */ - public function isStored($path, $filter) - { - return is_file($this->getFilePath($path, $filter)); - } - - /** - * {@inheritdoc} - */ - public function store(BinaryInterface $binary, $path, $filter) - { - $this->filesystem->dumpFile( - $this->getFilePath($path, $filter), - $binary->getContent() - ); - } - - /** - * {@inheritdoc} - */ - public function remove(array $paths, array $filters) - { - if (empty($paths) && empty($filters)) { - return; - } - - if (empty($paths)) { - $filtersCacheDir = []; - foreach ($filters as $filter) { - $filtersCacheDir[] = $this->cacheRoot.'/'.$filter; - } - - $this->filesystem->remove($filtersCacheDir); - - return; - } - - foreach ($paths as $path) { - foreach ($filters as $filter) { - $this->filesystem->remove($this->getFilePath($path, $filter)); - } - } - } - - /** - * {@inheritdoc} - */ - protected function getFilePath($path, $filter) - { - return $this->webRoot.'/'.$this->getFileUrl($path, $filter); - } - - /** - * {@inheritdoc} - */ - protected function getFileUrl($path, $filter) - { - // crude way of sanitizing URL scheme ("protocol") part - $path = str_replace('://', '---', $path); - - return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); - } - + /** * @return string */ diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index 7398aea23..6b82fbaea 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -200,14 +200,17 @@ - - + + + + + From e77871cc2586e485152d432a26ee2f9721ce4df8 Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Wed, 14 Feb 2018 17:30:22 +0200 Subject: [PATCH 02/13] tests fixed --- .../Factory/Resolver/WebPathResolverFactory.php | 6 +++--- .../Factory/Resolver/WebPathResolverFactoryTest.php | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php index 39b9e44a6..c68ab7b3a 100644 --- a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php @@ -22,10 +22,10 @@ class WebPathResolverFactory extends AbstractResolverFactory */ public function create(ContainerBuilder $container, $resolverName, array $config) { - if (!array_key_exists("alt_url", $config)) { - $resolverDefinition = $this->getChildResolverDefinition(); - } else { + if ($config["alt_url"]) { $resolverDefinition = $this->getChildResolverDefinition(sprintf("alt_%s", $this->getName())); + } else { + $resolverDefinition = $this->getChildResolverDefinition(); } $resolverDefinition->replaceArgument(2, $config['web_root']); $resolverDefinition->replaceArgument(3, $config['cache_prefix']); diff --git a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php index 5312d0690..65a7dbed2 100644 --- a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php @@ -54,6 +54,7 @@ public function testCreateResolverDefinitionOnCreate() $resolver->create($container, 'the_resolver_name', array( 'web_root' => 'theWebRoot', 'cache_prefix' => 'theCachePrefix', + 'alt_url' => false, )); $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); From cd6510ae658fa490805d2d726e8883379ec371f2 Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Thu, 15 Feb 2018 18:18:03 +0200 Subject: [PATCH 03/13] refactored --- .../Resolver/WebPathResolverFactory.php | 13 +++- .../Resolver/AbstractWebPathResolver.php | 60 ++++--------------- .../Resolver/AlternativeWebPathResolver.php | 2 +- Imagine/Cache/Resolver/WebPathResolver.php | 22 ++++++- Resources/config/imagine.xml | 13 ++-- .../Resolver/WebPathResolverFactoryTest.php | 36 ++++++----- Utility/Path/PathResolver.php | 57 ++++++++++++++++++ Utility/Path/PathResolverInterface.php | 27 +++++++++ 8 files changed, 160 insertions(+), 70 deletions(-) create mode 100644 Utility/Path/PathResolver.php create mode 100644 Utility/Path/PathResolverInterface.php diff --git a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php index c68ab7b3a..0c5f985c6 100644 --- a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php @@ -13,7 +13,9 @@ use Liip\ImagineBundle\Utility\Framework\SymfonyFramework; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; +use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; class WebPathResolverFactory extends AbstractResolverFactory { @@ -27,8 +29,15 @@ public function create(ContainerBuilder $container, $resolverName, array $config } else { $resolverDefinition = $this->getChildResolverDefinition(); } - $resolverDefinition->replaceArgument(2, $config['web_root']); - $resolverDefinition->replaceArgument(3, $config['cache_prefix']); + $pathResolverDefinition = new ChildDefinition('liip_imagine.resolver.prototype.path'); + $pathResolverDefinition->replaceArgument(0, $config['web_root']); + $pathResolverDefinition->replaceArgument(1, $config['cache_prefix']); + + $pathResolverServiceId = 'liip_imagine.resolver.path'; + $container->setDefinition($pathResolverServiceId, $pathResolverDefinition); + + $resolverDefinition->replaceArgument(1, new Reference($pathResolverServiceId)); + $resolverDefinition->addTag('liip_imagine.cache.resolver', [ 'resolver' => $resolverName, ]); diff --git a/Imagine/Cache/Resolver/AbstractWebPathResolver.php b/Imagine/Cache/Resolver/AbstractWebPathResolver.php index 3992d7aa7..36615a805 100644 --- a/Imagine/Cache/Resolver/AbstractWebPathResolver.php +++ b/Imagine/Cache/Resolver/AbstractWebPathResolver.php @@ -3,8 +3,8 @@ namespace Liip\ImagineBundle\Imagine\Cache\Resolver; use Liip\ImagineBundle\Binary\BinaryInterface; +use Liip\ImagineBundle\Utility\Path\PathResolverInterface; use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Routing\RequestContext; abstract class AbstractWebPathResolver implements ResolverInterface { @@ -14,43 +14,20 @@ abstract class AbstractWebPathResolver implements ResolverInterface protected $filesystem; /** - * @var RequestContext + * @var PathResolverInterface */ - protected $requestContext; - - /** - * @var string - */ - protected $webRoot; - - /** - * @var string - */ - protected $cachePrefix; - - /** - * @var string - */ - protected $cacheRoot; + protected $pathResolver; /** * @param Filesystem $filesystem - * @param RequestContext $requestContext - * @param string $webRootDir - * @param string $cachePrefix + * @param PathResolverInterface $pathResolver */ public function __construct( Filesystem $filesystem, - RequestContext $requestContext, - $webRootDir, - $cachePrefix = 'media/cache' + PathResolverInterface $pathResolver ) { $this->filesystem = $filesystem; - $this->requestContext = $requestContext; - - $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); - $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); - $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; + $this->pathResolver = $pathResolver; } /** @@ -63,7 +40,7 @@ public function __construct( */ public function isStored($path, $filter) { - return is_file($this->getFilePath($path, $filter)); + return is_file($this->pathResolver->getFilePath($path, $filter)); } /** @@ -72,7 +49,7 @@ public function isStored($path, $filter) public function store(BinaryInterface $binary, $path, $filter) { $this->filesystem->dumpFile( - $this->getFilePath($path, $filter), + $this->pathResolver->getFilePath($path, $filter), $binary->getContent() ); } @@ -89,7 +66,7 @@ public function remove(array $paths, array $filters) if (empty($paths)) { $filtersCacheDir = []; foreach ($filters as $filter) { - $filtersCacheDir[] = $this->cacheRoot.'/'.$filter; + $filtersCacheDir[] = $this->pathResolver->getCacheRoot().'/'.$filter; } $this->filesystem->remove($filtersCacheDir); @@ -99,27 +76,16 @@ public function remove(array $paths, array $filters) foreach ($paths as $path) { foreach ($filters as $filter) { - $this->filesystem->remove($this->getFilePath($path, $filter)); + $this->filesystem->remove($this->pathResolver->getFilePath($path, $filter)); } } } /** - * {@inheritdoc} + * @return PathResolverInterface */ - protected function getFilePath($path, $filter) + protected function getPathResolver() { - return $this->webRoot.'/'.$this->getFileUrl($path, $filter); - } - - /** - * {@inheritdoc} - */ - protected function getFileUrl($path, $filter) - { - // crude way of sanitizing URL scheme ("protocol") part - $path = str_replace('://', '---', $path); - - return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); + return $this->pathResolver; } } diff --git a/Imagine/Cache/Resolver/AlternativeWebPathResolver.php b/Imagine/Cache/Resolver/AlternativeWebPathResolver.php index 52895156e..1d072bc52 100644 --- a/Imagine/Cache/Resolver/AlternativeWebPathResolver.php +++ b/Imagine/Cache/Resolver/AlternativeWebPathResolver.php @@ -9,6 +9,6 @@ class AlternativeWebPathResolver extends AbstractWebPathResolver */ public function resolve($path, $filter) { - return sprintf('/%s', $this->getFileUrl($path, $filter)); + return sprintf('/%s', $this->getPathResolver()->getFileUrl($path, $filter)); } } diff --git a/Imagine/Cache/Resolver/WebPathResolver.php b/Imagine/Cache/Resolver/WebPathResolver.php index b42cc1a37..e39724b78 100644 --- a/Imagine/Cache/Resolver/WebPathResolver.php +++ b/Imagine/Cache/Resolver/WebPathResolver.php @@ -11,8 +11,28 @@ namespace Liip\ImagineBundle\Imagine\Cache\Resolver; +use Liip\ImagineBundle\Utility\Path\PathResolverInterface; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Routing\RequestContext; + class WebPathResolver extends AbstractWebPathResolver { + private $requestContext; + + /** + * @param Filesystem $filesystem + * @param PathResolverInterface $pathResolver + * @param RequestContext $requestContext + */ + public function __construct( + Filesystem $filesystem, + PathResolverInterface $pathResolver, + RequestContext $requestContext + ) { + parent::__construct($filesystem, $pathResolver); + $this->requestContext = $requestContext; + } + /** * {@inheritdoc} */ @@ -20,7 +40,7 @@ public function resolve($path, $filter) { return sprintf('%s/%s', $this->getBaseUrl(), - $this->getFileUrl($path, $filter) + $this->getPathResolver()->getFileUrl($path, $filter) ); } diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index 6b82fbaea..7fa854450 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -67,6 +67,11 @@ + + + + + @@ -202,12 +207,12 @@ - - - + - + + + diff --git a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php index 65a7dbed2..777e3fa20 100644 --- a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php @@ -50,21 +50,27 @@ public function testCreateResolverDefinitionOnCreate() $container = new ContainerBuilder(); $resolver = new WebPathResolverFactory(); - - $resolver->create($container, 'the_resolver_name', array( - 'web_root' => 'theWebRoot', - 'cache_prefix' => 'theCachePrefix', - 'alt_url' => false, - )); - - $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); - - $resolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.the_resolver_name'); - $this->assertInstanceOf(ChildDefinition::class, $resolverDefinition); - $this->assertEquals('liip_imagine.cache.resolver.prototype.web_path', $resolverDefinition->getParent()); - - $this->assertEquals('theWebRoot', $resolverDefinition->getArgument(2)); - $this->assertEquals('theCachePrefix', $resolverDefinition->getArgument(3)); + + $testCases = [ + ['alt_url' => false, 'resolverPrototype' => 'liip_imagine.cache.resolver.prototype.web_path'], + ['alt_url' => true, 'resolverPrototype' => 'liip_imagine.cache.resolver.prototype.alt_web_path'], + ]; + foreach ($testCases as $testCase) { + $resolver->create($container, 'the_resolver_name', array( + 'web_root' => 'theWebRoot', + 'cache_prefix' => 'theCachePrefix', + 'alt_url' => $testCase["alt_url"], + )); + + $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); + + $resolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.the_resolver_name'); + $this->assertInstanceOf(ChildDefinition::class, $resolverDefinition); + $this->assertEquals($testCase['resolverPrototype'], $resolverDefinition->getParent()); + + $this->assertEquals('theWebRoot', $resolverDefinition->getArgument(2)); + $this->assertEquals('theCachePrefix', $resolverDefinition->getArgument(3)); + } } public function testProcessCorrectlyOptionsOnAddConfiguration() diff --git a/Utility/Path/PathResolver.php b/Utility/Path/PathResolver.php new file mode 100644 index 000000000..016f4b1ee --- /dev/null +++ b/Utility/Path/PathResolver.php @@ -0,0 +1,57 @@ +webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); + $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); + $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; + } + + /** + * {@inheritdoc} + */ + public function getFilePath($path, $filter) + { + return $this->webRoot.'/'.$this->getFileUrl($path, $filter); + } + + /** + * {@inheritdoc} + */ + public function getFileUrl($path, $filter) + { + // crude way of sanitizing URL scheme ("protocol") part + $path = str_replace('://', '---', $path); + + return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); + } + + /** + * {@inheritdoc} + */ + public function getCacheRoot() + { + return $this->cacheRoot; + } +} diff --git a/Utility/Path/PathResolverInterface.php b/Utility/Path/PathResolverInterface.php new file mode 100644 index 000000000..93a6353af --- /dev/null +++ b/Utility/Path/PathResolverInterface.php @@ -0,0 +1,27 @@ + Date: Tue, 20 Feb 2018 13:26:27 +0200 Subject: [PATCH 04/13] PathResolver util added, WebPAthResolver refactored, tests fix --- .../Resolver/WebPathResolverFactory.php | 4 +- Resources/config/imagine.xml | 6 +- .../Resolver/WebPathResolverFactoryTest.php | 25 +- .../Cache/Resolver/WebPathResolverTest.php | 373 +++++++++--------- 4 files changed, 220 insertions(+), 188 deletions(-) diff --git a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php index 0c5f985c6..e8d7777b3 100644 --- a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php @@ -29,11 +29,11 @@ public function create(ContainerBuilder $container, $resolverName, array $config } else { $resolverDefinition = $this->getChildResolverDefinition(); } - $pathResolverDefinition = new ChildDefinition('liip_imagine.resolver.prototype.path'); + $pathResolverDefinition = new ChildDefinition('liip_imagine.util.resolver.prototype.path'); $pathResolverDefinition->replaceArgument(0, $config['web_root']); $pathResolverDefinition->replaceArgument(1, $config['cache_prefix']); - $pathResolverServiceId = 'liip_imagine.resolver.path'; + $pathResolverServiceId = 'liip_imagine.util.resolver.path'; $container->setDefinition($pathResolverServiceId, $pathResolverDefinition); $resolverDefinition->replaceArgument(1, new Reference($pathResolverServiceId)); diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index 7fa854450..90f2d3356 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -67,9 +67,9 @@ - - - + + + diff --git a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php index 777e3fa20..8c63decac 100644 --- a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php @@ -18,6 +18,7 @@ use Symfony\Component\Config\Definition\Processor; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ChildDefinition; +use Symfony\Component\DependencyInjection\Reference; /** * @covers \Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory @@ -64,12 +65,30 @@ public function testCreateResolverDefinitionOnCreate() $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); + /** + * @var ChildDefinition $resolverDefinition + */ $resolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.the_resolver_name'); $this->assertInstanceOf(ChildDefinition::class, $resolverDefinition); $this->assertEquals($testCase['resolverPrototype'], $resolverDefinition->getParent()); - - $this->assertEquals('theWebRoot', $resolverDefinition->getArgument(2)); - $this->assertEquals('theCachePrefix', $resolverDefinition->getArgument(3)); + /** + * @var Reference $utilPathResolverReference + */ + $utilPathResolverReference = $resolverDefinition->getArgument(1); + $this->assertInstanceOf(Reference::class, $utilPathResolverReference); + + $utilPathResolverServiceId = $utilPathResolverReference->__toString(); + $this->assertEquals('liip_imagine.util.resolver.path', $utilPathResolverServiceId); + $this->assertTrue($container->hasDefinition($utilPathResolverServiceId)); + /** + * @var ChildDefinition $utilPathResolverDefinition + */ + $utilPathResolverDefinition = $container->getDefinition($utilPathResolverServiceId); + $this->assertInstanceOf(ChildDefinition::class, $utilPathResolverDefinition); + $this->assertEquals('liip_imagine.util.resolver.prototype.path', $utilPathResolverDefinition->getParent()); + + $this->assertEquals('theWebRoot', $utilPathResolverDefinition->getArgument(0)); + $this->assertEquals('theCachePrefix', $utilPathResolverDefinition->getArgument(1)); } } diff --git a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php index 0adf7a001..dee906f4c 100644 --- a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php @@ -14,6 +14,8 @@ use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface; use Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver; use Liip\ImagineBundle\Model\Binary; +use Liip\ImagineBundle\Utility\Path\PathResolver; +use Liip\ImagineBundle\Utility\Path\PathResolverInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Routing\RequestContext; @@ -61,83 +63,26 @@ public function testImplementsResolverInterface() public function testCouldBeConstructedWithRequiredArguments() { $filesystemMock = $this->createFilesystemMock(); + $pathResolver = $this->createPathResolverMock(); $requestContext = new RequestContext(); - $webRoot = 'theWebRoot'; - - $resolver = new WebPathResolver($filesystemMock, $requestContext, $webRoot); + + /** + * @var PathResolverInterface $pathResolver + */ + $resolver = new WebPathResolver($filesystemMock, $pathResolver, $requestContext); $this->assertAttributeSame($filesystemMock, 'filesystem', $resolver); + $this->assertAttributeSame($pathResolver, 'pathResolver', $resolver); $this->assertAttributeSame($requestContext, 'requestContext', $resolver); - $this->assertAttributeSame($webRoot, 'webRoot', $resolver); - } - - public function testCouldBeConstructedWithOptionalArguments() - { - $resolver = new WebPathResolver( - $this->createFilesystemMock(), - new RequestContext(), - 'aWebRoot', - 'theCachePrefix' - ); - - $this->assertAttributeSame('theCachePrefix', 'cachePrefix', $resolver); - } - - public function testTrimRightSlashFromWebPathOnConstruct() - { - $resolver = new WebPathResolver( - $this->createFilesystemMock(), - new RequestContext(), - 'aWebRoot/', - 'theCachePrefix' - ); - - $this->assertAttributeSame('aWebRoot', 'webRoot', $resolver); - } - - public function testRemoveDoubleSlashFromWebRootOnConstruct() - { - $resolver = new WebPathResolver( - $this->createFilesystemMock(), - new RequestContext(), - 'aWeb//Root', - '/aCachePrefix' - ); - - $this->assertAttributeSame('aWeb/Root', 'webRoot', $resolver); - } - - public function testTrimRightSlashFromCachePrefixOnConstruct() - { - $resolver = new WebPathResolver( - $this->createFilesystemMock(), - new RequestContext(), - 'aWebRoot', - '/aCachePrefix' - ); - - $this->assertAttributeSame('aCachePrefix', 'cachePrefix', $resolver); - } - - public function testRemoveDoubleSlashFromCachePrefixOnConstruct() - { - $resolver = new WebPathResolver( - $this->createFilesystemMock(), - new RequestContext(), - 'aWebRoot', - 'aCache//Prefix' - ); - - $this->assertAttributeSame('aCache/Prefix', 'cachePrefix', $resolver); } public function testReturnTrueIfFileExistsOnIsStored() { + $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); $resolver = new WebPathResolver( $this->createFilesystemMock(), - new RequestContext(), - $this->basePath, - 'aCachePrefix' + $pathResolver, + new RequestContext() ); $this->assertTrue($resolver->isStored('existingPath', 'aFilter')); @@ -145,11 +90,11 @@ public function testReturnTrueIfFileExistsOnIsStored() public function testReturnFalseIfFileNotExistsOnIsStored() { + $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); $resolver = new WebPathResolver( $this->createFilesystemMock(), - new RequestContext(), - $this->basePath, - 'aCachePrefix' + $pathResolver, + new RequestContext() ); $this->assertFalse($resolver->isStored('nonExistingPath', 'aFilter')); @@ -157,11 +102,11 @@ public function testReturnFalseIfFileNotExistsOnIsStored() public function testReturnFalseIfIsNotFile() { + $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); $resolver = new WebPathResolver( $this->createFilesystemMock(), - new RequestContext(), - $this->basePath, - 'aCachePrefix' + $pathResolver, + new RequestContext() ); $this->assertFalse($resolver->isStored('', 'aFilter')); @@ -169,141 +114,195 @@ public function testReturnFalseIfIsNotFile() public function testComposeSchemaHostAndFileUrlOnResolve() { + $path = 'aPath'; + $filter = 'aFilter'; + $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); - + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFileUrl") + ->with($this->equalTo($path), $this->equalTo($filter)) + ->willReturn("aCachePrefix/aFilter/aPath"); + $resolver = new WebPathResolver( $this->createFilesystemMock(), - $requestContext, - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + $requestContext ); $this->assertEquals( 'theschema://thehost/aCachePrefix/aFilter/aPath', - $resolver->resolve('aPath', 'aFilter') + $resolver->resolve($path, $filter) ); } public function testComposeSchemaHostAndBasePathWithPhpFileAndFileUrlOnResolve() { + $path = 'aPath'; + $filter = 'aFilter'; + $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('/theBasePath/app.php'); - + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFileUrl") + ->with($this->equalTo($path), $this->equalTo($filter)) + ->willReturn("aCachePrefix/aFilter/aPath"); + $resolver = new WebPathResolver( $this->createFilesystemMock(), - $requestContext, - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + $requestContext ); $this->assertEquals( 'theschema://thehost/theBasePath/aCachePrefix/aFilter/aPath', - $resolver->resolve('aPath', 'aFilter') + $resolver->resolve($path, $filter) ); } public function testComposeSchemaHostAndBasePathWithDirsOnlyAndFileUrlOnResolve() { + $path = 'aPath'; + $filter = 'aFilter'; + $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('/theBasePath/theSubBasePath'); - + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFileUrl") + ->with($this->equalTo($path), $this->equalTo($filter)) + ->willReturn("aCachePrefix/aFilter/aPath"); + $resolver = new WebPathResolver( $this->createFilesystemMock(), - $requestContext, - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + $requestContext ); $this->assertEquals( 'theschema://thehost/theBasePath/theSubBasePath/aCachePrefix/aFilter/aPath', - $resolver->resolve('aPath', 'aFilter') + $resolver->resolve($path, $filter) ); } public function testComposeSchemaHostAndBasePathWithBackSplashOnResolve() { + $path = 'aPath'; + $filter = 'aFilter'; + $fileUrl = "aCachePrefix/aFilter/aPath"; + $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('\\'); - + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFileUrl") + ->with($this->equalTo($path), $this->equalTo($filter)) + ->willReturn($fileUrl); + $resolver = new WebPathResolver( $this->createFilesystemMock(), - $requestContext, - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + $requestContext ); $this->assertEquals( - 'theschema://thehost/aCachePrefix/aFilter/aPath', - $resolver->resolve('aPath', 'aFilter') + sprintf('theschema://thehost/%s', $fileUrl), + $resolver->resolve($path, $filter) ); } public function testComposeSchemaHttpAndCustomPortAndFileUrlOnResolve() { + $path = 'aPath'; + $filter = 'aFilter'; + $fileUrl = "aCachePrefix/aFilter/aPath"; + $requestContext = new RequestContext(); $requestContext->setScheme('http'); $requestContext->setHost('thehost'); $requestContext->setHttpPort(88); + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFileUrl") + ->with($this->equalTo($path), $this->equalTo($filter)) + ->willReturn($fileUrl); $resolver = new WebPathResolver( $this->createFilesystemMock(), - $requestContext, - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + $requestContext ); $this->assertEquals( - 'http://thehost:88/aCachePrefix/aFilter/aPath', - $resolver->resolve('aPath', 'aFilter') + sprintf('http://thehost:88/%s', $fileUrl), + $resolver->resolve($path, $filter) ); } public function testComposeSchemaHttpsAndCustomPortAndFileUrlOnResolve() { + $path = 'aPath'; + $filter = 'aFilter'; + $fileUrl = "aCachePrefix/aFilter/aPath"; + $requestContext = new RequestContext(); $requestContext->setScheme('https'); $requestContext->setHost('thehost'); $requestContext->setHttpsPort(444); + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFileUrl") + ->with($this->equalTo($path), $this->equalTo($filter)) + ->willReturn($fileUrl); $resolver = new WebPathResolver( $this->createFilesystemMock(), - $requestContext, - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + $requestContext ); $this->assertEquals( - 'https://thehost:444/aCachePrefix/aFilter/aPath', - $resolver->resolve('aPath', 'aFilter') + sprintf('https://thehost:444/%s', $fileUrl), + $resolver->resolve($path, $filter) ); } public function testDumpBinaryContentOnStore() { - $binary = new Binary('theContent', 'aMimeType', 'aFormat'); + $path = 'aPath'; + $filter = 'aFilter'; + $fileUrl = '/aWebRoot/aCachePrefix/aFilter/aPath'; + $fileContent = 'theContent'; + + $binary = new Binary($fileContent, 'aMimeType', 'aFormat'); $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->once()) ->method('dumpFile') - ->with('/aWebRoot/aCachePrefix/aFilter/aPath', 'theContent'); - + ->with($this->equalTo($fileUrl), $this->equalTo($fileContent)); + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFilePath") + ->with($this->equalTo($path), $this->equalTo($filter)) + ->willReturn($fileUrl); + $resolver = new WebPathResolver( $filesystemMock, - new RequestContext(), - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + new RequestContext() ); - $this->assertNull($resolver->store($binary, 'aPath', 'aFilter')); + $this->assertNull($resolver->store($binary, $path, $filter)); } public function testDoNothingIfFiltersAndPathsEmptyOnRemove() @@ -315,9 +314,8 @@ public function testDoNothingIfFiltersAndPathsEmptyOnRemove() $resolver = new WebPathResolver( $filesystemMock, - new RequestContext(), - '/aWebRoot', - 'aCachePrefix' + $this->createPathResolverMock(), + new RequestContext() ); $resolver->remove(array(), array()); @@ -325,17 +323,21 @@ public function testDoNothingIfFiltersAndPathsEmptyOnRemove() public function testRemoveCacheForPathAndFilterOnRemove() { + $filePath = '/aWebRoot/aCachePrefix/aFilter/aPath'; $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->once()) ->method('remove') - ->with('/aWebRoot/aCachePrefix/aFilter/aPath'); + ->with($filePath); + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFilePath") + ->willReturn($filePath); $resolver = new WebPathResolver( $filesystemMock, - new RequestContext(), - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + new RequestContext() ); $resolver->remove(array('aPath'), array('aFilter')); @@ -343,74 +345,108 @@ public function testRemoveCacheForPathAndFilterOnRemove() public function testRemoveCacheForSomePathsAndFilterOnRemove() { + $cacheRoot = '/aWebRoot/aCachePrefix'; + $pathOne = 'aPathOne'; + $pathTwo = 'aPathTwo'; + $filter = 'aFilter'; + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->at(0)) ->method('remove') - ->with('/aWebRoot/aCachePrefix/aFilter/aPathOne'); + ->with(sprintf("%s/%s/%s", $cacheRoot, $filter, $pathOne)); $filesystemMock ->expects($this->at(1)) ->method('remove') - ->with('/aWebRoot/aCachePrefix/aFilter/aPathTwo'); + ->with(sprintf("%s/%s/%s", $cacheRoot, $filter, $pathTwo)); + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFilePath") + ->willReturnMap( + [ + [$pathOne, $filter, sprintf("%s/%s/%s", $cacheRoot, $filter, $pathOne)], + [$pathTwo, $filter, sprintf("%s/%s/%s", $cacheRoot, $filter, $pathTwo)], + ] + ); $resolver = new WebPathResolver( $filesystemMock, - new RequestContext(), - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + new RequestContext() ); - $resolver->remove(array('aPathOne', 'aPathTwo'), array('aFilter')); + $resolver->remove(array($pathOne, $pathTwo), array($filter)); } public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() { + $cacheRoot = '/aWebRoot/aCachePrefix'; + $pathOne = 'aPathOne'; + $pathTwo = 'aPathTwo'; + $filterOne = 'aFilterOne'; + $filterTwo = 'aFilterTwo'; + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->at(0)) ->method('remove') - ->with('/aWebRoot/aCachePrefix/aFilterOne/aPathOne'); + ->with(sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne)); $filesystemMock ->expects($this->at(1)) ->method('remove') - ->with('/aWebRoot/aCachePrefix/aFilterTwo/aPathOne'); + ->with(sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne)); $filesystemMock ->expects($this->at(2)) ->method('remove') - ->with('/aWebRoot/aCachePrefix/aFilterOne/aPathTwo'); + ->with(sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo)); $filesystemMock ->expects($this->at(3)) ->method('remove') - ->with('/aWebRoot/aCachePrefix/aFilterTwo/aPathTwo'); - + ->with(sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo)); + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getFilePath") + ->willReturnMap( + [ + [$pathOne, $filterOne, sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne)], + [$pathOne, $filterTwo, sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne)], + [$pathTwo, $filterOne, sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo)], + [$pathTwo, $filterTwo, sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo)], + ] + ); + $resolver = new WebPathResolver( $filesystemMock, - new RequestContext(), - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + new RequestContext() ); $resolver->remove( - array('aPathOne', 'aPathTwo'), - array('aFilterOne', 'aFilterTwo') + array($pathOne, $pathTwo), + array($filterOne, $filterTwo) ); } public function testRemoveCacheForFilterOnRemove() { + $cacheRoot = '/aWebRoot/aCachePrefix'; + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->once()) ->method('remove') ->with(array( - '/aWebRoot/aCachePrefix/aFilter', + sprintf("%s/aFilter", $cacheRoot), )); + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getCacheRoot") + ->willReturn($cacheRoot); $resolver = new WebPathResolver( $filesystemMock, - new RequestContext(), - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + new RequestContext() ); $resolver->remove(array(), array('aFilter')); @@ -418,61 +454,30 @@ public function testRemoveCacheForFilterOnRemove() public function testRemoveCacheForSomeFiltersOnRemove() { + $cacheRoot = '/aWebRoot/aCachePrefix'; + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->once()) ->method('remove') ->with(array( - '/aWebRoot/aCachePrefix/aFilterOne', - '/aWebRoot/aCachePrefix/aFilterTwo', + sprintf("%s/aFilterOne", $cacheRoot), + sprintf("%s/aFilterTwo", $cacheRoot), )); - + + $pathResolver = $this->createPathResolverMock(); + $pathResolver->method("getCacheRoot") + ->willReturn($cacheRoot); + $resolver = new WebPathResolver( $filesystemMock, - new RequestContext(), - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + new RequestContext() ); $resolver->remove(array(), array('aFilterOne', 'aFilterTwo')); } - public function testShouldRemoveDoubleSlashInUrl() - { - $resolver = new WebPathResolver( - $this->createFilesystemMock(), - new RequestContext(), - '/aWebRoot', - 'aCachePrefix' - ); - - $rc = new \ReflectionClass($resolver); - $method = $rc->getMethod('getFileUrl'); - $method->setAccessible(true); - - $result = $method->invokeArgs($resolver, array('/cats.jpg', 'some_filter')); - - $this->assertEquals('aCachePrefix/some_filter/cats.jpg', $result); - } - - public function testShouldSanitizeSeparatorBetweenSchemeAndAuthorityInUrl() - { - $resolver = new WebPathResolver( - $this->createFilesystemMock(), - new RequestContext(), - '/aWebRoot', - 'aCachePrefix' - ); - - $rc = new \ReflectionClass($resolver); - $method = $rc->getMethod('getFileUrl'); - $method->setAccessible(true); - - $result = $method->invokeArgs($resolver, array('https://some.meme.com/cute/cats.jpg', 'some_filter')); - - $this->assertEquals('aCachePrefix/some_filter/https---some.meme.com/cute/cats.jpg', $result); - } - /** * @return \PHPUnit_Framework_MockObject_MockObject|Filesystem */ @@ -480,4 +485,12 @@ protected function createFilesystemMock() { return $this->getMockBuilder(Filesystem::class)->getMock(); } + + /** + * @return \PHPUnit\Framework\MockObject\MockObject|PathResolverInterface + */ + public function createPathResolverMock() + { + return $this->getMockBuilder(PathResolverInterface::class)->getMock(); + } } From bbbbc4ee56f6ee7b2e30d3874a07f6e33f171bce Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Tue, 20 Feb 2018 15:58:51 +0200 Subject: [PATCH 05/13] Tests added --- .../Resolver/WebPathResolverFactory.php | 6 +- ...solver.php => RelativeWebPathResolver.php} | 2 +- Resources/config/imagine.xml | 2 +- .../Resolver/WebPathResolverFactoryTest.php | 6 +- .../Resolver/RelativeWebPathResolverTest.php | 234 ++++++++++++++++++ .../Cache/Resolver/WebPathResolverTest.php | 5 +- Tests/Utility/Path/PathResolverTest.php | 211 ++++++++++++++++ 7 files changed, 454 insertions(+), 12 deletions(-) rename Imagine/Cache/Resolver/{AlternativeWebPathResolver.php => RelativeWebPathResolver.php} (78%) create mode 100644 Tests/Imagine/Cache/Resolver/RelativeWebPathResolverTest.php create mode 100644 Tests/Utility/Path/PathResolverTest.php diff --git a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php index e8d7777b3..9057ff75a 100644 --- a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php @@ -24,8 +24,8 @@ class WebPathResolverFactory extends AbstractResolverFactory */ public function create(ContainerBuilder $container, $resolverName, array $config) { - if ($config["alt_url"]) { - $resolverDefinition = $this->getChildResolverDefinition(sprintf("alt_%s", $this->getName())); + if ($config["rel_url"]) { + $resolverDefinition = $this->getChildResolverDefinition(sprintf("rel_%s", $this->getName())); } else { $resolverDefinition = $this->getChildResolverDefinition(); } @@ -71,7 +71,7 @@ public function addConfiguration(ArrayNodeDefinition $builder) ->defaultValue('media/cache') ->cannotBeEmpty() ->end() - ->booleanNode('alt_url') + ->booleanNode('rel_url') ->defaultFalse() ->end() ->end(); diff --git a/Imagine/Cache/Resolver/AlternativeWebPathResolver.php b/Imagine/Cache/Resolver/RelativeWebPathResolver.php similarity index 78% rename from Imagine/Cache/Resolver/AlternativeWebPathResolver.php rename to Imagine/Cache/Resolver/RelativeWebPathResolver.php index 1d072bc52..dfd93e328 100644 --- a/Imagine/Cache/Resolver/AlternativeWebPathResolver.php +++ b/Imagine/Cache/Resolver/RelativeWebPathResolver.php @@ -2,7 +2,7 @@ namespace Liip\ImagineBundle\Imagine\Cache\Resolver; -class AlternativeWebPathResolver extends AbstractWebPathResolver +class RelativeWebPathResolver extends AbstractWebPathResolver { /** * {@inheritdoc} diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index 90f2d3356..0ee6c2374 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -214,7 +214,7 @@ - + diff --git a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php index 8c63decac..d2fb3a1f8 100644 --- a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php @@ -53,14 +53,14 @@ public function testCreateResolverDefinitionOnCreate() $resolver = new WebPathResolverFactory(); $testCases = [ - ['alt_url' => false, 'resolverPrototype' => 'liip_imagine.cache.resolver.prototype.web_path'], - ['alt_url' => true, 'resolverPrototype' => 'liip_imagine.cache.resolver.prototype.alt_web_path'], + ['rel_url' => false, 'resolverPrototype' => 'liip_imagine.cache.resolver.prototype.web_path'], + ['rel_url' => true, 'resolverPrototype' => 'liip_imagine.cache.resolver.prototype.rel_web_path'], ]; foreach ($testCases as $testCase) { $resolver->create($container, 'the_resolver_name', array( 'web_root' => 'theWebRoot', 'cache_prefix' => 'theCachePrefix', - 'alt_url' => $testCase["alt_url"], + 'rel_url' => $testCase["rel_url"], )); $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); diff --git a/Tests/Imagine/Cache/Resolver/RelativeWebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/RelativeWebPathResolverTest.php new file mode 100644 index 000000000..51e311c65 --- /dev/null +++ b/Tests/Imagine/Cache/Resolver/RelativeWebPathResolverTest.php @@ -0,0 +1,234 @@ +filesystem = $this->getMockBuilder(Filesystem::class)->getMock(); + $this->pathResolverUtil = $this->getMockBuilder(PathResolverInterface::class)->getMock(); + $this->relativeWebPathResolver = new RelativeWebPathResolver($this->filesystem, $this->pathResolverUtil); + + $this->basePath = sys_get_temp_dir().'/aWebRoot'; + } + + public function testImplementsResolverInterface() + { + $this->assertInstanceOf(ResolverInterface::class, $this->relativeWebPathResolver); + } + + public function testResolve() + { + $path = 'aPath'; + $filter = 'aFilter'; + $fileUrl = 'cacheDir/aFilter/aPath'; + + $this->pathResolverUtil + ->expects($this->once()) + ->method('getFileUrl') + ->with($this->equalTo($path), $this->equalTo($filter)) + ->willReturn($fileUrl); + + $actualFileUrl = $this->relativeWebPathResolver->resolve($path, $filter); + + $this->assertEquals(sprintf('/%s', $fileUrl), $actualFileUrl); + } + + public function testOnSameConstructorArguments() + { + $this->assertAttributeEquals($this->filesystem,'filesystem', $this->relativeWebPathResolver); + $this->assertAttributeEquals($this->pathResolverUtil,'pathResolver', $this->relativeWebPathResolver); + } + + public function testFileIsStored() + { + $existingFile = $this->basePath.'/aCachePrefix/aFilter/existingPath'; + $filesystem = new Filesystem(); + $filesystem->mkdir(dirname($existingFile)); + $filesystem->touch($existingFile); + + $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); + $resolver = new RelativeWebPathResolver( + $this->filesystem, + $pathResolver + ); + + $this->assertTrue($resolver->isStored('existingPath', 'aFilter')); + $filesystem->remove($this->basePath); + } + + public function testFileIsNotStored() + { + $existingFile = $this->basePath.'/aCachePrefix/aFilter/existingPath'; + $filesystem = new Filesystem(); + $filesystem->mkdir(dirname($existingFile)); + $filesystem->touch($existingFile); + + $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); + $resolver = new RelativeWebPathResolver( + $this->filesystem, + $pathResolver + ); + + $this->assertFalse($resolver->isStored('notExisting file', 'aFilter')); + $filesystem->remove($this->basePath); + } + + public function testStore() + { + $path = 'aPath'; + $filter = 'aFilter'; + $filePath = '/rootDir/cacheDir/file'; + $fileContent = 'theFileContent'; + + $binary = new Binary($fileContent, 'applivation/customFile', 'custom'); + + $this->pathResolverUtil + ->expects($this->once()) + ->method('getFilePath') + ->with($this->equalTo($path), $this->equalTo($filter)) + ->willReturn($filePath); + + $this->filesystem + ->expects($this->once()) + ->method('dumpFile') + ->with($this->equalTo($filePath), $this->equalTo($fileContent)); + + $this->relativeWebPathResolver->store($binary, $path, $filter); + } + + public function testRemoveWithEmptyInputArrays() + { + $this->filesystem + ->expects($this->exactly(0)) + ->method('remove'); + + $this->relativeWebPathResolver->remove(array(), array()); + } + + public function testRemoveWithEmptyPathsArrayAndSingleFilter() + { + $filter = 'aFilter'; + $cacheRoot = '/root/cacheFolder'; + + $this->pathResolverUtil + ->expects($this->once()) + ->method('getCacheRoot') + ->willReturn($cacheRoot); + + $this->filesystem + ->expects($this->once()) + ->method('remove') + ->with( + $this->equalTo( + [ + sprintf('%s/%s', $cacheRoot, $filter), + ] + ) + ); + + $this->relativeWebPathResolver->remove(array(), array($filter)); + } + + public function testRemoveWithEmptyPathsArrayAndMultipleFilters() + { + $filterOne = 'aFilterOne'; + $filterTwo = 'aFilterTwo'; + $cacheRoot = '/root/cacheFolder'; + + $this->pathResolverUtil + ->expects($this->exactly(2)) + ->method('getCacheRoot') + ->willReturn($cacheRoot); + + $this->filesystem + ->expects($this->once()) + ->method('remove') + ->with( + $this->equalTo( + [ + sprintf('%s/%s', $cacheRoot, $filterOne), + sprintf('%s/%s', $cacheRoot, $filterTwo), + ] + ) + ); + + $this->relativeWebPathResolver->remove(array(), array($filterOne, $filterTwo)); + } + + public function testRemoveWithMultiplePathaAndFilters() + { + $filterOne = 'aFilterOne'; + $filterTwo = 'aFilterTwo'; + $pathOne = 'aPathOne'; + $pathTwo = 'aPathTwo'; + $cacheRoot = '/root/cacheFolder'; + + $this->pathResolverUtil + ->expects($this->exactly(0)) + ->method('getCacheRoot'); + + $this->pathResolverUtil + ->method("getFilePath") + ->willReturnMap( + [ + [$pathOne, $filterOne, sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne)], + [$pathOne, $filterTwo, sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne)], + [$pathTwo, $filterOne, sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo)], + [$pathTwo, $filterTwo, sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo)], + ] + ); + + $this->filesystem + ->expects($this->at(0)) + ->method('remove') + ->with(sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne)); + $this->filesystem + ->expects($this->at(1)) + ->method('remove') + ->with(sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne)); + $this->filesystem + ->expects($this->at(2)) + ->method('remove') + ->with(sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo)); + $this->filesystem + ->expects($this->at(3)) + ->method('remove') + ->with(sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo)); + + + $this->relativeWebPathResolver->remove( + array($pathOne, $pathTwo), + array($filterOne, $filterTwo) + ); + } +} diff --git a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php index dee906f4c..2390c7a3e 100644 --- a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php @@ -65,10 +65,7 @@ public function testCouldBeConstructedWithRequiredArguments() $filesystemMock = $this->createFilesystemMock(); $pathResolver = $this->createPathResolverMock(); $requestContext = new RequestContext(); - - /** - * @var PathResolverInterface $pathResolver - */ + $resolver = new WebPathResolver($filesystemMock, $pathResolver, $requestContext); $this->assertAttributeSame($filesystemMock, 'filesystem', $resolver); diff --git a/Tests/Utility/Path/PathResolverTest.php b/Tests/Utility/Path/PathResolverTest.php new file mode 100644 index 000000000..1058bec66 --- /dev/null +++ b/Tests/Utility/Path/PathResolverTest.php @@ -0,0 +1,211 @@ +assertTrue(is_a(PathResolver::class, PathResolverInterface::class, true)); + } + + public function testPropertiesForSameConstructorArguments() + { + $webRootDir = 'aWebRootDir'; + $cachePrefix = 'aCachePrefix'; + $pathResolver = new PathResolver($webRootDir, $cachePrefix); + $this->assertAttributeEquals($webRootDir, 'webRoot', $pathResolver); + $this->assertAttributeEquals($cachePrefix, 'cachePrefix', $pathResolver); + } + + public function testWebRootPathNormalizer() + { + $pathResolver = new PathResolver('aWebRootDir/'); + $this->assertAttributeEquals('aWebRootDir', 'webRoot', $pathResolver); + } + + public function testCachePrefixNormalizer() + { + $pathResolver = new PathResolver('aWebRootDir', '/cachePrefix'); + $this->assertAttributeEquals('cachePrefix', 'cachePrefix', $pathResolver); + } + + public function testForDoubleSlashReplacing() + { + $pathResolver = new PathResolver( + 'aWebRootDir//subRootDir', + 'cachePrefix//subCacheDir' + ); + $this->assertAttributeEquals( + 'aWebRootDir/subRootDir', + 'webRoot', + $pathResolver + ); + $this->assertAttributeEquals( + 'cachePrefix/subCacheDir', + 'cachePrefix', + $pathResolver + ); + } + + public function testPathsNormalizerWithSubfolders() + { + $pathResolver = new PathResolver( + 'aWebRootDir/subRootDir', + 'cachePrefix/subCacheDir/anotherSubCacheDir' + ); + $this->assertAttributeEquals( + 'aWebRootDir/subRootDir', + 'webRoot', + $pathResolver + ); + $this->assertAttributeEquals( + 'cachePrefix/subCacheDir/anotherSubCacheDir', + 'cachePrefix', + $pathResolver + ); + } + + public function testCacheRootPathDirCreationWithoutInvalidSlashes() + { + $pathResolver = new PathResolver( + 'aWebRootDir/subRootDir', + 'cachePrefix/subCacheDir' + ); + $this->assertAttributeEquals( + 'aWebRootDir/subRootDir', + 'webRoot', + $pathResolver + ); + $this->assertAttributeEquals( + 'cachePrefix/subCacheDir', + 'cachePrefix', + $pathResolver + ); + $this->assertAttributeEquals( + 'aWebRootDir/subRootDir/cachePrefix/subCacheDir', + 'cacheRoot', + $pathResolver + ); + } + + public function testCacheRootPathDirCreationWithInvalidSlashes() + { + $pathResolver = new PathResolver( + 'aWebRootDir/subRootDir/', + '/cachePrefix/subCacheDir' + ); + $this->assertAttributeEquals( + 'aWebRootDir/subRootDir', + 'webRoot', + $pathResolver + ); + $this->assertAttributeEquals( + 'cachePrefix/subCacheDir', + 'cachePrefix', + $pathResolver + ); + $this->assertAttributeEquals( + 'aWebRootDir/subRootDir/cachePrefix/subCacheDir', + 'cacheRoot', + $pathResolver + ); + } + + public function testCacheRootPathDirCreationWithDoubledSlashes() + { + $pathResolver = new PathResolver( + 'aWebRootDir//subRootDir/', + '/cachePrefix//subCacheDir' + ); + $this->assertAttributeEquals( + 'aWebRootDir/subRootDir', + 'webRoot', + $pathResolver + ); + $this->assertAttributeEquals( + 'cachePrefix/subCacheDir', + 'cachePrefix', + $pathResolver + ); + $this->assertAttributeEquals( + 'aWebRootDir/subRootDir/cachePrefix/subCacheDir', + 'cacheRoot', + $pathResolver + ); + } + + public function testGetCacheRoot() + { + $pathResolver = new PathResolver( + 'aWebRootDir', + '/cachePrefix//subCacheDir' + ); + $this->assertEquals( + 'aWebRootDir/cachePrefix/subCacheDir', + $pathResolver->getCacheRoot() + ); + } + + public function testGetFileUrlWithSchemePath() + { + $path = 'https://path-to-no-where'; + $filter = 'aFilter'; + $cachePrefix = 'aCahcePrefix'; + + $pathResolver = new PathResolver('aWebRootDir', $cachePrefix); + $actualFileUrl = $pathResolver->getFileUrl($path, $filter); + + $this->assertEquals(sprintf('%s/%s/https---path-to-no-where', $cachePrefix, $filter), $actualFileUrl); + } + + public function testGetFileUrlPathTrim() + { + $path = '/path-to-no-where'; + $filter = 'aFilter'; + $cachePrefix = 'aCahcePrefix'; + + $pathResolver = new PathResolver('aWebRootDir', $cachePrefix); + $actualFileUrl = $pathResolver->getFileUrl($path, $filter); + + $this->assertEquals(sprintf('%s/%s/path-to-no-where', $cachePrefix, $filter), $actualFileUrl); + } + + public function testGetFilePathWithSchemePath() + { + $path = 'https://path-to-no-where'; + $filter = 'aFilter'; + $webRootDir = 'aWebRootDir'; + $cachePrefix = 'aCahcePrefix'; + + $pathResolver = new PathResolver($webRootDir, $cachePrefix); + $actualFileUrl = $pathResolver->getFilePath($path, $filter); + + $this->assertEquals( + sprintf('%s/%s/%s/https---path-to-no-where', $webRootDir, $cachePrefix, $filter), + $actualFileUrl + ); + } + + public function testGetFilePathWithPathTrim() + { + $path = '/path-to-no-where'; + $filter = 'aFilter'; + $webRootDir = 'aWebRootDir'; + $cachePrefix = 'aCahcePrefix'; + + $pathResolver = new PathResolver($webRootDir, $cachePrefix); + $actualFileUrl = $pathResolver->getFilePath($path, $filter); + + $this->assertEquals( + sprintf('%s/%s/%s/path-to-no-where', $webRootDir, $cachePrefix, $filter), + $actualFileUrl + ); + } +} From cda461eee3738925e9dfa90cdecf3e89dec005aa Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Tue, 20 Feb 2018 16:30:53 +0200 Subject: [PATCH 06/13] WebPathResolver test fixed --- Tests/Imagine/Cache/Resolver/WebPathResolverTest.php | 5 ++--- Tests/Utility/Path/PathResolverTest.php | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php index 74dda11c3..500f0f9b7 100644 --- a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php @@ -126,9 +126,8 @@ public function testComposeSchemaHostAndFileUrlOnResolve() $resolver = new WebPathResolver( $this->createFilesystemMock(), - $requestContext, - '/aWebRoot', - 'aCachePrefix' + $pathResolver, + $requestContext ); $this->assertEquals( diff --git a/Tests/Utility/Path/PathResolverTest.php b/Tests/Utility/Path/PathResolverTest.php index 1058bec66..ee55c4404 100644 --- a/Tests/Utility/Path/PathResolverTest.php +++ b/Tests/Utility/Path/PathResolverTest.php @@ -4,11 +4,12 @@ use Liip\ImagineBundle\Utility\Path\PathResolver; use Liip\ImagineBundle\Utility\Path\PathResolverInterface; +use PHPUnit\Framework\TestCase; /** * @covers \Liip\ImagineBundle\Utility\Path\PathResolver */ -class PathResolverTest extends \PHPUnit\Framework\TestCase +class PathResolverTest extends TestCase { public function testForInterfaceImplementation() { From 7e7f3af33b4ad650acce3536fb215200584d357b Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Tue, 20 Feb 2018 17:04:53 +0200 Subject: [PATCH 07/13] standalone RelativeWebPathResolver added --- .../Resolver/AbstractWebPathResolver.php | 55 ++++++++++++++++ .../RelativeWebPathResolverFactory.php | 23 +++++++ .../Resolver/WebPathResolverFactory.php | 58 +---------------- LiipImagineBundle.php | 2 + Resources/config/imagine.xml | 2 +- .../Resolver/WebPathResolverFactoryTest.php | 65 +++++++++---------- Tests/LiipImagineBundleTest.php | 6 +- 7 files changed, 114 insertions(+), 97 deletions(-) create mode 100644 DependencyInjection/Factory/Resolver/AbstractWebPathResolver.php create mode 100644 DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php diff --git a/DependencyInjection/Factory/Resolver/AbstractWebPathResolver.php b/DependencyInjection/Factory/Resolver/AbstractWebPathResolver.php new file mode 100644 index 000000000..dcc3bcfcd --- /dev/null +++ b/DependencyInjection/Factory/Resolver/AbstractWebPathResolver.php @@ -0,0 +1,55 @@ +getChildResolverDefinition(); + $pathResolverDefinition = new ChildDefinition('liip_imagine.util.resolver.prototype.path'); + $pathResolverDefinition->replaceArgument(0, $config['web_root']); + $pathResolverDefinition->replaceArgument(1, $config['cache_prefix']); + + $pathResolverServiceId = 'liip_imagine.util.resolver.path'; + $container->setDefinition($pathResolverServiceId, $pathResolverDefinition); + + $resolverDefinition->replaceArgument(1, new Reference($pathResolverServiceId)); + + $resolverDefinition->addTag('liip_imagine.cache.resolver', [ + 'resolver' => $resolverName, + ]); + + $resolverId = 'liip_imagine.cache.resolver.'; + $container->setDefinition($resolverId.$resolverName, $resolverDefinition); + + return $resolverId; + } + + /** + * {@inheritdoc} + */ + public function addConfiguration(ArrayNodeDefinition $builder) + { + $builder + ->children() + ->scalarNode('web_root') + ->defaultValue(SymfonyFramework::getContainerResolvableRootWebPath()) + ->cannotBeEmpty() + ->end() + ->scalarNode('cache_prefix') + ->defaultValue('media/cache') + ->cannotBeEmpty() + ->end() + ->end(); + } +} diff --git a/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php new file mode 100644 index 000000000..c636d4276 --- /dev/null +++ b/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php @@ -0,0 +1,23 @@ +getChildResolverDefinition(sprintf("rel_%s", $this->getName())); - } else { - $resolverDefinition = $this->getChildResolverDefinition(); - } - $pathResolverDefinition = new ChildDefinition('liip_imagine.util.resolver.prototype.path'); - $pathResolverDefinition->replaceArgument(0, $config['web_root']); - $pathResolverDefinition->replaceArgument(1, $config['cache_prefix']); - - $pathResolverServiceId = 'liip_imagine.util.resolver.path'; - $container->setDefinition($pathResolverServiceId, $pathResolverDefinition); - - $resolverDefinition->replaceArgument(1, new Reference($pathResolverServiceId)); - - $resolverDefinition->addTag('liip_imagine.cache.resolver', [ - 'resolver' => $resolverName, - ]); - - $resolverId = 'liip_imagine.cache.resolver.'; - $container->setDefinition($resolverId.$resolverName, $resolverDefinition); - - return $resolverId; - } - /** * {@inheritdoc} */ @@ -55,25 +20,4 @@ public function getName() { return 'web_path'; } - - /** - * {@inheritdoc} - */ - public function addConfiguration(ArrayNodeDefinition $builder) - { - $builder - ->children() - ->scalarNode('web_root') - ->defaultValue(SymfonyFramework::getContainerResolvableRootWebPath()) - ->cannotBeEmpty() - ->end() - ->scalarNode('cache_prefix') - ->defaultValue('media/cache') - ->cannotBeEmpty() - ->end() - ->booleanNode('rel_url') - ->defaultFalse() - ->end() - ->end(); - } } diff --git a/LiipImagineBundle.php b/LiipImagineBundle.php index ebda57c88..a3ee34603 100644 --- a/LiipImagineBundle.php +++ b/LiipImagineBundle.php @@ -23,6 +23,7 @@ use Liip\ImagineBundle\DependencyInjection\Factory\Loader\StreamLoaderFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\AwsS3ResolverFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FlysystemResolverFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -55,6 +56,7 @@ public function build(ContainerBuilder $container) $extension->addResolverFactory(new WebPathResolverFactory()); $extension->addResolverFactory(new AwsS3ResolverFactory()); $extension->addResolverFactory(new FlysystemResolverFactory()); + $extension->addResolverFactory(new RelativeWebPathResolverFactory()); $extension->addLoaderFactory(new StreamLoaderFactory()); $extension->addLoaderFactory(new FileSystemLoaderFactory()); diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index 0ee6c2374..3d5aba63d 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -214,7 +214,7 @@ - + diff --git a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php index cae416d21..32842e553 100644 --- a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php @@ -53,44 +53,37 @@ public function testCreateResolverDefinitionOnCreate() $resolver = new WebPathResolverFactory(); - $testCases = [ - ['rel_url' => false, 'resolverPrototype' => 'liip_imagine.cache.resolver.prototype.web_path'], - ['rel_url' => true, 'resolverPrototype' => 'liip_imagine.cache.resolver.prototype.rel_web_path'], - ]; - foreach ($testCases as $testCase) { - $resolver->create($container, 'the_resolver_name', array( - 'web_root' => 'theWebRoot', - 'cache_prefix' => 'theCachePrefix', - 'rel_url' => $testCase["rel_url"], - )); + $resolver->create($container, 'the_resolver_name', array( + 'web_root' => 'theWebRoot', + 'cache_prefix' => 'theCachePrefix', + )); + + $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); + + /** + * @var ChildDefinition $resolverDefinition + */ + $resolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.the_resolver_name'); + $this->assertInstanceOf(ChildDefinition::class, $resolverDefinition); + $this->assertEquals("liip_imagine.cache.resolver.prototype.web_path", $resolverDefinition->getParent()); + /** + * @var Reference $utilPathResolverReference + */ + $utilPathResolverReference = $resolverDefinition->getArgument(1); + $this->assertInstanceOf(Reference::class, $utilPathResolverReference); - $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); + $utilPathResolverServiceId = $utilPathResolverReference->__toString(); + $this->assertEquals('liip_imagine.util.resolver.path', $utilPathResolverServiceId); + $this->assertTrue($container->hasDefinition($utilPathResolverServiceId)); + /** + * @var ChildDefinition $utilPathResolverDefinition + */ + $utilPathResolverDefinition = $container->getDefinition($utilPathResolverServiceId); + $this->assertInstanceOf(ChildDefinition::class, $utilPathResolverDefinition); + $this->assertEquals('liip_imagine.util.resolver.prototype.path', $utilPathResolverDefinition->getParent()); - /** - * @var ChildDefinition $resolverDefinition - */ - $resolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.the_resolver_name'); - $this->assertInstanceOf(ChildDefinition::class, $resolverDefinition); - $this->assertEquals($testCase['resolverPrototype'], $resolverDefinition->getParent()); - /** - * @var Reference $utilPathResolverReference - */ - $utilPathResolverReference = $resolverDefinition->getArgument(1); - $this->assertInstanceOf(Reference::class, $utilPathResolverReference); - - $utilPathResolverServiceId = $utilPathResolverReference->__toString(); - $this->assertEquals('liip_imagine.util.resolver.path', $utilPathResolverServiceId); - $this->assertTrue($container->hasDefinition($utilPathResolverServiceId)); - /** - * @var ChildDefinition $utilPathResolverDefinition - */ - $utilPathResolverDefinition = $container->getDefinition($utilPathResolverServiceId); - $this->assertInstanceOf(ChildDefinition::class, $utilPathResolverDefinition); - $this->assertEquals('liip_imagine.util.resolver.prototype.path', $utilPathResolverDefinition->getParent()); - - $this->assertEquals('theWebRoot', $utilPathResolverDefinition->getArgument(0)); - $this->assertEquals('theCachePrefix', $utilPathResolverDefinition->getArgument(1)); - } + $this->assertEquals('theWebRoot', $utilPathResolverDefinition->getArgument(0)); + $this->assertEquals('theCachePrefix', $utilPathResolverDefinition->getArgument(1)); } public function testProcessCorrectlyOptionsOnAddConfiguration() diff --git a/Tests/LiipImagineBundleTest.php b/Tests/LiipImagineBundleTest.php index 3775e4272..f6ac0cf3d 100644 --- a/Tests/LiipImagineBundleTest.php +++ b/Tests/LiipImagineBundleTest.php @@ -165,7 +165,7 @@ public function testAddStreamLoaderFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); $extensionMock - ->expects($this->at(3)) + ->expects($this->at(4)) ->method('addLoaderFactory') ->with($this->isInstanceOf(StreamLoaderFactory::class)); @@ -184,7 +184,7 @@ public function testAddFilesystemLoaderFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); $extensionMock - ->expects($this->at(4)) + ->expects($this->at(5)) ->method('addLoaderFactory') ->with($this->isInstanceOf(FileSystemLoaderFactory::class)); @@ -203,7 +203,7 @@ public function testAddFlysystemLoaderFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); $extensionMock - ->expects($this->at(5)) + ->expects($this->at(6)) ->method('addLoaderFactory') ->with($this->isInstanceOf(FlysystemLoaderFactory::class)); From 2d859961b906d88e00bf8c6c3822b7bb23f156e6 Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Tue, 20 Feb 2018 18:31:37 +0200 Subject: [PATCH 08/13] test added --- ...php => AbstractWebPathResolverFactory.php} | 2 +- ...RelativeWebPathResolverFactoryFactory.php} | 2 +- ....php => WebPathResolverFactoryFactory.php} | 2 +- LiipImagineBundle.php | 8 +- .../DependencyInjection/ConfigurationTest.php | 32 ++-- .../Resolver/AbstractWebPathResolverTest.php | 153 ++++++++++++++++++ .../RelativeWebPathResolverFactoryTest.php | 24 +++ .../Resolver/WebPathResolverFactoryTest.php | 123 +------------- .../LiipImagineExtensionTest.php | 6 +- Tests/LiipImagineBundleTest.php | 24 ++- 10 files changed, 232 insertions(+), 144 deletions(-) rename DependencyInjection/Factory/Resolver/{AbstractWebPathResolver.php => AbstractWebPathResolverFactory.php} (96%) rename DependencyInjection/Factory/Resolver/{RelativeWebPathResolverFactory.php => RelativeWebPathResolverFactoryFactory.php} (84%) rename DependencyInjection/Factory/Resolver/{WebPathResolverFactory.php => WebPathResolverFactoryFactory.php} (85%) create mode 100644 Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php create mode 100644 Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php diff --git a/DependencyInjection/Factory/Resolver/AbstractWebPathResolver.php b/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php similarity index 96% rename from DependencyInjection/Factory/Resolver/AbstractWebPathResolver.php rename to DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php index dcc3bcfcd..aefd220e0 100644 --- a/DependencyInjection/Factory/Resolver/AbstractWebPathResolver.php +++ b/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php @@ -8,7 +8,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; -abstract class AbstractWebPathResolver extends AbstractResolverFactory +abstract class AbstractWebPathResolverFactory extends AbstractResolverFactory { /** * {@inheritdoc} diff --git a/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryFactory.php similarity index 84% rename from DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php rename to DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryFactory.php index c636d4276..d48342de7 100644 --- a/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryFactory.php @@ -11,7 +11,7 @@ namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver; -class RelativeWebPathResolverFactory extends AbstractWebPathResolver +class RelativeWebPathResolverFactoryFactory extends AbstractWebPathResolverFactory { /** * {@inheritdoc} diff --git a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/WebPathResolverFactoryFactory.php similarity index 85% rename from DependencyInjection/Factory/Resolver/WebPathResolverFactory.php rename to DependencyInjection/Factory/Resolver/WebPathResolverFactoryFactory.php index d79cb5eaf..723d4caf6 100644 --- a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/WebPathResolverFactoryFactory.php @@ -11,7 +11,7 @@ namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver; -class WebPathResolverFactory extends AbstractWebPathResolver +class WebPathResolverFactoryFactory extends AbstractWebPathResolverFactory { /** * {@inheritdoc} diff --git a/LiipImagineBundle.php b/LiipImagineBundle.php index a3ee34603..7cda622d0 100644 --- a/LiipImagineBundle.php +++ b/LiipImagineBundle.php @@ -23,8 +23,8 @@ use Liip\ImagineBundle\DependencyInjection\Factory\Loader\StreamLoaderFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\AwsS3ResolverFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FlysystemResolverFactory; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactory; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactoryFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactoryFactory; use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -53,10 +53,10 @@ public function build(ContainerBuilder $container) /** @var $extension LiipImagineExtension */ $extension = $container->getExtension('liip_imagine'); - $extension->addResolverFactory(new WebPathResolverFactory()); + $extension->addResolverFactory(new WebPathResolverFactoryFactory()); $extension->addResolverFactory(new AwsS3ResolverFactory()); $extension->addResolverFactory(new FlysystemResolverFactory()); - $extension->addResolverFactory(new RelativeWebPathResolverFactory()); + $extension->addResolverFactory(new RelativeWebPathResolverFactoryFactory()); $extension->addLoaderFactory(new StreamLoaderFactory()); $extension->addLoaderFactory(new FileSystemLoaderFactory()); diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 88ee8048a..2d5797d21 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -15,7 +15,7 @@ use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FileSystemLoaderFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactoryFactory; use PHPUnit\Framework\TestCase; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -46,7 +46,7 @@ public function testInjectLoaderFactoryConfig() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FooLoaderFactory(), @@ -77,7 +77,7 @@ public function testAllowToUseLoaderFactorySeveralTimes() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FooLoaderFactory(), @@ -111,7 +111,7 @@ public function testSetFilesystemLoaderAsDefaultLoaderIfNotDefined() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), @@ -133,7 +133,7 @@ public function testSetFilesystemLoaderAsDefaultLoaderIfNull() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), @@ -158,7 +158,7 @@ public function testThrowIfLoadersNotArray() $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), @@ -175,7 +175,7 @@ public function testSetFilesystemLoaderAsDefaultIfLoadersSectionNotDefined() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), @@ -194,7 +194,7 @@ public function testSetWebPathResolversAsDefaultIfResolversSectionNotDefined() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), @@ -213,7 +213,7 @@ public function testShouldNotOverwriteDefaultLoaderIfDefined() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FooLoaderFactory(), @@ -243,7 +243,7 @@ public function testInjectResolverFactoryConfig() new Configuration( array( new BarResolverFactory(), - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), ) @@ -273,7 +273,7 @@ public function testAllowToUseResolverFactorySeveralTimes() new Configuration( array( new BarResolverFactory(), - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), @@ -306,7 +306,7 @@ public function testSetWebPathAsDefaultResolverIfNotDefined() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), ) @@ -327,7 +327,7 @@ public function testSetWebPathAsDefaultResolverIfNull() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), ) @@ -351,7 +351,7 @@ public function testThrowsIfResolversNotArray() $config = $this->processConfiguration( new Configuration( array( - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), ) @@ -372,7 +372,7 @@ public function testShouldNotOverwriteDefaultResolverIfDefined() new Configuration( array( new BarResolverFactory(), - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), @@ -401,7 +401,7 @@ public function testNewFilterQualitySettings() new Configuration( array( new BarResolverFactory(), - new WebPathResolverFactory(), + new WebPathResolverFactoryFactory(), ), array( new FileSystemLoaderFactory(), diff --git a/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php b/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php new file mode 100644 index 000000000..999befe44 --- /dev/null +++ b/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php @@ -0,0 +1,153 @@ +getClassName()); + + $this->assertTrue($rc->implementsInterface(ResolverFactoryInterface::class)); + } + + public function testCouldBeConstructedWithoutAnyArguments() + { + $loader = $this->createResolver(); + + $this->assertInstanceOf($this->getClassName(), $loader); + } + + public function testAbstractWebPathResolverFactoryImplementation() + { + $this->assertTrue(is_a($this->getClassName(), AbstractWebPathResolverFactory::class, true)); + } + + public function testCreateResolverDefinitionOnCreate() + { + $container = new ContainerBuilder(); + + $resolver = $this->createResolver(); + $resolver->create( + $container, + 'the_resolver_name', + array( + 'web_root' => 'theWebRoot', + 'cache_prefix' => 'theCachePrefix', + ) + ); + + $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); + + /** + * @var ChildDefinition $resolverDefinition + */ + $resolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.the_resolver_name'); + $this->assertInstanceOf(ChildDefinition::class, $resolverDefinition); + $this->assertEquals( + sprintf('liip_imagine.cache.resolver.prototype.%s', $resolver->getName()), + $resolverDefinition->getParent() + ); + + /** + * @var Reference $utilPathResolverReference + */ + $utilPathResolverReference = $resolverDefinition->getArgument(1); + $this->assertInstanceOf(Reference::class, $utilPathResolverReference); + + $utilPathResolverServiceId = $utilPathResolverReference->__toString(); + $this->assertEquals('liip_imagine.util.resolver.path', $utilPathResolverServiceId); + $this->assertTrue($container->hasDefinition($utilPathResolverServiceId)); + /** + * @var ChildDefinition $utilPathResolverDefinition + */ + $utilPathResolverDefinition = $container->getDefinition($utilPathResolverServiceId); + $this->assertInstanceOf(ChildDefinition::class, $utilPathResolverDefinition); + $this->assertEquals('liip_imagine.util.resolver.prototype.path', $utilPathResolverDefinition->getParent()); + + $this->assertEquals('theWebRoot', $utilPathResolverDefinition->getArgument(0)); + $this->assertEquals('theCachePrefix', $utilPathResolverDefinition->getArgument(1)); + } + + public function testProcessCorrectlyOptionsOnAddConfiguration() + { + $expectedWebPath = 'theWebPath'; + $expectedCachePrefix = 'theCachePrefix'; + + $treeBuilder = new TreeBuilder(); + $rootNode = $treeBuilder->root('test_resolver_name', 'array'); + + $resolver = $this->createResolver(); + $resolver->addConfiguration($rootNode); + + $config = $this->processConfigTree($treeBuilder, array( + $resolver->getName() => array( + 'web_root' => $expectedWebPath, + 'cache_prefix' => $expectedCachePrefix, + ), + )); + + $this->assertArrayHasKey('web_root', $config); + $this->assertEquals($expectedWebPath, $config['web_root']); + + $this->assertArrayHasKey('cache_prefix', $config); + $this->assertEquals($expectedCachePrefix, $config['cache_prefix']); + } + + public function testAddDefaultOptionsIfNotSetOnAddConfiguration() + { + $treeBuilder = new TreeBuilder(); + $rootNode = $treeBuilder->root('test_resolver_name', 'array'); + + $resolver = $this->createResolver(); + $resolver->addConfiguration($rootNode); + + $config = $this->processConfigTree($treeBuilder, array( + $resolver->getName() => array(), + )); + + $this->assertArrayHasKey('web_root', $config); + $this->assertEquals(SymfonyFramework::getContainerResolvableRootWebPath(), $config['web_root']); + + $this->assertArrayHasKey('cache_prefix', $config); + $this->assertEquals('media/cache', $config['cache_prefix']); + } + + /** + * @param TreeBuilder $treeBuilder + * @param array $configs + * + * @return array + */ + protected function processConfigTree(TreeBuilder $treeBuilder, array $configs) + { + $processor = new Processor(); + + return $processor->process($treeBuilder->buildTree(), $configs); + } + + /** + * @return string|ResolverFactoryInterface + */ + abstract protected function getClassName(); + + private function createResolver() + { + $className = $this->getClassName(); + + return new $className; + } +} diff --git a/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php new file mode 100644 index 000000000..c04843530 --- /dev/null +++ b/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php @@ -0,0 +1,24 @@ +assertEquals('relative_web_path', $resolver->getName()); + } + + /** + * @return string|ResolverFactoryInterface + */ + protected function getClassName() + { + return RelativeWebPathResolverFactoryFactory::class; + } +} diff --git a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php index 32842e553..6c4712e58 100644 --- a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php @@ -12,134 +12,25 @@ namespace Liip\ImagineBundle\Tests\DependencyInjection\Factory\Resolver; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; -use Liip\ImagineBundle\Utility\Framework\SymfonyFramework; -use PHPUnit\Framework\TestCase; -use Symfony\Component\Config\Definition\Builder\TreeBuilder; -use Symfony\Component\Config\Definition\Processor; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\ChildDefinition; -use Symfony\Component\DependencyInjection\Reference; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactoryFactory; /** - * @covers \Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory + * @covers \Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactoryFactory */ -class WebPathResolverFactoryTest extends TestCase +class WebPathResolverFactoryTest extends AbstractWebPathResolverTest { - public function testImplementsResolverFactoryInterface() - { - $rc = new \ReflectionClass(WebPathResolverFactory::class); - - $this->assertTrue($rc->implementsInterface(ResolverFactoryInterface::class)); - } - - public function testCouldBeConstructedWithoutAnyArguments() - { - $loader = new WebPathResolverFactory(); - - $this->assertInstanceOf(WebPathResolverFactory::class, $loader); - } - public function testReturnExpectedName() { - $resolver = new WebPathResolverFactory(); + $resolver = new WebPathResolverFactoryFactory(); $this->assertEquals('web_path', $resolver->getName()); } - - public function testCreateResolverDefinitionOnCreate() - { - $container = new ContainerBuilder(); - - $resolver = new WebPathResolverFactory(); - - $resolver->create($container, 'the_resolver_name', array( - 'web_root' => 'theWebRoot', - 'cache_prefix' => 'theCachePrefix', - )); - - $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); - /** - * @var ChildDefinition $resolverDefinition - */ - $resolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.the_resolver_name'); - $this->assertInstanceOf(ChildDefinition::class, $resolverDefinition); - $this->assertEquals("liip_imagine.cache.resolver.prototype.web_path", $resolverDefinition->getParent()); - /** - * @var Reference $utilPathResolverReference - */ - $utilPathResolverReference = $resolverDefinition->getArgument(1); - $this->assertInstanceOf(Reference::class, $utilPathResolverReference); - - $utilPathResolverServiceId = $utilPathResolverReference->__toString(); - $this->assertEquals('liip_imagine.util.resolver.path', $utilPathResolverServiceId); - $this->assertTrue($container->hasDefinition($utilPathResolverServiceId)); - /** - * @var ChildDefinition $utilPathResolverDefinition - */ - $utilPathResolverDefinition = $container->getDefinition($utilPathResolverServiceId); - $this->assertInstanceOf(ChildDefinition::class, $utilPathResolverDefinition); - $this->assertEquals('liip_imagine.util.resolver.prototype.path', $utilPathResolverDefinition->getParent()); - - $this->assertEquals('theWebRoot', $utilPathResolverDefinition->getArgument(0)); - $this->assertEquals('theCachePrefix', $utilPathResolverDefinition->getArgument(1)); - } - - public function testProcessCorrectlyOptionsOnAddConfiguration() - { - $expectedWebPath = 'theWebPath'; - $expectedCachePrefix = 'theCachePrefix'; - - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('web_path', 'array'); - - $resolver = new WebPathResolverFactory(); - $resolver->addConfiguration($rootNode); - - $config = $this->processConfigTree($treeBuilder, array( - 'web_path' => array( - 'web_root' => $expectedWebPath, - 'cache_prefix' => $expectedCachePrefix, - ), - )); - - $this->assertArrayHasKey('web_root', $config); - $this->assertEquals($expectedWebPath, $config['web_root']); - - $this->assertArrayHasKey('cache_prefix', $config); - $this->assertEquals($expectedCachePrefix, $config['cache_prefix']); - } - - public function testAddDefaultOptionsIfNotSetOnAddConfiguration() - { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('web_path', 'array'); - - $resolver = new WebPathResolverFactory(); - $resolver->addConfiguration($rootNode); - - $config = $this->processConfigTree($treeBuilder, array( - 'web_path' => array(), - )); - - $this->assertArrayHasKey('web_root', $config); - $this->assertEquals(SymfonyFramework::getContainerResolvableRootWebPath(), $config['web_root']); - - $this->assertArrayHasKey('cache_prefix', $config); - $this->assertEquals('media/cache', $config['cache_prefix']); - } - /** - * @param TreeBuilder $treeBuilder - * @param array $configs - * - * @return array + * @return string|ResolverFactoryInterface */ - protected function processConfigTree(TreeBuilder $treeBuilder, array $configs) + protected function getClassName() { - $processor = new Processor(); - - return $processor->process($treeBuilder->buildTree(), $configs); + return WebPathResolverFactoryFactory::class; } } diff --git a/Tests/DependencyInjection/LiipImagineExtensionTest.php b/Tests/DependencyInjection/LiipImagineExtensionTest.php index aec7901e3..7b906eec3 100644 --- a/Tests/DependencyInjection/LiipImagineExtensionTest.php +++ b/Tests/DependencyInjection/LiipImagineExtensionTest.php @@ -12,7 +12,7 @@ namespace Liip\ImagineBundle\Tests\DependencyInjection; use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FileSystemLoaderFactory; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactoryFactory; use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension; use Liip\ImagineBundle\Tests\AbstractTest; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -104,7 +104,7 @@ protected function createEmptyConfiguration() $this->containerBuilder = new ContainerBuilder(); $loader = new LiipImagineExtension(); $loader->addLoaderFactory(new FileSystemLoaderFactory()); - $loader->addResolverFactory(new WebPathResolverFactory()); + $loader->addResolverFactory(new WebPathResolverFactoryFactory()); $loader->load(array(array()), $this->containerBuilder); $this->assertTrue($this->containerBuilder instanceof ContainerBuilder); @@ -119,7 +119,7 @@ protected function createFullConfiguration() $this->containerBuilder = new ContainerBuilder(); $loader = new LiipImagineExtension(); $loader->addLoaderFactory(new FileSystemLoaderFactory()); - $loader->addResolverFactory(new WebPathResolverFactory()); + $loader->addResolverFactory(new WebPathResolverFactoryFactory()); $loader->load(array($this->getFullConfig()), $this->containerBuilder); $this->assertTrue($this->containerBuilder instanceof ContainerBuilder); diff --git a/Tests/LiipImagineBundleTest.php b/Tests/LiipImagineBundleTest.php index f6ac0cf3d..6f9f26fa7 100644 --- a/Tests/LiipImagineBundleTest.php +++ b/Tests/LiipImagineBundleTest.php @@ -20,7 +20,8 @@ use Liip\ImagineBundle\DependencyInjection\Factory\Loader\StreamLoaderFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\AwsS3ResolverFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FlysystemResolverFactory; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactoryFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactoryFactory; use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension; use Liip\ImagineBundle\LiipImagineBundle; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -110,7 +111,7 @@ public function testAddWebPathResolverFactoryOnBuild() $extensionMock ->expects($this->at(0)) ->method('addResolverFactory') - ->with($this->isInstanceOf(WebPathResolverFactory::class)); + ->with($this->isInstanceOf(WebPathResolverFactoryFactory::class)); $containerMock = $this->createContainerBuilderMock(); $containerMock @@ -160,6 +161,25 @@ public function testAddFlysystemResolverFactoryOnBuild() $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } + + public function testAddRelativeWebPAthResolverFactoryOnBuild() + { + $extensionMock = $this->createLiipImagineExtensionMock(); + $extensionMock + ->expects($this->at(3)) + ->method('addResolverFactory') + ->with($this->isInstanceOf(RelativeWebPathResolverFactoryFactory::class)); + + $containerMock = $this->createContainerBuilderMock(); + $containerMock + ->expects($this->atLeastOnce()) + ->method('getExtension') + ->with('liip_imagine') + ->will($this->returnValue($extensionMock)); + + $bundle = new LiipImagineBundle(); + $bundle->build($containerMock); + } public function testAddStreamLoaderFactoryOnBuild() { From 08f36cd6438b87e76adcb18076c26ed1ef5ce888 Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Wed, 21 Feb 2018 18:44:55 +0200 Subject: [PATCH 09/13] Docs added --- .../doc/cache-resolver/relative_web_path.rst | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Resources/doc/cache-resolver/relative_web_path.rst diff --git a/Resources/doc/cache-resolver/relative_web_path.rst b/Resources/doc/cache-resolver/relative_web_path.rst new file mode 100644 index 000000000..e4d2b6d15 --- /dev/null +++ b/Resources/doc/cache-resolver/relative_web_path.rst @@ -0,0 +1,70 @@ + +.. _cache-resolver-relative-web-path: + +Relative Web Path Resolver +================= + +``RelativeWebPathResolver`` is similar with :ref:`web path cache resolver ` + + +This cache resolver (``RelativeWebPathResolver``), as well as +:ref:`web path cache resolver `, enables cache resolution for +local, web-path-based setups. This means images will be cached on your +local filesystem, within the web path of your Symfony application. +The only difference between them is that first one generates relative url paths + +Configuration +------------- +.. code-block:: yaml + + liip_imagine: + resolvers: + profile_photos: + relative_web_path: + # use %kernel.project_dir%/web for Symfony prior to 4.0.0 + web_root: "%kernel.project_dir%/public" + cache_prefix: "media/cache" + +There are several configuration options available: + +* ``web_root`` - must be the absolute path to you application's web root. This + is used to determine where to put generated image files, so that apache + will pick them up before handing the request to Symfony next time they + are requested. The default value ends with ``web`` for Symfony prior to + version ``4.0.0``. + Default value: ``%kernel.project_dir%/(public|web)`` +* ``cache_prefix`` - this is also used in the path for image generation, so + as to not clutter your web root with cached images. For example by default, + the images would be written to the ``web/media/cache/`` directory. + Default value: ``/media/cache`` + +Usage +----- +After configuring ``RelativeWebPathResolver``, you can set it as the default cache resolver +for ``LiipImagineBundle`` using the following configuration. + +.. code-block:: yaml + + # app/config/config.yml + + liip_imagine: + cache: profile_photos + + +Usage on a Specific Filter +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Alternatively, you can set ``RelativeWebPathResolver`` as the cache resolver for a specific +filter set using the following configuration. + +.. code-block:: yaml + + # app/config/config.yml + + liip_imagine: + filter_sets: + cache: ~ + my_thumb: + cache: profile_photos + filters: + # the filter list From 7b395588c41e7189b9b8322ea2a9f1ca18145eda Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Wed, 21 Feb 2018 18:45:27 +0200 Subject: [PATCH 10/13] Doc fixed --- Resources/doc/cache-resolver/relative_web_path.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/Resources/doc/cache-resolver/relative_web_path.rst b/Resources/doc/cache-resolver/relative_web_path.rst index e4d2b6d15..9b9708d3f 100644 --- a/Resources/doc/cache-resolver/relative_web_path.rst +++ b/Resources/doc/cache-resolver/relative_web_path.rst @@ -4,9 +4,6 @@ Relative Web Path Resolver ================= -``RelativeWebPathResolver`` is similar with :ref:`web path cache resolver ` - - This cache resolver (``RelativeWebPathResolver``), as well as :ref:`web path cache resolver `, enables cache resolution for local, web-path-based setups. This means images will be cached on your From f8417c051d006b58f0e832803b422db38c86d1aa Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Tue, 1 May 2018 12:46:55 +0300 Subject: [PATCH 11/13] Class wording refactored --- ...toryFactory.php => RelativeWebPathResolverFactory.php} | 2 +- ...olverFactoryFactory.php => WebPathResolverFactory.php} | 2 +- LiipImagineBundle.php | 8 ++++---- .../Resolver/RelativeWebPathResolverFactoryTest.php | 6 +++--- Tests/LiipImagineBundleTest.php | 8 ++++---- 5 files changed, 13 insertions(+), 13 deletions(-) rename DependencyInjection/Factory/Resolver/{RelativeWebPathResolverFactoryFactory.php => RelativeWebPathResolverFactory.php} (84%) rename DependencyInjection/Factory/Resolver/{WebPathResolverFactoryFactory.php => WebPathResolverFactory.php} (85%) diff --git a/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryFactory.php b/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php similarity index 84% rename from DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryFactory.php rename to DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php index d48342de7..7c24cd416 100644 --- a/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryFactory.php +++ b/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactory.php @@ -11,7 +11,7 @@ namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver; -class RelativeWebPathResolverFactoryFactory extends AbstractWebPathResolverFactory +class RelativeWebPathResolverFactory extends AbstractWebPathResolverFactory { /** * {@inheritdoc} diff --git a/DependencyInjection/Factory/Resolver/WebPathResolverFactoryFactory.php b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php similarity index 85% rename from DependencyInjection/Factory/Resolver/WebPathResolverFactoryFactory.php rename to DependencyInjection/Factory/Resolver/WebPathResolverFactory.php index 723d4caf6..528f93f8c 100644 --- a/DependencyInjection/Factory/Resolver/WebPathResolverFactoryFactory.php +++ b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php @@ -11,7 +11,7 @@ namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver; -class WebPathResolverFactoryFactory extends AbstractWebPathResolverFactory +class WebPathResolverFactory extends AbstractWebPathResolverFactory { /** * {@inheritdoc} diff --git a/LiipImagineBundle.php b/LiipImagineBundle.php index 7cda622d0..a3ee34603 100644 --- a/LiipImagineBundle.php +++ b/LiipImagineBundle.php @@ -23,8 +23,8 @@ use Liip\ImagineBundle\DependencyInjection\Factory\Loader\StreamLoaderFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\AwsS3ResolverFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FlysystemResolverFactory; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactoryFactory; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactoryFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -53,10 +53,10 @@ public function build(ContainerBuilder $container) /** @var $extension LiipImagineExtension */ $extension = $container->getExtension('liip_imagine'); - $extension->addResolverFactory(new WebPathResolverFactoryFactory()); + $extension->addResolverFactory(new WebPathResolverFactory()); $extension->addResolverFactory(new AwsS3ResolverFactory()); $extension->addResolverFactory(new FlysystemResolverFactory()); - $extension->addResolverFactory(new RelativeWebPathResolverFactoryFactory()); + $extension->addResolverFactory(new RelativeWebPathResolverFactory()); $extension->addLoaderFactory(new StreamLoaderFactory()); $extension->addLoaderFactory(new FileSystemLoaderFactory()); diff --git a/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php index c04843530..69d7852f0 100644 --- a/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php @@ -2,14 +2,14 @@ namespace Liip\ImagineBundle\Tests\DependencyInjection\Factory\Resolver; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactoryFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface; class RelativeWebPathResolverFactoryTest extends AbstractWebPathResolverTest { public function testReturnExpectedName() { - $resolver = new RelativeWebPathResolverFactoryFactory(); + $resolver = new RelativeWebPathResolverFactory(); $this->assertEquals('relative_web_path', $resolver->getName()); } @@ -19,6 +19,6 @@ public function testReturnExpectedName() */ protected function getClassName() { - return RelativeWebPathResolverFactoryFactory::class; + return RelativeWebPathResolverFactory::class; } } diff --git a/Tests/LiipImagineBundleTest.php b/Tests/LiipImagineBundleTest.php index 0763a92d7..c3aea186c 100644 --- a/Tests/LiipImagineBundleTest.php +++ b/Tests/LiipImagineBundleTest.php @@ -20,8 +20,8 @@ use Liip\ImagineBundle\DependencyInjection\Factory\Loader\StreamLoaderFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\AwsS3ResolverFactory; use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FlysystemResolverFactory; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactoryFactory; -use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactoryFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\RelativeWebPathResolverFactory; +use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension; use Liip\ImagineBundle\LiipImagineBundle; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -111,7 +111,7 @@ public function testAddWebPathResolverFactoryOnBuild() $extensionMock ->expects($this->at(0)) ->method('addResolverFactory') - ->with($this->isInstanceOf(WebPathResolverFactoryFactory::class)); + ->with($this->isInstanceOf(WebPathResolverFactory::class)); $containerMock = $this->createContainerBuilderMock(); $containerMock @@ -168,7 +168,7 @@ public function testAddRelativeWebPAthResolverFactoryOnBuild() $extensionMock ->expects($this->at(3)) ->method('addResolverFactory') - ->with($this->isInstanceOf(RelativeWebPathResolverFactoryFactory::class)); + ->with($this->isInstanceOf(RelativeWebPathResolverFactory::class)); $containerMock = $this->createContainerBuilderMock(); $containerMock From 12cc072bd488bc775e05731b2bd089c7ebd29d6b Mon Sep 17 00:00:00 2001 From: mmuntianov Date: Wed, 16 May 2018 11:07:54 +0300 Subject: [PATCH 12/13] Code style fixed --- .../AbstractWebPathResolverFactory.php | 41 ++- .../Resolver/AbstractWebPathResolver.php | 13 +- .../Resolver/RelativeWebPathResolver.php | 9 + Imagine/Cache/Resolver/WebPathResolver.php | 16 +- .../Resolver/AbstractWebPathResolverTest.php | 49 ++-- .../RelativeWebPathResolverFactoryTest.php | 14 +- .../Resolver/WebPathResolverFactoryTest.php | 2 +- .../Cache/Resolver/WebPathResolverTest.php | 256 ++++++++++-------- Tests/LiipImagineBundleTest.php | 162 +++++++---- Tests/Utility/Path/PathResolverTest.php | 9 + Utility/Path/PathResolver.php | 17 +- Utility/Path/PathResolverInterface.php | 9 + 12 files changed, 385 insertions(+), 212 deletions(-) diff --git a/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php index aefd220e0..af829640f 100644 --- a/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php @@ -1,5 +1,14 @@ replaceArgument(1, new Reference($pathResolverServiceId)); - $resolverDefinition->addTag('liip_imagine.cache.resolver', [ - 'resolver' => $resolverName, - ]); + $resolverDefinition->addTag( + 'liip_imagine.cache.resolver', + [ + 'resolver' => $resolverName, + ] + ); $resolverId = 'liip_imagine.cache.resolver.'; - $container->setDefinition($resolverId.$resolverName, $resolverDefinition); + $container->setDefinition($resolverId . $resolverName, $resolverDefinition); return $resolverId; } @@ -40,16 +52,17 @@ public function create(ContainerBuilder $container, $resolverName, array $config */ public function addConfiguration(ArrayNodeDefinition $builder) { - $builder - ->children() - ->scalarNode('web_root') - ->defaultValue(SymfonyFramework::getContainerResolvableRootWebPath()) - ->cannotBeEmpty() - ->end() - ->scalarNode('cache_prefix') - ->defaultValue('media/cache') - ->cannotBeEmpty() - ->end() + $builder->children() + ->scalarNode('web_root') + ->defaultValue( + SymfonyFramework::getContainerResolvableRootWebPath() + ) + ->cannotBeEmpty() + ->end() + ->scalarNode('cache_prefix') + ->defaultValue('media/cache') + ->cannotBeEmpty() + ->end() ->end(); } } diff --git a/Imagine/Cache/Resolver/AbstractWebPathResolver.php b/Imagine/Cache/Resolver/AbstractWebPathResolver.php index 36615a805..8b641d4de 100644 --- a/Imagine/Cache/Resolver/AbstractWebPathResolver.php +++ b/Imagine/Cache/Resolver/AbstractWebPathResolver.php @@ -1,5 +1,14 @@ pathResolver->getCacheRoot().'/'.$filter; + $filtersCacheDir[] = $this->pathResolver->getCacheRoot() . '/' . $filter; } $this->filesystem->remove($filtersCacheDir); diff --git a/Imagine/Cache/Resolver/RelativeWebPathResolver.php b/Imagine/Cache/Resolver/RelativeWebPathResolver.php index dfd93e328..7b61bcf85 100644 --- a/Imagine/Cache/Resolver/RelativeWebPathResolver.php +++ b/Imagine/Cache/Resolver/RelativeWebPathResolver.php @@ -1,5 +1,14 @@ getBaseUrl(), $this->getPathResolver()->getFileUrl($path, $filter) ); @@ -53,18 +54,19 @@ protected function getBaseUrl() if ('https' === $this->requestContext->getScheme() && 443 !== $this->requestContext->getHttpsPort()) { $port = ":{$this->requestContext->getHttpsPort()}"; } - + if ('http' === $this->requestContext->getScheme() && 80 !== $this->requestContext->getHttpPort()) { $port = ":{$this->requestContext->getHttpPort()}"; } - + $baseUrl = $this->requestContext->getBaseUrl(); if ('.php' === mb_substr($this->requestContext->getBaseUrl(), -4)) { $baseUrl = pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME); } $baseUrl = rtrim($baseUrl, '/\\'); - - return sprintf('%s://%s%s%s', + + return sprintf( + '%s://%s%s%s', $this->requestContext->getScheme(), $this->requestContext->getHost(), $port, diff --git a/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php b/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php index 999befe44..d5cd6b7cd 100644 --- a/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php @@ -1,5 +1,14 @@ create( $container, 'the_resolver_name', - array( - 'web_root' => 'theWebRoot', + [ + 'web_root' => 'theWebRoot', 'cache_prefix' => 'theCachePrefix', - ) + ] ); $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); @@ -61,13 +70,13 @@ public function testCreateResolverDefinitionOnCreate() sprintf('liip_imagine.cache.resolver.prototype.%s', $resolver->getName()), $resolverDefinition->getParent() ); - + /** * @var Reference $utilPathResolverReference */ $utilPathResolverReference = $resolverDefinition->getArgument(1); $this->assertInstanceOf(Reference::class, $utilPathResolverReference); - + $utilPathResolverServiceId = $utilPathResolverReference->__toString(); $this->assertEquals('liip_imagine.util.resolver.path', $utilPathResolverServiceId); $this->assertTrue($container->hasDefinition($utilPathResolverServiceId)); @@ -77,7 +86,7 @@ public function testCreateResolverDefinitionOnCreate() $utilPathResolverDefinition = $container->getDefinition($utilPathResolverServiceId); $this->assertInstanceOf(ChildDefinition::class, $utilPathResolverDefinition); $this->assertEquals('liip_imagine.util.resolver.prototype.path', $utilPathResolverDefinition->getParent()); - + $this->assertEquals('theWebRoot', $utilPathResolverDefinition->getArgument(0)); $this->assertEquals('theCachePrefix', $utilPathResolverDefinition->getArgument(1)); } @@ -93,12 +102,15 @@ public function testProcessCorrectlyOptionsOnAddConfiguration() $resolver = $this->createResolver(); $resolver->addConfiguration($rootNode); - $config = $this->processConfigTree($treeBuilder, array( - $resolver->getName() => array( - 'web_root' => $expectedWebPath, - 'cache_prefix' => $expectedCachePrefix, - ), - )); + $config = $this->processConfigTree( + $treeBuilder, + [ + $resolver->getName() => [ + 'web_root' => $expectedWebPath, + 'cache_prefix' => $expectedCachePrefix, + ], + ] + ); $this->assertArrayHasKey('web_root', $config); $this->assertEquals($expectedWebPath, $config['web_root']); @@ -111,13 +123,16 @@ public function testAddDefaultOptionsIfNotSetOnAddConfiguration() { $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('test_resolver_name', 'array'); - + $resolver = $this->createResolver(); $resolver->addConfiguration($rootNode); - $config = $this->processConfigTree($treeBuilder, array( - $resolver->getName() => array(), - )); + $config = $this->processConfigTree( + $treeBuilder, + [ + $resolver->getName() => [], + ] + ); $this->assertArrayHasKey('web_root', $config); $this->assertEquals(SymfonyFramework::getContainerResolvableRootWebPath(), $config['web_root']); @@ -134,7 +149,7 @@ public function testAddDefaultOptionsIfNotSetOnAddConfiguration() */ protected function processConfigTree(TreeBuilder $treeBuilder, array $configs) { - $processor = new Processor(); + $processor = new Processor; return $processor->process($treeBuilder->buildTree(), $configs); } diff --git a/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php index 69d7852f0..feb220474 100644 --- a/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php @@ -1,15 +1,27 @@ assertEquals('relative_web_path', $resolver->getName()); } diff --git a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php index 68ce70242..cc1179ad2 100644 --- a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php @@ -22,7 +22,7 @@ class WebPathResolverFactoryTest extends AbstractWebPathResolverTest public function testReturnExpectedName() { $resolver = new WebPathResolverFactory(); - + $this->assertEquals('web_path', $resolver->getName()); } diff --git a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php index fd7d90655..059fc5f7c 100644 --- a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php @@ -29,38 +29,38 @@ class WebPathResolverTest extends TestCase * @var Filesystem */ private $filesystem; - + /** * @var string */ private $basePath; - + /** * @var string */ private $existingFile; - + public function setUp() { $this->filesystem = new Filesystem(); - $this->basePath = sys_get_temp_dir().'/aWebRoot'; - $this->existingFile = $this->basePath.'/aCachePrefix/aFilter/existingPath'; + $this->basePath = sys_get_temp_dir() . '/aWebRoot'; + $this->existingFile = $this->basePath . '/aCachePrefix/aFilter/existingPath'; $this->filesystem->mkdir(dirname($this->existingFile)); $this->filesystem->touch($this->existingFile); } - + public function tearDown() { $this->filesystem->remove($this->basePath); } - + public function testImplementsResolverInterface() { $rc = new \ReflectionClass(WebPathResolver::class); - + $this->assertTrue($rc->implementsInterface(ResolverInterface::class)); } - + public function testCouldBeConstructedWithRequiredArguments() { $filesystemMock = $this->createFilesystemMock(); @@ -68,48 +68,48 @@ public function testCouldBeConstructedWithRequiredArguments() $requestContext = new RequestContext(); $resolver = new WebPathResolver($filesystemMock, $pathResolver, $requestContext); - + $this->assertAttributeSame($filesystemMock, 'filesystem', $resolver); $this->assertAttributeSame($pathResolver, 'pathResolver', $resolver); $this->assertAttributeSame($requestContext, 'requestContext', $resolver); } - + public function testReturnTrueIfFileExistsOnIsStored() { - $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); + $pathResolver = new PathResolver($this->basePath, 'aCachePrefix'); $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, new RequestContext() ); - + $this->assertTrue($resolver->isStored('existingPath', 'aFilter')); } - + public function testReturnFalseIfFileNotExistsOnIsStored() { - $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); + $pathResolver = new PathResolver($this->basePath, 'aCachePrefix'); $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, new RequestContext() ); - + $this->assertFalse($resolver->isStored('nonExistingPath', 'aFilter')); } - + public function testReturnFalseIfIsNotFile() { - $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); + $pathResolver = new PathResolver($this->basePath, 'aCachePrefix'); $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, new RequestContext() ); - + $this->assertFalse($resolver->isStored('', 'aFilter')); } - + public function testComposeSchemaHostAndFileUrlOnResolve() { $path = 'aPath'; @@ -118,24 +118,28 @@ public function testComposeSchemaHostAndFileUrlOnResolve() $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); - + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFileUrl") - ->with($this->equalTo($path), $this->equalTo($filter)) - ->willReturn("aCachePrefix/aFilter/aPath"); + $pathResolver + ->method("getFileUrl") + ->with( + $this->equalTo($path), + $this->equalTo($filter) + ) + ->willReturn( + "aCachePrefix/aFilter/aPath" + ); $resolver = new WebPathResolver( - $this->createFilesystemMock(), - $pathResolver, - $requestContext + $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( 'theschema://thehost/aCachePrefix/aFilter/aPath', $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHostAndBasePathWithPhpFileAndFileUrlOnResolve() { $path = 'aPath'; @@ -145,24 +149,27 @@ public function testComposeSchemaHostAndBasePathWithPhpFileAndFileUrlOnResolve() $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('/theBasePath/app.php'); - + $pathResolver = $this->createPathResolverMock(); $pathResolver->method("getFileUrl") - ->with($this->equalTo($path), $this->equalTo($filter)) - ->willReturn("aCachePrefix/aFilter/aPath"); + ->with( + $this->equalTo($path), + $this->equalTo($filter) + ) + ->willReturn( + "aCachePrefix/aFilter/aPath" + ); $resolver = new WebPathResolver( - $this->createFilesystemMock(), - $pathResolver, - $requestContext + $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( 'theschema://thehost/theBasePath/aCachePrefix/aFilter/aPath', $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHostAndBasePathWithDirsOnlyAndFileUrlOnResolve() { $path = 'aPath'; @@ -172,24 +179,27 @@ public function testComposeSchemaHostAndBasePathWithDirsOnlyAndFileUrlOnResolve( $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('/theBasePath/theSubBasePath'); - + $pathResolver = $this->createPathResolverMock(); $pathResolver->method("getFileUrl") - ->with($this->equalTo($path), $this->equalTo($filter)) - ->willReturn("aCachePrefix/aFilter/aPath"); + ->with( + $this->equalTo($path), + $this->equalTo($filter) + ) + ->willReturn( + "aCachePrefix/aFilter/aPath" + ); $resolver = new WebPathResolver( - $this->createFilesystemMock(), - $pathResolver, - $requestContext + $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( 'theschema://thehost/theBasePath/theSubBasePath/aCachePrefix/aFilter/aPath', $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHostAndBasePathWithBackSplashOnResolve() { $path = 'aPath'; @@ -200,10 +210,13 @@ public function testComposeSchemaHostAndBasePathWithBackSplashOnResolve() $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('\\'); - + $pathResolver = $this->createPathResolverMock(); $pathResolver->method("getFileUrl") - ->with($this->equalTo($path), $this->equalTo($filter)) + ->with( + $this->equalTo($path), + $this->equalTo($filter) + ) ->willReturn($fileUrl); $resolver = new WebPathResolver( @@ -211,13 +224,13 @@ public function testComposeSchemaHostAndBasePathWithBackSplashOnResolve() $pathResolver, $requestContext ); - + $this->assertSame( sprintf('theschema://thehost/%s', $fileUrl), $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHttpAndCustomPortAndFileUrlOnResolve() { $path = 'aPath'; @@ -228,24 +241,27 @@ public function testComposeSchemaHttpAndCustomPortAndFileUrlOnResolve() $requestContext->setScheme('http'); $requestContext->setHost('thehost'); $requestContext->setHttpPort(88); - + $pathResolver = $this->createPathResolverMock(); $pathResolver->method("getFileUrl") - ->with($this->equalTo($path), $this->equalTo($filter)) + ->with( + $this->equalTo($path), + $this->equalTo($filter) + ) ->willReturn($fileUrl); - + $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( sprintf('http://thehost:88/%s', $fileUrl), $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHttpsAndCustomPortAndFileUrlOnResolve() { $path = 'aPath'; @@ -256,24 +272,27 @@ public function testComposeSchemaHttpsAndCustomPortAndFileUrlOnResolve() $requestContext->setScheme('https'); $requestContext->setHost('thehost'); $requestContext->setHttpsPort(444); - + $pathResolver = $this->createPathResolverMock(); $pathResolver->method("getFileUrl") - ->with($this->equalTo($path), $this->equalTo($filter)) + ->with( + $this->equalTo($path), + $this->equalTo($filter) + ) ->willReturn($fileUrl); - + $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( sprintf('https://thehost:444/%s', $fileUrl), $resolver->resolve($path, $filter) ); } - + public function testDumpBinaryContentOnStore() { $path = 'aPath'; @@ -282,65 +301,67 @@ public function testDumpBinaryContentOnStore() $fileContent = 'theContent'; $binary = new Binary($fileContent, 'aMimeType', 'aFormat'); - + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->once()) ->method('dumpFile') - ->with($this->equalTo($fileUrl), $this->equalTo($fileContent)); + ->with( + $this->equalTo($fileUrl), + $this->equalTo($fileContent) + ); $pathResolver = $this->createPathResolverMock(); $pathResolver->method("getFilePath") - ->with($this->equalTo($path), $this->equalTo($filter)) - ->willReturn($fileUrl); + ->with( + $this->equalTo($path), + $this->equalTo($filter) + ) + ->willReturn( + $fileUrl + ); $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $this->assertNull($resolver->store($binary, $path, $filter)); } - + public function testDoNothingIfFiltersAndPathsEmptyOnRemove() { $filesystemMock = $this->createFilesystemMock(); - $filesystemMock - ->expects($this->never()) - ->method('remove'); - + $filesystemMock->expects($this->never())->method('remove'); + $resolver = new WebPathResolver( $filesystemMock, $this->createPathResolverMock(), new RequestContext() ); - + $resolver->remove([], []); } - + public function testRemoveCacheForPathAndFilterOnRemove() { $filePath = '/aWebRoot/aCachePrefix/aFilter/aPath'; $filesystemMock = $this->createFilesystemMock(); - $filesystemMock - ->expects($this->once()) - ->method('remove') - ->with($filePath); + $filesystemMock->expects($this->once())->method('remove')->with($filePath); $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFilePath") - ->willReturn($filePath); - + $pathResolver->method("getFilePath")->willReturn($filePath); + $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $resolver->remove(['aPath'], ['aFilter']); } - + public function testRemoveCacheForSomePathsAndFilterOnRemove() { $cacheRoot = '/aWebRoot/aCachePrefix'; @@ -352,12 +373,16 @@ public function testRemoveCacheForSomePathsAndFilterOnRemove() $filesystemMock ->expects($this->at(0)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filter, $pathOne)); + ->with( + sprintf("%s/%s/%s", $cacheRoot, $filter, $pathOne) + ); $filesystemMock ->expects($this->at(1)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filter, $pathTwo)); - + ->with( + sprintf("%s/%s/%s", $cacheRoot, $filter, $pathTwo) + ); + $pathResolver = $this->createPathResolverMock(); $pathResolver->method("getFilePath") ->willReturnMap( @@ -366,16 +391,16 @@ public function testRemoveCacheForSomePathsAndFilterOnRemove() [$pathTwo, $filter, sprintf("%s/%s/%s", $cacheRoot, $filter, $pathTwo)], ] ); - + $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $resolver->remove([$pathOne, $pathTwo], [$filter]); } - + public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() { $cacheRoot = '/aWebRoot/aCachePrefix'; @@ -388,22 +413,31 @@ public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() $filesystemMock ->expects($this->at(0)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne)); + ->with( + sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne) + ); $filesystemMock ->expects($this->at(1)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne)); + ->with( + sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne) + ); $filesystemMock ->expects($this->at(2)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo)); + ->with( + sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo) + ); $filesystemMock ->expects($this->at(3)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo)); - + ->with( + sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo) + ); + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFilePath") + $pathResolver + ->method("getFilePath") ->willReturnMap( [ [$pathOne, $filterOne, sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne)], @@ -418,13 +452,13 @@ public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() $pathResolver, new RequestContext() ); - + $resolver->remove( [$pathOne, $pathTwo], [$filterOne, $filterTwo] ); } - + public function testRemoveCacheForFilterOnRemove() { $cacheRoot = '/aWebRoot/aCachePrefix'; @@ -433,23 +467,26 @@ public function testRemoveCacheForFilterOnRemove() $filesystemMock ->expects($this->once()) ->method('remove') - ->with([ - sprintf("%s/aFilter", $cacheRoot), - ]); + ->with( + [ + sprintf("%s/aFilter", $cacheRoot), + ] + ); $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getCacheRoot") + $pathResolver + ->method("getCacheRoot") ->willReturn($cacheRoot); - + $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $resolver->remove([], ['aFilter']); } - + public function testRemoveCacheForSomeFiltersOnRemove() { $cacheRoot = '/aWebRoot/aCachePrefix'; @@ -458,13 +495,16 @@ public function testRemoveCacheForSomeFiltersOnRemove() $filesystemMock ->expects($this->once()) ->method('remove') - ->with([ - sprintf("%s/aFilterOne", $cacheRoot), - sprintf("%s/aFilterTwo", $cacheRoot), - ]); + ->with( + [ + sprintf("%s/aFilterOne", $cacheRoot), + sprintf("%s/aFilterTwo", $cacheRoot), + ] + ); $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getCacheRoot") + $pathResolver + ->method("getCacheRoot") ->willReturn($cacheRoot); $resolver = new WebPathResolver( @@ -472,10 +512,10 @@ public function testRemoveCacheForSomeFiltersOnRemove() $pathResolver, new RequestContext() ); - + $resolver->remove([], ['aFilterOne', 'aFilterTwo']); } - + /** * @return \PHPUnit_Framework_MockObject_MockObject|Filesystem */ diff --git a/Tests/LiipImagineBundleTest.php b/Tests/LiipImagineBundleTest.php index c3aea186c..61fcac672 100644 --- a/Tests/LiipImagineBundleTest.php +++ b/Tests/LiipImagineBundleTest.php @@ -36,7 +36,7 @@ public function testSubClassOfBundle() { $this->assertInstanceOf(Bundle::class, new LiipImagineBundle()); } - + public function testAddLoadersCompilerPassOnBuild() { $containerMock = $this->createContainerBuilderMock(); @@ -44,16 +44,20 @@ public function testAddLoadersCompilerPassOnBuild() ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($this->createLiipImagineExtensionMock())); + ->will( + $this->returnValue($this->createLiipImagineExtensionMock()) + ); $containerMock ->expects($this->at(0)) ->method('addCompilerPass') - ->with($this->isInstanceOf(LoadersCompilerPass::class)); - + ->with( + $this->isInstanceOf(LoadersCompilerPass::class) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddFiltersCompilerPassOnBuild() { $containerMock = $this->createContainerBuilderMock(); @@ -61,33 +65,40 @@ public function testAddFiltersCompilerPassOnBuild() ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($this->createLiipImagineExtensionMock())); + ->will( + $this->returnValue($this->createLiipImagineExtensionMock()) + ); $containerMock ->expects($this->at(1)) ->method('addCompilerPass') - ->with($this->isInstanceOf(FiltersCompilerPass::class)); - + ->with( + $this->isInstanceOf(FiltersCompilerPass::class) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddPostProcessorsCompilerPassOnBuild() { $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') - ->with('liip_imagine') - ->will($this->returnValue($this->createLiipImagineExtensionMock())); + ->with('liip_imagine')->will( + $this->returnValue($this->createLiipImagineExtensionMock()) + ); $containerMock ->expects($this->at(2)) ->method('addCompilerPass') - ->with($this->isInstanceOf(PostProcessorsCompilerPass::class)); - + ->with( + $this->isInstanceOf(PostProcessorsCompilerPass::class) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddResolversCompilerPassOnBuild() { $containerMock = $this->createContainerBuilderMock(); @@ -95,69 +106,84 @@ public function testAddResolversCompilerPassOnBuild() ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($this->createLiipImagineExtensionMock())); + ->will( + $this->returnValue($this->createLiipImagineExtensionMock()) + ); $containerMock ->expects($this->at(3)) ->method('addCompilerPass') - ->with($this->isInstanceOf(ResolversCompilerPass::class)); - + ->with( + $this->isInstanceOf(ResolversCompilerPass::class) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddWebPathResolverFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); $extensionMock ->expects($this->at(0)) ->method('addResolverFactory') - ->with($this->isInstanceOf(WebPathResolverFactory::class)); - + ->with( + $this->isInstanceOf(WebPathResolverFactory::class) + ); + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($extensionMock)); - + ->will( + $this->returnValue($extensionMock) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddAwsS3ResolverFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); $extensionMock ->expects($this->at(1)) ->method('addResolverFactory') - ->with($this->isInstanceOf(AwsS3ResolverFactory::class)); - + ->with( + $this->isInstanceOf(AwsS3ResolverFactory::class) + ); + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($extensionMock)); - + ->will( + $this->returnValue($extensionMock) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddFlysystemResolverFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); - $extensionMock - ->expects($this->at(2)) + $extensionMock->expects($this->at(2)) ->method('addResolverFactory') - ->with($this->isInstanceOf(FlysystemResolverFactory::class)); - + ->with( + $this->isInstanceOf(FlysystemResolverFactory::class) + ); + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($extensionMock)); - + ->will( + $this->returnValue($extensionMock) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } @@ -168,76 +194,92 @@ public function testAddRelativeWebPAthResolverFactoryOnBuild() $extensionMock ->expects($this->at(3)) ->method('addResolverFactory') - ->with($this->isInstanceOf(RelativeWebPathResolverFactory::class)); + ->with( + $this->isInstanceOf(RelativeWebPathResolverFactory::class) + ); $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($extensionMock)); + ->will( + $this->returnValue($extensionMock) + ); $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddStreamLoaderFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); $extensionMock ->expects($this->at(4)) ->method('addLoaderFactory') - ->with($this->isInstanceOf(StreamLoaderFactory::class)); - + ->with( + $this->isInstanceOf(StreamLoaderFactory::class) + ); + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($extensionMock)); - + ->will( + $this->returnValue($extensionMock) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddFilesystemLoaderFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); $extensionMock ->expects($this->at(5)) ->method('addLoaderFactory') - ->with($this->isInstanceOf(FileSystemLoaderFactory::class)); - + ->with( + $this->isInstanceOf(FileSystemLoaderFactory::class) + ); + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($extensionMock)); - + ->will( + $this->returnValue($extensionMock) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddFlysystemLoaderFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); $extensionMock ->expects($this->at(6)) ->method('addLoaderFactory') - ->with($this->isInstanceOf(FlysystemLoaderFactory::class)); - + ->with( + $this->isInstanceOf(FlysystemLoaderFactory::class) + ); + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) ->method('getExtension') ->with('liip_imagine') - ->will($this->returnValue($extensionMock)); - + ->will( + $this->returnValue($extensionMock) + ); + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + /** * @return \PHPUnit_Framework_MockObject_MockObject|ContainerBuilder */ @@ -245,16 +287,20 @@ protected function createContainerBuilderMock() { return $this->createObjectMock(ContainerBuilder::class, [], false); } - + /** * @return \PHPUnit_Framework_MockObject_MockObject|LiipImagineExtension */ protected function createLiipImagineExtensionMock() { - return $this->createObjectMock(LiipImagineExtension::class, [ - 'getNamespace', - 'addResolverFactory', - 'addLoaderFactory', - ], false); + return $this->createObjectMock( + LiipImagineExtension::class, + [ + 'getNamespace', + 'addResolverFactory', + 'addLoaderFactory', + ], + false + ); } } diff --git a/Tests/Utility/Path/PathResolverTest.php b/Tests/Utility/Path/PathResolverTest.php index ee55c4404..6efd68374 100644 --- a/Tests/Utility/Path/PathResolverTest.php +++ b/Tests/Utility/Path/PathResolverTest.php @@ -1,5 +1,14 @@ webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); - $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; + $this->cacheRoot = $this->webRoot . '/' . $this->cachePrefix; } /** @@ -33,7 +42,7 @@ public function __construct( */ public function getFilePath($path, $filter) { - return $this->webRoot.'/'.$this->getFileUrl($path, $filter); + return $this->webRoot . '/' . $this->getFileUrl($path, $filter); } /** @@ -43,8 +52,8 @@ public function getFileUrl($path, $filter) { // crude way of sanitizing URL scheme ("protocol") part $path = str_replace('://', '---', $path); - - return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); + + return $this->cachePrefix . '/' . $filter . '/' . ltrim($path, '/'); } /** diff --git a/Utility/Path/PathResolverInterface.php b/Utility/Path/PathResolverInterface.php index 93a6353af..fd35bbd92 100644 --- a/Utility/Path/PathResolverInterface.php +++ b/Utility/Path/PathResolverInterface.php @@ -1,5 +1,14 @@ Date: Wed, 16 May 2018 11:40:22 +0300 Subject: [PATCH 13/13] Code style fixed --- .../AbstractWebPathResolverFactory.php | 14 +- .../Resolver/AbstractWebPathResolver.php | 22 +- Imagine/Cache/Resolver/WebPathResolver.php | 12 +- .../Resolver/AbstractWebPathResolverTest.php | 92 +++---- .../RelativeWebPathResolverFactoryTest.php | 8 +- .../Resolver/WebPathResolverFactoryTest.php | 6 +- .../Resolver/RelativeWebPathResolverTest.php | 114 ++++---- .../Cache/Resolver/WebPathResolverTest.php | 244 +++++++++--------- Tests/LiipImagineBundleTest.php | 62 ++--- Tests/Utility/Path/PathResolverTest.php | 86 +++--- Utility/Path/PathResolver.php | 20 +- Utility/Path/PathResolverInterface.php | 4 +- 12 files changed, 342 insertions(+), 342 deletions(-) diff --git a/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php index af829640f..c88c5842e 100644 --- a/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/AbstractWebPathResolverFactory.php @@ -28,25 +28,25 @@ public function create(ContainerBuilder $container, $resolverName, array $config $pathResolverDefinition = new ChildDefinition('liip_imagine.util.resolver.prototype.path'); $pathResolverDefinition->replaceArgument(0, $config['web_root']); $pathResolverDefinition->replaceArgument(1, $config['cache_prefix']); - + $pathResolverServiceId = 'liip_imagine.util.resolver.path'; $container->setDefinition($pathResolverServiceId, $pathResolverDefinition); - + $resolverDefinition->replaceArgument(1, new Reference($pathResolverServiceId)); - + $resolverDefinition->addTag( 'liip_imagine.cache.resolver', [ 'resolver' => $resolverName, ] ); - + $resolverId = 'liip_imagine.cache.resolver.'; - $container->setDefinition($resolverId . $resolverName, $resolverDefinition); - + $container->setDefinition($resolverId.$resolverName, $resolverDefinition); + return $resolverId; } - + /** * {@inheritdoc} */ diff --git a/Imagine/Cache/Resolver/AbstractWebPathResolver.php b/Imagine/Cache/Resolver/AbstractWebPathResolver.php index 8b641d4de..8da38efb7 100644 --- a/Imagine/Cache/Resolver/AbstractWebPathResolver.php +++ b/Imagine/Cache/Resolver/AbstractWebPathResolver.php @@ -21,12 +21,12 @@ abstract class AbstractWebPathResolver implements ResolverInterface * @var Filesystem */ protected $filesystem; - + /** * @var PathResolverInterface */ protected $pathResolver; - + /** * @param Filesystem $filesystem * @param PathResolverInterface $pathResolver @@ -38,7 +38,7 @@ public function __construct( $this->filesystem = $filesystem; $this->pathResolver = $pathResolver; } - + /** * Checks whether the given path is stored within this Resolver. * @@ -51,7 +51,7 @@ public function isStored($path, $filter) { return is_file($this->pathResolver->getFilePath($path, $filter)); } - + /** * {@inheritdoc} */ @@ -62,7 +62,7 @@ public function store(BinaryInterface $binary, $path, $filter) $binary->getContent() ); } - + /** * {@inheritdoc} */ @@ -71,25 +71,25 @@ public function remove(array $paths, array $filters) if (empty($paths) && empty($filters)) { return; } - + if (empty($paths)) { $filtersCacheDir = []; foreach ($filters as $filter) { - $filtersCacheDir[] = $this->pathResolver->getCacheRoot() . '/' . $filter; + $filtersCacheDir[] = $this->pathResolver->getCacheRoot().'/'.$filter; } - + $this->filesystem->remove($filtersCacheDir); - + return; } - + foreach ($paths as $path) { foreach ($filters as $filter) { $this->filesystem->remove($this->pathResolver->getFilePath($path, $filter)); } } } - + /** * @return PathResolverInterface */ diff --git a/Imagine/Cache/Resolver/WebPathResolver.php b/Imagine/Cache/Resolver/WebPathResolver.php index 5d165d4ed..4b0b2fa3b 100644 --- a/Imagine/Cache/Resolver/WebPathResolver.php +++ b/Imagine/Cache/Resolver/WebPathResolver.php @@ -18,7 +18,7 @@ class WebPathResolver extends AbstractWebPathResolver { private $requestContext; - + /** * @param Filesystem $filesystem * @param PathResolverInterface $pathResolver @@ -32,7 +32,7 @@ public function __construct( parent::__construct($filesystem, $pathResolver); $this->requestContext = $requestContext; } - + /** * {@inheritdoc} */ @@ -44,7 +44,7 @@ public function resolve($path, $filter) $this->getPathResolver()->getFileUrl($path, $filter) ); } - + /** * @return string */ @@ -54,17 +54,17 @@ protected function getBaseUrl() if ('https' === $this->requestContext->getScheme() && 443 !== $this->requestContext->getHttpsPort()) { $port = ":{$this->requestContext->getHttpsPort()}"; } - + if ('http' === $this->requestContext->getScheme() && 80 !== $this->requestContext->getHttpPort()) { $port = ":{$this->requestContext->getHttpPort()}"; } - + $baseUrl = $this->requestContext->getBaseUrl(); if ('.php' === mb_substr($this->requestContext->getBaseUrl(), -4)) { $baseUrl = pathinfo($this->requestContext->getBaseurl(), PATHINFO_DIRNAME); } $baseUrl = rtrim($baseUrl, '/\\'); - + return sprintf( '%s://%s%s%s', $this->requestContext->getScheme(), diff --git a/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php b/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php index d5cd6b7cd..cdba1b1e7 100644 --- a/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/AbstractWebPathResolverTest.php @@ -17,8 +17,8 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\Processor; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ChildDefinition; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; /** @@ -29,118 +29,110 @@ abstract class AbstractWebPathResolverTest extends TestCase public function testImplementsResolverFactoryInterface() { $rc = new \ReflectionClass($this->getClassName()); - + $this->assertTrue($rc->implementsInterface(ResolverFactoryInterface::class)); } - + public function testCouldBeConstructedWithoutAnyArguments() { $loader = $this->createResolver(); - + $this->assertInstanceOf($this->getClassName(), $loader); } - + public function testAbstractWebPathResolverFactoryImplementation() { $this->assertTrue(is_a($this->getClassName(), AbstractWebPathResolverFactory::class, true)); } - + public function testCreateResolverDefinitionOnCreate() { $container = new ContainerBuilder(); - + $resolver = $this->createResolver(); $resolver->create( $container, 'the_resolver_name', [ - 'web_root' => 'theWebRoot', + 'web_root' => 'theWebRoot', 'cache_prefix' => 'theCachePrefix', ] ); - + $this->assertTrue($container->hasDefinition('liip_imagine.cache.resolver.the_resolver_name')); - - /** - * @var ChildDefinition $resolverDefinition - */ + $resolverDefinition = $container->getDefinition('liip_imagine.cache.resolver.the_resolver_name'); $this->assertInstanceOf(ChildDefinition::class, $resolverDefinition); - $this->assertEquals( + $this->assertSame( sprintf('liip_imagine.cache.resolver.prototype.%s', $resolver->getName()), $resolverDefinition->getParent() ); - - /** - * @var Reference $utilPathResolverReference - */ + $utilPathResolverReference = $resolverDefinition->getArgument(1); $this->assertInstanceOf(Reference::class, $utilPathResolverReference); - + $utilPathResolverServiceId = $utilPathResolverReference->__toString(); - $this->assertEquals('liip_imagine.util.resolver.path', $utilPathResolverServiceId); + $this->assertSame('liip_imagine.util.resolver.path', $utilPathResolverServiceId); $this->assertTrue($container->hasDefinition($utilPathResolverServiceId)); - /** - * @var ChildDefinition $utilPathResolverDefinition - */ + $utilPathResolverDefinition = $container->getDefinition($utilPathResolverServiceId); $this->assertInstanceOf(ChildDefinition::class, $utilPathResolverDefinition); - $this->assertEquals('liip_imagine.util.resolver.prototype.path', $utilPathResolverDefinition->getParent()); - - $this->assertEquals('theWebRoot', $utilPathResolverDefinition->getArgument(0)); - $this->assertEquals('theCachePrefix', $utilPathResolverDefinition->getArgument(1)); + $this->assertSame('liip_imagine.util.resolver.prototype.path', $utilPathResolverDefinition->getParent()); + + $this->assertSame('theWebRoot', $utilPathResolverDefinition->getArgument(0)); + $this->assertSame('theCachePrefix', $utilPathResolverDefinition->getArgument(1)); } - + public function testProcessCorrectlyOptionsOnAddConfiguration() { $expectedWebPath = 'theWebPath'; $expectedCachePrefix = 'theCachePrefix'; - + $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('test_resolver_name', 'array'); - + $resolver = $this->createResolver(); $resolver->addConfiguration($rootNode); - + $config = $this->processConfigTree( $treeBuilder, [ $resolver->getName() => [ - 'web_root' => $expectedWebPath, + 'web_root' => $expectedWebPath, 'cache_prefix' => $expectedCachePrefix, ], ] ); - + $this->assertArrayHasKey('web_root', $config); - $this->assertEquals($expectedWebPath, $config['web_root']); - + $this->assertSame($expectedWebPath, $config['web_root']); + $this->assertArrayHasKey('cache_prefix', $config); - $this->assertEquals($expectedCachePrefix, $config['cache_prefix']); + $this->assertSame($expectedCachePrefix, $config['cache_prefix']); } - + public function testAddDefaultOptionsIfNotSetOnAddConfiguration() { $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('test_resolver_name', 'array'); - + $resolver = $this->createResolver(); $resolver->addConfiguration($rootNode); - + $config = $this->processConfigTree( $treeBuilder, [ $resolver->getName() => [], ] ); - + $this->assertArrayHasKey('web_root', $config); - $this->assertEquals(SymfonyFramework::getContainerResolvableRootWebPath(), $config['web_root']); - + $this->assertSame(SymfonyFramework::getContainerResolvableRootWebPath(), $config['web_root']); + $this->assertArrayHasKey('cache_prefix', $config); - $this->assertEquals('media/cache', $config['cache_prefix']); + $this->assertSame('media/cache', $config['cache_prefix']); } - + /** * @param TreeBuilder $treeBuilder * @param array $configs @@ -149,20 +141,20 @@ public function testAddDefaultOptionsIfNotSetOnAddConfiguration() */ protected function processConfigTree(TreeBuilder $treeBuilder, array $configs) { - $processor = new Processor; - + $processor = new Processor(); + return $processor->process($treeBuilder->buildTree(), $configs); } - + /** * @return string|ResolverFactoryInterface */ abstract protected function getClassName(); - + private function createResolver() { $className = $this->getClassName(); - - return new $className; + + return new $className(); } } diff --git a/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php index feb220474..5497c96a9 100644 --- a/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/RelativeWebPathResolverFactoryTest.php @@ -21,11 +21,11 @@ class RelativeWebPathResolverFactoryTest extends AbstractWebPathResolverTest { public function testReturnExpectedName() { - $resolver = new RelativeWebPathResolverFactory; - - $this->assertEquals('relative_web_path', $resolver->getName()); + $resolver = new RelativeWebPathResolverFactory(); + + $this->assertSame('relative_web_path', $resolver->getName()); } - + /** * @return string|ResolverFactoryInterface */ diff --git a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php index cc1179ad2..669b52e86 100644 --- a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php @@ -22,10 +22,10 @@ class WebPathResolverFactoryTest extends AbstractWebPathResolverTest public function testReturnExpectedName() { $resolver = new WebPathResolverFactory(); - - $this->assertEquals('web_path', $resolver->getName()); + + $this->assertSame('web_path', $resolver->getName()); } - + /** * @return string|ResolverFactoryInterface */ diff --git a/Tests/Imagine/Cache/Resolver/RelativeWebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/RelativeWebPathResolverTest.php index 51e311c65..c70d4a14c 100644 --- a/Tests/Imagine/Cache/Resolver/RelativeWebPathResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/RelativeWebPathResolverTest.php @@ -1,5 +1,14 @@ filesystem = $this->getMockBuilder(Filesystem::class)->getMock(); $this->pathResolverUtil = $this->getMockBuilder(PathResolverInterface::class)->getMock(); $this->relativeWebPathResolver = new RelativeWebPathResolver($this->filesystem, $this->pathResolverUtil); - + $this->basePath = sys_get_temp_dir().'/aWebRoot'; } - + public function testImplementsResolverInterface() { $this->assertInstanceOf(ResolverInterface::class, $this->relativeWebPathResolver); } - + public function testResolve() { $path = 'aPath'; $filter = 'aFilter'; $fileUrl = 'cacheDir/aFilter/aPath'; - + $this->pathResolverUtil ->expects($this->once()) ->method('getFileUrl') ->with($this->equalTo($path), $this->equalTo($filter)) ->willReturn($fileUrl); - + $actualFileUrl = $this->relativeWebPathResolver->resolve($path, $filter); - - $this->assertEquals(sprintf('/%s', $fileUrl), $actualFileUrl); + + $this->assertSame(sprintf('/%s', $fileUrl), $actualFileUrl); } - + public function testOnSameConstructorArguments() { - $this->assertAttributeEquals($this->filesystem,'filesystem', $this->relativeWebPathResolver); - $this->assertAttributeEquals($this->pathResolverUtil,'pathResolver', $this->relativeWebPathResolver); + $this->assertAttributeSame($this->filesystem, 'filesystem', $this->relativeWebPathResolver); + $this->assertAttributeSame($this->pathResolverUtil, 'pathResolver', $this->relativeWebPathResolver); } - + public function testFileIsStored() { $existingFile = $this->basePath.'/aCachePrefix/aFilter/existingPath'; $filesystem = new Filesystem(); $filesystem->mkdir(dirname($existingFile)); $filesystem->touch($existingFile); - - $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); + + $pathResolver = new PathResolver($this->basePath, 'aCachePrefix'); $resolver = new RelativeWebPathResolver( $this->filesystem, $pathResolver ); - + $this->assertTrue($resolver->isStored('existingPath', 'aFilter')); $filesystem->remove($this->basePath); } - + public function testFileIsNotStored() { $existingFile = $this->basePath.'/aCachePrefix/aFilter/existingPath'; $filesystem = new Filesystem(); $filesystem->mkdir(dirname($existingFile)); $filesystem->touch($existingFile); - - $pathResolver = new PathResolver($this->basePath,'aCachePrefix'); + + $pathResolver = new PathResolver($this->basePath, 'aCachePrefix'); $resolver = new RelativeWebPathResolver( $this->filesystem, $pathResolver ); - + $this->assertFalse($resolver->isStored('notExisting file', 'aFilter')); $filesystem->remove($this->basePath); } - + public function testStore() { $path = 'aPath'; $filter = 'aFilter'; $filePath = '/rootDir/cacheDir/file'; $fileContent = 'theFileContent'; - + $binary = new Binary($fileContent, 'applivation/customFile', 'custom'); - + $this->pathResolverUtil ->expects($this->once()) ->method('getFilePath') ->with($this->equalTo($path), $this->equalTo($filter)) ->willReturn($filePath); - + $this->filesystem ->expects($this->once()) ->method('dumpFile') ->with($this->equalTo($filePath), $this->equalTo($fileContent)); - + $this->relativeWebPathResolver->store($binary, $path, $filter); } - + public function testRemoveWithEmptyInputArrays() { $this->filesystem ->expects($this->exactly(0)) ->method('remove'); - - $this->relativeWebPathResolver->remove(array(), array()); + + $this->relativeWebPathResolver->remove([], []); } - + public function testRemoveWithEmptyPathsArrayAndSingleFilter() { $filter = 'aFilter'; $cacheRoot = '/root/cacheFolder'; - + $this->pathResolverUtil ->expects($this->once()) ->method('getCacheRoot') ->willReturn($cacheRoot); - + $this->filesystem ->expects($this->once()) ->method('remove') @@ -155,21 +164,21 @@ public function testRemoveWithEmptyPathsArrayAndSingleFilter() ] ) ); - - $this->relativeWebPathResolver->remove(array(), array($filter)); + + $this->relativeWebPathResolver->remove([], [$filter]); } - + public function testRemoveWithEmptyPathsArrayAndMultipleFilters() { $filterOne = 'aFilterOne'; $filterTwo = 'aFilterTwo'; $cacheRoot = '/root/cacheFolder'; - + $this->pathResolverUtil ->expects($this->exactly(2)) ->method('getCacheRoot') ->willReturn($cacheRoot); - + $this->filesystem ->expects($this->once()) ->method('remove') @@ -181,10 +190,10 @@ public function testRemoveWithEmptyPathsArrayAndMultipleFilters() ] ) ); - - $this->relativeWebPathResolver->remove(array(), array($filterOne, $filterTwo)); + + $this->relativeWebPathResolver->remove([], [$filterOne, $filterTwo]); } - + public function testRemoveWithMultiplePathaAndFilters() { $filterOne = 'aFilterOne'; @@ -192,43 +201,42 @@ public function testRemoveWithMultiplePathaAndFilters() $pathOne = 'aPathOne'; $pathTwo = 'aPathTwo'; $cacheRoot = '/root/cacheFolder'; - + $this->pathResolverUtil ->expects($this->exactly(0)) ->method('getCacheRoot'); - + $this->pathResolverUtil - ->method("getFilePath") + ->method('getFilePath') ->willReturnMap( [ - [$pathOne, $filterOne, sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne)], - [$pathOne, $filterTwo, sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne)], - [$pathTwo, $filterOne, sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo)], - [$pathTwo, $filterTwo, sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo)], + [$pathOne, $filterOne, sprintf('%s/%s/%s', $cacheRoot, $filterOne, $pathOne)], + [$pathOne, $filterTwo, sprintf('%s/%s/%s', $cacheRoot, $filterTwo, $pathOne)], + [$pathTwo, $filterOne, sprintf('%s/%s/%s', $cacheRoot, $filterOne, $pathTwo)], + [$pathTwo, $filterTwo, sprintf('%s/%s/%s', $cacheRoot, $filterTwo, $pathTwo)], ] ); - + $this->filesystem ->expects($this->at(0)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne)); + ->with(sprintf('%s/%s/%s', $cacheRoot, $filterOne, $pathOne)); $this->filesystem ->expects($this->at(1)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne)); + ->with(sprintf('%s/%s/%s', $cacheRoot, $filterTwo, $pathOne)); $this->filesystem ->expects($this->at(2)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo)); + ->with(sprintf('%s/%s/%s', $cacheRoot, $filterOne, $pathTwo)); $this->filesystem ->expects($this->at(3)) ->method('remove') - ->with(sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo)); - + ->with(sprintf('%s/%s/%s', $cacheRoot, $filterTwo, $pathTwo)); $this->relativeWebPathResolver->remove( - array($pathOne, $pathTwo), - array($filterOne, $filterTwo) + [$pathOne, $pathTwo], + [$filterOne, $filterTwo] ); } } diff --git a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php index 059fc5f7c..26a47d553 100644 --- a/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php +++ b/Tests/Imagine/Cache/Resolver/WebPathResolverTest.php @@ -14,9 +14,9 @@ use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface; use Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver; use Liip\ImagineBundle\Model\Binary; -use PHPUnit\Framework\TestCase; use Liip\ImagineBundle\Utility\Path\PathResolver; use Liip\ImagineBundle\Utility\Path\PathResolverInterface; +use PHPUnit\Framework\TestCase; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Routing\RequestContext; @@ -29,51 +29,51 @@ class WebPathResolverTest extends TestCase * @var Filesystem */ private $filesystem; - + /** * @var string */ private $basePath; - + /** * @var string */ private $existingFile; - + public function setUp() { $this->filesystem = new Filesystem(); - $this->basePath = sys_get_temp_dir() . '/aWebRoot'; - $this->existingFile = $this->basePath . '/aCachePrefix/aFilter/existingPath'; + $this->basePath = sys_get_temp_dir().'/aWebRoot'; + $this->existingFile = $this->basePath.'/aCachePrefix/aFilter/existingPath'; $this->filesystem->mkdir(dirname($this->existingFile)); $this->filesystem->touch($this->existingFile); } - + public function tearDown() { $this->filesystem->remove($this->basePath); } - + public function testImplementsResolverInterface() { $rc = new \ReflectionClass(WebPathResolver::class); - + $this->assertTrue($rc->implementsInterface(ResolverInterface::class)); } - + public function testCouldBeConstructedWithRequiredArguments() { $filesystemMock = $this->createFilesystemMock(); $pathResolver = $this->createPathResolverMock(); $requestContext = new RequestContext(); - + $resolver = new WebPathResolver($filesystemMock, $pathResolver, $requestContext); - + $this->assertAttributeSame($filesystemMock, 'filesystem', $resolver); $this->assertAttributeSame($pathResolver, 'pathResolver', $resolver); $this->assertAttributeSame($requestContext, 'requestContext', $resolver); } - + public function testReturnTrueIfFileExistsOnIsStored() { $pathResolver = new PathResolver($this->basePath, 'aCachePrefix'); @@ -82,10 +82,10 @@ public function testReturnTrueIfFileExistsOnIsStored() $pathResolver, new RequestContext() ); - + $this->assertTrue($resolver->isStored('existingPath', 'aFilter')); } - + public function testReturnFalseIfFileNotExistsOnIsStored() { $pathResolver = new PathResolver($this->basePath, 'aCachePrefix'); @@ -94,10 +94,10 @@ public function testReturnFalseIfFileNotExistsOnIsStored() $pathResolver, new RequestContext() ); - + $this->assertFalse($resolver->isStored('nonExistingPath', 'aFilter')); } - + public function testReturnFalseIfIsNotFile() { $pathResolver = new PathResolver($this->basePath, 'aCachePrefix'); @@ -106,202 +106,202 @@ public function testReturnFalseIfIsNotFile() $pathResolver, new RequestContext() ); - + $this->assertFalse($resolver->isStored('', 'aFilter')); } - + public function testComposeSchemaHostAndFileUrlOnResolve() { $path = 'aPath'; $filter = 'aFilter'; - + $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); - + $pathResolver = $this->createPathResolverMock(); $pathResolver - ->method("getFileUrl") + ->method('getFileUrl') ->with( $this->equalTo($path), $this->equalTo($filter) ) ->willReturn( - "aCachePrefix/aFilter/aPath" + 'aCachePrefix/aFilter/aPath' ); - + $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( 'theschema://thehost/aCachePrefix/aFilter/aPath', $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHostAndBasePathWithPhpFileAndFileUrlOnResolve() { $path = 'aPath'; $filter = 'aFilter'; - + $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('/theBasePath/app.php'); - + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFileUrl") + $pathResolver->method('getFileUrl') ->with( $this->equalTo($path), $this->equalTo($filter) ) ->willReturn( - "aCachePrefix/aFilter/aPath" + 'aCachePrefix/aFilter/aPath' ); - + $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( 'theschema://thehost/theBasePath/aCachePrefix/aFilter/aPath', $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHostAndBasePathWithDirsOnlyAndFileUrlOnResolve() { $path = 'aPath'; $filter = 'aFilter'; - + $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('/theBasePath/theSubBasePath'); - + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFileUrl") + $pathResolver->method('getFileUrl') ->with( $this->equalTo($path), $this->equalTo($filter) ) ->willReturn( - "aCachePrefix/aFilter/aPath" + 'aCachePrefix/aFilter/aPath' ); - + $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( 'theschema://thehost/theBasePath/theSubBasePath/aCachePrefix/aFilter/aPath', $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHostAndBasePathWithBackSplashOnResolve() { $path = 'aPath'; $filter = 'aFilter'; - $fileUrl = "aCachePrefix/aFilter/aPath"; - + $fileUrl = 'aCachePrefix/aFilter/aPath'; + $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('\\'); - + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFileUrl") + $pathResolver->method('getFileUrl') ->with( $this->equalTo($path), $this->equalTo($filter) ) ->willReturn($fileUrl); - + $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( sprintf('theschema://thehost/%s', $fileUrl), $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHttpAndCustomPortAndFileUrlOnResolve() { $path = 'aPath'; $filter = 'aFilter'; - $fileUrl = "aCachePrefix/aFilter/aPath"; - + $fileUrl = 'aCachePrefix/aFilter/aPath'; + $requestContext = new RequestContext(); $requestContext->setScheme('http'); $requestContext->setHost('thehost'); $requestContext->setHttpPort(88); - + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFileUrl") + $pathResolver->method('getFileUrl') ->with( $this->equalTo($path), $this->equalTo($filter) ) ->willReturn($fileUrl); - + $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( sprintf('http://thehost:88/%s', $fileUrl), $resolver->resolve($path, $filter) ); } - + public function testComposeSchemaHttpsAndCustomPortAndFileUrlOnResolve() { $path = 'aPath'; $filter = 'aFilter'; - $fileUrl = "aCachePrefix/aFilter/aPath"; - + $fileUrl = 'aCachePrefix/aFilter/aPath'; + $requestContext = new RequestContext(); $requestContext->setScheme('https'); $requestContext->setHost('thehost'); $requestContext->setHttpsPort(444); - + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFileUrl") + $pathResolver->method('getFileUrl') ->with( $this->equalTo($path), $this->equalTo($filter) ) ->willReturn($fileUrl); - + $resolver = new WebPathResolver( $this->createFilesystemMock(), $pathResolver, $requestContext ); - + $this->assertSame( sprintf('https://thehost:444/%s', $fileUrl), $resolver->resolve($path, $filter) ); } - + public function testDumpBinaryContentOnStore() { $path = 'aPath'; $filter = 'aFilter'; $fileUrl = '/aWebRoot/aCachePrefix/aFilter/aPath'; $fileContent = 'theContent'; - + $binary = new Binary($fileContent, 'aMimeType', 'aFormat'); - + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->once()) @@ -310,9 +310,9 @@ public function testDumpBinaryContentOnStore() $this->equalTo($fileUrl), $this->equalTo($fileContent) ); - + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFilePath") + $pathResolver->method('getFilePath') ->with( $this->equalTo($path), $this->equalTo($filter) @@ -320,87 +320,87 @@ public function testDumpBinaryContentOnStore() ->willReturn( $fileUrl ); - + $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $this->assertNull($resolver->store($binary, $path, $filter)); } - + public function testDoNothingIfFiltersAndPathsEmptyOnRemove() { $filesystemMock = $this->createFilesystemMock(); $filesystemMock->expects($this->never())->method('remove'); - + $resolver = new WebPathResolver( $filesystemMock, $this->createPathResolverMock(), new RequestContext() ); - + $resolver->remove([], []); } - + public function testRemoveCacheForPathAndFilterOnRemove() { $filePath = '/aWebRoot/aCachePrefix/aFilter/aPath'; $filesystemMock = $this->createFilesystemMock(); $filesystemMock->expects($this->once())->method('remove')->with($filePath); - + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFilePath")->willReturn($filePath); - + $pathResolver->method('getFilePath')->willReturn($filePath); + $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $resolver->remove(['aPath'], ['aFilter']); } - + public function testRemoveCacheForSomePathsAndFilterOnRemove() { $cacheRoot = '/aWebRoot/aCachePrefix'; $pathOne = 'aPathOne'; $pathTwo = 'aPathTwo'; $filter = 'aFilter'; - + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->at(0)) ->method('remove') ->with( - sprintf("%s/%s/%s", $cacheRoot, $filter, $pathOne) + sprintf('%s/%s/%s', $cacheRoot, $filter, $pathOne) ); $filesystemMock ->expects($this->at(1)) ->method('remove') ->with( - sprintf("%s/%s/%s", $cacheRoot, $filter, $pathTwo) + sprintf('%s/%s/%s', $cacheRoot, $filter, $pathTwo) ); - + $pathResolver = $this->createPathResolverMock(); - $pathResolver->method("getFilePath") + $pathResolver->method('getFilePath') ->willReturnMap( [ - [$pathOne, $filter, sprintf("%s/%s/%s", $cacheRoot, $filter, $pathOne)], - [$pathTwo, $filter, sprintf("%s/%s/%s", $cacheRoot, $filter, $pathTwo)], + [$pathOne, $filter, sprintf('%s/%s/%s', $cacheRoot, $filter, $pathOne)], + [$pathTwo, $filter, sprintf('%s/%s/%s', $cacheRoot, $filter, $pathTwo)], ] ); - + $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $resolver->remove([$pathOne, $pathTwo], [$filter]); } - + public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() { $cacheRoot = '/aWebRoot/aCachePrefix'; @@ -408,127 +408,127 @@ public function testRemoveCacheForSomePathsAndSomeFiltersOnRemove() $pathTwo = 'aPathTwo'; $filterOne = 'aFilterOne'; $filterTwo = 'aFilterTwo'; - + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->at(0)) ->method('remove') ->with( - sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne) + sprintf('%s/%s/%s', $cacheRoot, $filterOne, $pathOne) ); $filesystemMock ->expects($this->at(1)) ->method('remove') ->with( - sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne) + sprintf('%s/%s/%s', $cacheRoot, $filterTwo, $pathOne) ); $filesystemMock ->expects($this->at(2)) ->method('remove') ->with( - sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo) + sprintf('%s/%s/%s', $cacheRoot, $filterOne, $pathTwo) ); $filesystemMock ->expects($this->at(3)) ->method('remove') ->with( - sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo) + sprintf('%s/%s/%s', $cacheRoot, $filterTwo, $pathTwo) ); - + $pathResolver = $this->createPathResolverMock(); $pathResolver - ->method("getFilePath") + ->method('getFilePath') ->willReturnMap( [ - [$pathOne, $filterOne, sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathOne)], - [$pathOne, $filterTwo, sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathOne)], - [$pathTwo, $filterOne, sprintf("%s/%s/%s", $cacheRoot, $filterOne, $pathTwo)], - [$pathTwo, $filterTwo, sprintf("%s/%s/%s", $cacheRoot, $filterTwo, $pathTwo)], + [$pathOne, $filterOne, sprintf('%s/%s/%s', $cacheRoot, $filterOne, $pathOne)], + [$pathOne, $filterTwo, sprintf('%s/%s/%s', $cacheRoot, $filterTwo, $pathOne)], + [$pathTwo, $filterOne, sprintf('%s/%s/%s', $cacheRoot, $filterOne, $pathTwo)], + [$pathTwo, $filterTwo, sprintf('%s/%s/%s', $cacheRoot, $filterTwo, $pathTwo)], ] ); - + $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $resolver->remove( [$pathOne, $pathTwo], [$filterOne, $filterTwo] ); } - + public function testRemoveCacheForFilterOnRemove() { $cacheRoot = '/aWebRoot/aCachePrefix'; - + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->once()) ->method('remove') ->with( [ - sprintf("%s/aFilter", $cacheRoot), + sprintf('%s/aFilter', $cacheRoot), ] ); - + $pathResolver = $this->createPathResolverMock(); $pathResolver - ->method("getCacheRoot") + ->method('getCacheRoot') ->willReturn($cacheRoot); - + $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $resolver->remove([], ['aFilter']); } - + public function testRemoveCacheForSomeFiltersOnRemove() { $cacheRoot = '/aWebRoot/aCachePrefix'; - + $filesystemMock = $this->createFilesystemMock(); $filesystemMock ->expects($this->once()) ->method('remove') ->with( [ - sprintf("%s/aFilterOne", $cacheRoot), - sprintf("%s/aFilterTwo", $cacheRoot), + sprintf('%s/aFilterOne', $cacheRoot), + sprintf('%s/aFilterTwo', $cacheRoot), ] ); - + $pathResolver = $this->createPathResolverMock(); $pathResolver - ->method("getCacheRoot") + ->method('getCacheRoot') ->willReturn($cacheRoot); - + $resolver = new WebPathResolver( $filesystemMock, $pathResolver, new RequestContext() ); - + $resolver->remove([], ['aFilterOne', 'aFilterTwo']); } - + /** - * @return \PHPUnit_Framework_MockObject_MockObject|Filesystem + * @return \PHPUnit\Framework\MockObject\MockObject|PathResolverInterface */ - protected function createFilesystemMock() + public function createPathResolverMock() { - return $this->getMockBuilder(Filesystem::class)->getMock(); + return $this->getMockBuilder(PathResolverInterface::class)->getMock(); } - + /** - * @return \PHPUnit\Framework\MockObject\MockObject|PathResolverInterface + * @return \PHPUnit_Framework_MockObject_MockObject|Filesystem */ - public function createPathResolverMock() + protected function createFilesystemMock() { - return $this->getMockBuilder(PathResolverInterface::class)->getMock(); + return $this->getMockBuilder(Filesystem::class)->getMock(); } } diff --git a/Tests/LiipImagineBundleTest.php b/Tests/LiipImagineBundleTest.php index 61fcac672..b7058f5d5 100644 --- a/Tests/LiipImagineBundleTest.php +++ b/Tests/LiipImagineBundleTest.php @@ -36,7 +36,7 @@ public function testSubClassOfBundle() { $this->assertInstanceOf(Bundle::class, new LiipImagineBundle()); } - + public function testAddLoadersCompilerPassOnBuild() { $containerMock = $this->createContainerBuilderMock(); @@ -53,11 +53,11 @@ public function testAddLoadersCompilerPassOnBuild() ->with( $this->isInstanceOf(LoadersCompilerPass::class) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddFiltersCompilerPassOnBuild() { $containerMock = $this->createContainerBuilderMock(); @@ -74,11 +74,11 @@ public function testAddFiltersCompilerPassOnBuild() ->with( $this->isInstanceOf(FiltersCompilerPass::class) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddPostProcessorsCompilerPassOnBuild() { $containerMock = $this->createContainerBuilderMock(); @@ -94,11 +94,11 @@ public function testAddPostProcessorsCompilerPassOnBuild() ->with( $this->isInstanceOf(PostProcessorsCompilerPass::class) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddResolversCompilerPassOnBuild() { $containerMock = $this->createContainerBuilderMock(); @@ -115,11 +115,11 @@ public function testAddResolversCompilerPassOnBuild() ->with( $this->isInstanceOf(ResolversCompilerPass::class) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddWebPathResolverFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); @@ -129,7 +129,7 @@ public function testAddWebPathResolverFactoryOnBuild() ->with( $this->isInstanceOf(WebPathResolverFactory::class) ); - + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) @@ -138,11 +138,11 @@ public function testAddWebPathResolverFactoryOnBuild() ->will( $this->returnValue($extensionMock) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddAwsS3ResolverFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); @@ -152,7 +152,7 @@ public function testAddAwsS3ResolverFactoryOnBuild() ->with( $this->isInstanceOf(AwsS3ResolverFactory::class) ); - + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) @@ -161,11 +161,11 @@ public function testAddAwsS3ResolverFactoryOnBuild() ->will( $this->returnValue($extensionMock) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddFlysystemResolverFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); @@ -174,7 +174,7 @@ public function testAddFlysystemResolverFactoryOnBuild() ->with( $this->isInstanceOf(FlysystemResolverFactory::class) ); - + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) @@ -183,11 +183,11 @@ public function testAddFlysystemResolverFactoryOnBuild() ->will( $this->returnValue($extensionMock) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddRelativeWebPAthResolverFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); @@ -197,7 +197,7 @@ public function testAddRelativeWebPAthResolverFactoryOnBuild() ->with( $this->isInstanceOf(RelativeWebPathResolverFactory::class) ); - + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) @@ -206,11 +206,11 @@ public function testAddRelativeWebPAthResolverFactoryOnBuild() ->will( $this->returnValue($extensionMock) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddStreamLoaderFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); @@ -220,7 +220,7 @@ public function testAddStreamLoaderFactoryOnBuild() ->with( $this->isInstanceOf(StreamLoaderFactory::class) ); - + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) @@ -229,11 +229,11 @@ public function testAddStreamLoaderFactoryOnBuild() ->will( $this->returnValue($extensionMock) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddFilesystemLoaderFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); @@ -243,7 +243,7 @@ public function testAddFilesystemLoaderFactoryOnBuild() ->with( $this->isInstanceOf(FileSystemLoaderFactory::class) ); - + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) @@ -252,11 +252,11 @@ public function testAddFilesystemLoaderFactoryOnBuild() ->will( $this->returnValue($extensionMock) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + public function testAddFlysystemLoaderFactoryOnBuild() { $extensionMock = $this->createLiipImagineExtensionMock(); @@ -266,7 +266,7 @@ public function testAddFlysystemLoaderFactoryOnBuild() ->with( $this->isInstanceOf(FlysystemLoaderFactory::class) ); - + $containerMock = $this->createContainerBuilderMock(); $containerMock ->expects($this->atLeastOnce()) @@ -275,11 +275,11 @@ public function testAddFlysystemLoaderFactoryOnBuild() ->will( $this->returnValue($extensionMock) ); - + $bundle = new LiipImagineBundle(); $bundle->build($containerMock); } - + /** * @return \PHPUnit_Framework_MockObject_MockObject|ContainerBuilder */ @@ -287,7 +287,7 @@ protected function createContainerBuilderMock() { return $this->createObjectMock(ContainerBuilder::class, [], false); } - + /** * @return \PHPUnit_Framework_MockObject_MockObject|LiipImagineExtension */ diff --git a/Tests/Utility/Path/PathResolverTest.php b/Tests/Utility/Path/PathResolverTest.php index 6efd68374..da60e0881 100644 --- a/Tests/Utility/Path/PathResolverTest.php +++ b/Tests/Utility/Path/PathResolverTest.php @@ -24,196 +24,196 @@ public function testForInterfaceImplementation() { $this->assertTrue(is_a(PathResolver::class, PathResolverInterface::class, true)); } - + public function testPropertiesForSameConstructorArguments() { $webRootDir = 'aWebRootDir'; $cachePrefix = 'aCachePrefix'; $pathResolver = new PathResolver($webRootDir, $cachePrefix); - $this->assertAttributeEquals($webRootDir, 'webRoot', $pathResolver); - $this->assertAttributeEquals($cachePrefix, 'cachePrefix', $pathResolver); + $this->assertAttributeSame($webRootDir, 'webRoot', $pathResolver); + $this->assertAttributeSame($cachePrefix, 'cachePrefix', $pathResolver); } - + public function testWebRootPathNormalizer() { $pathResolver = new PathResolver('aWebRootDir/'); - $this->assertAttributeEquals('aWebRootDir', 'webRoot', $pathResolver); + $this->assertAttributeSame('aWebRootDir', 'webRoot', $pathResolver); } - + public function testCachePrefixNormalizer() { $pathResolver = new PathResolver('aWebRootDir', '/cachePrefix'); - $this->assertAttributeEquals('cachePrefix', 'cachePrefix', $pathResolver); + $this->assertAttributeSame('cachePrefix', 'cachePrefix', $pathResolver); } - + public function testForDoubleSlashReplacing() { $pathResolver = new PathResolver( 'aWebRootDir//subRootDir', 'cachePrefix//subCacheDir' ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'aWebRootDir/subRootDir', 'webRoot', $pathResolver ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'cachePrefix/subCacheDir', 'cachePrefix', $pathResolver ); } - + public function testPathsNormalizerWithSubfolders() { $pathResolver = new PathResolver( 'aWebRootDir/subRootDir', 'cachePrefix/subCacheDir/anotherSubCacheDir' ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'aWebRootDir/subRootDir', 'webRoot', $pathResolver ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'cachePrefix/subCacheDir/anotherSubCacheDir', 'cachePrefix', $pathResolver ); } - + public function testCacheRootPathDirCreationWithoutInvalidSlashes() { $pathResolver = new PathResolver( 'aWebRootDir/subRootDir', 'cachePrefix/subCacheDir' ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'aWebRootDir/subRootDir', 'webRoot', $pathResolver ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'cachePrefix/subCacheDir', 'cachePrefix', $pathResolver ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'aWebRootDir/subRootDir/cachePrefix/subCacheDir', 'cacheRoot', $pathResolver ); } - + public function testCacheRootPathDirCreationWithInvalidSlashes() { $pathResolver = new PathResolver( 'aWebRootDir/subRootDir/', '/cachePrefix/subCacheDir' ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'aWebRootDir/subRootDir', 'webRoot', $pathResolver ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'cachePrefix/subCacheDir', 'cachePrefix', $pathResolver ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'aWebRootDir/subRootDir/cachePrefix/subCacheDir', 'cacheRoot', $pathResolver ); } - + public function testCacheRootPathDirCreationWithDoubledSlashes() { $pathResolver = new PathResolver( 'aWebRootDir//subRootDir/', '/cachePrefix//subCacheDir' ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'aWebRootDir/subRootDir', 'webRoot', $pathResolver ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'cachePrefix/subCacheDir', 'cachePrefix', $pathResolver ); - $this->assertAttributeEquals( + $this->assertAttributeSame( 'aWebRootDir/subRootDir/cachePrefix/subCacheDir', 'cacheRoot', $pathResolver ); } - + public function testGetCacheRoot() { $pathResolver = new PathResolver( 'aWebRootDir', '/cachePrefix//subCacheDir' ); - $this->assertEquals( + $this->assertSame( 'aWebRootDir/cachePrefix/subCacheDir', $pathResolver->getCacheRoot() ); } - + public function testGetFileUrlWithSchemePath() { $path = 'https://path-to-no-where'; $filter = 'aFilter'; $cachePrefix = 'aCahcePrefix'; - + $pathResolver = new PathResolver('aWebRootDir', $cachePrefix); $actualFileUrl = $pathResolver->getFileUrl($path, $filter); - - $this->assertEquals(sprintf('%s/%s/https---path-to-no-where', $cachePrefix, $filter), $actualFileUrl); + + $this->assertSame(sprintf('%s/%s/https---path-to-no-where', $cachePrefix, $filter), $actualFileUrl); } - + public function testGetFileUrlPathTrim() { $path = '/path-to-no-where'; $filter = 'aFilter'; $cachePrefix = 'aCahcePrefix'; - + $pathResolver = new PathResolver('aWebRootDir', $cachePrefix); $actualFileUrl = $pathResolver->getFileUrl($path, $filter); - - $this->assertEquals(sprintf('%s/%s/path-to-no-where', $cachePrefix, $filter), $actualFileUrl); + + $this->assertSame(sprintf('%s/%s/path-to-no-where', $cachePrefix, $filter), $actualFileUrl); } - + public function testGetFilePathWithSchemePath() { $path = 'https://path-to-no-where'; $filter = 'aFilter'; $webRootDir = 'aWebRootDir'; $cachePrefix = 'aCahcePrefix'; - + $pathResolver = new PathResolver($webRootDir, $cachePrefix); $actualFileUrl = $pathResolver->getFilePath($path, $filter); - - $this->assertEquals( + + $this->assertSame( sprintf('%s/%s/%s/https---path-to-no-where', $webRootDir, $cachePrefix, $filter), $actualFileUrl ); } - + public function testGetFilePathWithPathTrim() { $path = '/path-to-no-where'; $filter = 'aFilter'; $webRootDir = 'aWebRootDir'; $cachePrefix = 'aCahcePrefix'; - + $pathResolver = new PathResolver($webRootDir, $cachePrefix); $actualFileUrl = $pathResolver->getFilePath($path, $filter); - - $this->assertEquals( + + $this->assertSame( sprintf('%s/%s/%s/path-to-no-where', $webRootDir, $cachePrefix, $filter), $actualFileUrl ); diff --git a/Utility/Path/PathResolver.php b/Utility/Path/PathResolver.php index 33bbe3312..cb9466264 100644 --- a/Utility/Path/PathResolver.php +++ b/Utility/Path/PathResolver.php @@ -17,34 +17,34 @@ class PathResolver implements PathResolverInterface * @var string */ protected $webRoot; - + /** * @var string */ protected $cachePrefix; - + /** * @var string */ protected $cacheRoot; - + public function __construct( $webRootDir, $cachePrefix = 'media/cache' ) { $this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); - $this->cacheRoot = $this->webRoot . '/' . $this->cachePrefix; + $this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; } - + /** * {@inheritdoc} */ public function getFilePath($path, $filter) { - return $this->webRoot . '/' . $this->getFileUrl($path, $filter); + return $this->webRoot.'/'.$this->getFileUrl($path, $filter); } - + /** * {@inheritdoc} */ @@ -52,10 +52,10 @@ public function getFileUrl($path, $filter) { // crude way of sanitizing URL scheme ("protocol") part $path = str_replace('://', '---', $path); - - return $this->cachePrefix . '/' . $filter . '/' . ltrim($path, '/'); + + return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); } - + /** * {@inheritdoc} */ diff --git a/Utility/Path/PathResolverInterface.php b/Utility/Path/PathResolverInterface.php index fd35bbd92..101e4d981 100644 --- a/Utility/Path/PathResolverInterface.php +++ b/Utility/Path/PathResolverInterface.php @@ -20,7 +20,7 @@ interface PathResolverInterface * @return string */ public function getFilePath($path, $filter); - + /** * @param string $path * @param string $filter @@ -28,7 +28,7 @@ public function getFilePath($path, $filter); * @return string */ public function getFileUrl($path, $filter); - + /** * @return string */