Pass DispatcherInterface to RouteFactory on construction

This commit is contained in:
PJ Dietz 2015-05-10 11:46:51 -04:00
parent 87caa09b61
commit 37af085ec5
7 changed files with 34 additions and 8 deletions

View File

@ -2,6 +2,7 @@
namespace WellRESTed\Routing\Route;
use WellRESTed\Dispatching\DispatcherInterface;
use WellRESTed\Routing\MethodMap;
/**
@ -9,6 +10,13 @@ use WellRESTed\Routing\MethodMap;
*/
class RouteFactory implements RouteFactoryInterface
{
private $dispatcher;
public function __construct(DispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
/**
* Creates a route for the given target.
*
@ -28,19 +36,19 @@ class RouteFactory implements RouteFactoryInterface
// PrefixRoutes end with *
if (substr($target, -1) === "*") {
return new PrefixRoute($target, new MethodMap());
return new PrefixRoute($target, new MethodMap($this->dispatcher));
}
// TempalateRoutes contain {variable}
if (preg_match(TemplateRoute::URI_TEMPLATE_EXPRESSION_RE, $target)) {
return new TemplateRoute($target, new MethodMap());
return new TemplateRoute($target, new MethodMap($this->dispatcher));
}
// StaticRoute
return new StaticRoute($target, new MethodMap());
return new StaticRoute($target, new MethodMap($this->dispatcher));
}
// Regex
return new RegexRoute($target, new MethodMap());
return new RegexRoute($target, new MethodMap($this->dispatcher));
}
}

View File

@ -10,6 +10,8 @@ use WellRESTed\Routing\Route\RouteInterface;
* @coversDefaultClass WellRESTed\Routing\Route\PrefixRoute
* @uses WellRESTed\Routing\Route\PrefixRoute
* @uses WellRESTed\Routing\Route\Route
* @group route
* @group routing
*/
class PrefixRouteTest extends \PHPUnit_Framework_TestCase
{

View File

@ -10,6 +10,8 @@ use WellRESTed\Routing\Route\RouteInterface;
* @coversDefaultClass WellRESTed\Routing\Route\RegexRoute
* @uses WellRESTed\Routing\Route\RegexRoute
* @uses WellRESTed\Routing\Route\Route
* @group route
* @group routing
*/
class RegexRouteTest extends \PHPUnit_Framework_TestCase
{

View File

@ -14,33 +14,42 @@ use WellRESTed\Routing\Route\RouteInterface;
* @uses WellRESTed\Routing\Route\StaticRoute
* @uses WellRESTed\Routing\Route\Route
* @uses WellRESTed\Routing\MethodMap
* @group route
* @group routing
*/
class RouteFactoryTest extends \PHPUnit_Framework_TestCase
{
private $dispatcher;
public function setUp()
{
$this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface');
}
public function testCreatesStaticRoute()
{
$factory = new RouteFactory();
$factory = new RouteFactory($this->dispatcher->reveal());
$route = $factory->create("/cats/");
$this->assertSame(RouteInterface::TYPE_STATIC, $route->getType());
}
public function testCreatesPrefixRoute()
{
$factory = new RouteFactory();
$factory = new RouteFactory($this->dispatcher->reveal());
$route = $factory->create("/cats/*");
$this->assertSame(RouteInterface::TYPE_PREFIX, $route->getType());
}
public function testCreatesRegexRoute()
{
$factory = new RouteFactory();
$factory = new RouteFactory($this->dispatcher->reveal());
$route = $factory->create("~/cat/[0-9]+~");
$this->assertSame(RouteInterface::TYPE_PATTERN, $route->getType());
}
public function testCreatesTemplateRoute()
{
$factory = new RouteFactory();
$factory = new RouteFactory($this->dispatcher->reveal());
$route = $factory->create("/cat/{id}");
$this->assertSame(RouteInterface::TYPE_PATTERN, $route->getType());
}

View File

@ -8,6 +8,7 @@ use Prophecy\Argument;
* @coversDefaultClass WellRESTed\Routing\Route\Route
* @uses WellRESTed\Routing\Route\Route
* @group route
* @group routing
*/
class RouteTest extends \PHPUnit_Framework_TestCase
{

View File

@ -10,6 +10,8 @@ use WellRESTed\Routing\Route\StaticRoute;
* @coversDefaultClass WellRESTed\Routing\Route\StaticRoute
* @uses WellRESTed\Routing\Route\StaticRoute
* @uses WellRESTed\Routing\Route\Route
* @group route
* @group routing
*/
class StaticRouteTest extends \PHPUnit_Framework_TestCase
{

View File

@ -10,6 +10,8 @@ use WellRESTed\Routing\Route\TemplateRoute;
* @covers WellRESTed\Routing\Route\TemplateRoute
* @uses WellRESTed\Routing\Route\RegexRoute
* @uses WellRESTed\Routing\Route\Route
* @group route
* @group routing
*/
class TemplateRouteTest extends \PHPUnit_Framework_TestCase
{