Update MethodMap to match new MiddlewareInterface

This commit is contained in:
PJ Dietz 2015-05-09 17:53:25 -04:00
parent a0e4ace6a5
commit d8352e71d9
2 changed files with 56 additions and 91 deletions

View File

@ -53,26 +53,29 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
// ------------------------------------------------------------------------
// MiddlewareInterface
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
* @return ResponseInterface
*/
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
{
$method = $request->getMethod();
// Dispatch middleware registered with the explicitly matching method.
if (isset($this->map[$method])) {
$middleware = $this->map[$method];
$this->dispatchMiddleware($middleware, $request, $response);
return;
return $this->dispatchMiddleware($middleware, $request, $response, $next);
}
// For HEAD, dispatch GET by default.
if ($method === "HEAD" && isset($this->map["GET"])) {
$middleware = $this->map["GET"];
$this->dispatchMiddleware($middleware, $request, $response);
return;
return $this->dispatchMiddleware($middleware, $request, $response, $next);
}
// Dispatch * middleware, if registered.
if (isset($this->map["*"])) {
$middleware = $this->map["*"];
$this->dispatchMiddleware($middleware, $request, $response);
return;
return $this->dispatchMiddleware($middleware, $request, $response, $next);
}
// Respond describing the allowed methods, either as a 405 response or
// in response to an OPTIONS request.
@ -81,15 +84,15 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
} else {
$response = $response->withStatus(405);
}
$this->addAllowHeader($response);
return $this->addAllowHeader($response);
}
// ------------------------------------------------------------------------
protected function addAllowHeader(ResponseInterface &$response)
protected function addAllowHeader(ResponseInterface $response)
{
$methods = join(",", $this->getAllowedMethods());
$response = $response->withHeader("Allow", $methods);
return $response->withHeader("Allow", $methods);
}
protected function getAllowedMethods()
@ -117,9 +120,16 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
return new Dispatcher();
}
private function dispatchMiddleware($middleware, ServerRequestInterface $request, ResponseInterface &$response)
/**
* @param $middleware
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param $next
* @return ResponseInterface
*/
private function dispatchMiddleware($middleware, ServerRequestInterface $request, ResponseInterface &$response, $next)
{
$dispatcher = $this->getDispatcher();
$dispatcher->dispatch($middleware, $request, $response);
return $dispatcher->dispatch($middleware, $request, $response, $next);
}
}

View File

@ -14,13 +14,20 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
private $next;
private $middleware;
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())->willReturn($this->response->reveal());
$this->response->withHeader(Argument::cetera())->willReturn($this->response->reveal());
$this->next = function ($request, $response) {
return $response;
};
$this->middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$this->middleware->dispatch(Argument::cetera())->willReturn();
}
/**
@ -42,17 +49,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{
$this->request->getMethod()->willReturn("GET");
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middleware->dispatch(Argument::cetera())->willReturn();
$map = new MethodMap();
$map->setMethod("GET", $middleware->reveal());
$map->setMethod("GET", $this->middleware->reveal());
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$middleware->dispatch($request, $response)->shouldHaveBeenCalled();
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
}
/**
@ -71,12 +72,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$map = new MethodMap();
$map->setMethod("GET", $middlewareUpper->reveal());
$map->setMethod("get", $middlewareLower->reveal());
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$middlewareLower->dispatch($request, $response)->shouldHaveBeenCalled();
$middlewareLower->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
}
/**
@ -89,17 +87,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{
$this->request->getMethod()->willReturn("GET");
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middleware->dispatch(Argument::cetera())->willReturn();
$map = new MethodMap();
$map->setMethod("*", $middleware->reveal());
$map->setMethod("*", $this->middleware->reveal());
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$middleware->dispatch($request, $response)->shouldHaveBeenCalled();
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
}
/**
@ -109,17 +101,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{
$this->request->getMethod()->willReturn("HEAD");
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middleware->dispatch(Argument::cetera())->willReturn();
$map = new MethodMap();
$map->setMethod("GET", $middleware->reveal());
$map->setMethod("GET", $this->middleware->reveal());
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$middleware->dispatch($request, $response)->shouldHaveBeenCalled();
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
}
/*
@ -127,37 +113,26 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
*/
public function testRegistersMiddlewareForMultipleMethods()
{
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middleware->dispatch(Argument::cetera())->willReturn();
$map = new MethodMap();
$map->setMethod("GET,POST", $middleware->reveal());
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->setMethod("GET,POST", $this->middleware->reveal());
$this->request->getMethod()->willReturn("GET");
$map->dispatch($request, $response);
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->request->getMethod()->willReturn("POST");
$map->dispatch($request, $response);
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$middleware->dispatch($request, $response)->shouldHaveBeenCalledTimes(2);
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalledTimes(2);
}
public function testSettingNullUnregistersMiddleware()
{
$this->request->getMethod()->willReturn("POST");
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$map = new MethodMap();
$map->setMethod("POST", $middleware->reveal());
$map->setMethod("POST", $this->middleware->reveal());
$map->setMethod("POST", null);
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->response->withStatus(405)->shouldHaveBeenCalled();
}
@ -171,14 +146,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{
$this->request->getMethod()->willReturn("OPTIONS");
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$map = new MethodMap();
$map->setMethod("GET", $middleware->reveal());
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$map->setMethod("GET", $this->middleware->reveal());
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->response->withStatus(200)->shouldHaveBeenCalled();
}
@ -193,16 +163,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{
$this->request->getMethod()->willReturn("OPTIONS");
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$map = new MethodMap();
foreach ($methodsDeclared as $method) {
$map->setMethod($method, $middleware->reveal());
$map->setMethod($method, $this->middleware->reveal());
}
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$containsAllMethods = function ($headerValue) use ($methodsAllowed) {
foreach ($methodsAllowed as $method) {
@ -225,14 +190,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{
$this->request->getMethod()->willReturn("POST");
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$map = new MethodMap();
$map->setMethod("GET", $middleware->reveal());
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$map->setMethod("GET", $this->middleware->reveal());
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$this->response->withStatus(405)->shouldHaveBeenCalled();
}
@ -247,16 +207,11 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{
$this->request->getMethod()->willReturn("BAD");
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$map = new MethodMap();
foreach ($methodsDeclared as $method) {
$map->setMethod($method, $middleware->reveal());
$map->setMethod($method, $this->middleware->reveal());
}
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$containsAllMethods = function ($headerValue) use ($methodsAllowed) {
foreach ($methodsAllowed as $method) {