Revise PrefixRoute

This commit is contained in:
PJ Dietz 2015-05-07 23:30:42 -04:00
parent 86d36e8c15
commit cfcc3b9690
3 changed files with 51 additions and 44 deletions

View File

@ -2,14 +2,17 @@
namespace WellRESTed\Routing\Route;
class PrefixRoute extends Route implements PrefixRouteInterface
class PrefixRoute extends Route
{
private $prefix;
public function __construct($prefix, $middleware)
public function __construct($target, $methodMap)
{
parent::__construct($middleware);
$this->prefix = $prefix;
$this->target = rtrim($target, "*");
$this->methodMap = $methodMap;
}
public function getType()
{
return RouteInterface::TYPE_PREFIX;
}
/**
@ -19,14 +22,6 @@ class PrefixRoute extends Route implements PrefixRouteInterface
*/
public function matchesRequestTarget($requestTarget, &$captures = null)
{
return strrpos($requestTarget, $this->prefix, -strlen($requestTarget)) !== false;
}
/**
* @return string
*/
public function getPrefix()
{
return $this->prefix;
return strrpos($requestTarget, $this->target, -strlen($requestTarget)) !== false;
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace WellRESTed\Routing\Route;
interface PrefixRouteInterface extends RouteInterface
{
/**
* @return string
*/
public function getPrefix();
}

View File

@ -4,39 +4,62 @@ namespace WellRESTed\Test\Unit\Routing\Route;
use Prophecy\Argument;
use WellRESTed\Routing\Route\PrefixRoute;
use WellRESTed\Routing\Route\RouteInterface;
/**
* @covers WellRESTed\Routing\Route\PrefixRoute
* @coversDefaultClass WellRESTed\Routing\Route\PrefixRoute
* @uses WellRESTed\Routing\Route\PrefixRoute
* @uses WellRESTed\Routing\Route\Route
*/
class PrefixRouteTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
private $middleware;
public function setUp()
/**
* @covers ::__construct
*/
public function testTrimsAsteriskFromEndOfTarget()
{
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface");
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface");
$this->middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface");
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = new PrefixRoute("/cats/*", $methodMap->reveal());
$this->assertEquals("/cats/", $route->getTarget());
}
public function testMatchesPrefix()
/**
* @covers ::getType
*/
public function testReturnsPrefixType()
{
$route = new PrefixRoute("/cats/", $this->middleware->reveal());
$this->assertTrue($route->matchesRequestTarget("/cats/molly"));
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = new PrefixRoute("/*", $methodMap->reveal());
$this->assertSame(RouteInterface::TYPE_PREFIX, $route->getType());
}
public function testFailsToMatchWrongPath()
/**
* @covers ::matchesRequestTarget
*/
public function testMatchesExactRequestTarget()
{
$route = new PrefixRoute("/dogs/", $this->middleware->reveal());
$this->assertFalse($route->matchesRequestTarget("/cats/"));
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = new PrefixRoute("/*", $methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget("/"));
}
public function testReturnsPrefix()
/**
* @covers ::matchesRequestTarget
*/
public function testMatchesRequestTargetWithSamePrefix()
{
$route = new PrefixRoute("/cats/", $this->middleware->reveal());
$this->assertEquals("/cats/", $route->getPrefix());
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = new PrefixRoute("/*", $methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget("/cats/"));
}
/**
* @covers ::matchesRequestTarget
*/
public function testDoesNotMatchNonmatchingRequestTarget()
{
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = new PrefixRoute("/animals/cats/", $methodMap->reveal());
$this->assertFalse($route->matchesRequestTarget("/animals/dogs/"));
}
}