Revise Route
This commit is contained in:
parent
7a53a02c5f
commit
58b5107289
|
|
@ -4,20 +4,41 @@ namespace WellRESTed\Routing\Route;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use WellRESTed\Routing\Dispatcher;
|
use WellRESTed\Routing\MethodMapInterface;
|
||||||
|
|
||||||
abstract class Route implements RouteInterface
|
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)
|
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
|
||||||
{
|
{
|
||||||
$dispatcher = new Dispatcher();
|
$this->methodMap->dispatch($request, $response);
|
||||||
$dispatcher->dispatch($this->middleware, $request, $response);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,17 +23,9 @@ interface RouteInterface extends MiddlewareInterface
|
||||||
public function getMethodMap();
|
public function getMethodMap();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Examines a path (request target) and returns whether or not the route
|
* Examines a path (request target) to see if it is a match for the route.
|
||||||
* should handle the request providing the target.
|
|
||||||
*
|
*
|
||||||
* 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
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function matchesRequestTarget($requestTarget, &$captures = null);
|
public function matchesRequestTarget($requestTarget);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,34 +3,65 @@
|
||||||
namespace WellRESTed\Test\Unit\Routing\Route;
|
namespace WellRESTed\Test\Unit\Routing\Route;
|
||||||
|
|
||||||
use Prophecy\Argument;
|
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\Route
|
||||||
* @uses WellRESTed\Routing\Route\StaticRoute
|
|
||||||
*/
|
*/
|
||||||
class RouteTest extends \PHPUnit_Framework_TestCase
|
class RouteTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $request;
|
/**
|
||||||
private $response;
|
* @covers ::__construct
|
||||||
|
*/
|
||||||
public function setUp()
|
public function testCreatesInstance()
|
||||||
{
|
{
|
||||||
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface");
|
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
|
||||||
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface");
|
$route = $this->getMockForAbstractClass(
|
||||||
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
|
'WellRESTed\Routing\Route\Route',
|
||||||
|
["/target", $methodMap->reveal()]);
|
||||||
|
$this->assertNotNull($route);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDispatchesMiddleware()
|
/**
|
||||||
|
* @covers ::getTarget
|
||||||
|
*/
|
||||||
|
public function testReturnsTarget()
|
||||||
{
|
{
|
||||||
$middleware = function ($request, &$response) {
|
$methodMap = $this->prophesize('WellRESTed\Routing\MethodMapInterface');
|
||||||
$response = $response->withStatus(200);
|
$route = $this->getMockForAbstractClass(
|
||||||
};
|
'WellRESTed\Routing\Route\Route',
|
||||||
$route = new StaticRoute("/", $middleware);
|
["/target", $methodMap->reveal()]);
|
||||||
$request = $this->request->reveal();
|
$this->assertSame("/target", $route->getTarget());
|
||||||
$response = $this->response->reveal();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
$route->dispatch($request, $response);
|
||||||
$this->response->withStatus(200)->shouldHaveBeenCalled();
|
|
||||||
|
$methodMap->dispatch(Argument::cetera())->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue