Pass DispatchProvider to Router on construction

This commit is contained in:
PJ Dietz 2015-05-10 12:04:36 -04:00
parent 37af085ec5
commit 6507028dd3
4 changed files with 30 additions and 7 deletions

View File

@ -2,7 +2,7 @@
namespace WellRESTed\Dispatching; namespace WellRESTed\Dispatching;
interface DispatchProvider interface DispatchProviderInterface
{ {
/** /**
* @return DispatcherInterface * @return DispatcherInterface

View File

@ -4,12 +4,15 @@ namespace WellRESTed\Routing;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Dispatching\DispatchProviderInterface;
use WellRESTed\Routing\Route\RouteFactory; use WellRESTed\Routing\Route\RouteFactory;
use WellRESTed\Routing\Route\RouteFactoryInterface; use WellRESTed\Routing\Route\RouteFactoryInterface;
use WellRESTed\Routing\Route\RouteInterface; use WellRESTed\Routing\Route\RouteInterface;
class Router implements RouterInterface class Router implements RouterInterface
{ {
/** @var DispatchProviderInterface */
private $dispatchProvider;
/** @var RouteFactoryInterface */ /** @var RouteFactoryInterface */
private $factory; private $factory;
/** @var RouteInterface[] Array of Route objects */ /** @var RouteInterface[] Array of Route objects */
@ -21,9 +24,10 @@ class Router implements RouterInterface
/** @var RouteInterface[] Hash array mapping path prefixes to routes */ /** @var RouteInterface[] Hash array mapping path prefixes to routes */
private $patternRoutes; private $patternRoutes;
public function __construct() public function __construct(DispatchProviderInterface $dispatchProvider)
{ {
$this->factory = $this->getRouteFactory(); $this->dispatchProvider = $dispatchProvider;
$this->factory = $this->getRouteFactory($this->dispatchProvider->getDispatcher());
$this->routes = []; $this->routes = [];
$this->staticRoutes = []; $this->staticRoutes = [];
$this->prefixRoutes = []; $this->prefixRoutes = [];
@ -91,11 +95,12 @@ class Router implements RouterInterface
} }
/** /**
* @param DispatcherInterface
* @return RouteFactoryInterface * @return RouteFactoryInterface
*/ */
protected function getRouteFactory() protected function getRouteFactory($dispatcher)
{ {
return new RouteFactory(); return new RouteFactory($dispatcher);
} }
/** /**

View File

@ -9,6 +9,7 @@ use WellRESTed\Routing\MethodMap;
* @coversDefaultClass WellRESTed\Routing\MethodMap * @coversDefaultClass WellRESTed\Routing\MethodMap
* @uses WellRESTed\Routing\MethodMap * @uses WellRESTed\Routing\MethodMap
* @uses WellRESTed\Dispatching\Dispatcher * @uses WellRESTed\Dispatching\Dispatcher
* @group routing
*/ */
class MethodMapTest extends \PHPUnit_Framework_TestCase class MethodMapTest extends \PHPUnit_Framework_TestCase
{ {

View File

@ -6,12 +6,17 @@ use Prophecy\Argument;
use WellRESTed\Routing\Route\RouteInterface; use WellRESTed\Routing\Route\RouteInterface;
use WellRESTed\Routing\Router; use WellRESTed\Routing\Router;
// TODO: register with array of middleware creates stack
/** /**
* @coversDefaultClass WellRESTed\Routing\Router * @coversDefaultClass WellRESTed\Routing\Router
* @uses WellRESTed\Routing\Router * @uses WellRESTed\Routing\Router
* @group routing
*/ */
class RouterTest extends \PHPUnit_Framework_TestCase class RouterTest extends \PHPUnit_Framework_TestCase
{ {
private $dispatcher;
private $dispatchProvider;
private $methodMap; private $methodMap;
private $factory; private $factory;
private $request; private $request;
@ -42,6 +47,17 @@ class RouterTest extends \PHPUnit_Framework_TestCase
return $response; return $response;
}; };
$this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface');
$this->dispatcher->dispatch(Argument::cetera())->will(
function ($args) {
list($middleware, $request, $response, $next) = $args;
return $middleware->dispatch($request, $response, $next);
}
);
$this->dispatchProvider = $this->prophesize('WellRESTed\Dispatching\DispatchProviderInterface');
$this->dispatchProvider->getDispatcher()->willReturn($this->dispatcher->reveal());
$this->router = $this->getMockBuilder('WellRESTed\Routing\Router') $this->router = $this->getMockBuilder('WellRESTed\Routing\Router')
->setMethods(["getRouteFactory"]) ->setMethods(["getRouteFactory"])
->disableOriginalConstructor() ->disableOriginalConstructor()
@ -49,7 +65,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->router->expects($this->any()) $this->router->expects($this->any())
->method("getRouteFactory") ->method("getRouteFactory")
->will($this->returnValue($this->factory->reveal())); ->will($this->returnValue($this->factory->reveal()));
$this->router->__construct(); $this->router->__construct($this->dispatchProvider->reveal());
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -58,10 +74,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers ::__construct * @covers ::__construct
* @covers ::getRouteFactory * @covers ::getRouteFactory
* @uses WellRESTed\Routing\Route\RouteFactory
*/ */
public function testCreatesInstance() public function testCreatesInstance()
{ {
$routeMap = new Router(); $routeMap = new Router($this->dispatchProvider->reveal());
$this->assertNotNull($routeMap); $this->assertNotNull($routeMap);
} }