Add PrefixRoute

This commit is contained in:
PJ Dietz 2014-12-29 13:09:51 -05:00
parent 8b5f9e40a6
commit 6d499a6643
3 changed files with 139 additions and 1 deletions

View File

@ -0,0 +1,43 @@
<?php
/**
* pjdietz\WellRESTed\PrefixRoute
*
* @author PJ Dietz <pj@pjdietz.com>
* @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;
}
}

View File

@ -19,7 +19,7 @@ use pjdietz\WellRESTed\Interfaces\RequestInterface;
class StaticRoute extends BaseRoute class StaticRoute extends BaseRoute
{ {
/** @var array List of static URI paths */ /** @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. * Create a new StaticRoute for a given path or paths and a handler class.

View File

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