Rework MethodMap

This commit is contained in:
PJ Dietz 2015-05-07 08:10:38 -04:00
parent 7cbbe6d7c5
commit ccbe8bb2e0
6 changed files with 187 additions and 50 deletions

View File

@ -9,34 +9,40 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
{ {
protected $map; protected $map;
/** // ------------------------------------------------------------------------
* @param array $map
*/ public function __construct()
public function __construct($map = null)
{ {
$this->map = []; $this->map = [];
if ($map) {
$this->addMap($map);
}
} }
/** // ------------------------------------------------------------------------
* @param array $map // MethodMapInterface
*/
public function addMap($map)
{
foreach ($map as $method => $middleware) {
$this->add($method, $middleware);
}
}
/** /**
* Register middleware with a method.
*
* $method may be:
* - A single verb ("GET"),
* - A comma-separated list of verbs ("GET,PUT,DELETE")
* - "*" to indicate any method.
*
* $middleware may be:
* - An instance implementing MiddlewareInterface
* - A string containing the fully qualified class name of a class
* implementing MiddlewareInterface
* - A callable that returns an instance implementing MiddleInterface
* - A callable maching the signature of MiddlewareInteraface::dispatch
* @see DispatchedInterface::dispatch
*
* $middleware may also be null, in which case any previously set
* middleware for that method or methods will be unset.
*
* @param string $method * @param string $method
* @param mixed $middleware * @param mixed $middleware
*/ */
public function add($method, $middleware) public function setMethod($method, $middleware)
{ {
$method = strtoupper($method);
$methods = explode(",", $method); $methods = explode(",", $method);
$methods = array_map("trim", $methods); $methods = array_map("trim", $methods);
foreach ($methods as $method) { foreach ($methods as $method) {
@ -44,23 +50,32 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
} }
} }
// ------------------------------------------------------------------------
// MiddlewareInterface
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
{ {
$method = strtoupper($request->getMethod()); $method = $request->getMethod();
// Dispatch middleware registered with the explicitly matching method. // Dispatch middleware registered with the explicitly matching method.
if (isset($this->map[$method])) { if (isset($this->map[$method])) {
$middleware = $this->map[$method]; $middleware = $this->map[$method];
$this->disptchMiddleware($middleware, $request, $response); $this->dispatchMiddleware($middleware, $request, $response);
return; return;
} }
// For HEAD, dispatch GET by default. // For HEAD, dispatch GET by default.
if ($method === "HEAD" && isset($this->map["GET"])) { if ($method === "HEAD" && isset($this->map["GET"])) {
$middleware = $this->map["GET"]; $middleware = $this->map["GET"];
$this->disptchMiddleware($middleware, $request, $response); $this->dispatchMiddleware($middleware, $request, $response);
return; return;
} }
// Method is not defined. Respond describing the allowed methods, // Dispatch * middleware, if registered.
// either as a 405 response or in response to an OPTIONS request. if (isset($this->map["*"])) {
$middleware = $this->map["*"];
$this->dispatchMiddleware($middleware, $request, $response);
return;
}
// Respond describing the allowed methods, either as a 405 response or
// in response to an OPTIONS request.
if ($method === "OPTIONS") { if ($method === "OPTIONS") {
$response = $response->withStatus(200); $response = $response->withStatus(200);
} else { } else {
@ -69,6 +84,8 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
$this->addAllowHeader($response); $this->addAllowHeader($response);
} }
// ------------------------------------------------------------------------
protected function addAllowHeader(ResponseInterface &$response) protected function addAllowHeader(ResponseInterface &$response)
{ {
$methods = join(",", $this->getAllowedMethods()); $methods = join(",", $this->getAllowedMethods());
@ -100,7 +117,7 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
return new Dispatcher(); return new Dispatcher();
} }
private function disptchMiddleware($middleware, ServerRequestInterface $request, ResponseInterface &$response) private function dispatchMiddleware($middleware, ServerRequestInterface $request, ResponseInterface &$response)
{ {
$dispatcher = $this->getDispatcher(); $dispatcher = $this->getDispatcher();
$dispatcher->dispatch($middleware, $request, $response); $dispatcher->dispatch($middleware, $request, $response);

View File

@ -2,16 +2,29 @@
namespace WellRESTed\Routing; namespace WellRESTed\Routing;
interface MethodMapInterface interface MethodMapInterface extends MiddlewareInterface
{ {
/** /**
* @param array $map * Register middleware with a method.
*/ *
public function addMap($map); * $method may be:
* - A single verb ("GET"),
/** * - A comma-separated list of verbs ("GET,PUT,DELETE")
* - "*" to indicate any method.
*
* $middleware may be:
* - An instance implementing MiddlewareInterface
* - A string containing the fully qualified class name of a class
* implementing MiddlewareInterface
* - A callable that returns an instance implementing MiddleInterface
* - A callable maching the signature of MiddlewareInteraface::dispatch
* @see DispatchedInterface::dispatch
*
* $middleware may also be null, in which case any previously set
* middleware for that method or methods will be unset.
*
* @param string $method * @param string $method
* @param mixed $middleware * @param mixed $middleware
*/ */
public function add($method, $middleware); public function setMethod($method, $middleware);
} }

View File

@ -14,7 +14,7 @@ class RouteMap implements RouteMapInterface
* - A single verb ("GET"), * - A single verb ("GET"),
* - A comma-separated list of verbs ("GET,PUT,DELETE") * - A comma-separated list of verbs ("GET,PUT,DELETE")
* - "*" to indicate any method. * - "*" to indicate any method.
* @see MethodMapInterface::addMethod * @see MethodMapInterface::setMethod
* *
* $target may be: * $target may be:
* - An exact path (e.g., "/path/") * - An exact path (e.g., "/path/")

View File

@ -11,7 +11,7 @@ interface RouteMapInterface extends MiddlewareInterface
* - A single verb ("GET"), * - A single verb ("GET"),
* - A comma-separated list of verbs ("GET,PUT,DELETE") * - A comma-separated list of verbs ("GET,PUT,DELETE")
* - "*" to indicate any method. * - "*" to indicate any method.
* @see MethodMapInterface::addMethod * @see MethodMapInterface::setMethod
* *
* $target may be: * $target may be:
* - An exact path (e.g., "/path/") * - An exact path (e.g., "/path/")

View File

@ -68,7 +68,7 @@ class Router implements MiddlewareInterface, RouteMapInterface
* - A single verb ("GET"), * - A single verb ("GET"),
* - A comma-separated list of verbs ("GET,PUT,DELETE") * - A comma-separated list of verbs ("GET,PUT,DELETE")
* - "*" to indicate any method. * - "*" to indicate any method.
* @see MethodMapInterface::addMethod * @see MethodMapInterface::setMethod
* *
* $target may be: * $target may be:
* - An exact path (e.g., "/path/") * - An exact path (e.g., "/path/")

View File

@ -6,7 +6,8 @@ use Prophecy\Argument;
use WellRESTed\Routing\MethodMap; use WellRESTed\Routing\MethodMap;
/** /**
* @covers WellRESTed\Routing\MethodMap * @coversDefaultClass WellRESTed\Routing\MethodMap
* @uses WellRESTed\Routing\MethodMap
* @uses WellRESTed\Routing\Dispatcher * @uses WellRESTed\Routing\Dispatcher
*/ */
class MethodMapTest extends \PHPUnit_Framework_TestCase class MethodMapTest extends \PHPUnit_Framework_TestCase
@ -22,14 +23,30 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$this->response->withHeader(Argument::cetera())->willReturn($this->response->reveal()); $this->response->withHeader(Argument::cetera())->willReturn($this->response->reveal());
} }
/**
* @covers ::__construct
*/
public function testCreatesInstance()
{
$methodMap = new MethodMap();
$this->assertNotNull($methodMap);
}
/**
* @covers ::dispatch
* @covers ::dispatchMiddleware
* @covers ::getDispatcher
* @covers ::setMethod
*/
public function testDispatchesMiddlewareWithMatchingMethod() public function testDispatchesMiddlewareWithMatchingMethod()
{ {
$this->request->getMethod()->willReturn("GET"); $this->request->getMethod()->willReturn("GET");
$middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); $middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middleware->dispatch(Argument::cetera())->willReturn(); $middleware->dispatch(Argument::cetera())->willReturn();
$map = new MethodMap(["GET" => $middleware->reveal()]); $map = new MethodMap();
$map->setMethod("GET", $middleware->reveal());
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
@ -38,14 +55,65 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$middleware->dispatch($request, $response)->shouldHaveBeenCalled(); $middleware->dispatch($request, $response)->shouldHaveBeenCalled();
} }
/**
* @coversNothing
*/
public function testTreatsMethodNamesCaseSensitively()
{
$this->request->getMethod()->willReturn("get");
$middlewareUpper = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middlewareUpper->dispatch(Argument::cetera())->willReturn();
$middlewareLower = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middlewareLower->dispatch(Argument::cetera())->willReturn();
$map = new MethodMap();
$map->setMethod("GET", $middlewareUpper->reveal());
$map->setMethod("get", $middlewareLower->reveal());
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$middlewareLower->dispatch($request, $response)->shouldHaveBeenCalled();
}
/**
* @covers ::dispatch
* @covers ::dispatchMiddleware
* @covers ::getDispatcher
* @covers ::setMethod
*/
public function testDispatchesWildcardMiddlewareWithNonMatchingMethod()
{
$this->request->getMethod()->willReturn("GET");
$middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middleware->dispatch(Argument::cetera())->willReturn();
$map = new MethodMap();
$map->setMethod("*", $middleware->reveal());
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$middleware->dispatch($request, $response)->shouldHaveBeenCalled();
}
/**
* @covers ::dispatch
*/
public function testDispatchesGetMiddlewareForHeadByDefault() public function testDispatchesGetMiddlewareForHeadByDefault()
{ {
$this->request->getMethod()->willReturn("HEAD"); $this->request->getMethod()->willReturn("HEAD");
$middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); $middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middleware->dispatch(Argument::cetera())->willReturn(); $middleware->dispatch(Argument::cetera())->willReturn();
$map = new MethodMap(["GET" => $middleware->reveal()]); $map = new MethodMap();
$map->setMethod("GET", $middleware->reveal());
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
@ -54,13 +122,16 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$middleware->dispatch($request, $response)->shouldHaveBeenCalled(); $middleware->dispatch($request, $response)->shouldHaveBeenCalled();
} }
/*
* @covers ::setMethod
*/
public function testRegistersMiddlewareForMultipleMethods() public function testRegistersMiddlewareForMultipleMethods()
{ {
$middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); $middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$middleware->dispatch(Argument::cetera())->willReturn(); $middleware->dispatch(Argument::cetera())->willReturn();
$map = new MethodMap(); $map = new MethodMap();
$map->add("GET,POST", $middleware->reveal()); $map->setMethod("GET,POST", $middleware->reveal());
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
@ -74,13 +145,36 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$middleware->dispatch($request, $response)->shouldHaveBeenCalledTimes(2); $middleware->dispatch($request, $response)->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", null);
$request = $this->request->reveal();
$response = $this->response->reveal();
$map->dispatch($request, $response);
$this->response->withStatus(405)->shouldHaveBeenCalled();
}
/**
* @covers ::dispatch
* @covers ::addAllowHeader
* @covers ::getAllowedMethods
*/
public function testSetsStatusTo200ForOptions() public function testSetsStatusTo200ForOptions()
{ {
$this->request->getMethod()->willReturn("OPTIONS"); $this->request->getMethod()->willReturn("OPTIONS");
$middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); $middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$map = new MethodMap(["GET" => $middleware->reveal()]); $map = new MethodMap();
$map->setMethod("GET", $middleware->reveal());
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
@ -90,17 +184,20 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::dispatch
* @covers ::addAllowHeader
* @covers ::getAllowedMethods
* @dataProvider allowedMethodProvider * @dataProvider allowedMethodProvider
*/ */
public function testSetsAllowHeaderForOptions($methodsDeclared, $methodsAllowed) public function testSetsAllowHeaderForOptions($methodsDeclared, $methodsAllowed)
{ {
$this->request->getMethod()->willReturn("OPTIONS"); $this->request->getMethod()->willReturn("OPTIONS");
$middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); $middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$map = new MethodMap(); $map = new MethodMap();
foreach ($methodsDeclared as $method) { foreach ($methodsDeclared as $method) {
$map->add($method, $middleware->reveal()); $map->setMethod($method, $middleware->reveal());
} }
$request = $this->request->reveal(); $request = $this->request->reveal();
@ -118,13 +215,20 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$this->response->withHeader("Allow", Argument::that($containsAllMethods))->shouldHaveBeenCalled(); $this->response->withHeader("Allow", Argument::that($containsAllMethods))->shouldHaveBeenCalled();
} }
/**
* @covers ::dispatch
* @covers ::addAllowHeader
* @covers ::getAllowedMethods
* @dataProvider allowedMethodProvider
*/
public function testSetsStatusTo405ForBadMethod() public function testSetsStatusTo405ForBadMethod()
{ {
$this->request->getMethod()->willReturn("POST"); $this->request->getMethod()->willReturn("POST");
$middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); $middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$map = new MethodMap(["GET" => $middleware->reveal()]); $map = new MethodMap();
$map->setMethod("GET", $middleware->reveal());
$request = $this->request->reveal(); $request = $this->request->reveal();
$response = $this->response->reveal(); $response = $this->response->reveal();
@ -134,17 +238,20 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::dispatch
* @covers ::addAllowHeader
* @covers ::getAllowedMethods
* @dataProvider allowedMethodProvider * @dataProvider allowedMethodProvider
*/ */
public function testSetsAlloweHeaderForBadMethod($methodsDeclared, $methodsAllowed) public function testSetsAllowHeaderForBadMethod($methodsDeclared, $methodsAllowed)
{ {
$this->request->getMethod()->willReturn("BAD"); $this->request->getMethod()->willReturn("BAD");
$middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); $middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
$map = new MethodMap(); $map = new MethodMap();
foreach ($methodsDeclared as $method) { foreach ($methodsDeclared as $method) {
$map->add($method, $middleware->reveal()); $map->setMethod($method, $middleware->reveal());
} }
$request = $this->request->reveal(); $request = $this->request->reveal();