Update RouteMap to match updated MiddlewareInterface

This commit is contained in:
PJ Dietz 2015-05-09 18:51:58 -04:00
parent 36263ba3de
commit 5a01d20f8e
2 changed files with 20 additions and 36 deletions

View File

@ -63,32 +63,29 @@ class RouteMap implements RouteMapInterface
$route->getMethodMap()->register($method, $middleware); $route->getMethodMap()->register($method, $middleware);
} }
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
{ {
$requestTarget = $request->getRequestTarget(); $requestTarget = $request->getRequestTarget();
$route = $this->getStaticRoute($requestTarget); $route = $this->getStaticRoute($requestTarget);
if ($route) { if ($route) {
$route->dispatch($request, $response); return $route->dispatch($request, $response, $next);
return;
} }
$route = $this->getPrefixRoute($requestTarget); $route = $this->getPrefixRoute($requestTarget);
if ($route) { if ($route) {
$route->dispatch($request, $response); return $route->dispatch($request, $response, $next);
return;
} }
// Try each of the routes. // Try each of the routes.
foreach ($this->patternRoutes as $route) { foreach ($this->patternRoutes as $route) {
if ($route->matchesRequestTarget($requestTarget)) { if ($route->matchesRequestTarget($requestTarget)) {
$route->dispatch($request, $response); return $route->dispatch($request, $response, $next);
return;
} }
} }
// If no route exists, set the status code of the response to 404. // If no route exists, set the status code of the response to 404.
$response = $response->withStatus(404); return $response->withStatus(404);
} }
/** /**

View File

@ -21,13 +21,14 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase
private $response; private $response;
private $route; private $route;
private $routeMap; private $routeMap;
private $next;
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); $this->methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$this->methodMap->setMethod(Argument::cetera()); $this->methodMap->register(Argument::cetera());
$this->route = $this->prophesize('WellRESTed\Routing\Route\RouteInterface'); $this->route = $this->prophesize('WellRESTed\Routing\Route\RouteInterface');
$this->route->dispatch(Argument::cetera())->willReturn(); $this->route->dispatch(Argument::cetera())->willReturn();
@ -39,8 +40,10 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase
$this->factory->create(Argument::any())->willReturn($this->route->reveal()); $this->factory->create(Argument::any())->willReturn($this->route->reveal());
$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->next = function ($request, $response) {
return $response;
};
$this->routeMap = $this->getMockBuilder('WellRESTed\Routing\RouteMap') $this->routeMap = $this->getMockBuilder('WellRESTed\Routing\RouteMap')
->setMethods(["getRouteFactory"]) ->setMethods(["getRouteFactory"])
@ -96,7 +99,7 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase
public function testAddPassesMethodAndMiddlewareToMethodMap() public function testAddPassesMethodAndMiddlewareToMethodMap()
{ {
$this->routeMap->add("GET", "/", "middleware"); $this->routeMap->add("GET", "/", "middleware");
$this->methodMap->setMethod("GET", "middleware")->shouldHaveBeenCalled(); $this->methodMap->register("GET", "middleware")->shouldHaveBeenCalled();
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -116,12 +119,9 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase
$this->route->getType()->willReturn(RouteInterface::TYPE_STATIC); $this->route->getType()->willReturn(RouteInterface::TYPE_STATIC);
$this->routeMap->add("GET", $target, "middleware"); $this->routeMap->add("GET", $target, "middleware");
$this->routeMap->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$request = $this->request->reveal(); $this->route->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
$response = $this->response->reveal();
$this->routeMap->dispatch($request, $response);
$this->route->dispatch(Argument::cetera())->shouldHaveBeenCalled();
} }
/** /**
@ -137,12 +137,9 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase
$this->route->getType()->willReturn(RouteInterface::TYPE_PREFIX); $this->route->getType()->willReturn(RouteInterface::TYPE_PREFIX);
$this->routeMap->add("GET", $target, "middleware"); $this->routeMap->add("GET", $target, "middleware");
$this->routeMap->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$request = $this->request->reveal(); $this->route->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
$response = $this->response->reveal();
$this->routeMap->dispatch($request, $response);
$this->route->dispatch(Argument::cetera())->shouldHaveBeenCalled();
} }
/** /**
@ -169,12 +166,9 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase
$this->routeMap->add("GET", "/animals/*", "middleware"); $this->routeMap->add("GET", "/animals/*", "middleware");
$this->routeMap->add("GET", "/animals/cats/*", "middleware"); $this->routeMap->add("GET", "/animals/cats/*", "middleware");
$this->routeMap->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$request = $this->request->reveal(); $routeCats->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
$response = $this->response->reveal();
$this->routeMap->dispatch($request, $response);
$routeCats->dispatch(Argument::cetera())->shouldHaveBeenCalled();
} }
/** /**
@ -191,12 +185,9 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase
$this->route->matchesRequestTarget(Argument::cetera())->willReturn(true); $this->route->matchesRequestTarget(Argument::cetera())->willReturn(true);
$this->routeMap->add("GET", $target, "middleware"); $this->routeMap->add("GET", $target, "middleware");
$this->routeMap->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$request = $this->request->reveal(); $this->route->dispatch($this->request->reveal(), $this->response->reveal(), $this->next)->shouldHaveBeenCalled();
$response = $this->response->reveal();
$this->routeMap->dispatch($request, $response);
$this->route->dispatch(Argument::cetera())->shouldHaveBeenCalled();
} }
/** /**
@ -207,11 +198,7 @@ class RouteMapTest extends \PHPUnit_Framework_TestCase
public function testResponds404WhenNoRouteMatches() public function testResponds404WhenNoRouteMatches()
{ {
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal()); $this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
$this->routeMap->dispatch($this->request->reveal(), $this->response->reveal(), $this->next);
$request = $this->request->reveal();
$response = $this->response->reveal();
$this->routeMap->dispatch($request, $response);
$this->response->withStatus(404)->shouldHaveBeenCalled(); $this->response->withStatus(404)->shouldHaveBeenCalled();
} }
} }