Add Router::respond

This commit is contained in:
PJ Dietz 2015-04-12 11:49:48 -04:00
parent 90b9503c72
commit 6e83b6b050
3 changed files with 110 additions and 9 deletions

View File

@ -6,7 +6,7 @@ use WellRESTed\Routing\Route\PrefixRouteInterface;
use WellRESTed\Routing\Route\RouteInterface;
use WellRESTed\Routing\Route\StaticRouteInterface;
interface RouteTableInterface
interface RouteTableInterface extends MiddlewareInterface
{
public function addRoute(RouteInterface $route);

View File

@ -5,6 +5,8 @@ namespace WellRESTed\Routing;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\HttpExceptions\HttpException;
use WellRESTed\Message\Response;
use WellRESTed\Message\ServerRequest;
use WellRESTed\Routing\Route\RouteFactory;
use WellRESTed\Routing\Route\RouteFactoryInterface;
use WellRESTed\Stream\StringStream;
@ -21,7 +23,7 @@ class Router implements MiddlewareInterface
public function __construct()
{
$this->routeFactory = $this->getRouteFactory();
$this->routeTable = new RouteTable();
$this->routeTable = $this->getRouteTable();
$this->statusHandlers = [];
}
@ -72,6 +74,21 @@ class Router implements MiddlewareInterface
}
}
public function respond()
{
$request = $this->getRequest();
$response = $this->getResponse();
$this->dispatch($request, $response);
$responder = $this->getResponder();
$responder->respond($response);
}
// ------------------------------------------------------------------------
// The following methods provide instaces the router will use. Override
// to provide custom classes or configured instances.
// @codeCoverageIgnoreStart
/**
* Return an instance that can dispatch middleware.
* Override to provide a custom class.
@ -91,6 +108,30 @@ class Router implements MiddlewareInterface
return new MethodMap();
}
/**
* @return ServerRequestInterface
*/
protected function getRequest()
{
return ServerRequest::getServerRequest();
}
/**
* @return ResponderInterface
*/
protected function getResponder()
{
return new Responder();
}
/**
* @return ResponseInterface
*/
protected function getResponse()
{
return new Response();
}
/**
* @return RouteFactoryInterface
*/
@ -98,4 +139,14 @@ class Router implements MiddlewareInterface
{
return new RouteFactory();
}
/**
* @return RouteTableInterface
*/
protected function getRouteTable()
{
return new RouteTable();
}
// @codeCoverageIgnoreEnd
}

View File

@ -22,9 +22,10 @@ use WellRESTed\Routing\Router;
*/
class RouterTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
private $middleware;
private $request;
private $responder;
private $response;
public function setUp()
{
@ -35,9 +36,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$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 testDispatchedRoute()
public function testDispatchesRoute()
{
$this->request->getRequestTarget()->willReturn("/cats/");
@ -77,13 +80,60 @@ class RouterTest extends \PHPUnit_Framework_TestCase
public function testRegisterRouteWithMethodMap()
{
$this->request->getRequestTarget()->willReturn("/cats/");
$this->request->getMethod()->willReturn("GET");
$router = new SettableRouter();
$methodMap = $this->prophesize('\WellRESTed\Routing\MethodMapInterface');
$router->methodMap = $methodMap->reveal();
$router = new Router();
$router->add("/cats/", ["GET" => $this->middleware->reveal()]);
$router->dispatch($this->request->reveal(), $this->response->reveal());
$methodMap->addMap(["GET" => $this->middleware->reveal()])->shouldHaveBeenCalled();
}
public function testRespondDispatchesRequest()
{
$this->request->getRequestTarget()->willReturn("/cats/");
$router = new SettableRouter();
$router->request = $this->request->reveal();
$router->response = $this->response->reveal();
$router->responder = $this->responder->reveal();
$router->add("/cats/", $this->middleware->reveal());
$router->respond();
$this->middleware->dispatch(Argument::cetera())->shouldHaveBeenCalled();
}
}
// ----------------------------------------------------------------------------
/**
* Overrides the methods that return new instances to return public ivars for
* easy testing.
*/
class SettableRouter extends Router
{
public $methodMap;
public $request;
public $response;
public $responder;
public function getMethodMap()
{
return $this->methodMap ?: parent::getMethodMap();
}
public function getRequest()
{
return $this->request ?: parent::getRequest();
}
public function getResponse()
{
return $this->response ?: parent::getResponse();
}
public function getResponder()
{
return $this->responder ?: parent::getResponder();
}
}