diff --git a/src/pjdietz/WellRESTed/Routes/StaticRoute.php b/src/pjdietz/WellRESTed/Routes/StaticRoute.php index ff3c653..21243bd 100644 --- a/src/pjdietz/WellRESTed/Routes/StaticRoute.php +++ b/src/pjdietz/WellRESTed/Routes/StaticRoute.php @@ -1,18 +1,29 @@ + * @copyright Copyright 2014 by PJ Dietz + * @license MIT + */ + namespace pjdietz\WellRESTed\Routes; use InvalidArgumentException; use pjdietz\WellRESTed\Interfaces\RequestInterface; /** - * Class StaticRoute + * Maps a list of static URI paths to a Handler */ class StaticRoute extends BaseRoute { + /** @var array List of static URI paths */ private $paths; /** + * Create a new StaticRoute for a given path or paths and a handler class. + * * @param string|array $paths Path or list of paths the request must match * @param string $targetClassName Fully qualified name to an autoloadable handler class. * @throws \InvalidArgumentException @@ -32,6 +43,15 @@ class StaticRoute extends BaseRoute // ------------------------------------------------------------------------ /* 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(); diff --git a/test/Routes/StaticRouteTest.php b/test/Routes/StaticRouteTest.php new file mode 100644 index 0000000..6f2c3b6 --- /dev/null +++ b/test/Routes/StaticRouteTest.php @@ -0,0 +1,83 @@ +getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue($path)); + + $route = new StaticRoute($path, 'HandlerStub'); + $resp = $route->getResponse($mockRequest); + $this->assertEquals(200, $resp->getStatusCode()); + } + + public function testSinglePathNoMatch() + { + $path = "/"; + + $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue("/not-this-path/")); + + $route = new StaticRoute($path, 'HandlerStub'); + $resp = $route->getResponse($mockRequest); + $this->assertNull($resp); + } + + public function testListPathMatch() + { + $path = "/"; + $paths = array($path, "/cats/", "/dogs/"); + + $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue($path)); + + $route = new StaticRoute($paths, 'HandlerStub'); + $resp = $route->getResponse($mockRequest); + $this->assertEquals(200, $resp->getStatusCode()); + } + + /** + * @dataProvider invalidPathsProvider + * @expectedException \InvalidArgumentException + */ + public function testInvalidPath($path) + { + $route = new StaticRoute($path, 'HandlerStub'); + } + + public function invalidPathsProvider() + { + return array( + array(false), + array(17), + array(null) + ); + } + +} + +/** + * Mini Handler class that allways returns a 200 status code Response. + */ +class HandlerStub implements HandlerInterface +{ + public function getResponse(\pjdietz\WellRESTed\Interfaces\RequestInterface $request, array $args = null) + { + $resp = new Response(); + $resp->setStatusCode(200); + return $resp; + } +}