From 58b5107289ba9d7f9b2d30f2f44d92ee4c2bb336 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 7 May 2015 23:14:48 -0400 Subject: [PATCH] Revise Route --- src/Routing/Route/Route.php | 33 ++++++++-- src/Routing/Route/RouteInterface.php | 12 +--- test/tests/unit/Routing/Route/RouteTest.php | 67 +++++++++++++++------ 3 files changed, 78 insertions(+), 34 deletions(-) diff --git a/src/Routing/Route/Route.php b/src/Routing/Route/Route.php index 55fa67a..4e04d0e 100644 --- a/src/Routing/Route/Route.php +++ b/src/Routing/Route/Route.php @@ -4,20 +4,41 @@ namespace WellRESTed\Routing\Route; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use WellRESTed\Routing\Dispatcher; +use WellRESTed\Routing\MethodMapInterface; abstract class Route implements RouteInterface { - private $middleware; + /** @var string */ + protected $target; + /** @var MethodMapInterface */ + protected $methodMap; - public function __construct($middleware) + public function __construct($target, $methodMap) { - $this->middleware = $middleware; + $this->target = $target; + $this->methodMap = $methodMap; + } + + /** + * Return the instance mapping methods to middleware for this route. + * + * @return MethodMapInterface + */ + public function getMethodMap() + { + return $this->methodMap; + } + + /** + * @return string + */ + public function getTarget() + { + return $this->target; } public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) { - $dispatcher = new Dispatcher(); - $dispatcher->dispatch($this->middleware, $request, $response); + $this->methodMap->dispatch($request, $response); } } diff --git a/src/Routing/Route/RouteInterface.php b/src/Routing/Route/RouteInterface.php index c488819..01c28be 100644 --- a/src/Routing/Route/RouteInterface.php +++ b/src/Routing/Route/RouteInterface.php @@ -23,17 +23,9 @@ interface RouteInterface extends MiddlewareInterface public function getMethodMap(); /** - * Examines a path (request target) and returns whether or not the route - * should handle the request providing the target. + * Examines a path (request target) to see if it is a match for the route. * - * If a successful examination also extracts items (such as captures from - * matching a regular expression), store them to $captures. - * - * $captures should have no meaning for calls that return false. - * - * @param string $requestTarget - * @param array $captures * @return boolean */ - public function matchesRequestTarget($requestTarget, &$captures = null); + public function matchesRequestTarget($requestTarget); } diff --git a/test/tests/unit/Routing/Route/RouteTest.php b/test/tests/unit/Routing/Route/RouteTest.php index bcbff69..358b1d2 100644 --- a/test/tests/unit/Routing/Route/RouteTest.php +++ b/test/tests/unit/Routing/Route/RouteTest.php @@ -3,34 +3,65 @@ namespace WellRESTed\Test\Unit\Routing\Route; use Prophecy\Argument; -use WellRESTed\Routing\Route\Route; -use WellRESTed\Routing\Route\StaticRoute; /** + * @coversDefaultClass WellRESTed\Routing\Route\Route * @uses WellRESTed\Routing\Route\Route - * @uses WellRESTed\Routing\Route\StaticRoute */ class RouteTest extends \PHPUnit_Framework_TestCase { - private $request; - private $response; - - public function setUp() + /** + * @covers ::__construct + */ + public function testCreatesInstance() { - $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()); + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = $this->getMockForAbstractClass( + 'WellRESTed\Routing\Route\Route', + ["/target", $methodMap->reveal()]); + $this->assertNotNull($route); } - public function testDispatchesMiddleware() + /** + * @covers ::getTarget + */ + public function testReturnsTarget() { - $middleware = function ($request, &$response) { - $response = $response->withStatus(200); - }; - $route = new StaticRoute("/", $middleware); - $request = $this->request->reveal(); - $response = $this->response->reveal(); + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = $this->getMockForAbstractClass( + 'WellRESTed\Routing\Route\Route', + ["/target", $methodMap->reveal()]); + $this->assertSame("/target", $route->getTarget()); + } + + /** + * @covers ::getMethodMap + */ + public function testReturnsMethodMap() + { + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $route = $this->getMockForAbstractClass( + 'WellRESTed\Routing\Route\Route', + ["/target", $methodMap->reveal()]); + $this->assertSame($methodMap->reveal(), $route->getMethodMap()); + } + + /** + * @covers ::dispatch + */ + public function testDispatchesMethodMap() + { + $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface'); + $methodMap->dispatch(Argument::cetera())->willReturn(); + + $route = $this->getMockForAbstractClass( + 'WellRESTed\Routing\Route\Route', + ["/target", $methodMap->reveal()]); + + $request = $this->prophesize('Psr\Http\Message\ServerRequestInterface')->reveal(); + $response = $this->prophesize('Psr\Http\Message\ResponseInterface')->reveal(); $route->dispatch($request, $response); - $this->response->withStatus(200)->shouldHaveBeenCalled(); + + $methodMap->dispatch(Argument::cetera())->shouldHaveBeenCalled(); } }