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; namespace WellRESTed\Routing\Route;
class PrefixRoute extends Route implements PrefixRouteInterface class PrefixRoute extends Route
{ {
private $prefix; public function __construct($target, $methodMap)
public function __construct($prefix, $middleware)
{ {
parent::__construct($middleware); $this->target = rtrim($target, "*");
$this->prefix = $prefix; $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) public function matchesRequestTarget($requestTarget, &$captures = null)
{ {
return strrpos($requestTarget, $this->prefix, -strlen($requestTarget)) !== false; return strrpos($requestTarget, $this->target, -strlen($requestTarget)) !== false;
}
/**
* @return string
*/
public function getPrefix()
{
return $this->prefix;
} }
} }

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 Prophecy\Argument;
use WellRESTed\Routing\Route\PrefixRoute; 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 * @uses WellRESTed\Routing\Route\Route
*/ */
class PrefixRouteTest extends \PHPUnit_Framework_TestCase class PrefixRouteTest extends \PHPUnit_Framework_TestCase
{ {
private $request; /**
private $response; * @covers ::__construct
private $middleware; */
public function testTrimsAsteriskFromEndOfTarget()
public function setUp()
{ {
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface"); $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface"); $route = new PrefixRoute("/cats/*", $methodMap->reveal());
$this->middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface"); $this->assertEquals("/cats/", $route->getTarget());
} }
public function testMatchesPrefix() /**
* @covers ::getType
*/
public function testReturnsPrefixType()
{ {
$route = new PrefixRoute("/cats/", $this->middleware->reveal()); $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$this->assertTrue($route->matchesRequestTarget("/cats/molly")); $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()); $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$this->assertFalse($route->matchesRequestTarget("/cats/")); $route = new PrefixRoute("/*", $methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget("/"));
} }
public function testReturnsPrefix() /**
* @covers ::matchesRequestTarget
*/
public function testMatchesRequestTargetWithSamePrefix()
{ {
$route = new PrefixRoute("/cats/", $this->middleware->reveal()); $methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$this->assertEquals("/cats/", $route->getPrefix()); $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/"));
} }
} }