diff --git a/.travis.yml b/.travis.yml index d9807d3..c386823 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,6 @@ language: php sudo: false php: - - 5.5 - - 5.6 - 7.0 - hhvm @@ -13,6 +11,10 @@ env: - DEPENDENCIES="" - DEPENDENCIES="--prefer-lowest --prefer-stable" +matrix: + allow_failures: + - php: hhvm + before_script: - composer self-update - composer update --prefer-source $DEPENDENCIES diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..69d8193 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,22 @@ +# Changelog + +All notable changes to this project will be documented in this file, in reverse chronological order by release. + +## 4.0.0 - 2016-06-04 + +### Added + +- Support for `zendframework/zend-mvc` 3.x + +### Deprecated + +- Nothing. + +### Removed + +- Support for PHP 5 has been dropped +- Factories now follow the interfaces suggested by `zendframework/zend-servicemanager` 3.x + +### Fixed + +- Nothing. diff --git a/README.md b/README.md index 51b67a6..efd6777 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ OcraCachedViewResolver is performance-oriented Zend Framework 2 Module that increases performance in your application by caching the process of resolving template names to template paths. -In ZF2, the process of resolving template paths causes a lot of stat calls. This module adds +In ZF3, the process of resolving template paths causes a lot of stat calls. This module adds a cache layer to avoid that. | Tests | Releases | Downloads | Dependencies | @@ -19,8 +19,8 @@ The recommended way to install `ocramius/ocra-cached-view-resolver` is through php composer.phar require ocramius/ocra-cached-view-resolver:3.0.* ``` -If you use legacy/outdated PHP versions, such as `5.3.x` and `5.4.x`, you can use any -[`1.x`](https://github.com/Ocramius/OcraCachedViewResolver/tree/1.1.0) version +If you use legacy/outdated PHP versions, such as `5.5.x` and `5.6.x`, you can use any +[`3.x`](https://github.com/Ocramius/OcraCachedViewResolver/tree/3.0.0) version of `ocramius/ocra-cached-view-resolver`. You can then enable the module in your `config/application.config.php` by adding diff --git a/composer.json b/composer.json index ae0fc37..36cc945 100644 --- a/composer.json +++ b/composer.json @@ -18,17 +18,16 @@ } ], "require": { - "php": "~5.5|~7.0", - "zendframework/zend-servicemanager": "~2.3", - "zendframework/zend-mvc": "~2.3", - "zendframework/zend-view": "~2.3", - "zendframework/zend-cache": "~2.3", - "zendframework/zend-config": "~2.3" + "php": "~7.0", + "zendframework/zend-servicemanager": "^3.1.0", + "zendframework/zend-mvc": "^3.0", + "zendframework/zend-view": "^2.7", + "zendframework/zend-cache": "^2.7.1", + "zendframework/zend-config": "^2.6" }, "require-dev": { - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "~2.0", - "zendframework/zendframework": "2.*" + "phpunit/phpunit": "^5.4.2", + "squizlabs/php_codesniffer": "~2.6.1" }, "autoload": { "psr-0": { @@ -42,7 +41,7 @@ }, "extra": { "branch-alias": { - "dev-master": "3.1.x-dev" + "dev-master": "4.1.x-dev" } } } diff --git a/src/OcraCachedViewResolver/Compiler/TemplateMapCompiler.php b/src/OcraCachedViewResolver/Compiler/TemplateMapCompiler.php index e54d995..f43ecf0 100644 --- a/src/OcraCachedViewResolver/Compiler/TemplateMapCompiler.php +++ b/src/OcraCachedViewResolver/Compiler/TemplateMapCompiler.php @@ -46,8 +46,10 @@ class TemplateMapCompiler * @param ResolverInterface $resolver * * @return array + * + * @throws \Zend\View\Exception\DomainException */ - public function compileMap(ResolverInterface $resolver) + public function compileMap(ResolverInterface $resolver) : array { if ($resolver instanceof AggregateResolver) { return $this->compileFromAggregateResolver($resolver); @@ -64,12 +66,7 @@ public function compileMap(ResolverInterface $resolver) return []; } - /** - * @param AggregateResolver $resolver - * - * @return array - */ - protected function compileFromAggregateResolver(AggregateResolver $resolver) + protected function compileFromAggregateResolver(AggregateResolver $resolver) : array { $map = []; @@ -85,8 +82,10 @@ protected function compileFromAggregateResolver(AggregateResolver $resolver) * @param TemplatePathStack $resolver * * @return array + * + * @throws \Zend\View\Exception\DomainException */ - protected function compileFromTemplatePathStack(TemplatePathStack $resolver) + protected function compileFromTemplatePathStack(TemplatePathStack $resolver) : array { $map = []; @@ -105,12 +104,7 @@ protected function compileFromTemplatePathStack(TemplatePathStack $resolver) return $map; } - /** - * @param TemplateMapResolver $resolver - * - * @return array - */ - protected function compileFromTemplateMapResolver(TemplateMapResolver $resolver) + protected function compileFromTemplateMapResolver(TemplateMapResolver $resolver) : array { return $resolver->getMap(); } @@ -124,6 +118,8 @@ protected function compileFromTemplateMapResolver(TemplateMapResolver $resolver) * @param TemplatePathStack $resolver * * @return void + * + * @throws \Zend\View\Exception\DomainException */ private function addResolvedPath(SplFileInfo $file, array & $map, $basePath, TemplatePathStack $resolver) { diff --git a/src/OcraCachedViewResolver/Factory/CacheFactory.php b/src/OcraCachedViewResolver/Factory/CacheFactory.php index 3de9481..dcfc0bf 100644 --- a/src/OcraCachedViewResolver/Factory/CacheFactory.php +++ b/src/OcraCachedViewResolver/Factory/CacheFactory.php @@ -18,9 +18,12 @@ namespace OcraCachedViewResolver\Factory; +use Interop\Container\ContainerInterface; +use Interop\Container\Exception\ContainerException; +use Interop\Container\Exception\NotFoundException; use OcraCachedViewResolver\Module; -use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\ServiceLocatorInterface; +use Zend\Cache\Exception\InvalidArgumentException; +use Zend\Cache\Storage\StorageInterface; use Zend\Cache\StorageFactory; /** @@ -30,14 +33,18 @@ * @author Marco Pivetta * @license MIT */ -final class CacheFactory implements FactoryInterface +final class CacheFactory { /** * {@inheritDoc} + * + * @throws InvalidArgumentException + * @throws ContainerException + * @throws NotFoundException */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container) : StorageInterface { - $config = $serviceLocator->get('Config'); + $config = $container->get('Config'); return StorageFactory::factory($config[Module::CONFIG][Module::CONFIG_CACHE_DEFINITION]); } diff --git a/src/OcraCachedViewResolver/Factory/CompiledMapResolverDelegatorFactory.php b/src/OcraCachedViewResolver/Factory/CompiledMapResolverDelegatorFactory.php index f322173..be89fa2 100644 --- a/src/OcraCachedViewResolver/Factory/CompiledMapResolverDelegatorFactory.php +++ b/src/OcraCachedViewResolver/Factory/CompiledMapResolverDelegatorFactory.php @@ -18,12 +18,15 @@ namespace OcraCachedViewResolver\Factory; +use Interop\Container\ContainerInterface; +use Interop\Container\Exception\ContainerException; +use Interop\Container\Exception\NotFoundException; use OcraCachedViewResolver\Module; use OcraCachedViewResolver\View\Resolver\CachingMapResolver; use OcraCachedViewResolver\View\Resolver\LazyResolver; -use Zend\ServiceManager\DelegatorFactoryInterface; -use Zend\ServiceManager\ServiceLocatorInterface; +use Zend\ServiceManager\Factory\DelegatorFactoryInterface; use Zend\View\Resolver\AggregateResolver; +use Zend\View\Resolver\ResolverInterface; /** * Factory responsible of building a {@see \Zend\View\Resolver\TemplateMapResolver} @@ -38,12 +41,19 @@ final class CompiledMapResolverDelegatorFactory implements DelegatorFactoryInter * {@inheritDoc} * * @return AggregateResolver + * + * @throws ContainerException + * @throws NotFoundException */ - public function createDelegatorWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName, $callback) - { - $config = $serviceLocator->get('Config')[Module::CONFIG]; + public function __invoke( + ContainerInterface $container, + $name, + callable $callback, + array $options = null + ) : ResolverInterface { + $config = $container->get('Config')[Module::CONFIG]; /* @var $cache \Zend\Cache\Storage\StorageInterface */ - $cache = $serviceLocator->get($config[Module::CONFIG_CACHE_SERVICE]); + $cache = $container->get($config[Module::CONFIG_CACHE_SERVICE]); $resolver = new AggregateResolver(); diff --git a/src/OcraCachedViewResolver/Module.php b/src/OcraCachedViewResolver/Module.php index 155ccd3..ce76324 100644 --- a/src/OcraCachedViewResolver/Module.php +++ b/src/OcraCachedViewResolver/Module.php @@ -56,7 +56,7 @@ final class Module implements ConfigProviderInterface /** * {@inheritDoc} */ - public function getConfig() + public function getConfig() : array { return [ self::CONFIG => [ diff --git a/src/OcraCachedViewResolver/View/Resolver/Exception/InvalidResolverInstantiatorException.php b/src/OcraCachedViewResolver/View/Resolver/Exception/InvalidResolverInstantiatorException.php index 7179d53..0bc49fa 100644 --- a/src/OcraCachedViewResolver/View/Resolver/Exception/InvalidResolverInstantiatorException.php +++ b/src/OcraCachedViewResolver/View/Resolver/Exception/InvalidResolverInstantiatorException.php @@ -34,7 +34,7 @@ final class InvalidResolverInstantiatorException extends InvalidArgumentExceptio * * @return self */ - public static function fromInvalidInstantiator($instantiator) + public static function fromInvalidInstantiator($instantiator) : self { return new self(sprintf( 'Invalid instantiator given, expected `callable`, `%s` given.', @@ -47,7 +47,7 @@ public static function fromInvalidInstantiator($instantiator) * * @return self */ - public static function fromInvalidResolver($resolver) + public static function fromInvalidResolver($resolver) : self { return new self(sprintf( 'Invalid resolver found, expected `' . ResolverInterface::class . '`, `%s` given.', diff --git a/tests/OcraCachedViewResolverTest/Compiler/TemplateMapCompilerFunctionalTest.php b/tests/OcraCachedViewResolverTest/Compiler/TemplateMapCompilerFunctionalTest.php index ddb2074..9bc3733 100644 --- a/tests/OcraCachedViewResolverTest/Compiler/TemplateMapCompilerFunctionalTest.php +++ b/tests/OcraCachedViewResolverTest/Compiler/TemplateMapCompilerFunctionalTest.php @@ -51,6 +51,8 @@ public function setUp() /** * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileMap * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileFromTemplatePathStack + * + * @throws \Zend\View\Exception\InvalidArgumentException */ public function testCompileFromTemplatePathStack() { @@ -61,10 +63,10 @@ public function testCompileFromTemplatePathStack() $template2 = realpath(__DIR__ . '/_files/subdir2/template2.phtml'); $template4 = realpath(__DIR__ . '/_files/subdir1/valid/template4.phtml'); - $this->assertInternalType('string', $template2); - $this->assertInternalType('string', $template4); + self::assertInternalType('string', $template2); + self::assertInternalType('string', $template4); - $this->assertSame( + self::assertSame( [ 'template2' => $template2, 'valid/template4' => $template4, @@ -76,14 +78,16 @@ public function testCompileFromTemplatePathStack() /** * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileMap * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileFromTemplatePathStack + * + * @throws \Zend\View\Exception\InvalidArgumentException */ public function testCompileFromTemplatePathStackWithDifferentPaths() { $template2 = realpath(__DIR__ . '/_files/subdir1/template2.phtml'); $template4 = realpath(__DIR__ . '/_files/subdir1/valid/template4.phtml'); - $this->assertInternalType('string', $template2); - $this->assertInternalType('string', $template4); + self::assertInternalType('string', $template2); + self::assertInternalType('string', $template4); // inverse directory order $resolver = new TemplatePathStack(); @@ -92,8 +96,8 @@ public function testCompileFromTemplatePathStackWithDifferentPaths() $map = $this->compiler->compileMap($resolver); - $this->assertCount(2, $map); - $this->assertSame($template2, $map['template2']); - $this->assertSame($template4, $map['valid/template4']); + self::assertCount(2, $map); + self::assertSame($template2, $map['template2']); + self::assertSame($template4, $map['valid/template4']); } } diff --git a/tests/OcraCachedViewResolverTest/Compiler/TemplateMapCompilerTest.php b/tests/OcraCachedViewResolverTest/Compiler/TemplateMapCompilerTest.php index 2f96f98..e01ff19 100644 --- a/tests/OcraCachedViewResolverTest/Compiler/TemplateMapCompilerTest.php +++ b/tests/OcraCachedViewResolverTest/Compiler/TemplateMapCompilerTest.php @@ -56,59 +56,65 @@ public function setUp() /** * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileMap + * + * @throws \PHPUnit_Framework_Exception */ public function testCompileFromUnknownResolverProducesEmptyMap() { /* @var $resolver ResolverInterface */ - $resolver = $this->getMock(ResolverInterface::class); + $resolver = $this->createMock(ResolverInterface::class); - $this->assertSame([], $this->compiler->compileMap($resolver)); + self::assertSame([], $this->compiler->compileMap($resolver)); } /** * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileMap * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileFromTemplateMapResolver * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::addResolvedPath + * + * @throws \PHPUnit_Framework_Exception */ public function testCompileFromMapResolver() { /* @var $mapResolver TemplateMapResolver|\PHPUnit_Framework_MockObject_MockObject */ - $mapResolver = $this->getMock(TemplateMapResolver::class); + $mapResolver = $this->createMock(TemplateMapResolver::class); $mapResolver - ->expects($this->any()) + ->expects(self::any()) ->method('getMap') - ->will($this->returnValue(['a' => 'b', 'c' => 'd'])); + ->will(self::returnValue(['a' => 'b', 'c' => 'd'])); $map = $this->compiler->compileMap($mapResolver); - $this->assertCount(2, $map); - $this->assertSame('b', $map['a']); - $this->assertSame('d', $map['c']); + self::assertCount(2, $map); + self::assertSame('b', $map['a']); + self::assertSame('d', $map['c']); } /** * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileMap * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileFromTemplatePathStack * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::addResolvedPath + * + * @throws \PHPUnit_Framework_Exception */ public function testCompileFromTemplatePathStack() { /* @var $templatePathStack TemplatePathStack|\PHPUnit_Framework_MockObject_MockObject */ - $templatePathStack = $this->getMock(TemplatePathStack::class); - $paths = $this->getMock(SplStack::class); + $templatePathStack = $this->createMock(TemplatePathStack::class); + $paths = $this->createMock(SplStack::class); $paths - ->expects($this->any()) + ->expects(self::any()) ->method('toArray') - ->will($this->returnValue([__DIR__ . '/_files/subdir2', __DIR__ . '/_files/subdir1'])); + ->will(self::returnValue([__DIR__ . '/_files/subdir2', __DIR__ . '/_files/subdir1'])); $templatePathStack - ->expects($this->any()) + ->expects(self::any()) ->method('getPaths') - ->will($this->returnValue($paths)); + ->will(self::returnValue($paths)); $templatePathStack - ->expects($this->any()) + ->expects(self::any()) ->method('resolve') - ->will($this->returnCallback(function ($name) { + ->willReturnCallback(function ($name) { $keys = [ 'template2' => __DIR__ . '/_files/subdir2/template2.phtml', 'template3' => false, @@ -116,62 +122,64 @@ public function testCompileFromTemplatePathStack() ]; return $keys[$name]; - })); + }); $map = $this->compiler->compileMap($templatePathStack); - $this->assertCount(2, $map); + self::assertCount(2, $map); $template2 = realpath(__DIR__ . '/_files/subdir2/template2.phtml'); $template4 = realpath(__DIR__ . '/_files/subdir1/valid/template4.phtml'); - $this->assertInternalType('string', $template2); - $this->assertInternalType('string', $template4); + self::assertInternalType('string', $template2); + self::assertInternalType('string', $template4); - $this->assertSame($template2, $map['template2']); - $this->assertSame($template4, $map['valid/template4']); + self::assertSame($template2, $map['template2']); + self::assertSame($template4, $map['valid/template4']); } /** * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileMap * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::compileFromAggregateResolver * @covers \OcraCachedViewResolver\Compiler\TemplateMapCompiler::addResolvedPath + * + * @throws \PHPUnit_Framework_Exception */ public function testCompileFromAggregateResolver() { /* @var $aggregateResolver AggregateResolver|\PHPUnit_Framework_MockObject_MockObject */ - $aggregateResolver = $this->getMock(AggregateResolver::class); + $aggregateResolver = $this->createMock(AggregateResolver::class); /* @var $mapResolver1 TemplateMapResolver|\PHPUnit_Framework_MockObject_MockObject */ - $mapResolver1 = $this->getMock(TemplateMapResolver::class); + $mapResolver1 = $this->createMock(TemplateMapResolver::class); $mapResolver1 - ->expects($this->any()) + ->expects(self::any()) ->method('getMap') - ->will($this->returnValue(['a' => 'a-value', 'b' => 'b-value'])); + ->will(self::returnValue(['a' => 'a-value', 'b' => 'b-value'])); /* @var $mapResolver2 TemplateMapResolver|\PHPUnit_Framework_MockObject_MockObject */ - $mapResolver2 = $this->getMock(TemplateMapResolver::class); + $mapResolver2 = $this->createMock(TemplateMapResolver::class); $mapResolver2 - ->expects($this->any()) + ->expects(self::any()) ->method('getMap') - ->will($this->returnValue(['c' => 'c-value', 'd' => 'd-value'])); + ->will(self::returnValue(['c' => 'c-value', 'd' => 'd-value'])); /* @var $mapResolver3 TemplateMapResolver|\PHPUnit_Framework_MockObject_MockObject */ - $mapResolver3 = $this->getMock(TemplateMapResolver::class); + $mapResolver3 = $this->createMock(TemplateMapResolver::class); $mapResolver3 - ->expects($this->any()) + ->expects(self::any()) ->method('getMap') - ->will($this->returnValue(['a' => 'override-a-value', 'd' => 'override-d-value', 'e' => 'e-value'])); + ->will(self::returnValue(['a' => 'override-a-value', 'd' => 'override-d-value', 'e' => 'e-value'])); $iterator = new ArrayIterator([$mapResolver1, $mapResolver2, $mapResolver3]); $aggregateResolver - ->expects($this->any()) + ->expects(self::any()) ->method('getIterator') - ->will($this->returnValue($iterator)); + ->will(self::returnValue($iterator)); $map = $this->compiler->compileMap($aggregateResolver); - $this->assertCount(5, $map); - $this->assertSame('a-value', $map['a']); // should not be overridden - $this->assertSame('b-value', $map['b']); - $this->assertSame('c-value', $map['c']); - $this->assertSame('d-value', $map['d']); // should not be overridden - $this->assertSame('e-value', $map['e']); + self::assertCount(5, $map); + self::assertSame('a-value', $map['a']); // should not be overridden + self::assertSame('b-value', $map['b']); + self::assertSame('c-value', $map['c']); + self::assertSame('d-value', $map['d']); // should not be overridden + self::assertSame('e-value', $map['e']); } } diff --git a/tests/OcraCachedViewResolverTest/Factory/CacheFactoryTest.php b/tests/OcraCachedViewResolverTest/Factory/CacheFactoryTest.php index cb449fc..edeeb87 100644 --- a/tests/OcraCachedViewResolverTest/Factory/CacheFactoryTest.php +++ b/tests/OcraCachedViewResolverTest/Factory/CacheFactoryTest.php @@ -18,11 +18,11 @@ namespace OcraCachedViewResolverTest\View\Resolver; +use Interop\Container\ContainerInterface; use OcraCachedViewResolver\Factory\CacheFactory; use OcraCachedViewResolver\Module; use PHPUnit_Framework_TestCase; use Zend\Cache\Storage\Adapter\Memory; -use Zend\ServiceManager\ServiceLocatorInterface; /** * Tests for {@see \OcraCachedViewResolver\Factory\CacheFactory} @@ -38,10 +38,10 @@ class CacheFactoryTest extends PHPUnit_Framework_TestCase { public function testCreateService() { - /* @var $locator ServiceLocatorInterface|\PHPUnit_Framework_MockObject_MockObject */ - $locator = $this->getMock(ServiceLocatorInterface::class); + /* @var $locator ContainerInterface|\PHPUnit_Framework_MockObject_MockObject */ + $locator = $this->createMock(ContainerInterface::class); - $locator->expects($this->any())->method('get')->with('Config')->will($this->returnValue([ + $locator->expects(self::any())->method('get')->with('Config')->will(self::returnValue([ Module::CONFIG => [ Module::CONFIG_CACHE_DEFINITION => [ 'adapter' => Memory::class, @@ -49,6 +49,6 @@ public function testCreateService() ], ])); - $this->assertInstanceOf(Memory::class, (new CacheFactory())->createService($locator)); + self::assertInstanceOf(Memory::class, (new CacheFactory())->__invoke($locator)); } } diff --git a/tests/OcraCachedViewResolverTest/Factory/CompiledMapResolverDelegatorFactoryTest.php b/tests/OcraCachedViewResolverTest/Factory/CompiledMapResolverDelegatorFactoryTest.php index d30f086..6b0dadd 100644 --- a/tests/OcraCachedViewResolverTest/Factory/CompiledMapResolverDelegatorFactoryTest.php +++ b/tests/OcraCachedViewResolverTest/Factory/CompiledMapResolverDelegatorFactoryTest.php @@ -18,6 +18,7 @@ namespace OcraCachedViewResolverTest\View\Resolver; +use Interop\Container\ContainerInterface; use OcraCachedViewResolver\Factory\CompiledMapResolverDelegatorFactory; use OcraCachedViewResolver\Module; use OcraCachedViewResolver\View\Resolver\CachingMapResolver; @@ -25,7 +26,6 @@ use PHPUnit_Framework_TestCase; use stdClass; use Zend\Cache\Storage\StorageInterface; -use Zend\ServiceManager\ServiceLocatorInterface; use Zend\View\Resolver\AggregateResolver; use Zend\View\Resolver\TemplateMapResolver; @@ -42,7 +42,7 @@ class CompiledMapResolverDelegatorFactoryTest extends PHPUnit_Framework_TestCase { /** - * @var ServiceLocatorInterface|\PHPUnit_Framework_MockObject_MockObject + * @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $locator; @@ -58,14 +58,16 @@ class CompiledMapResolverDelegatorFactoryTest extends PHPUnit_Framework_TestCase /** * {@inheritDoc} + * + * @throws \PHPUnit_Framework_Exception */ protected function setUp() { - $this->locator = $this->getMock(ServiceLocatorInterface::class); - $this->callback = $this->getMock(stdClass::class, ['__invoke']); - $this->cache = $this->getMock(StorageInterface::class); + $this->locator = $this->createMock(ContainerInterface::class); + $this->callback = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock(); + $this->cache = $this->createMock(StorageInterface::class); - $this->locator->expects($this->any())->method('get')->will($this->returnValueMap([ + $this->locator->expects(self::any())->method('get')->will(self::returnValueMap([ [ 'Config', [ @@ -86,44 +88,44 @@ public function testCreateServiceWithExistingCachedTemplateMap() { $this ->cache - ->expects($this->once()) + ->expects(self::once()) ->method('getItem') ->with('key-name') - ->will($this->returnValue(['foo' => 'bar'])); + ->will(self::returnValue(['foo' => 'bar'])); - $this->callback->expects($this->never())->method('__invoke'); + $this->callback->expects(self::never())->method('__invoke'); $factory = new CompiledMapResolverDelegatorFactory(); - $resolver = $factory->createDelegatorWithName($this->locator, 'resolver', 'resolver', $this->callback); + $resolver = $factory->__invoke($this->locator, 'resolver', $this->callback); - $this->assertInstanceOf(AggregateResolver::class, $resolver); + self::assertInstanceOf(AggregateResolver::class, $resolver); $resolvers = $resolver->getIterator()->toArray(); - $this->assertInstanceOf(LazyResolver::class, $resolvers[0]); - $this->assertInstanceOf(CachingMapResolver::class, $resolvers[1]); + self::assertInstanceOf(LazyResolver::class, $resolvers[0]); + self::assertInstanceOf(CachingMapResolver::class, $resolvers[1]); - $this->assertSame('bar', $resolver->resolve('foo')); + self::assertSame('bar', $resolver->resolve('foo')); } public function testCreateServiceWithEmptyCachedTemplateMap() { $realResolver = new TemplateMapResolver(['bar' => 'baz']); - $this->cache->expects($this->once())->method('getItem')->with('key-name')->will($this->returnValue(null)); - $this->cache->expects($this->once())->method('setItem')->with('key-name', ['bar' => 'baz']); - $this->callback->expects($this->once())->method('__invoke')->will($this->returnValue($realResolver)); + $this->cache->expects(self::once())->method('getItem')->with('key-name')->will(self::returnValue(null)); + $this->cache->expects(self::once())->method('setItem')->with('key-name', ['bar' => 'baz']); + $this->callback->expects(self::once())->method('__invoke')->will(self::returnValue($realResolver)); $factory = new CompiledMapResolverDelegatorFactory(); - $resolver = $factory->createDelegatorWithName($this->locator, 'resolver', 'resolver', $this->callback); + $resolver = $factory->__invoke($this->locator, 'resolver', $this->callback); - $this->assertInstanceOf(AggregateResolver::class, $resolver); + self::assertInstanceOf(AggregateResolver::class, $resolver); $resolvers = $resolver->getIterator()->toArray(); - $this->assertInstanceOf(LazyResolver::class, $resolvers[0]); - $this->assertInstanceOf(CachingMapResolver::class, $resolvers[1]); + self::assertInstanceOf(LazyResolver::class, $resolvers[0]); + self::assertInstanceOf(CachingMapResolver::class, $resolvers[1]); - $this->assertSame('baz', $resolver->resolve('bar')); + self::assertSame('baz', $resolver->resolve('bar')); } } diff --git a/tests/OcraCachedViewResolverTest/ModuleFunctionalTest.php b/tests/OcraCachedViewResolverTest/ModuleFunctionalTest.php index 3722fd9..9b92755 100644 --- a/tests/OcraCachedViewResolverTest/ModuleFunctionalTest.php +++ b/tests/OcraCachedViewResolverTest/ModuleFunctionalTest.php @@ -18,11 +18,14 @@ namespace OcraCachedViewResolverTest; +use Interop\Container\Exception\ContainerException; +use Interop\Container\Exception\NotFoundException; use OcraCachedViewResolver\View\Resolver\CachingMapResolver; use OcraCachedViewResolver\View\Resolver\LazyResolver; use PHPUnit_Framework_TestCase; -use Zend\ServiceManager\ServiceManager; +use Zend\Cache\Storage\StorageInterface; use Zend\Mvc\Service\ServiceManagerConfig; +use Zend\ServiceManager\ServiceManager; use Zend\View\Resolver\AggregateResolver; use Zend\View\Resolver\ResolverInterface; use Zend\View\Resolver\TemplateMapResolver; @@ -56,16 +59,25 @@ class ModuleFunctionalTest extends PHPUnit_Framework_TestCase /** * {@inheritDoc} + * + * @throws \PHPUnit_Framework_Exception + * @throws ContainerException + * @throws NotFoundException */ public function setUp() { - $this->serviceManager = new ServiceManager(new ServiceManagerConfig()); + $this->serviceManager = new ServiceManager(); + + (new ServiceManagerConfig())->configureServiceManager($this->serviceManager); $this->serviceManager->setAllowOverride(true); $this->serviceManager->setService( 'ApplicationConfig', [ - 'modules' => ['OcraCachedViewResolver'], + 'modules' => [ + 'Zend\Router', + 'OcraCachedViewResolver', + ], 'module_listener_options' => [ 'config_glob_paths' => [ __DIR__ . '/../testing.config.php', @@ -80,10 +92,10 @@ public function setUp() $this->originalResolver = new AggregateResolver(); /* @var $mapResolver TemplateMapResolver|\PHPUnit_Framework_MockObject_MockObject */ - $mapResolver = $this->getMock(TemplateMapResolver::class); - $this->fallbackResolver = $this->getMock(ResolverInterface::class); + $mapResolver = $this->createMock(TemplateMapResolver::class); + $this->fallbackResolver = $this->createMock(ResolverInterface::class); - $mapResolver->expects($this->any())->method('getMap')->will($this->returnValue(['a' => 'b'])); + $mapResolver->expects(self::any())->method('getMap')->will(self::returnValue(['a' => 'b'])); $this->originalResolver->attach($mapResolver, 10); $this->originalResolver->attach($this->fallbackResolver, 5); @@ -99,23 +111,23 @@ function () use ($originalResolver) { public function testDefinedServices() { - $this->assertInstanceOf( - 'Zend\\Cache\\Storage\\StorageInterface', + self::assertInstanceOf( + StorageInterface::class, $this->serviceManager->get('OcraCachedViewResolver\\Cache\\ResolverCache') ); /* @var $resolver \Zend\View\Resolver\AggregateResolver */ $resolver = $this->serviceManager->get('ViewResolver'); - $this->assertInstanceOf(AggregateResolver::class, $resolver); - $this->assertSame($resolver, $this->serviceManager->get('ViewResolver')); + self::assertInstanceOf(AggregateResolver::class, $resolver); + self::assertSame($resolver, $this->serviceManager->get('ViewResolver')); foreach ($resolver->getIterator() as $previousResolver) { - $this->assertThat( + self::assertThat( $previousResolver, - $this->logicalOr( - $this->isInstanceOf(CachingMapResolver::class), - $this->isInstanceOf(LazyResolver::class) + self::logicalOr( + self::isInstanceOf(CachingMapResolver::class), + self::isInstanceOf(LazyResolver::class) ) ); } @@ -126,16 +138,16 @@ public function testCachesResolvedTemplates() /* @var $cache \Zend\Cache\Storage\StorageInterface */ $cache = $this->serviceManager->get('OcraCachedViewResolver\\Cache\\ResolverCache'); - $this->assertFalse($cache->hasItem('testing_cache_key')); + self::assertFalse($cache->hasItem('testing_cache_key')); /* @var $resolver AggregateResolver */ - $resolver = $this->serviceManager->create('ViewResolver'); + $resolver = $this->serviceManager->build('ViewResolver'); - $this->assertFalse($cache->hasItem('testing_cache_key')); - $this->assertSame('b', $resolver->resolve('a')); - $this->assertTrue($cache->hasItem('testing_cache_key')); - $this->assertSame(['a' => 'b'], $cache->getItem('testing_cache_key')); - $this->serviceManager->create('ViewResolver'); + self::assertFalse($cache->hasItem('testing_cache_key')); + self::assertSame('b', $resolver->resolve('a')); + self::assertTrue($cache->hasItem('testing_cache_key')); + self::assertSame(['a' => 'b'], $cache->getItem('testing_cache_key')); + $this->serviceManager->build('ViewResolver'); } public function testFallbackResolverCall() @@ -145,11 +157,11 @@ public function testFallbackResolverCall() $this ->fallbackResolver - ->expects($this->once()) + ->expects(self::once()) ->method('resolve') ->with('fallback.phtml') - ->will($this->returnValue('fallback-path.phtml')); + ->will(self::returnValue('fallback-path.phtml')); - $this->assertSame('fallback-path.phtml', $resolver->resolve('fallback.phtml')); + self::assertSame('fallback-path.phtml', $resolver->resolve('fallback.phtml')); } } diff --git a/tests/OcraCachedViewResolverTest/ModuleTest.php b/tests/OcraCachedViewResolverTest/ModuleTest.php index 21b4b9b..7e6354b 100644 --- a/tests/OcraCachedViewResolverTest/ModuleTest.php +++ b/tests/OcraCachedViewResolverTest/ModuleTest.php @@ -33,13 +33,13 @@ class ModuleTest extends PHPUnit_Framework_TestCase { public function testConfigIsAnArray() { - $this->assertInternalType('array', (new Module())->getConfig()); + self::assertInternalType('array', (new Module())->getConfig()); } public function testConfigIsSerializable() { $module = new Module(); - $this->assertSame($module->getConfig(), unserialize(serialize($module->getConfig()))); + self::assertSame($module->getConfig(), unserialize(serialize($module->getConfig()))); } } diff --git a/tests/OcraCachedViewResolverTest/View/Resolver/CachingMapResolverTest.php b/tests/OcraCachedViewResolverTest/View/Resolver/CachingMapResolverTest.php index f567eaf..9a15118 100644 --- a/tests/OcraCachedViewResolverTest/View/Resolver/CachingMapResolverTest.php +++ b/tests/OcraCachedViewResolverTest/View/Resolver/CachingMapResolverTest.php @@ -70,13 +70,15 @@ class CachingMapResolverTest extends PHPUnit_Framework_TestCase /** * {@inheritDoc} + * + * @throws \PHPUnit_Framework_Exception */ protected function setUp() { - $this->resolverInstantiator = $this->getMock(stdClass::class, ['__invoke']); - $this->realResolver = $this->getMock(TemplateMapResolver::class); - $this->renderer = $this->getMock(RendererInterface::class); - $this->cache = $this->getMock(StorageInterface::class); + $this->resolverInstantiator = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock(); + $this->realResolver = $this->createMock(TemplateMapResolver::class); + $this->renderer = $this->createMock(RendererInterface::class); + $this->cache = $this->createMock(StorageInterface::class); $this->cachingMapResolver = new CachingMapResolver( $this->cache, $this->cacheKey, @@ -85,70 +87,70 @@ protected function setUp() $this ->realResolver - ->expects($this->any()) + ->expects(self::any()) ->method('getMap') - ->will($this->returnValue(['view-name' => 'path/to/script'])); + ->will(self::returnValue(['view-name' => 'path/to/script'])); } public function testResolverCacheIsPopulatedOnResolve() { $this ->resolverInstantiator - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->returnValue($this->realResolver)); + ->will(self::returnValue($this->realResolver)); $this ->cache - ->expects($this->once()) + ->expects(self::once()) ->method('getItem') ->with($this->cacheKey); $this ->cache - ->expects($this->once()) + ->expects(self::once()) ->method('setItem') ->with($this->cacheKey, ['view-name' => 'path/to/script']); - $this->assertSame('path/to/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); + self::assertSame('path/to/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); } public function testResolvingMultipleTimesDoesNotHitResolverInstantiatorOrCache() { $this ->resolverInstantiator - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->returnValue($this->realResolver)); + ->will(self::returnValue($this->realResolver)); $this ->cache - ->expects($this->once()) + ->expects(self::once()) ->method('getItem') ->with($this->cacheKey); $this ->cache - ->expects($this->once()) + ->expects(self::once()) ->method('setItem') ->with($this->cacheKey, ['view-name' => 'path/to/script']); - $this->assertSame('path/to/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); - $this->assertSame('path/to/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); - $this->assertFalse($this->cachingMapResolver->resolve('unknown-view-name', $this->renderer)); + self::assertSame('path/to/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); + self::assertSame('path/to/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); + self::assertFalse($this->cachingMapResolver->resolve('unknown-view-name', $this->renderer)); } public function testResolvingWithNonEmptyCacheWillNotHitResolverInstantiatorOrWriteToCache() { - $this->resolverInstantiator->expects($this->never())->method('__invoke'); - $this->cache->expects($this->never())->method('setItem'); + $this->resolverInstantiator->expects(self::never())->method('__invoke'); + $this->cache->expects(self::never())->method('setItem'); $this ->cache - ->expects($this->once()) + ->expects(self::once()) ->method('getItem') ->with($this->cacheKey) - ->will($this->returnValue(['view-name' => 'path/to/cached/script'])); + ->will(self::returnValue(['view-name' => 'path/to/cached/script'])); - $this->assertSame('path/to/cached/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); - $this->assertSame('path/to/cached/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); - $this->assertFalse($this->cachingMapResolver->resolve('unknown-view-name', $this->renderer)); + self::assertSame('path/to/cached/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); + self::assertSame('path/to/cached/script', $this->cachingMapResolver->resolve('view-name', $this->renderer)); + self::assertFalse($this->cachingMapResolver->resolve('unknown-view-name', $this->renderer)); } /** @@ -158,17 +160,17 @@ public function testResolveWithoutRenderer() { $this ->resolverInstantiator - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->returnValue($this->realResolver)); + ->will(self::returnValue($this->realResolver)); $this ->realResolver - ->expects($this->any()) + ->expects(self::any()) ->method('resolve') ->with('view-name', null) - ->will($this->returnValue('path/to/script')); + ->will(self::returnValue('path/to/script')); - $this->assertSame('path/to/script', $this->cachingMapResolver->resolve('view-name')); + self::assertSame('path/to/script', $this->cachingMapResolver->resolve('view-name')); } /** @@ -178,13 +180,13 @@ public function testLazyResolverRefusesInvalidRealResolver() { $this ->resolverInstantiator - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->returnValue(null)); + ->will(self::returnValue(null)); $cachingMapResolver = new CachingMapResolver($this->cache, $this->cacheKey, $this->resolverInstantiator); - $this->setExpectedException(InvalidResolverInstantiatorException::class); + $this->expectException(InvalidResolverInstantiatorException::class); $cachingMapResolver->resolve('foo'); } diff --git a/tests/OcraCachedViewResolverTest/View/Resolver/Exception/InvalidResolverInstantiatorExceptionTest.php b/tests/OcraCachedViewResolverTest/View/Resolver/Exception/InvalidResolverInstantiatorExceptionTest.php index 5b99e96..c360ca3 100644 --- a/tests/OcraCachedViewResolverTest/View/Resolver/Exception/InvalidResolverInstantiatorExceptionTest.php +++ b/tests/OcraCachedViewResolverTest/View/Resolver/Exception/InvalidResolverInstantiatorExceptionTest.php @@ -34,7 +34,7 @@ class InvalidResolverInstantiatorExceptionTest extends PHPUnit_Framework_TestCas { public function testInstanceOfBaseExceptionInterface() { - $this->assertInstanceOf( + self::assertInstanceOf( InvalidResolverInstantiatorException::class, new InvalidResolverInstantiatorException() ); @@ -44,8 +44,8 @@ public function testFromInvalidNullInstantiator() { $exception = InvalidResolverInstantiatorException::fromInvalidInstantiator(null); - $this->assertInstanceOf(InvalidResolverInstantiatorException::class, $exception); - $this->assertSame( + self::assertInstanceOf(InvalidResolverInstantiatorException::class, $exception); + self::assertSame( 'Invalid instantiator given, expected `callable`, `NULL` given.', $exception->getMessage() ); @@ -55,8 +55,8 @@ public function testFromInvalidObjectInstantiator() { $exception = InvalidResolverInstantiatorException::fromInvalidInstantiator($this); - $this->assertInstanceOf(InvalidResolverInstantiatorException::class, $exception); - $this->assertSame( + self::assertInstanceOf(InvalidResolverInstantiatorException::class, $exception); + self::assertSame( 'Invalid instantiator given, expected `callable`, `' . __CLASS__ . '` given.', $exception->getMessage() ); @@ -66,8 +66,8 @@ public function testFromInvalidNullResolver() { $exception = InvalidResolverInstantiatorException::fromInvalidResolver(null); - $this->assertInstanceOf(InvalidResolverInstantiatorException::class, $exception); - $this->assertSame( + self::assertInstanceOf(InvalidResolverInstantiatorException::class, $exception); + self::assertSame( 'Invalid resolver found, expected `Zend\View\Resolver\ResolverInterface`, `NULL` given.', $exception->getMessage() ); @@ -77,8 +77,8 @@ public function testFromInvalidObjectResolver() { $exception = InvalidResolverInstantiatorException::fromInvalidResolver($this); - $this->assertInstanceOf(InvalidResolverInstantiatorException::class, $exception); - $this->assertSame( + self::assertInstanceOf(InvalidResolverInstantiatorException::class, $exception); + self::assertSame( 'Invalid resolver found, expected `Zend\View\Resolver\ResolverInterface`, `' . __CLASS__ . '` given.', $exception->getMessage() ); diff --git a/tests/OcraCachedViewResolverTest/View/Resolver/LazyResolverTest.php b/tests/OcraCachedViewResolverTest/View/Resolver/LazyResolverTest.php index 746116a..e795923 100644 --- a/tests/OcraCachedViewResolverTest/View/Resolver/LazyResolverTest.php +++ b/tests/OcraCachedViewResolverTest/View/Resolver/LazyResolverTest.php @@ -60,13 +60,13 @@ class LazyResolverTest extends PHPUnit_Framework_TestCase /** * {@inheritDoc} * - * @covers \OcraCachedViewResolver\View\Resolver\LazyResolver::__construct + * @throws \PHPUnit_Framework_Exception */ protected function setUp() { - $this->resolverInstantiator = $this->getMock(stdClass::class, ['__invoke']); - $this->realResolver = $this->getMock(ResolverInterface::class); - $this->renderer = $this->getMock(RendererInterface::class); + $this->resolverInstantiator = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock(); + $this->realResolver = $this->createMock(ResolverInterface::class); + $this->renderer = $this->createMock(RendererInterface::class); $this->lazyResolver = new LazyResolver($this->resolverInstantiator); } @@ -77,17 +77,17 @@ public function testResolve() { $this ->resolverInstantiator - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->returnValue($this->realResolver)); + ->will(self::returnValue($this->realResolver)); $this ->realResolver - ->expects($this->any()) + ->expects(self::any()) ->method('resolve') ->with('view-name', $this->renderer) - ->will($this->returnValue('path/to/script')); + ->will(self::returnValue('path/to/script')); - $this->assertSame('path/to/script', $this->lazyResolver->resolve('view-name', $this->renderer)); + self::assertSame('path/to/script', $this->lazyResolver->resolve('view-name', $this->renderer)); } /** @@ -97,17 +97,17 @@ public function testResolveWithoutRenderer() { $this ->resolverInstantiator - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->returnValue($this->realResolver)); + ->will(self::returnValue($this->realResolver)); $this ->realResolver - ->expects($this->any()) + ->expects(self::any()) ->method('resolve') ->with('view-name', null) - ->will($this->returnValue('path/to/script')); + ->will(self::returnValue('path/to/script')); - $this->assertSame('path/to/script', $this->lazyResolver->resolve('view-name')); + self::assertSame('path/to/script', $this->lazyResolver->resolve('view-name')); } /** @@ -115,7 +115,7 @@ public function testResolveWithoutRenderer() */ public function testRealResolverNotCreatedIfNotNeeded() { - $this->resolverInstantiator->expects($this->never())->method('__invoke'); + $this->resolverInstantiator->expects(self::never())->method('__invoke'); new LazyResolver($this->resolverInstantiator); } @@ -127,18 +127,18 @@ public function testResolveCausesRealResolverInstantiationOnlyOnce() { $this ->resolverInstantiator - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->returnValue($this->realResolver)); + ->will(self::returnValue($this->realResolver)); $this ->realResolver - ->expects($this->exactly(2)) + ->expects(self::exactly(2)) ->method('resolve') ->with('view-name', $this->renderer) - ->will($this->returnValue('path/to/script')); + ->will(self::returnValue('path/to/script')); - $this->assertSame('path/to/script', $this->lazyResolver->resolve('view-name', $this->renderer)); - $this->assertSame('path/to/script', $this->lazyResolver->resolve('view-name', $this->renderer)); + self::assertSame('path/to/script', $this->lazyResolver->resolve('view-name', $this->renderer)); + self::assertSame('path/to/script', $this->lazyResolver->resolve('view-name', $this->renderer)); } /** @@ -146,7 +146,7 @@ public function testResolveCausesRealResolverInstantiationOnlyOnce() */ public function testLazyResolverRefusesNonCallableInstantiator() { - $this->setExpectedException(InvalidResolverInstantiatorException::class); + $this->expectException(InvalidResolverInstantiatorException::class); new LazyResolver($this); } @@ -158,13 +158,13 @@ public function testLazyResolverRefusesInvalidRealResolver() { $this ->resolverInstantiator - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->returnValue(null)); + ->will(self::returnValue(null)); $lazyResolver = new LazyResolver($this->resolverInstantiator); - $this->setExpectedException(InvalidResolverInstantiatorException::class); + $this->expectException(InvalidResolverInstantiatorException::class); $lazyResolver->resolve('foo'); }