Revise Route

This commit is contained in:
PJ Dietz 2015-05-07 23:14:48 -04:00
parent 7a53a02c5f
commit 58b5107289
3 changed files with 78 additions and 34 deletions

View File

@ -4,20 +4,41 @@ namespace WellRESTed\Routing\Route;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Routing\Dispatcher;
use WellRESTed\Routing\MethodMapInterface;
abstract class Route implements RouteInterface
{
private $middleware;
/** @var string */
protected $target;
/** @var MethodMapInterface */
protected $methodMap;
public function __construct($middleware)
public function __construct($target, $methodMap)
{
$this->middleware = $middleware;
$this->target = $target;
$this->methodMap = $methodMap;
}
/**
* Return the instance mapping methods to middleware for this route.
*
* @return MethodMapInterface
*/
public function getMethodMap()
{
return $this->methodMap;
}
/**
* @return string
*/
public function getTarget()
{
return $this->target;
}
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
{
$dispatcher = new Dispatcher();
$dispatcher->dispatch($this->middleware, $request, $response);
$this->methodMap->dispatch($request, $response);
}
}

View File

@ -23,17 +23,9 @@ interface RouteInterface extends MiddlewareInterface
public function getMethodMap();
/**
* Examines a path (request target) and returns whether or not the route
* should handle the request providing the target.
* Examines a path (request target) to see if it is a match for the route.
*
* If a successful examination also extracts items (such as captures from
* matching a regular expression), store them to $captures.
*
* $captures should have no meaning for calls that return false.
*
* @param string $requestTarget
* @param array $captures
* @return boolean
*/
public function matchesRequestTarget($requestTarget, &$captures = null);
public function matchesRequestTarget($requestTarget);
}

View File

@ -3,34 +3,65 @@
namespace WellRESTed\Test\Unit\Routing\Route;
use Prophecy\Argument;
use WellRESTed\Routing\Route\Route;
use WellRESTed\Routing\Route\StaticRoute;
/**
* @coversDefaultClass WellRESTed\Routing\Route\Route
* @uses WellRESTed\Routing\Route\Route
* @uses WellRESTed\Routing\Route\StaticRoute
*/
class RouteTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
public function setUp()
/**
* @covers ::__construct
*/
public function testCreatesInstance()
{
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface");
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface");
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = $this->getMockForAbstractClass(
'WellRESTed\Routing\Route\Route',
["/target", $methodMap->reveal()]);
$this->assertNotNull($route);
}
public function testDispatchesMiddleware()
/**
* @covers ::getTarget
*/
public function testReturnsTarget()
{
$middleware = function ($request, &$response) {
$response = $response->withStatus(200);
};
$route = new StaticRoute("/", $middleware);
$request = $this->request->reveal();
$response = $this->response->reveal();
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = $this->getMockForAbstractClass(
'WellRESTed\Routing\Route\Route',
["/target", $methodMap->reveal()]);
$this->assertSame("/target", $route->getTarget());
}
/**
* @covers ::getMethodMap
*/
public function testReturnsMethodMap()
{
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$route = $this->getMockForAbstractClass(
'WellRESTed\Routing\Route\Route',
["/target", $methodMap->reveal()]);
$this->assertSame($methodMap->reveal(), $route->getMethodMap());
}
/**
* @covers ::dispatch
*/
public function testDispatchesMethodMap()
{
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
$methodMap->dispatch(Argument::cetera())->willReturn();
$route = $this->getMockForAbstractClass(
'WellRESTed\Routing\Route\Route',
["/target", $methodMap->reveal()]);
$request = $this->prophesize('Psr\Http\Message\ServerRequestInterface')->reveal();
$response = $this->prophesize('Psr\Http\Message\ResponseInterface')->reveal();
$route->dispatch($request, $response);
$this->response->withStatus(200)->shouldHaveBeenCalled();
$methodMap->dispatch(Argument::cetera())->shouldHaveBeenCalled();
}
}