From d367f1de7925c7c5af2bcb9c8926ebb6fff5b863 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 2 Apr 2015 20:10:13 -0400 Subject: [PATCH] Add Static- and PrefixRoutes --- src/Routing/Route/PrefixRoute.php | 32 ++++++++++++++ src/Routing/Route/PrefixRouteInterface.php | 11 +++++ src/Routing/Route/Route.php | 23 ++++++++++ src/Routing/Route/RouteInterface.php | 23 ++++++++++ src/Routing/Route/StaticRoute.php | 32 ++++++++++++++ src/Routing/Route/StaticRouteInterface.php | 11 +++++ .../unit/Routing/Route/PrefixRouteTest.php | 42 +++++++++++++++++++ test/tests/unit/Routing/Route/RouteTest.php | 34 +++++++++++++++ .../unit/Routing/Route/StaticRouteTest.php | 42 +++++++++++++++++++ 9 files changed, 250 insertions(+) create mode 100644 src/Routing/Route/PrefixRoute.php create mode 100644 src/Routing/Route/PrefixRouteInterface.php create mode 100644 src/Routing/Route/Route.php create mode 100644 src/Routing/Route/RouteInterface.php create mode 100644 src/Routing/Route/StaticRoute.php create mode 100644 src/Routing/Route/StaticRouteInterface.php create mode 100644 test/tests/unit/Routing/Route/PrefixRouteTest.php create mode 100644 test/tests/unit/Routing/Route/RouteTest.php create mode 100644 test/tests/unit/Routing/Route/StaticRouteTest.php diff --git a/src/Routing/Route/PrefixRoute.php b/src/Routing/Route/PrefixRoute.php new file mode 100644 index 0000000..7b424da --- /dev/null +++ b/src/Routing/Route/PrefixRoute.php @@ -0,0 +1,32 @@ +prefix = $prefix; + } + + /** + * @param string $requestTarget + * @param array $captures + * @return bool + */ + public function matchesRequestTarget($requestTarget, &$captures = null) + { + return strrpos($requestTarget, $this->prefix, -strlen($requestTarget)) !== false; + } + + /** + * @return string + */ + public function getPrefix() + { + return $this->prefix; + } +} diff --git a/src/Routing/Route/PrefixRouteInterface.php b/src/Routing/Route/PrefixRouteInterface.php new file mode 100644 index 0000000..00c68db --- /dev/null +++ b/src/Routing/Route/PrefixRouteInterface.php @@ -0,0 +1,11 @@ +middleware = $middleware; + } + + public function dispatch(ServerRequestInterface $request, ResponseInterface &$response) + { + $dispatcher = new Dispatcher(); + $dispatcher->dispatch($this->middleware, $request, $response); + } +} diff --git a/src/Routing/Route/RouteInterface.php b/src/Routing/Route/RouteInterface.php new file mode 100644 index 0000000..bf08043 --- /dev/null +++ b/src/Routing/Route/RouteInterface.php @@ -0,0 +1,23 @@ +path = $path; + } + + /** + * @param string $requestTarget + * @param array $captures + * @return bool + */ + public function matchesRequestTarget($requestTarget, &$captures = null) + { + return $requestTarget == $this->path; + } + + /** + * @return string + */ + public function getPath() + { + return $this->path; + } +} diff --git a/src/Routing/Route/StaticRouteInterface.php b/src/Routing/Route/StaticRouteInterface.php new file mode 100644 index 0000000..e64ab21 --- /dev/null +++ b/src/Routing/Route/StaticRouteInterface.php @@ -0,0 +1,11 @@ +request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); + $this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); + $this->middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); + } + + public function testMatchesPrefix() + { + $route = new PrefixRoute("/cats/", $this->middleware->reveal()); + $this->assertTrue($route->matchesRequestTarget("/cats/molly")); + } + + public function testFailsToMatchWrongPath() + { + $route = new PrefixRoute("/dogs/", $this->middleware->reveal()); + $this->assertFalse($route->matchesRequestTarget("/cats/")); + } + + public function testReturnsPrefix() + { + $route = new PrefixRoute("/cats/", $this->middleware->reveal()); + $this->assertEquals("/cats/", $route->getPrefix()); + } +} diff --git a/test/tests/unit/Routing/Route/RouteTest.php b/test/tests/unit/Routing/Route/RouteTest.php new file mode 100644 index 0000000..968e9f2 --- /dev/null +++ b/test/tests/unit/Routing/Route/RouteTest.php @@ -0,0 +1,34 @@ +request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); + $this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); + $this->response->withStatus(Argument::any())->willReturn($this->response->reveal()); + } + + public function testDispatchesMiddleware() + { + $middleware = function ($request, &$response) { + $response = $response->withStatus(200); + }; + $route = new StaticRoute("/", $middleware); + $route->dispatch($this->request->reveal(), $this->response->reveal()); + $this->response->withStatus(200)->shouldHaveBeenCalled(); + } +} diff --git a/test/tests/unit/Routing/Route/StaticRouteTest.php b/test/tests/unit/Routing/Route/StaticRouteTest.php new file mode 100644 index 0000000..ba945c9 --- /dev/null +++ b/test/tests/unit/Routing/Route/StaticRouteTest.php @@ -0,0 +1,42 @@ +request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); + $this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); + $this->middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); + } + + public function testMatchesPath() + { + $route = new StaticRoute("/cats/", $this->middleware->reveal()); + $this->assertTrue($route->matchesRequestTarget("/cats/")); + } + + public function testFailsToMatchWrongPath() + { + $route = new StaticRoute("/dogs/", $this->middleware->reveal()); + $this->assertFalse($route->matchesRequestTarget("/cats/")); + } + + public function testReturnsPaths() + { + $route = new StaticRoute("/cats/", $this->middleware->reveal()); + $this->assertEquals("/cats/", $route->getPath()); + } +}