From 2375206a09910f44783ec8fa33a5d67d77144660 Mon Sep 17 00:00:00 2001 From: l0gicgate Date: Tue, 20 Aug 2019 09:59:44 -0600 Subject: [PATCH] add unit test coverage for MiddlewareDispatcherInterface --- tests/AppTest.php | 25 +++++++++++++++++++++++++ tests/Factory/AppFactoryTest.php | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/tests/AppTest.php b/tests/AppTest.php index a9c22cfcc..899377ac0 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -25,6 +25,7 @@ use Slim\Exception\HttpNotFoundException; use Slim\Handlers\Strategies\RequestResponseArgs; use Slim\Interfaces\CallableResolverInterface; +use Slim\Interfaces\MiddlewareDispatcherInterface; use Slim\Interfaces\RouteCollectorInterface; use Slim\Interfaces\RouteCollectorProxyInterface; use Slim\Interfaces\RouteParserInterface; @@ -118,6 +119,30 @@ public function testCreatesRouteCollectorWhenNullWithInjectedContainer() $this->assertEquals($routeCollector, $app->getRouteCollector()); } + public function testGetMiddlewareDispatcherReturnsInjectedInstance() + { + $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); + $middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class); + + $app = new App($responseFactoryProphecy->reveal(), null, null, null, null, $middlewareDispatcherProphecy->reveal()); + + $this->assertSame($middlewareDispatcherProphecy->reveal(), $app->getMiddlewareDispatcher()); + } + + public function testMiddlewareDispatcherGetsSeededWithInjectedInstance() + { + $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); + + $middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class); + $middlewareDispatcherProphecy + ->seedMiddlewareStack(Argument::any()) + ->shouldBeCalledOnce(); + + $app = new App($responseFactoryProphecy->reveal(), null, null, null, null, $middlewareDispatcherProphecy->reveal()); + + $this->assertSame($middlewareDispatcherProphecy->reveal(), $app->getMiddlewareDispatcher()); + } + public function lowerCaseRequestMethodsProvider() { return [ diff --git a/tests/Factory/AppFactoryTest.php b/tests/Factory/AppFactoryTest.php index d490ff720..29211e3f7 100644 --- a/tests/Factory/AppFactoryTest.php +++ b/tests/Factory/AppFactoryTest.php @@ -26,6 +26,7 @@ use Slim\Http\Factory\DecoratedResponseFactory; use Slim\Http\Response as DecoratedResponse; use Slim\Interfaces\CallableResolverInterface; +use Slim\Interfaces\MiddlewareDispatcherInterface; use Slim\Interfaces\RouteCollectorInterface; use Slim\Interfaces\RouteParserInterface; use Slim\Interfaces\RouteResolverInterface; @@ -117,6 +118,7 @@ public function testAppIsCreatedWithInstancesFromSetters() $routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class); $routeParserProphecy = $this->prophesize(RouteParserInterface::class); $routeResolverProphecy = $this->prophesize(RouteResolverInterface::class); + $middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class); $routeCollectorProphecy->getRouteParser()->willReturn($routeParserProphecy); @@ -126,6 +128,7 @@ public function testAppIsCreatedWithInstancesFromSetters() AppFactory::setCallableResolver($callableResolverProphecy->reveal()); AppFactory::setRouteCollector($routeCollectorProphecy->reveal()); AppFactory::setRouteResolver($routeResolverProphecy->reveal()); + AppFactory::setMiddlewareDispatcher($middlewareDispatcherProphecy->reveal()); $app = AppFactory::create(); @@ -153,6 +156,11 @@ public function testAppIsCreatedWithInstancesFromSetters() $routeResolverProphecy->reveal(), $app->getRouteResolver() ); + + $this->assertSame( + $middlewareDispatcherProphecy->reveal(), + $app->getMiddlewareDispatcher() + ); } public function testAppIsCreatedWithInjectedInstancesFromFunctionArguments() @@ -242,6 +250,8 @@ public function testCreateAppWithContainerUsesContainerDependenciesWhenPresent() ->willReturn($routeParserProphecy->reveal()) ->shouldBeCalledOnce(); + $middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class); + $containerProphecy = $this->prophesize(ContainerInterface::class); $containerProphecy @@ -284,6 +294,16 @@ public function testCreateAppWithContainerUsesContainerDependenciesWhenPresent() ->willReturn($routeResolverProphecy->reveal()) ->shouldBeCalledOnce(); + $containerProphecy + ->has(MiddlewareDispatcherInterface::class) + ->willReturn(true) + ->shouldBeCalledOnce(); + + $containerProphecy + ->get(MiddlewareDispatcherInterface::class) + ->willReturn($middlewareDispatcherProphecy->reveal()) + ->shouldBeCalledOnce(); + AppFactory::setSlimHttpDecoratorsAutomaticDetection(false); $app = AppFactory::createFromContainer($containerProphecy->reveal()); @@ -292,5 +312,6 @@ public function testCreateAppWithContainerUsesContainerDependenciesWhenPresent() $this->assertSame($app->getCallableResolver(), $callableResolverProphecy->reveal()); $this->assertSame($app->getRouteCollector(), $routeCollectorProphecy->reveal()); $this->assertSame($app->getRouteResolver(), $routeResolverProphecy->reveal()); + $this->assertSame($app->getMiddlewareDispatcher(), $middlewareDispatcherProphecy->reveal()); } }