Pass DispatcherInterface into MethodMap on construction
This commit is contained in:
parent
94d6cc23b2
commit
87caa09b61
|
|
@ -4,19 +4,20 @@ namespace WellRESTed\Routing;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use WellRESTed\Dispatching\Dispatcher;
|
|
||||||
use WellRESTed\Dispatching\DispatcherInterface;
|
use WellRESTed\Dispatching\DispatcherInterface;
|
||||||
use WellRESTed\MiddlewareInterface;
|
use WellRESTed\MiddlewareInterface;
|
||||||
|
|
||||||
class MethodMap implements MiddlewareInterface, MethodMapInterface
|
class MethodMap implements MiddlewareInterface, MethodMapInterface
|
||||||
{
|
{
|
||||||
protected $map;
|
private $dispatcher;
|
||||||
|
private $map;
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
public function __construct()
|
public function __construct(DispatcherInterface $dispatcher)
|
||||||
{
|
{
|
||||||
$this->map = [];
|
$this->map = [];
|
||||||
|
$this->dispatcher = $dispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
@ -92,13 +93,13 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
protected function addAllowHeader(ResponseInterface $response)
|
private function addAllowHeader(ResponseInterface $response)
|
||||||
{
|
{
|
||||||
$methods = join(",", $this->getAllowedMethods());
|
$methods = join(",", $this->getAllowedMethods());
|
||||||
return $response->withHeader("Allow", $methods);
|
return $response->withHeader("Allow", $methods);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getAllowedMethods()
|
private function getAllowedMethods()
|
||||||
{
|
{
|
||||||
$methods = array_keys($this->map);
|
$methods = array_keys($this->map);
|
||||||
// Add HEAD if GET is allowed and HEAD is not present.
|
// Add HEAD if GET is allowed and HEAD is not present.
|
||||||
|
|
@ -112,17 +113,6 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
|
||||||
return $methods;
|
return $methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an instance that can dispatch middleware.
|
|
||||||
* Override to provide a custom class.
|
|
||||||
*
|
|
||||||
* @return DispatcherInterface
|
|
||||||
*/
|
|
||||||
protected function getDispatcher()
|
|
||||||
{
|
|
||||||
return new Dispatcher();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $middleware
|
* @param $middleware
|
||||||
* @param ServerRequestInterface $request
|
* @param ServerRequestInterface $request
|
||||||
|
|
@ -132,7 +122,6 @@ class MethodMap implements MiddlewareInterface, MethodMapInterface
|
||||||
*/
|
*/
|
||||||
private function dispatchMiddleware($middleware, ServerRequestInterface $request, ResponseInterface &$response, $next)
|
private function dispatchMiddleware($middleware, ServerRequestInterface $request, ResponseInterface &$response, $next)
|
||||||
{
|
{
|
||||||
$dispatcher = $this->getDispatcher();
|
return $this->dispatcher->dispatch($middleware, $request, $response, $next);
|
||||||
return $dispatcher->dispatch($middleware, $request, $response, $next);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ use WellRESTed\Routing\MethodMap;
|
||||||
*/
|
*/
|
||||||
class MethodMapTest extends \PHPUnit_Framework_TestCase
|
class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
private $dispatcher;
|
||||||
private $request;
|
private $request;
|
||||||
private $response;
|
private $response;
|
||||||
private $next;
|
private $next;
|
||||||
|
|
@ -28,6 +29,13 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
};
|
};
|
||||||
$this->middleware = $this->prophesize('WellRESTed\MiddlewareInterface');
|
$this->middleware = $this->prophesize('WellRESTed\MiddlewareInterface');
|
||||||
$this->middleware->dispatch(Argument::cetera())->willReturn();
|
$this->middleware->dispatch(Argument::cetera())->willReturn();
|
||||||
|
$this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface');
|
||||||
|
$this->dispatcher->dispatch(Argument::cetera())->will(
|
||||||
|
function ($args) {
|
||||||
|
list($middleware, $request, $response, $next) = $args;
|
||||||
|
return $middleware->dispatch($request, $response, $next);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -35,25 +43,28 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
*/
|
*/
|
||||||
public function testCreatesInstance()
|
public function testCreatesInstance()
|
||||||
{
|
{
|
||||||
$methodMap = new MethodMap();
|
$methodMap = new MethodMap($this->dispatcher->reveal());
|
||||||
$this->assertNotNull($methodMap);
|
$this->assertNotNull($methodMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers ::dispatch
|
* @covers ::dispatch
|
||||||
* @covers ::dispatchMiddleware
|
* @covers ::dispatchMiddleware
|
||||||
* @covers ::getDispatcher
|
|
||||||
* @covers ::register
|
* @covers ::register
|
||||||
*/
|
*/
|
||||||
public function testDispatchesMiddlewareWithMatchingMethod()
|
public function testDispatchesMiddlewareWithMatchingMethod()
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("GET");
|
$this->request->getMethod()->willReturn("GET");
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
$map->register("GET", $this->middleware->reveal());
|
$map->register("GET", $this->middleware->reveal());
|
||||||
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
|
$this->middleware->dispatch(
|
||||||
|
$this->request->reveal(),
|
||||||
|
$this->response->reveal(),
|
||||||
|
$this->next
|
||||||
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -69,29 +80,36 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
$middlewareLower = $this->prophesize('WellRESTed\MiddlewareInterface');
|
$middlewareLower = $this->prophesize('WellRESTed\MiddlewareInterface');
|
||||||
$middlewareLower->dispatch(Argument::cetera())->willReturn();
|
$middlewareLower->dispatch(Argument::cetera())->willReturn();
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
$map->register("GET", $middlewareUpper->reveal());
|
$map->register("GET", $middlewareUpper->reveal());
|
||||||
$map->register("get", $middlewareLower->reveal());
|
$map->register("get", $middlewareLower->reveal());
|
||||||
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$middlewareLower->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
|
$middlewareLower->dispatch(
|
||||||
|
$this->request->reveal(),
|
||||||
|
$this->response->reveal(),
|
||||||
|
$this->next
|
||||||
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers ::dispatch
|
* @covers ::dispatch
|
||||||
* @covers ::dispatchMiddleware
|
* @covers ::dispatchMiddleware
|
||||||
* @covers ::getDispatcher
|
|
||||||
* @covers ::register
|
* @covers ::register
|
||||||
*/
|
*/
|
||||||
public function testDispatchesWildcardMiddlewareWithNonMatchingMethod()
|
public function testDispatchesWildcardMiddlewareWithNonMatchingMethod()
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("GET");
|
$this->request->getMethod()->willReturn("GET");
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
$map->register("*", $this->middleware->reveal());
|
$map->register("*", $this->middleware->reveal());
|
||||||
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
|
$this->middleware->dispatch(
|
||||||
|
$this->request->reveal(),
|
||||||
|
$this->response->reveal(),
|
||||||
|
$this->next
|
||||||
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -101,19 +119,23 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("HEAD");
|
$this->request->getMethod()->willReturn("HEAD");
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
$map->register("GET", $this->middleware->reveal());
|
$map->register("GET", $this->middleware->reveal());
|
||||||
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
|
$this->middleware->dispatch(
|
||||||
|
$this->request->reveal(),
|
||||||
|
$this->response->reveal(),
|
||||||
|
$this->next
|
||||||
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* @covers ::register
|
* @covers ::register
|
||||||
*/
|
*/
|
||||||
public function testRegistersMiddlewareForMultipleMethods()
|
public function testRegistersMiddlewareForMultipleMethods()
|
||||||
{
|
{
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
$map->register("GET,POST", $this->middleware->reveal());
|
$map->register("GET,POST", $this->middleware->reveal());
|
||||||
|
|
||||||
$this->request->getMethod()->willReturn("GET");
|
$this->request->getMethod()->willReturn("GET");
|
||||||
|
|
@ -122,14 +144,18 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->request->getMethod()->willReturn("POST");
|
$this->request->getMethod()->willReturn("POST");
|
||||||
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
$this->middleware->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalledTimes(2);
|
$this->middleware->dispatch(
|
||||||
|
$this->request->reveal(),
|
||||||
|
$this->response->reveal(),
|
||||||
|
$this->next
|
||||||
|
)->shouldHaveBeenCalledTimes(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSettingNullUnregistersMiddleware()
|
public function testSettingNullUnregistersMiddleware()
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("POST");
|
$this->request->getMethod()->willReturn("POST");
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
$map->register("POST", $this->middleware->reveal());
|
$map->register("POST", $this->middleware->reveal());
|
||||||
$map->register("POST", null);
|
$map->register("POST", null);
|
||||||
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
@ -146,7 +172,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("OPTIONS");
|
$this->request->getMethod()->willReturn("OPTIONS");
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
$map->register("GET", $this->middleware->reveal());
|
$map->register("GET", $this->middleware->reveal());
|
||||||
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
|
|
@ -163,7 +189,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("OPTIONS");
|
$this->request->getMethod()->willReturn("OPTIONS");
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
foreach ($methodsDeclared as $method) {
|
foreach ($methodsDeclared as $method) {
|
||||||
$map->register($method, $this->middleware->reveal());
|
$map->register($method, $this->middleware->reveal());
|
||||||
}
|
}
|
||||||
|
|
@ -190,7 +216,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("POST");
|
$this->request->getMethod()->willReturn("POST");
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
$map->register("GET", $this->middleware->reveal());
|
$map->register("GET", $this->middleware->reveal());
|
||||||
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
|
||||||
|
|
||||||
|
|
@ -210,7 +236,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
};
|
};
|
||||||
$this->request->getMethod()->willReturn("POST");
|
$this->request->getMethod()->willReturn("POST");
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
$map->register("GET", $this->middleware->reveal());
|
$map->register("GET", $this->middleware->reveal());
|
||||||
$map->dispatch($this->request->reveal(), $this->response->reveal(), $next);
|
$map->dispatch($this->request->reveal(), $this->response->reveal(), $next);
|
||||||
$this->assertTrue($calledNext);
|
$this->assertTrue($calledNext);
|
||||||
|
|
@ -226,7 +252,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("BAD");
|
$this->request->getMethod()->willReturn("BAD");
|
||||||
|
|
||||||
$map = new MethodMap();
|
$map = new MethodMap($this->dispatcher->reveal());
|
||||||
foreach ($methodsDeclared as $method) {
|
foreach ($methodsDeclared as $method) {
|
||||||
$map->register($method, $this->middleware->reveal());
|
$map->register($method, $this->middleware->reveal());
|
||||||
}
|
}
|
||||||
|
|
@ -247,10 +273,10 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
[["GET"], ["GET", "HEAD", "OPTIONS"]],
|
[["GET"], ["GET", "HEAD", "OPTIONS"]],
|
||||||
[["GET","POST"], ["GET", "POST", "HEAD", "OPTIONS"]],
|
[["GET", "POST"], ["GET", "POST", "HEAD", "OPTIONS"]],
|
||||||
[["POST"], ["POST", "OPTIONS"]],
|
[["POST"], ["POST", "OPTIONS"]],
|
||||||
[["POST"], ["POST", "OPTIONS"]],
|
[["POST"], ["POST", "OPTIONS"]],
|
||||||
[["GET","PUT,DELETE"], ["GET", "PUT", "DELETE", "HEAD", "OPTIONS"]],
|
[["GET", "PUT,DELETE"], ["GET", "PUT", "DELETE", "HEAD", "OPTIONS"]],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue