Update StaticRoute documentation and add tests

This commit is contained in:
PJ Dietz 2014-07-13 15:50:30 -04:00
parent 97836e7fa0
commit 921d84cf8b
2 changed files with 104 additions and 1 deletions

View File

@ -1,18 +1,29 @@
<?php <?php
/**
* pjdietz\WellRESTed\StaticRoute
*
* @author PJ Dietz <pj@pjdietz.com>
* @copyright Copyright 2014 by PJ Dietz
* @license MIT
*/
namespace pjdietz\WellRESTed\Routes; namespace pjdietz\WellRESTed\Routes;
use InvalidArgumentException; use InvalidArgumentException;
use pjdietz\WellRESTed\Interfaces\RequestInterface; use pjdietz\WellRESTed\Interfaces\RequestInterface;
/** /**
* Class StaticRoute * Maps a list of static URI paths to a Handler
*/ */
class StaticRoute extends BaseRoute class StaticRoute extends BaseRoute
{ {
/** @var array List of static URI paths */
private $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|array $paths Path or list of paths the request must match
* @param string $targetClassName Fully qualified name to an autoloadable handler class. * @param string $targetClassName Fully qualified name to an autoloadable handler class.
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
@ -32,6 +43,15 @@ class StaticRoute extends BaseRoute
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/* HandlerInterface */ /* 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) public function getResponse(RequestInterface $request, array $args = null)
{ {
$requestPath = $request->getPath(); $requestPath = $request->getPath();

View File

@ -0,0 +1,83 @@
<?php
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Response;
use pjdietz\WellRESTed\Routes\StaticRoute;
class StaticRouteTest extends \PHPUnit_Framework_TestCase
{
public function testSinglePathMatch()
{
$path = "/";
$mockRequest = $this->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;
}
}