From a0e4ace6a59477efe097bd132cea6caafbcc931e Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sat, 9 May 2015 17:22:38 -0400 Subject: [PATCH] Update Dispatcher --- src/Routing/Dispatcher.php | 12 ++++-- src/Routing/DispatcherInterface.php | 4 +- test/tests/unit/Routing/DispatcherTest.php | 50 ++++++++++++---------- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/Routing/Dispatcher.php b/src/Routing/Dispatcher.php index eb38834..5ccd1c3 100644 --- a/src/Routing/Dispatcher.php +++ b/src/Routing/Dispatcher.php @@ -11,16 +11,22 @@ class Dispatcher implements DispatcherInterface * @param $middleware * @param ServerRequestInterface $request * @param ResponseInterface $response + * @return ResponseInterface + * @throws \InvalidArgumentException $middleware is not a valid type. */ - public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface &$response) + public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next) { if (is_callable($middleware)) { - $middleware = $middleware($request, $response); + $middleware = $middleware($request, $response, $next); } elseif (is_string($middleware)) { $middleware = new $middleware(); } if ($middleware instanceof MiddlewareInterface) { - $middleware->dispatch($request, $response); + return $middleware->dispatch($request, $response, $next); + } elseif ($middleware instanceof ResponseInterface) { + return $middleware; + } else { + throw new \InvalidArgumentException("Unable to dispatch middleware."); } } } diff --git a/src/Routing/DispatcherInterface.php b/src/Routing/DispatcherInterface.php index 1c50411..4fb5da9 100644 --- a/src/Routing/DispatcherInterface.php +++ b/src/Routing/DispatcherInterface.php @@ -16,6 +16,8 @@ interface DispatcherInterface * @param $middleware * @param ServerRequestInterface $request * @param ResponseInterface $response + * @param callable $next + * @return ResponseInterface */ - public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface &$response); + public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next); } diff --git a/test/tests/unit/Routing/DispatcherTest.php b/test/tests/unit/Routing/DispatcherTest.php index c2a156a..db32f90 100644 --- a/test/tests/unit/Routing/DispatcherTest.php +++ b/test/tests/unit/Routing/DispatcherTest.php @@ -15,30 +15,31 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { private $request; private $response; + private $next; public function setUp() { - $this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); - $this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); + $this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); + $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $this->response->withStatus(Argument::any())->will( function ($args) { $this->getStatusCode()->willReturn($args[0]); return $this; } ); + $this->next = function ($request, $response) { + return $response; + }; } - public function testDispatchesCallable() + public function testDispatchesCallableThatReturnsResponse() { - $middleware = function ($request, &$response) { - $response = $response->withStatus(200); + $middleware = function ($request, $response, $next) { + return $next($request, $response->withStatus(200)); }; $dispatcher = new Dispatcher(); - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $dispatcher->dispatch($middleware, $request, $response); - + $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); $this->assertEquals(200, $response->getStatusCode()); } @@ -49,22 +50,16 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase }; $dispatcher = new Dispatcher(); - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $dispatcher->dispatch($middleware, $request, $response); - + $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); $this->assertEquals(200, $response->getStatusCode()); } public function testDispatchesMiddlewareFromClassNameString() { - $middleware = __NAMESPACE__ . "\\DispatcherTest_Middleware"; + $middleware = __NAMESPACE__ . '\DispatcherTest_Middleware'; $dispatcher = new Dispatcher(); - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $dispatcher->dispatch($middleware, $request, $response); - + $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); $this->assertEquals(200, $response->getStatusCode()); } @@ -73,18 +68,27 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase $middleware = new DispatcherTest_Middleware(); $dispatcher = new Dispatcher(); - $request = $this->request->reveal(); - $response = $this->response->reveal(); - $dispatcher->dispatch($middleware, $request, $response); - + $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); $this->assertEquals(200, $response->getStatusCode()); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testThrowExceptionWhenUnableToDispatch() + { + $middleware = null; + + $dispatcher = new Dispatcher(); + $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); + } } class DispatcherTest_Middleware implements MiddlewareInterface { - public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) + public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next) { $response = $response->withStatus(200); + return $next($request, $response); } }