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

View File

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

View File

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

View File

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

View File

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