Skip to content

Commit

Permalink
add unit test coverage for MiddlewareDispatcherInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
l0gicgate committed Aug 20, 2019
1 parent c5f31e8 commit 2375206
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/AppTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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 [
Expand Down
21 changes: 21 additions & 0 deletions tests/Factory/AppFactoryTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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();

Expand Down Expand Up @@ -153,6 +156,11 @@ public function testAppIsCreatedWithInstancesFromSetters()
$routeResolverProphecy->reveal(),
$app->getRouteResolver()
);

$this->assertSame(
$middlewareDispatcherProphecy->reveal(),
$app->getMiddlewareDispatcher()
);
}

public function testAppIsCreatedWithInjectedInstancesFromFunctionArguments()
Expand Down Expand Up @@ -242,6 +250,8 @@ public function testCreateAppWithContainerUsesContainerDependenciesWhenPresent()
->willReturn($routeParserProphecy->reveal())
->shouldBeCalledOnce();

$middlewareDispatcherProphecy = $this->prophesize(MiddlewareDispatcherInterface::class);

$containerProphecy = $this->prophesize(ContainerInterface::class);

$containerProphecy
Expand Down Expand Up @@ -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());

Expand All @@ -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());
}
}

0 comments on commit 2375206

Please sign in to comment.