From 6d499a66433231b314366a5195ee4518a10bbaf4 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Mon, 29 Dec 2014 13:09:51 -0500 Subject: [PATCH] Add PrefixRoute --- src/pjdietz/WellRESTed/Routes/PrefixRoute.php | 43 +++++++++ src/pjdietz/WellRESTed/Routes/StaticRoute.php | 2 +- test/Routes/PrefixRouteTest.php | 95 +++++++++++++++++++ 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/pjdietz/WellRESTed/Routes/PrefixRoute.php create mode 100644 test/Routes/PrefixRouteTest.php diff --git a/src/pjdietz/WellRESTed/Routes/PrefixRoute.php b/src/pjdietz/WellRESTed/Routes/PrefixRoute.php new file mode 100644 index 0000000..0262cd7 --- /dev/null +++ b/src/pjdietz/WellRESTed/Routes/PrefixRoute.php @@ -0,0 +1,43 @@ + + * @copyright Copyright 2014 by PJ Dietz + * @license MIT + */ + +namespace pjdietz\WellRESTed\Routes; + +use pjdietz\WellRESTed\Interfaces\RequestInterface; + +/** + * Maps a list of static URI paths to a Handler + */ +class PrefixRoute extends StaticRoute +{ + // ------------------------------------------------------------------------ + /* HandlerInterface */ + + /** + * Return the response issued by the handler class or null. + * + * A null return value indicates that this route failed to match the request. + * + * @param RequestInterface $request + * @param array $args + * @return null|\pjdietz\WellRESTed\Interfaces\ResponseInterface + */ + public function getResponse(RequestInterface $request, array $args = null) + { + $requestPath = $request->getPath(); + foreach ($this->paths as $path) { + if (substr($requestPath, 0, strlen($path)) === $path) { + $target = $this->getTarget(); + return $target->getResponse($request, $args); + } + } + return null; + } +} diff --git a/src/pjdietz/WellRESTed/Routes/StaticRoute.php b/src/pjdietz/WellRESTed/Routes/StaticRoute.php index 21243bd..85d5604 100644 --- a/src/pjdietz/WellRESTed/Routes/StaticRoute.php +++ b/src/pjdietz/WellRESTed/Routes/StaticRoute.php @@ -19,7 +19,7 @@ use pjdietz\WellRESTed\Interfaces\RequestInterface; class StaticRoute extends BaseRoute { /** @var array List of static URI paths */ - private $paths; + protected $paths; /** * Create a new StaticRoute for a given path or paths and a handler class. diff --git a/test/Routes/PrefixRouteTest.php b/test/Routes/PrefixRouteTest.php new file mode 100644 index 0000000..c0aab28 --- /dev/null +++ b/test/Routes/PrefixRouteTest.php @@ -0,0 +1,95 @@ +getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue($path)); + + $route = new PrefixRoute($path, __NAMESPACE__ . '\PrefixRouteTestHandler'); + $resp = $route->getResponse($mockRequest); + $this->assertNotNull($resp); + } + + public function testMatchSinglePathWithPrefix() + { + $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue("/cats/")); + + $route = new PrefixRoute("/", __NAMESPACE__ . '\PrefixRouteTestHandler'); + $resp = $route->getResponse($mockRequest); + $this->assertNotNull($resp); + } + + public function testMatchPathInList() + { + $paths = array("/cats/", "/dogs/"); + + $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue("/cats/")); + + $route = new PrefixRoute($paths, __NAMESPACE__ . '\StaticRouteTestHandler'); + $resp = $route->getResponse($mockRequest); + $this->assertEquals(200, $resp->getStatusCode()); + } + + public function testFailToMatchPath() + { + $path = "/cat/"; + + $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue("/not-this-path/")); + + $route = new PrefixRoute($path, 'NoClass'); + $resp = $route->getResponse($mockRequest); + $this->assertNull($resp); + } + + /** + * @dataProvider invalidPathsProvider + * @expectedException \InvalidArgumentException + */ + public function testFailOnInvalidPath($path) + { + new PrefixRoute($path, 'NoClass'); + } + + public function invalidPathsProvider() + { + return array( + array(false), + array(17), + array(null) + ); + } +} + +/** + * Mini Handler class that allways returns a 200 status code Response. + */ +class PrefixRouteTestHandler implements HandlerInterface +{ + public function getResponse(\pjdietz\WellRESTed\Interfaces\RequestInterface $request, array $args = null) + { + $resp = new Response(); + $resp->setStatusCode(200); + return $resp; + } +}