Update RouteInterface and routes

This commit is contained in:
PJ Dietz 2015-05-13 21:17:07 -04:00
parent 61fd0f3354
commit 6232f67b9c
7 changed files with 92 additions and 21 deletions

View File

@ -25,4 +25,12 @@ class PrefixRoute extends Route
{
return strrpos($requestTarget, $this->target, -strlen($requestTarget)) !== false;
}
/**
* Always returns an empty array.
*/
public function getPathVariables()
{
return [];
}
}

View File

@ -2,12 +2,8 @@
namespace WellRESTed\Routing\Route;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class RegexRoute extends Route
{
private $capturesAttribute = "uriVariables";
private $captures;
public function getType()
@ -23,6 +19,7 @@ class RegexRoute extends Route
*/
public function matchesRequestTarget($requestTarget)
{
$this->captures = [];
$matched = @preg_match($this->getTarget(), $requestTarget, $captures);
if ($matched) {
$this->captures = $captures;
@ -33,11 +30,14 @@ class RegexRoute extends Route
return false;
}
public function dispatch(ServerRequestInterface $request, ResponseInterface $response, $next)
/**
* Returns an array of matches from the last call to matchesRequestTarget.
*
* @see \preg_match
* @return array
*/
public function getPathVariables()
{
if ($this->captures) {
$request = $request->withAttribute($this->capturesAttribute, $this->captures);
}
return parent::dispatch($request, $response, $next);
return $this->captures;
}
}

View File

@ -7,14 +7,47 @@ use WellRESTed\Routing\MethodMapInterface;
interface RouteInterface extends MiddlewareInterface
{
/** Matches when path is an exact match only */
const TYPE_STATIC = 0;
/** Matches when path has the expected beginning */
const TYPE_PREFIX = 1;
/** Matches by pattern. Use matchesRequestTarget to test for matches */
const TYPE_PATTERN = 2;
/**
* @return string
*/
public function getTarget();
/**
* Return the RouteInterface::TYPE_ contants that identifies the type.
*
* TYPE_STATIC indicates the route MUST match only when the path is an
* exact match to the route's target. This route type SHOULD NOT
* provide path variables.
*
* TYPE_PREFIX indicates the route MUST match when the route's target
* appears in its entirety at the beginning of the path.
*
* TYPE_PATTERN indicates that matchesRequestTarget MUST be used
* to determine a match against a given path. This route type SHOULD
* provide path variables.
*
* @return int One of the RouteInterface::TYPE_ constants.
*/
public function getType();
/**
* Return an array of variables extracted from the path most recently
* passed to matchesRequestTarget.
*
* If the path does not contain variables, or if matchesRequestTarget
* has not yet been called, this method MUST return an empty array.
*
* @return array
*/
public function getPathVariables();
/**
* Return the instance mapping methods to middleware for this route.
*
@ -27,6 +60,8 @@ interface RouteInterface extends MiddlewareInterface
*
* @param string $requestTarget
* @return boolean
* @throw \RuntimeException Error occured testing the target such as an
* invalid regular expression
*/
public function matchesRequestTarget($requestTarget);
}

View File

@ -19,4 +19,12 @@ class StaticRoute extends Route
{
return $requestTarget === $this->getTarget();
}
/**
* Always returns an empty array.
*/
public function getPathVariables()
{
return [];
}
}

View File

@ -35,6 +35,16 @@ class PrefixRouteTest extends \PHPUnit_Framework_TestCase
$this->assertSame(RouteInterface::TYPE_PREFIX, $route->getType());
}
/**
* @covers ::getPathVariables
*/
public function testReturnsEmptyArrayForPathVariables()
{
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = new PrefixRoute("/*", $methodMap->reveal());
$this->assertSame([], $route->getPathVariables());
}
/**
* @covers ::matchesRequestTarget
*/

View File

@ -43,23 +43,23 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
/**
* @covers ::matchesRequestTarget
* @covers ::dispatch
* @dataProvider matchingRouteProvider
*/
public function testProvidesCapturesAsRequestAttributes($pattern, $path, $expectedCaptures)
public function testMatchesTargetByRegex($pattern, $target)
{
$request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
$request->withAttribute(Argument::cetera())->willReturn($request->reveal());
$response = $this->prophesize('Psr\Http\Message\ResponseInterface');
$next = function ($request, $response) {
return $response;
};
$route = new RegexRoute($pattern, $this->methodMap->reveal());
$route->matchesRequestTarget($path);
$route->dispatch($request->reveal(), $response->reveal(), $next);
$this->assertTrue($route->matchesRequestTarget($target));
}
$request->withAttribute("uriVariables", $expectedCaptures)->shouldHaveBeenCalled();
/**
* @covers ::getPathVariables
* @dataProvider matchingRouteProvider
*/
public function testExtractsPathVariablesByRegex($pattern, $target, $expectedCaptures)
{
$route = new RegexRoute($pattern, $this->methodMap->reveal());
$route->matchesRequestTarget($target);
$this->assertEquals($expectedCaptures, $route->getPathVariables());
}
public function matchingRouteProvider()

View File

@ -35,6 +35,16 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($route->matchesRequestTarget("/"));
}
/**
* @covers ::getPathVariables
*/
public function testReturnsEmptyArrayForPathVariables()
{
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = new StaticRoute("/", $methodMap->reveal());
$this->assertSame([], $route->getPathVariables());
}
/**
* @covers ::matchesRequestTarget
*/