Pass DispatchProvider to Router on construction
This commit is contained in:
parent
37af085ec5
commit
6507028dd3
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace WellRESTed\Dispatching;
|
||||
|
||||
interface DispatchProvider
|
||||
interface DispatchProviderInterface
|
||||
{
|
||||
/**
|
||||
* @return DispatcherInterface
|
||||
|
|
|
|||
|
|
@ -4,12 +4,15 @@ namespace WellRESTed\Routing;
|
|||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use WellRESTed\Dispatching\DispatchProviderInterface;
|
||||
use WellRESTed\Routing\Route\RouteFactory;
|
||||
use WellRESTed\Routing\Route\RouteFactoryInterface;
|
||||
use WellRESTed\Routing\Route\RouteInterface;
|
||||
|
||||
class Router implements RouterInterface
|
||||
{
|
||||
/** @var DispatchProviderInterface */
|
||||
private $dispatchProvider;
|
||||
/** @var RouteFactoryInterface */
|
||||
private $factory;
|
||||
/** @var RouteInterface[] Array of Route objects */
|
||||
|
|
@ -21,9 +24,10 @@ class Router implements RouterInterface
|
|||
/** @var RouteInterface[] Hash array mapping path prefixes to routes */
|
||||
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->staticRoutes = [];
|
||||
$this->prefixRoutes = [];
|
||||
|
|
@ -91,11 +95,12 @@ class Router implements RouterInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* @param DispatcherInterface
|
||||
* @return RouteFactoryInterface
|
||||
*/
|
||||
protected function getRouteFactory()
|
||||
protected function getRouteFactory($dispatcher)
|
||||
{
|
||||
return new RouteFactory();
|
||||
return new RouteFactory($dispatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use WellRESTed\Routing\MethodMap;
|
|||
* @coversDefaultClass WellRESTed\Routing\MethodMap
|
||||
* @uses WellRESTed\Routing\MethodMap
|
||||
* @uses WellRESTed\Dispatching\Dispatcher
|
||||
* @group routing
|
||||
*/
|
||||
class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,12 +6,17 @@ use Prophecy\Argument;
|
|||
use WellRESTed\Routing\Route\RouteInterface;
|
||||
use WellRESTed\Routing\Router;
|
||||
|
||||
// TODO: register with array of middleware creates stack
|
||||
|
||||
/**
|
||||
* @coversDefaultClass WellRESTed\Routing\Router
|
||||
* @uses WellRESTed\Routing\Router
|
||||
* @group routing
|
||||
*/
|
||||
class RouterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $dispatcher;
|
||||
private $dispatchProvider;
|
||||
private $methodMap;
|
||||
private $factory;
|
||||
private $request;
|
||||
|
|
@ -42,6 +47,17 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
|||
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')
|
||||
->setMethods(["getRouteFactory"])
|
||||
->disableOriginalConstructor()
|
||||
|
|
@ -49,7 +65,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
|||
$this->router->expects($this->any())
|
||||
->method("getRouteFactory")
|
||||
->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 ::getRouteFactory
|
||||
* @uses WellRESTed\Routing\Route\RouteFactory
|
||||
*/
|
||||
public function testCreatesInstance()
|
||||
{
|
||||
$routeMap = new Router();
|
||||
$routeMap = new Router($this->dispatchProvider->reveal());
|
||||
$this->assertNotNull($routeMap);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue