Update Dispatcher

This commit is contained in:
PJ Dietz 2015-05-09 17:22:38 -04:00
parent 8874827524
commit a0e4ace6a5
3 changed files with 39 additions and 27 deletions

View File

@ -11,16 +11,22 @@ class Dispatcher implements DispatcherInterface
* @param $middleware * @param $middleware
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param ResponseInterface $response * @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)) { if (is_callable($middleware)) {
$middleware = $middleware($request, $response); $middleware = $middleware($request, $response, $next);
} elseif (is_string($middleware)) { } elseif (is_string($middleware)) {
$middleware = new $middleware(); $middleware = new $middleware();
} }
if ($middleware instanceof MiddlewareInterface) { 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.");
} }
} }
} }

View File

@ -16,6 +16,8 @@ interface DispatcherInterface
* @param $middleware * @param $middleware
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param ResponseInterface $response * @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);
} }

View File

@ -15,30 +15,31 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
{ {
private $request; private $request;
private $response; private $response;
private $next;
public function setUp() public function setUp()
{ {
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); $this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
$this->response->withStatus(Argument::any())->will( $this->response->withStatus(Argument::any())->will(
function ($args) { function ($args) {
$this->getStatusCode()->willReturn($args[0]); $this->getStatusCode()->willReturn($args[0]);
return $this; return $this;
} }
); );
$this->next = function ($request, $response) {
return $response;
};
} }
public function testDispatchesCallable() public function testDispatchesCallableThatReturnsResponse()
{ {
$middleware = function ($request, &$response) { $middleware = function ($request, $response, $next) {
$response = $response->withStatus(200); return $next($request, $response->withStatus(200));
}; };
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$request = $this->request->reveal(); $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next);
$response = $this->response->reveal();
$dispatcher->dispatch($middleware, $request, $response);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
@ -49,22 +50,16 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
}; };
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$request = $this->request->reveal(); $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next);
$response = $this->response->reveal();
$dispatcher->dispatch($middleware, $request, $response);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
public function testDispatchesMiddlewareFromClassNameString() public function testDispatchesMiddlewareFromClassNameString()
{ {
$middleware = __NAMESPACE__ . "\\DispatcherTest_Middleware"; $middleware = __NAMESPACE__ . '\DispatcherTest_Middleware';
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$request = $this->request->reveal(); $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next);
$response = $this->response->reveal();
$dispatcher->dispatch($middleware, $request, $response);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
@ -73,18 +68,27 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
$middleware = new DispatcherTest_Middleware(); $middleware = new DispatcherTest_Middleware();
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$request = $this->request->reveal(); $response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next);
$response = $this->response->reveal();
$dispatcher->dispatch($middleware, $request, $response);
$this->assertEquals(200, $response->getStatusCode()); $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 class DispatcherTest_Middleware implements MiddlewareInterface
{ {
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
{ {
$response = $response->withStatus(200); $response = $response->withStatus(200);
return $next($request, $response);
} }
} }