From 6b9621e9541567ecb7d7ac3643842227417eeb9f Mon Sep 17 00:00:00 2001 From: Maks3w Date: Mon, 23 Nov 2015 09:06:25 +0100 Subject: [PATCH 1/2] Removed $checkAbstractFactories flag from ServiceManager::has() Reverts 117b2195371eb8285bc63c443babc6bb520f58e5 Related: * https://github.com/zendframework/zend-servicemanager/pull/49 * https://github.com/zendframework/zend-expressive/pull/156 --- src/Application.php | 10 +++++----- test/ApplicationTest.php | 8 ++++---- test/Container/ApplicationFactoryTest.php | 10 +++++----- test/RouteMiddlewareTest.php | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Application.php b/src/Application.php index 585fbecb..cf367075 100644 --- a/src/Application.php +++ b/src/Application.php @@ -201,13 +201,13 @@ public function pipe($path, $middleware = null) { // Lazy-load middleware from the container when possible $container = $this->container; - if (null === $middleware && is_string($path) && $container && $container->has($path, true)) { + if (null === $middleware && is_string($path) && $container && $container->has($path)) { $middleware = $this->marshalLazyMiddlewareService($path, $container); $path = '/'; } elseif (is_string($middleware) && ! is_callable($middleware) && $container - && $container->has($middleware, true) + && $container->has($middleware) ) { $middleware = $this->marshalLazyMiddlewareService($middleware, $container); } elseif (null === $middleware && is_callable($path)) { @@ -263,13 +263,13 @@ public function pipeErrorHandler($path, $middleware = null) { // Lazy-load middleware from the container $container = $this->container; - if (null === $middleware && is_string($path) && $container && $container->has($path, true)) { + if (null === $middleware && is_string($path) && $container && $container->has($path)) { $middleware = $this->marshalLazyErrorMiddlewareService($path, $container); $path = '/'; } elseif (is_string($middleware) && ! is_callable($middleware) && $container - && $container->has($middleware, true) + && $container->has($middleware) ) { $middleware = $this->marshalLazyErrorMiddlewareService($middleware, $container); } elseif (null === $middleware && is_callable($path)) { @@ -542,7 +542,7 @@ private function checkForDuplicateRoute($path, $methods = null) private function marshalMiddlewareFromContainer($middleware) { $container = $this->container; - if (! $container || ! $container->has($middleware, true)) { + if (! $container || ! $container->has($middleware)) { return $middleware; } diff --git a/test/ApplicationTest.php b/test/ApplicationTest.php index a7be9f5f..9c3cc8ec 100644 --- a/test/ApplicationTest.php +++ b/test/ApplicationTest.php @@ -418,7 +418,7 @@ public function testPipingAllowsPassingMiddlewareServiceNameAsSoleArgument() }; $container = $this->prophesize(ContainerInterface::class); - $container->has('foo', true)->willReturn(true); + $container->has('foo')->willReturn(true); $container->get('foo')->willReturn($middleware); $app = new Application($this->router->reveal(), $container->reveal()); @@ -445,7 +445,7 @@ public function testAllowsPipingErrorMiddlewareUsingServiceNameAsSoleArgument() }; $container = $this->prophesize(ContainerInterface::class); - $container->has('foo', true)->willReturn(true); + $container->has('foo')->willReturn(true); $container->get('foo')->willReturn($middleware); $app = new Application($this->router->reveal(), $container->reveal()); @@ -472,7 +472,7 @@ public function testAllowsPipingMiddlewareAsServiceNameWithPath() }; $container = $this->prophesize(ContainerInterface::class); - $container->has('foo', true)->willReturn(true); + $container->has('foo')->willReturn(true); $container->get('foo')->willReturn($middleware); $app = new Application($this->router->reveal(), $container->reveal()); @@ -499,7 +499,7 @@ public function testAllowsPipingErrorMiddlewareAsServiceNameWithPath() }; $container = $this->prophesize(ContainerInterface::class); - $container->has('foo', true)->willReturn(true); + $container->has('foo')->willReturn(true); $container->get('foo')->willReturn($middleware); $app = new Application($this->router->reveal(), $container->reveal()); diff --git a/test/Container/ApplicationFactoryTest.php b/test/Container/ApplicationFactoryTest.php index 16f64a11..7e6fe46a 100644 --- a/test/Container/ApplicationFactoryTest.php +++ b/test/Container/ApplicationFactoryTest.php @@ -448,7 +448,7 @@ public function testPipedMiddlewareAsServiceNamesAreReturnedAsClosuresThatPullFr ->willReturn($config); $this->container - ->has('Middleware', true) + ->has('Middleware') ->willReturn(true); $this->container @@ -551,7 +551,7 @@ public function testRaisesExceptionForNonCallableNonServiceMiddleware($middlewar ->willReturn($config); $this->container - ->has('/', true) + ->has('/') ->willReturn(false); $this->setExpectedException('InvalidArgumentException'); @@ -611,7 +611,7 @@ public function testRaisesExceptionForPipedMiddlewareServiceNamesNotFoundInConta ->willReturn($config); $this->container - ->has('Middleware', true) + ->has('Middleware') ->willReturn(false); $this->setExpectedException('InvalidArgumentException'); @@ -673,7 +673,7 @@ public function testRaisesExceptionOnInvocationOfUninvokableServiceSpecifiedMidd ->willReturn($config); $this->container - ->has('Middleware', true) + ->has('Middleware') ->willReturn(true); $this->container @@ -830,7 +830,7 @@ public function testCanMarkPipedMiddlewareServiceAsErrorMiddleware() ->willReturn($config); $this->container - ->has('Middleware', true) + ->has('Middleware') ->willReturn(true); $this->container diff --git a/test/RouteMiddlewareTest.php b/test/RouteMiddlewareTest.php index 6b132f84..c95bc86f 100644 --- a/test/RouteMiddlewareTest.php +++ b/test/RouteMiddlewareTest.php @@ -218,7 +218,7 @@ public function testRoutingSuccessResolvingToContainerMiddlewareCallsIt() $this->router->match($request)->willReturn($result); - $this->container->has('TestAsset\Middleware', true)->willReturn(true); + $this->container->has('TestAsset\Middleware')->willReturn(true); $this->container->get('TestAsset\Middleware')->willReturn($middleware); $app = $this->getApplication(); @@ -246,7 +246,7 @@ public function testRoutingSuccessResultingInContainerExceptionReRaisesException $this->router->match($request)->willReturn($result); - $this->container->has('TestAsset\Middleware', true)->willReturn(true); + $this->container->has('TestAsset\Middleware')->willReturn(true); $this->container->get('TestAsset\Middleware')->willThrow(new TestAsset\ContainerException()); $app = $this->getApplication(); From 0a82f3fb45c9534d6422ca2a45de5b9c4f92f998 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Mon, 23 Nov 2015 09:01:03 +0100 Subject: [PATCH 2/2] [test] Add helpers for deal with ContainerInterface mock --- test/ApplicationTest.php | 28 +- test/Container/ApplicationFactoryTest.php | 590 ++---------------- .../TemplatedErrorHandlerFactoryTest.php | 23 +- .../WhoopsErrorHandlerFactoryTest.php | 27 +- test/Container/WhoopsFactoryTest.php | 18 +- .../WhoopsPageHandlerFactoryTest.php | 26 +- test/ContainerTrait.php | 52 ++ test/RouteMiddlewareTest.php | 11 +- 8 files changed, 166 insertions(+), 609 deletions(-) create mode 100644 test/ContainerTrait.php diff --git a/test/ApplicationTest.php b/test/ApplicationTest.php index 9c3cc8ec..10de2952 100644 --- a/test/ApplicationTest.php +++ b/test/ApplicationTest.php @@ -9,7 +9,6 @@ namespace ZendTest\Expressive; -use Interop\Container\ContainerInterface; use PHPUnit_Framework_TestCase as TestCase; use Prophecy\Argument; use ReflectionProperty; @@ -26,6 +25,8 @@ */ class ApplicationTest extends TestCase { + use ContainerTrait; + public function setUp() { $this->noopMiddleware = function ($req, $res, $next) { @@ -377,9 +378,8 @@ public function testInvocationWillPipeRoutingMiddlewareIfNotAlreadyPiped() $this->router->match($request)->willReturn(RouteResult::fromRouteMatch('foo', 'foo', [])); - $container = $this->prophesize(ContainerInterface::class); - $container->has('foo')->willReturn(true); - $container->get('foo')->willReturn($middleware); + $container = $this->mockContainerInterface(); + $this->injectServiceInContainer($container, 'foo', $middleware); $app = new Application($this->router->reveal(), $container->reveal()); @@ -417,9 +417,8 @@ public function testPipingAllowsPassingMiddlewareServiceNameAsSoleArgument() return 'invoked'; }; - $container = $this->prophesize(ContainerInterface::class); - $container->has('foo')->willReturn(true); - $container->get('foo')->willReturn($middleware); + $container = $this->mockContainerInterface(); + $this->injectServiceInContainer($container, 'foo', $middleware); $app = new Application($this->router->reveal(), $container->reveal()); $app->pipe('foo'); @@ -444,9 +443,8 @@ public function testAllowsPipingErrorMiddlewareUsingServiceNameAsSoleArgument() return 'invoked'; }; - $container = $this->prophesize(ContainerInterface::class); - $container->has('foo')->willReturn(true); - $container->get('foo')->willReturn($middleware); + $container = $this->mockContainerInterface(); + $this->injectServiceInContainer($container, 'foo', $middleware); $app = new Application($this->router->reveal(), $container->reveal()); $app->pipeErrorHandler('foo'); @@ -471,9 +469,8 @@ public function testAllowsPipingMiddlewareAsServiceNameWithPath() return 'invoked'; }; - $container = $this->prophesize(ContainerInterface::class); - $container->has('foo')->willReturn(true); - $container->get('foo')->willReturn($middleware); + $container = $this->mockContainerInterface(); + $this->injectServiceInContainer($container, 'foo', $middleware); $app = new Application($this->router->reveal(), $container->reveal()); $app->pipe('/foo', 'foo'); @@ -498,9 +495,8 @@ public function testAllowsPipingErrorMiddlewareAsServiceNameWithPath() return 'invoked'; }; - $container = $this->prophesize(ContainerInterface::class); - $container->has('foo')->willReturn(true); - $container->get('foo')->willReturn($middleware); + $container = $this->mockContainerInterface(); + $this->injectServiceInContainer($container, 'foo', $middleware); $app = new Application($this->router->reveal(), $container->reveal()); $app->pipeErrorHandler('/foo', 'foo'); diff --git a/test/Container/ApplicationFactoryTest.php b/test/Container/ApplicationFactoryTest.php index 7e6fe46a..117a38a3 100644 --- a/test/Container/ApplicationFactoryTest.php +++ b/test/Container/ApplicationFactoryTest.php @@ -10,21 +10,48 @@ namespace ZendTest\Expressive\Container; use PHPUnit_Framework_TestCase as TestCase; +use Prophecy\Prophecy\ObjectProphecy; use ReflectionFunction; use ReflectionProperty; +use Zend\Diactoros\Response\EmitterInterface; use Zend\Expressive\Application; use Zend\Expressive\Container\ApplicationFactory; use Zend\Expressive\Router\Route; +use Zend\Expressive\Router\RouterInterface; +use ZendTest\Expressive\ContainerTrait; /** * @covers Zend\Expressive\Container\ApplicationFactory */ class ApplicationFactoryTest extends TestCase { + use ContainerTrait; + + /** @var ObjectProphecy */ + protected $container; + + /** @var ObjectProphecy */ + protected $emitter; + + /** @var ObjectProphecy */ + protected $finalHandler; + + /** @var ObjectProphecy */ + protected $router; + public function setUp() { - $this->container = $this->prophesize('Interop\Container\ContainerInterface'); + $this->container = $this->mockContainerInterface(); $this->factory = new ApplicationFactory(); + + $this->router = $this->prophesize(RouterInterface::class); + $this->emitter = $this->prophesize(EmitterInterface::class); + $this->finalHandler = function ($req, $res, $err = null) { + }; + + $this->injectServiceInContainer($this->container, RouterInterface::class, $this->router->reveal()); + $this->injectServiceInContainer($this->container, EmitterInterface::class, $this->emitter->reveal()); + $this->injectServiceInContainer($this->container, 'Zend\Expressive\FinalHandler', $this->finalHandler); } public function assertRoute($spec, array $routes) @@ -67,56 +94,17 @@ public function getRouterFromApplication(Application $app) public function testFactoryWillPullAllReplaceableDependenciesFromContainerWhenPresent() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(false); - $app = $this->factory->__invoke($this->container->reveal()); $this->assertInstanceOf('Zend\Expressive\Application', $app); $test = $this->getRouterFromApplication($app); - $this->assertSame($router->reveal(), $test); + $this->assertSame($this->router->reveal(), $test); $this->assertSame($this->container->reveal(), $app->getContainer()); - $this->assertSame($emitter->reveal(), $app->getEmitter()); - $this->assertSame($finalHandler, $app->getFinalHandler()); + $this->assertSame($this->emitter->reveal(), $app->getEmitter()); + $this->assertSame($this->finalHandler, $app->getFinalHandler()); } public function testFactorySetsUpRoutesFromConfig() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $config = [ 'routes' => [ [ @@ -132,38 +120,7 @@ public function testFactorySetsUpRoutesFromConfig() ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $app = $this->factory->__invoke($this->container->reveal()); @@ -178,36 +135,14 @@ public function testFactorySetsUpRoutesFromConfig() public function testWillUseSaneDefaultsForOptionalServices() { - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(false); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->shouldNotBeCalled(); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(false); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->shouldNotBeCalled(); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(false); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->shouldNotBeCalled(); - - $this->container - ->has('config') - ->willReturn(false); + $container = $this->mockContainerInterface(); + $factory = new ApplicationFactory(); - $app = $this->factory->__invoke($this->container->reveal()); + $app = $factory->__invoke($container->reveal()); $this->assertInstanceOf('Zend\Expressive\Application', $app); $router = $this->getRouterFromApplication($app); $this->assertInstanceOf('Zend\Expressive\Router\FastRouteRouter', $router); - $this->assertSame($this->container->reveal(), $app->getContainer()); + $this->assertSame($container->reveal(), $app->getContainer()); $this->assertInstanceOf('Zend\Expressive\Emitter\EmitterStack', $app->getEmitter()); $this->assertCount(1, $app->getEmitter()); $this->assertInstanceOf('Zend\Diactoros\Response\SapiEmitter', $app->getEmitter()->pop()); @@ -219,11 +154,6 @@ public function testWillUseSaneDefaultsForOptionalServices() */ public function testCanPipeMiddlewareProvidedDuringConfigurationPriorToSettingRoutes() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $middleware = function ($req, $res, $next = null) { }; @@ -244,38 +174,7 @@ public function testCanPipeMiddlewareProvidedDuringConfigurationPriorToSettingRo ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $app = $this->factory->__invoke($this->container->reveal()); @@ -306,11 +205,6 @@ public function testCanPipeMiddlewareProvidedDuringConfigurationPriorToSettingRo */ public function testCanPipeMiddlewareProvidedDuringConfigurationAfterSettingRoutes() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $middleware = function ($req, $res, $next = null) { return true; }; @@ -332,38 +226,7 @@ public function testCanPipeMiddlewareProvidedDuringConfigurationAfterSettingRout ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $app = $this->factory->__invoke($this->container->reveal()); @@ -396,11 +259,6 @@ public function testCanPipeMiddlewareProvidedDuringConfigurationAfterSettingRout */ public function testPipedMiddlewareAsServiceNamesAreReturnedAsClosuresThatPullFromContainer() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $middleware = function ($req, $res, $next = null) { return true; }; @@ -414,46 +272,8 @@ public function testPipedMiddlewareAsServiceNamesAreReturnedAsClosuresThatPullFr ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); - - $this->container - ->has('Middleware') - ->willReturn(true); - - $this->container - ->get('Middleware') - ->willReturn($middleware); + $this->injectServiceInContainer($this->container, 'config', $config); + $this->injectServiceInContainer($this->container, 'Middleware', $middleware); $app = $this->factory->__invoke($this->container->reveal()); @@ -503,11 +323,6 @@ public function uncallableMiddleware() */ public function testRaisesExceptionForNonCallableNonServiceMiddleware($middleware) { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $config = [ 'middleware_pipeline' => [ 'post_routing' => [ @@ -517,42 +332,7 @@ public function testRaisesExceptionForNonCallableNonServiceMiddleware($middlewar ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); - - $this->container - ->has('/') - ->willReturn(false); + $this->injectServiceInContainer($this->container, 'config', $config); $this->setExpectedException('InvalidArgumentException'); $app = $this->factory->__invoke($this->container->reveal()); @@ -563,11 +343,6 @@ public function testRaisesExceptionForNonCallableNonServiceMiddleware($middlewar */ public function testRaisesExceptionForPipedMiddlewareServiceNamesNotFoundInContainer() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $config = [ 'middleware_pipeline' => [ 'post_routing' => [ @@ -577,42 +352,7 @@ public function testRaisesExceptionForPipedMiddlewareServiceNamesNotFoundInConta ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); - - $this->container - ->has('Middleware') - ->willReturn(false); + $this->injectServiceInContainer($this->container, 'config', $config); $this->setExpectedException('InvalidArgumentException'); $app = $this->factory->__invoke($this->container->reveal()); @@ -623,11 +363,6 @@ public function testRaisesExceptionForPipedMiddlewareServiceNamesNotFoundInConta */ public function testRaisesExceptionOnInvocationOfUninvokableServiceSpecifiedMiddlewarePulledFromContainer() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $middleware = (object) []; $config = [ @@ -639,46 +374,8 @@ public function testRaisesExceptionOnInvocationOfUninvokableServiceSpecifiedMidd ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); - - $this->container - ->has('Middleware') - ->willReturn(true); - - $this->container - ->get('Middleware') - ->willReturn($middleware); + $this->injectServiceInContainer($this->container, 'config', $config); + $this->injectServiceInContainer($this->container, 'Middleware', $middleware); $app = $this->factory->__invoke($this->container->reveal()); @@ -716,11 +413,6 @@ public function testRaisesExceptionOnInvocationOfUninvokableServiceSpecifiedMidd public function testCanSpecifyRouteViaConfigurationWithNoMethods() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $config = [ 'routes' => [ [ @@ -730,38 +422,7 @@ public function testCanSpecifyRouteViaConfigurationWithNoMethods() ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $app = $this->factory->__invoke($this->container->reveal()); @@ -779,11 +440,6 @@ public function testCanSpecifyRouteViaConfigurationWithNoMethods() */ public function testCanMarkPipedMiddlewareServiceAsErrorMiddleware() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $middleware = function ($err, $req, $res, $next) { return true; }; @@ -796,46 +452,8 @@ public function testCanMarkPipedMiddlewareServiceAsErrorMiddleware() ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); - - $this->container - ->has('Middleware') - ->willReturn(true); - - $this->container - ->get('Middleware') - ->willReturn($middleware); + $this->injectServiceInContainer($this->container, 'config', $config); + $this->injectServiceInContainer($this->container, 'Middleware', $middleware); $app = $this->factory->__invoke($this->container->reveal()); @@ -865,11 +483,6 @@ public function testCanMarkPipedMiddlewareServiceAsErrorMiddleware() */ public function testWillPipeRoutingMiddlewareEvenIfNoRoutesAreRegistered() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $middleware = function ($req, $res, $next = null) { }; @@ -883,38 +496,7 @@ public function testWillPipeRoutingMiddlewareEvenIfNoRoutesAreRegistered() ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $app = $this->factory->__invoke($this->container->reveal()); @@ -942,11 +524,6 @@ public function testWillPipeRoutingMiddlewareEvenIfNoRoutesAreRegistered() public function testCanSpecifyRouteNamesViaConfiguration() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $config = [ 'routes' => [ [ @@ -957,38 +534,7 @@ public function testCanSpecifyRouteNamesViaConfiguration() ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $app = $this->factory->__invoke($this->container->reveal()); @@ -1003,11 +549,6 @@ public function testCanSpecifyRouteNamesViaConfiguration() public function testCanSpecifyRouteOptionsViaConfiguration() { - $router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface'); - $finalHandler = function ($req, $res, $err = null) { - }; - $expected = [ 'values' => [ 'foo' => 'bar' @@ -1028,38 +569,7 @@ public function testCanSpecifyRouteOptionsViaConfiguration() ], ]; - $this->container - ->has('Zend\Expressive\Router\RouterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\Router\RouterInterface') - ->will(function () use ($router) { - return $router->reveal(); - }); - - $this->container - ->has('Zend\Diactoros\Response\EmitterInterface') - ->willReturn(true); - $this->container - ->get('Zend\Diactoros\Response\EmitterInterface') - ->will(function () use ($emitter) { - return $emitter->reveal(); - }); - - $this->container - ->has('Zend\Expressive\FinalHandler') - ->willReturn(true); - $this->container - ->get('Zend\Expressive\FinalHandler') - ->willReturn($finalHandler); - - $this->container - ->has('config') - ->willReturn(true); - - $this->container - ->get('config') - ->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $app = $this->factory->__invoke($this->container->reveal()); diff --git a/test/Container/TemplatedErrorHandlerFactoryTest.php b/test/Container/TemplatedErrorHandlerFactoryTest.php index 52ac3e63..add2eec7 100644 --- a/test/Container/TemplatedErrorHandlerFactoryTest.php +++ b/test/Container/TemplatedErrorHandlerFactoryTest.php @@ -10,31 +10,30 @@ namespace ZendTest\Expressive\Container; use PHPUnit_Framework_TestCase as TestCase; +use Prophecy\Prophecy\ObjectProphecy; use Zend\Expressive\Container\TemplatedErrorHandlerFactory; use Zend\Expressive\Template\TemplateRendererInterface; use Zend\Expressive\TemplatedErrorHandler; +use ZendTest\Expressive\ContainerTrait; /** * @covers Zend\Expressive\Container\TemplatedErrorHandlerFactory */ class TemplatedErrorHandlerFactoryTest extends TestCase { - /** - * @var \Interop\Container\ContainerInterface - */ - private $container; + use ContainerTrait; + + /** @var ObjectProphecy */ + protected $container; public function setUp() { - $this->container = $this->prophesize('Interop\Container\ContainerInterface'); + $this->container = $this->mockContainerInterface(); $this->factory = new TemplatedErrorHandlerFactory(); } public function testReturnsATemplatedErrorHandler() { - $this->container->has(TemplateRendererInterface::class)->willReturn(false); - $this->container->has('config')->willReturn(false); - $factory = $this->factory; $result = $factory($this->container->reveal()); $this->assertInstanceOf(TemplatedErrorHandler::class, $result); @@ -43,9 +42,7 @@ public function testReturnsATemplatedErrorHandler() public function testWillInjectTemplateIntoErrorHandlerWhenServiceIsPresent() { $renderer = $this->prophesize(TemplateRendererInterface::class); - $this->container->has(TemplateRendererInterface::class)->willReturn(true); - $this->container->get(TemplateRendererInterface::class)->willReturn($renderer->reveal()); - $this->container->has('config')->willReturn(false); + $this->injectServiceInContainer($this->container, TemplateRendererInterface::class, $renderer->reveal()); $factory = $this->factory; $result = $factory($this->container->reveal()); @@ -59,9 +56,7 @@ public function testWillInjectTemplateNamesFromConfigurationWhenPresent() 'template_404' => 'error::404', 'template_error' => 'error::500', ]]]; - $this->container->has(TemplateRendererInterface::class)->willReturn(false); - $this->container->has('config')->willReturn(true); - $this->container->get('config')->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $factory = $this->factory; $result = $factory($this->container->reveal()); diff --git a/test/Container/WhoopsErrorHandlerFactoryTest.php b/test/Container/WhoopsErrorHandlerFactoryTest.php index e40d1a67..6c595e66 100644 --- a/test/Container/WhoopsErrorHandlerFactoryTest.php +++ b/test/Container/WhoopsErrorHandlerFactoryTest.php @@ -10,38 +10,37 @@ namespace ZendTest\Expressive\Container; use PHPUnit_Framework_TestCase as TestCase; +use Prophecy\Prophecy\ObjectProphecy; use Whoops\Handler\PrettyPageHandler; use Whoops\Run as Whoops; use Zend\Expressive\Container\WhoopsErrorHandlerFactory; use Zend\Expressive\Template\TemplateRendererInterface; use Zend\Expressive\WhoopsErrorHandler; +use ZendTest\Expressive\ContainerTrait; /** * @covers Zend\Expressive\Container\WhoopsErrorHandlerFactory */ class WhoopsErrorHandlerFactoryTest extends TestCase { - /** - * @var \Interop\Container\ContainerInterface - */ - private $container; + use ContainerTrait; + + /** @var ObjectProphecy */ + protected $container; public function setUp() { $whoops = $this->prophesize(Whoops::class); $pageHandler = $this->prophesize(PrettyPageHandler::class); - $this->container = $this->prophesize('Interop\Container\ContainerInterface'); - $this->container->get('Zend\Expressive\WhoopsPageHandler')->willReturn($pageHandler->reveal()); - $this->container->get('Zend\Expressive\Whoops')->willReturn($whoops->reveal()); + $this->container = $this->mockContainerInterface(); + $this->injectServiceInContainer($this->container, 'Zend\Expressive\WhoopsPageHandler', $pageHandler->reveal()); + $this->injectServiceInContainer($this->container, 'Zend\Expressive\Whoops', $whoops->reveal()); $this->factory = new WhoopsErrorHandlerFactory(); } public function testReturnsAWhoopsErrorHandler() { - $this->container->has(TemplateRendererInterface::class)->willReturn(false); - $this->container->has('config')->willReturn(false); - $factory = $this->factory; $result = $factory($this->container->reveal()); $this->assertInstanceOf(WhoopsErrorHandler::class, $result); @@ -50,9 +49,7 @@ public function testReturnsAWhoopsErrorHandler() public function testWillInjectTemplateIntoErrorHandlerWhenServiceIsPresent() { $renderer = $this->prophesize(TemplateRendererInterface::class); - $this->container->has(TemplateRendererInterface::class)->willReturn(true); - $this->container->get(TemplateRendererInterface::class)->willReturn($renderer->reveal()); - $this->container->has('config')->willReturn(false); + $this->injectServiceInContainer($this->container, TemplateRendererInterface::class, $renderer->reveal()); $factory = $this->factory; $result = $factory($this->container->reveal()); @@ -66,9 +63,7 @@ public function testWillInjectTemplateNamesFromConfigurationWhenPresent() 'template_404' => 'error::404', 'template_error' => 'error::500', ]]]; - $this->container->has(TemplateRendererInterface::class)->willReturn(false); - $this->container->has('config')->willReturn(true); - $this->container->get('config')->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $factory = $this->factory; $result = $factory($this->container->reveal()); diff --git a/test/Container/WhoopsFactoryTest.php b/test/Container/WhoopsFactoryTest.php index 73aabee4..9275e62f 100644 --- a/test/Container/WhoopsFactoryTest.php +++ b/test/Container/WhoopsFactoryTest.php @@ -10,23 +10,30 @@ namespace ZendTest\Expressive\Container; use PHPUnit_Framework_TestCase as TestCase; +use Prophecy\Prophecy\ObjectProphecy; use ReflectionFunction; use ReflectionProperty; use Whoops\Handler\JsonResponseHandler; use Whoops\Handler\PrettyPageHandler; use Whoops\Run as Whoops; use Zend\Expressive\Container\WhoopsFactory; +use ZendTest\Expressive\ContainerTrait; /** * @covers Zend\Expressive\Container\WhoopsFactory */ class WhoopsFactoryTest extends TestCase { + use ContainerTrait; + + /** @var ObjectProphecy */ + protected $container; + public function setUp() { $pageHandler = $this->prophesize(PrettyPageHandler::class); - $this->container = $this->prophesize('Interop\Container\ContainerInterface'); - $this->container->get('Zend\Expressive\WhoopsPageHandler')->willReturn($pageHandler->reveal()); + $this->container = $this->mockContainerInterface(); + $this->injectServiceInContainer($this->container, 'Zend\Expressive\WhoopsPageHandler', $pageHandler->reveal()); $this->factory = new WhoopsFactory(); } @@ -51,7 +58,6 @@ public function assertWhoopsContainsHandler($type, Whoops $whoops, $message = nu public function testReturnsAWhoopsRuntimeWithPageHandlerComposed() { - $this->container->has('config')->willReturn(false); $factory = $this->factory; $result = $factory($this->container->reveal()); $this->assertInstanceOf(Whoops::class, $result); @@ -61,8 +67,7 @@ public function testReturnsAWhoopsRuntimeWithPageHandlerComposed() public function testWillInjectJsonResponseHandlerIfConfigurationExpectsIt() { $config = ['whoops' => ['json_exceptions' => ['display' => true]]]; - $this->container->has('config')->willReturn(true); - $this->container->get('config')->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $factory = $this->factory; $result = $factory($this->container->reveal()); @@ -81,8 +86,7 @@ public function testJsonResponseHandlerCanBeConfigured() 'show_trace' => true, 'ajax_only' => true, ]]]; - $this->container->has('config')->willReturn(true); - $this->container->get('config')->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $factory = $this->factory; $whoops = $factory($this->container->reveal()); diff --git a/test/Container/WhoopsPageHandlerFactoryTest.php b/test/Container/WhoopsPageHandlerFactoryTest.php index f8cf2ba9..51eb803c 100644 --- a/test/Container/WhoopsPageHandlerFactoryTest.php +++ b/test/Container/WhoopsPageHandlerFactoryTest.php @@ -10,26 +10,32 @@ namespace ZendTest\Expressive\Container; use PHPUnit_Framework_TestCase as TestCase; +use Prophecy\Prophecy\ObjectProphecy; use ReflectionFunction; use ReflectionProperty; use Whoops\Handler\PrettyPageHandler; use Zend\Expressive\Container\Exception\InvalidServiceException; use Zend\Expressive\Container\WhoopsPageHandlerFactory; +use ZendTest\Expressive\ContainerTrait; /** * @covers Zend\Expressive\Container\WhoopsPageHandlerFactory */ class WhoopsPageHandlerFactoryTest extends TestCase { + use ContainerTrait; + + /** @var ObjectProphecy */ + protected $container; + public function setUp() { - $this->container = $this->prophesize('Interop\Container\ContainerInterface'); + $this->container = $this->mockContainerInterface(); $this->factory = new WhoopsPageHandlerFactory(); } public function testReturnsAPrettyPageHandler() { - $this->container->has('config')->willReturn(false); $factory = $this->factory; $result = $factory($this->container->reveal()); @@ -39,9 +45,7 @@ public function testReturnsAPrettyPageHandler() public function testWillInjectStringEditor() { $config = ['whoops' => ['editor' => 'emacs']]; - $this->container->has('config')->willReturn(true); - $this->container->get('config')->willReturn($config); - $this->container->has('emacs')->willReturn(false); + $this->injectServiceInContainer($this->container, 'config', $config); $factory = $this->factory; $result = $factory($this->container->reveal()); @@ -53,8 +57,7 @@ public function testWillInjectCallableEditor() { $config = ['whoops' => ['editor' => function () { }]]; - $this->container->has('config')->willReturn(true); - $this->container->get('config')->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $factory = $this->factory; $result = $factory($this->container->reveal()); @@ -67,10 +70,8 @@ public function testWillInjectEditorAsAService() $config = ['whoops' => ['editor' => 'custom']]; $editor = function () { }; - $this->container->has('config')->willReturn(true); - $this->container->get('config')->willReturn($config); - $this->container->has('custom')->willReturn(true); - $this->container->get('custom')->willReturn($editor); + $this->injectServiceInContainer($this->container, 'config', $config); + $this->injectServiceInContainer($this->container, 'custom', $editor); $factory = $this->factory; $result = $factory($this->container->reveal()); @@ -98,8 +99,7 @@ public function invalidEditors() public function testInvalidEditorWillRaiseException($editor) { $config = ['whoops' => ['editor' => $editor]]; - $this->container->has('config')->willReturn(true); - $this->container->get('config')->willReturn($config); + $this->injectServiceInContainer($this->container, 'config', $config); $factory = $this->factory; diff --git a/test/ContainerTrait.php b/test/ContainerTrait.php new file mode 100644 index 00000000..85e64c92 --- /dev/null +++ b/test/ContainerTrait.php @@ -0,0 +1,52 @@ +prophesize(ContainerInterface::class); + $container->has(Argument::type('string'))->willReturn(false); + + return $container; + } + + /** + * Inject a service into the container mock. + * + * Adjust `has('service')` and `get('service')` returns. + * + * @param ObjectProphecy $container + * @param string $serviceName + * @param mixed $service + * + * @return void + */ + protected function injectServiceInContainer(ObjectProphecy $container, $serviceName, $service) + { + $container->has($serviceName)->willReturn(true); + $container->get($serviceName)->willReturn($service); + } +} diff --git a/test/RouteMiddlewareTest.php b/test/RouteMiddlewareTest.php index c95bc86f..fa4b9cb5 100644 --- a/test/RouteMiddlewareTest.php +++ b/test/RouteMiddlewareTest.php @@ -10,6 +10,7 @@ namespace ZendTest\Expressive; use PHPUnit_Framework_TestCase as TestCase; +use Prophecy\Prophecy\ObjectProphecy; use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequest; use Zend\Expressive\Application; @@ -18,10 +19,15 @@ class RouteMiddlewareTest extends TestCase { + use ContainerTrait; + + /** @var ObjectProphecy */ + protected $container; + public function setUp() { $this->router = $this->prophesize('Zend\Expressive\Router\RouterInterface'); - $this->container = $this->prophesize('Interop\Container\ContainerInterface'); + $this->container = $this->mockContainerInterface(); } public function getApplication() @@ -218,8 +224,7 @@ public function testRoutingSuccessResolvingToContainerMiddlewareCallsIt() $this->router->match($request)->willReturn($result); - $this->container->has('TestAsset\Middleware')->willReturn(true); - $this->container->get('TestAsset\Middleware')->willReturn($middleware); + $this->injectServiceInContainer($this->container, 'TestAsset\Middleware', $middleware); $app = $this->getApplication();