Rewrite RouterTest
This commit is contained in:
parent
121b8be044
commit
9083f2a444
|
|
@ -7,17 +7,20 @@ use WellRESTed\HttpExceptions\NotFoundException;
|
||||||
use WellRESTed\Routing\Router;
|
use WellRESTed\Routing\Router;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers WellRESTed\Routing\Router
|
* @coversDefaultClass WellRESTed\Routing\Router
|
||||||
|
* @uses WellRESTed\Routing\Router
|
||||||
|
* @uses WellRESTed\Message\Stream
|
||||||
* @uses WellRESTed\Routing\Dispatcher
|
* @uses WellRESTed\Routing\Dispatcher
|
||||||
* @uses WellRESTed\Routing\MethodMap
|
* @uses WellRESTed\Routing\MethodMap
|
||||||
* @uses WellRESTed\Routing\RouteTable
|
* @uses WellRESTed\Routing\ResponsePrep\ContentLengthPrep
|
||||||
* @uses WellRESTed\Routing\Route\RouteFactory
|
* @uses WellRESTed\Routing\ResponsePrep\HeadPrep
|
||||||
* @uses WellRESTed\Routing\Route\RegexRoute
|
|
||||||
* @uses WellRESTed\Routing\Route\TemplateRoute
|
|
||||||
* @uses WellRESTed\Routing\Route\PrefixRoute
|
* @uses WellRESTed\Routing\Route\PrefixRoute
|
||||||
* @uses WellRESTed\Routing\Route\StaticRoute
|
* @uses WellRESTed\Routing\Route\RegexRoute
|
||||||
* @uses WellRESTed\Routing\Route\Route
|
* @uses WellRESTed\Routing\Route\Route
|
||||||
* @uses WellRESTed\Message\Stream
|
* @uses WellRESTed\Routing\Route\RouteFactory
|
||||||
|
* @uses WellRESTed\Routing\Route\StaticRoute
|
||||||
|
* @uses WellRESTed\Routing\Route\TemplateRoute
|
||||||
|
* @uses WellRESTed\Routing\RouteTable
|
||||||
*/
|
*/
|
||||||
class RouterTest extends \PHPUnit_Framework_TestCase
|
class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -29,20 +32,128 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->dispatcher = $this->prophesize('\WellRESTed\Routing\DispatcherInterface');
|
$this->dispatcher = $this->prophesize('WellRESTed\Routing\DispatcherInterface');
|
||||||
$this->dispatcher->dispatch(Argument::any())->willReturn();
|
$this->dispatcher->dispatch(Argument::any())->willReturn();
|
||||||
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface");
|
$this->middleware = $this->prophesize('WellRESTed\Routing\MiddlewareInterface');
|
||||||
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface");
|
$this->middleware->dispatch(Argument::cetera())->willReturn();
|
||||||
|
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
|
||||||
|
$this->request->getRequestTarget()->willReturn("/");
|
||||||
|
$this->request->getMethod()->willReturn("GET");
|
||||||
|
$this->responder = $this->prophesize('WellRESTed\Routing\ResponderInterface');
|
||||||
|
$this->responder->respond(Argument::any())->willReturn();
|
||||||
|
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
|
||||||
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
|
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
|
||||||
$this->response->withBody(Argument::any())->willReturn($this->response->reveal());
|
$this->response->withBody(Argument::any())->willReturn($this->response->reveal());
|
||||||
|
$this->response->hasHeader("Content-length")->willReturn(true);
|
||||||
$this->response->getStatusCode()->willReturn(200);
|
$this->response->getStatusCode()->willReturn(200);
|
||||||
$this->middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface");
|
|
||||||
$this->middleware->dispatch(Argument::cetera())->willReturn();
|
|
||||||
$this->responder = $this->prophesize("\\WellRESTed\\Routing\\ResponderInterface");
|
|
||||||
$this->responder->respond(Argument::any())->willReturn();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDispatchesRoute()
|
// ------------------------------------------------------------------------
|
||||||
|
// Construction
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::__construct
|
||||||
|
*/
|
||||||
|
public function testCreatesInstance()
|
||||||
|
{
|
||||||
|
$router = new Router();
|
||||||
|
$this->assertNotNull($router);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// Adding routes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::add
|
||||||
|
*/
|
||||||
|
public function testAddWithSimpleRouteRegistersRoute()
|
||||||
|
{
|
||||||
|
$factory = $this->prophesize('WellRESTed\Routing\Route\RouteFactoryInterface');
|
||||||
|
$factory->registerRoute(Argument::cetera())->willReturn();
|
||||||
|
|
||||||
|
$router = $this->getMockBuilder('WellRESTed\Routing\Router')
|
||||||
|
->setMethods(["getRouteFactory"])
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getRouteFactory")
|
||||||
|
->will($this->returnValue($factory->reveal()));
|
||||||
|
$router->__construct();
|
||||||
|
|
||||||
|
$target = "/cats/";
|
||||||
|
$middleware = $this->middleware->reveal();
|
||||||
|
$router->add($target, $middleware);
|
||||||
|
|
||||||
|
$factory->registerRoute(Argument::any(), $target, $middleware, Argument::any())->shouldHaveBeenCalled();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::add
|
||||||
|
*/
|
||||||
|
public function testAddWithMapAddsMiddlewareToMethodMap()
|
||||||
|
{
|
||||||
|
$map = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
|
||||||
|
$map->addMap(Argument::any())->willReturn();
|
||||||
|
|
||||||
|
$factory = $this->prophesize('WellRESTed\Routing\Route\RouteFactoryInterface');
|
||||||
|
$factory->registerRoute(Argument::cetera())->willReturn();
|
||||||
|
|
||||||
|
$router = $this->getMockBuilder('WellRESTed\Routing\Router')
|
||||||
|
->setMethods(["getRouteFactory", "getMethodMap"])
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getMethodMap")
|
||||||
|
->will($this->returnValue($map->reveal()));
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getRouteFactory")
|
||||||
|
->will($this->returnValue($factory->reveal()));
|
||||||
|
$router->__construct();
|
||||||
|
|
||||||
|
$target = "/cats/";
|
||||||
|
$middleware = ["GET" => $this->middleware->reveal()];
|
||||||
|
$router->add($target, $middleware);
|
||||||
|
|
||||||
|
$map->addMap($middleware)->shouldHaveBeenCalled();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::add
|
||||||
|
*/
|
||||||
|
public function testAddWithMapRegistersMethodMap()
|
||||||
|
{
|
||||||
|
$map = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
|
||||||
|
$map->addMap(Argument::any())->willReturn();
|
||||||
|
|
||||||
|
$factory = $this->prophesize('WellRESTed\Routing\Route\RouteFactoryInterface');
|
||||||
|
$factory->registerRoute(Argument::cetera())->willReturn();
|
||||||
|
|
||||||
|
$router = $this->getMockBuilder('WellRESTed\Routing\Router')
|
||||||
|
->setMethods(["getRouteFactory", "getMethodMap"])
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getMethodMap")
|
||||||
|
->will($this->returnValue($map->reveal()));
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getRouteFactory")
|
||||||
|
->will($this->returnValue($factory->reveal()));
|
||||||
|
$router->__construct();
|
||||||
|
|
||||||
|
$target = "/cats/";
|
||||||
|
$middleware = ["GET" => $this->middleware->reveal()];
|
||||||
|
$router->add($target, $middleware);
|
||||||
|
|
||||||
|
$factory->registerRoute(Argument::any(), $target, $map, Argument::any())->shouldHaveBeenCalled();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// Dispatching Routes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::dispatch
|
||||||
|
*/
|
||||||
|
public function testDispatchesMatchedRoute()
|
||||||
{
|
{
|
||||||
$this->request->getRequestTarget()->willReturn("/cats/");
|
$this->request->getRequestTarget()->willReturn("/cats/");
|
||||||
|
|
||||||
|
|
@ -56,6 +167,13 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->middleware->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
$this->middleware->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// Hooks
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::addPreRouteHook
|
||||||
|
* @covers ::disptachPreRouteHooks
|
||||||
|
*/
|
||||||
public function testDispatchesPreRouteHooks()
|
public function testDispatchesPreRouteHooks()
|
||||||
{
|
{
|
||||||
$hook = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface');
|
$hook = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface');
|
||||||
|
|
@ -74,6 +192,10 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
$hook->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
$hook->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::addPostRouteHook
|
||||||
|
* @covers ::disptachPostRouteHooks
|
||||||
|
*/
|
||||||
public function testDispatchesPostRouteHooks()
|
public function testDispatchesPostRouteHooks()
|
||||||
{
|
{
|
||||||
$hook = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface');
|
$hook = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface');
|
||||||
|
|
@ -92,7 +214,58 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
$hook->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
$hook->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRespondsWithErrorResponseForHttpException()
|
/**
|
||||||
|
* @covers ::addResponsePreparationHook
|
||||||
|
* @covers ::dispatchResponsePreparationHooks
|
||||||
|
*/
|
||||||
|
public function testDispatchesResponsePreparationHooks()
|
||||||
|
{
|
||||||
|
$hook = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface');
|
||||||
|
$hook->dispatch(Argument::cetera())->willReturn();
|
||||||
|
|
||||||
|
$this->request->getRequestTarget()->willReturn("/cats/");
|
||||||
|
|
||||||
|
$router = new Router();
|
||||||
|
$router->addResponsePreparationHook($hook->reveal());
|
||||||
|
$router->add("/cats/", $this->middleware->reveal());
|
||||||
|
|
||||||
|
$request = $this->request->reveal();
|
||||||
|
$response = $this->response->reveal();
|
||||||
|
$router->dispatch($request, $response);
|
||||||
|
|
||||||
|
$hook->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// Status Handlers
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::dispatch
|
||||||
|
* @covers ::setStatusHandler
|
||||||
|
*/
|
||||||
|
public function testDispatchesHandlerForStatusCode()
|
||||||
|
{
|
||||||
|
$this->response->getStatusCode()->willReturn(403);
|
||||||
|
|
||||||
|
$statusMiddleware = $this->prophesize('\WellRESTed\Routing\MiddlewareInterface');
|
||||||
|
$statusMiddleware->dispatch(Argument::cetera())->willReturn();
|
||||||
|
|
||||||
|
$router = new Router();
|
||||||
|
$router->add("/cats/", $this->middleware->reveal());
|
||||||
|
$router->setStatusHandler(403, $statusMiddleware->reveal());
|
||||||
|
|
||||||
|
$request = $this->request->reveal();
|
||||||
|
$response = $this->response->reveal();
|
||||||
|
$router->dispatch($request, $response);
|
||||||
|
|
||||||
|
$statusMiddleware->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::dispatch
|
||||||
|
* @covers ::setStatusHandler
|
||||||
|
*/
|
||||||
|
public function testDispatchesHandlerForStatusCodeForHttpException()
|
||||||
{
|
{
|
||||||
$this->request->getRequestTarget()->willReturn("/cats/");
|
$this->request->getRequestTarget()->willReturn("/cats/");
|
||||||
$this->middleware->dispatch(Argument::cetera())->willThrow(new NotFoundException());
|
$this->middleware->dispatch(Argument::cetera())->willThrow(new NotFoundException());
|
||||||
|
|
@ -107,86 +280,65 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->response->withStatus(404)->shouldHaveBeenCalled();
|
$this->response->withStatus(404)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDispatchesErrorHandlerForStatusCode()
|
// ------------------------------------------------------------------------
|
||||||
{
|
// Respond
|
||||||
$this->response->getStatusCode()->willReturn(403);
|
|
||||||
|
|
||||||
$statusMiddleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface");
|
|
||||||
$statusMiddleware->dispatch(Argument::cetera())->willReturn();
|
|
||||||
|
|
||||||
$router = new Router();
|
|
||||||
$router->add("/cats/", $this->middleware->reveal());
|
|
||||||
$router->setStatusHandler(403, $statusMiddleware->reveal());
|
|
||||||
|
|
||||||
$request = $this->request->reveal();
|
|
||||||
$response = $this->response->reveal();
|
|
||||||
$router->dispatch($request, $response);
|
|
||||||
|
|
||||||
$statusMiddleware->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRegisterRouteWithMethodMap()
|
|
||||||
{
|
|
||||||
$router = new SettableRouter();
|
|
||||||
$methodMap = $this->prophesize('\WellRESTed\Routing\MethodMapInterface');
|
|
||||||
$router->methodMap = $methodMap->reveal();
|
|
||||||
|
|
||||||
$router->add("/cats/", ["GET" => $this->middleware->reveal()]);
|
|
||||||
$methodMap->addMap(["GET" => $this->middleware->reveal()])->shouldHaveBeenCalled();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::respond
|
||||||
|
*/
|
||||||
public function testRespondDispatchesRequest()
|
public function testRespondDispatchesRequest()
|
||||||
{
|
{
|
||||||
$this->request->getRequestTarget()->willReturn("/cats/");
|
$target = "/cats/";
|
||||||
|
|
||||||
$router = new SettableRouter();
|
$this->request->getRequestTarget()->willReturn($target);
|
||||||
$router->request = $this->request->reveal();
|
$this->responder->respond(Argument::any())->willReturn();
|
||||||
$router->response = $this->response->reveal();
|
|
||||||
$router->responder = $this->responder->reveal();
|
$router = $this->getMockBuilder('WellRESTed\Routing\Router')
|
||||||
$router->add("/cats/", $this->middleware->reveal());
|
->setMethods(["getRequest", "getResponse", "getResponder"])
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getRequest")
|
||||||
|
->will($this->returnValue($this->request->reveal()));
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getResponse")
|
||||||
|
->will($this->returnValue($this->response->reveal()));
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getResponder")
|
||||||
|
->will($this->returnValue($this->responder->reveal()));
|
||||||
|
$router->__construct();
|
||||||
|
$router->add($target, $this->middleware->reveal());
|
||||||
$router->respond();
|
$router->respond();
|
||||||
|
|
||||||
$this->middleware->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
$this->middleware->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
/**
|
||||||
|
* @covers ::respond
|
||||||
/**
|
*/
|
||||||
* Overrides the methods that return new instances to return public ivars for
|
public function testSendsResponseToResponder()
|
||||||
* easy testing.
|
|
||||||
*/
|
|
||||||
class SettableRouter extends Router
|
|
||||||
{
|
|
||||||
public $dispatcher;
|
|
||||||
public $methodMap;
|
|
||||||
public $request;
|
|
||||||
public $response;
|
|
||||||
public $responder;
|
|
||||||
|
|
||||||
public function getDispatcher()
|
|
||||||
{
|
{
|
||||||
return $this->dispatcher;
|
$target = "/cats/";
|
||||||
}
|
|
||||||
|
|
||||||
public function getMethodMap()
|
$this->request->getRequestTarget()->willReturn($target);
|
||||||
{
|
|
||||||
return $this->methodMap ?: parent::getMethodMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getRequest()
|
$router = $this->getMockBuilder('WellRESTed\Routing\Router')
|
||||||
{
|
->setMethods(["getRequest", "getResponse", "getResponder"])
|
||||||
return $this->request ?: parent::getRequest();
|
->disableOriginalConstructor()
|
||||||
}
|
->getMock();
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getRequest")
|
||||||
|
->will($this->returnValue($this->request->reveal()));
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getResponse")
|
||||||
|
->will($this->returnValue($this->response->reveal()));
|
||||||
|
$router->expects($this->any())
|
||||||
|
->method("getResponder")
|
||||||
|
->will($this->returnValue($this->responder->reveal()));
|
||||||
|
$router->__construct();
|
||||||
|
$router->add($target, $this->middleware->reveal());
|
||||||
|
$router->respond();
|
||||||
|
|
||||||
public function getResponse()
|
$this->responder->respond($this->response->reveal())->shouldHaveBeenCalled();
|
||||||
{
|
|
||||||
return $this->response ?: parent::getResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getResponder()
|
|
||||||
{
|
|
||||||
return $this->responder ?: parent::getResponder();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue