Propagate $request and $args to route and errorHandler callables
This commit is contained in:
parent
d785e21fee
commit
812012bdbf
|
|
@ -12,13 +12,6 @@ namespace pjdietz\WellRESTed\Interfaces\Routes;
|
|||
|
||||
interface PrefixRouteInterface
|
||||
{
|
||||
/**
|
||||
* Returns the target class this maps to.
|
||||
*
|
||||
* @return string Fully qualified name for a HandlerInterface
|
||||
*/
|
||||
public function getHandler();
|
||||
|
||||
/**
|
||||
* Returns the path prefixes this maps to a target handler.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -12,13 +12,6 @@ namespace pjdietz\WellRESTed\Interfaces\Routes;
|
|||
|
||||
interface StaticRouteInterface
|
||||
{
|
||||
/**
|
||||
* Returns the target class this maps to.
|
||||
*
|
||||
* @return string Fully qualified name for a HandlerInterface
|
||||
*/
|
||||
public function getHandler();
|
||||
|
||||
/**
|
||||
* Returns the paths this maps to a target handler.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -49,18 +49,16 @@ class RouteTable implements HandlerInterface
|
|||
{
|
||||
$response = null;
|
||||
|
||||
$path = $request->getPath();
|
||||
|
||||
// First check if there is a handler for this exact path.
|
||||
$handler = $this->getStaticHandler($path);
|
||||
if ($handler) {
|
||||
return $handler->getResponse($request, $args);
|
||||
// First check if there is a static route.
|
||||
$response = $this->getStaticResponse($request, $args);
|
||||
if ($response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
// Check prefix routes for any routes that match. Use the longest matching prefix.
|
||||
$handler = $this->getPrefixHandler($path);
|
||||
if ($handler) {
|
||||
return $handler->getResponse($request, $args);
|
||||
$response = $this->getPrefixResponse($request, $args);
|
||||
if ($response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
// Try each of the routes.
|
||||
|
|
@ -76,16 +74,18 @@ class RouteTable implements HandlerInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the handler associated with the matching static route, or null if none match.
|
||||
* Return the response associated with the matching static route, or null if none match.
|
||||
*
|
||||
* @param $path string The request's path
|
||||
* @return HandlerInterface|null
|
||||
* @param RequestInterface $request
|
||||
* @param array|null $args
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
private function getStaticHandler($path)
|
||||
private function getStaticResponse(RequestInterface $request, array $args = null)
|
||||
{
|
||||
$path = $request->getPath();
|
||||
if (isset($this->staticRoutes[$path])) {
|
||||
$route = $this->staticRoutes[$path];
|
||||
return $route->getHandler();
|
||||
return $route->getResponse($request, $args);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -93,11 +93,14 @@ class RouteTable implements HandlerInterface
|
|||
/**
|
||||
* Returning the best-matching prefix handler, or null if none match.
|
||||
*
|
||||
* @param $path string The request's path
|
||||
* @return HandlerInterface|null
|
||||
* @param RequestInterface $request
|
||||
* @param array|null $args
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
private function getPrefixHandler($path)
|
||||
private function getPrefixResponse(RequestInterface $request, array $args = null)
|
||||
{
|
||||
$path = $request->getPath();
|
||||
|
||||
// Find all prefixes that match the start of this path.
|
||||
$prefixes = array_keys($this->prefixRoutes);
|
||||
$matches = array_filter(
|
||||
|
|
@ -115,9 +118,8 @@ class RouteTable implements HandlerInterface
|
|||
};
|
||||
usort($matches, $compareByLength);
|
||||
}
|
||||
// Instantiate and return the handler identified as the best match.
|
||||
$route = $this->prefixRoutes[$matches[0]];
|
||||
return $route->getHandler();
|
||||
return $route->getResponse($request, $args);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ class Router implements HandlerInterface
|
|||
* Add a custom error handler.
|
||||
*
|
||||
* @param integer $statusCode The error status code
|
||||
* @param callable|string|HandlerInterface $errorHandler
|
||||
* @param mixed $errorHandler
|
||||
*/
|
||||
public function setErrorHandler($statusCode, $errorHandler)
|
||||
{
|
||||
|
|
@ -168,14 +168,17 @@ class Router implements HandlerInterface
|
|||
private function getErrorResponse($status, $request, $args = null, $response = null)
|
||||
{
|
||||
if (isset($this->errorHandlers[$status])) {
|
||||
$unpacker = new HandlerUnpacker();
|
||||
$errorHandler = $unpacker->unpack($this->errorHandlers[$status]);
|
||||
// Pass the response triggering this along to the error handler.
|
||||
$errorArgs = array("response" => $response);
|
||||
if ($args) {
|
||||
$errorArgs = array_merge($args, $errorArgs);
|
||||
}
|
||||
return $errorHandler->getResponse($request, $errorArgs);
|
||||
$unpacker = new HandlerUnpacker();
|
||||
$handler = $unpacker->unpack($this->errorHandlers[$status], $request, $errorArgs);
|
||||
if (!is_null($handler) && $handler instanceof HandlerInterface) {
|
||||
return $handler->getResponse($request, $errorArgs);
|
||||
}
|
||||
return $handler;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ namespace pjdietz\WellRESTed\Routes;
|
|||
|
||||
use pjdietz\WellRESTed\HandlerUnpacker;
|
||||
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
|
||||
use pjdietz\WellRESTed\Interfaces\RequestInterface;
|
||||
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Base class for Routes.
|
||||
|
|
@ -37,14 +39,19 @@ abstract class BaseRoute implements HandlerInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the assigned handler
|
||||
* Return the handled response from the target.
|
||||
*
|
||||
* @throws \UnexpectedValueException
|
||||
* @return HandlerInterface
|
||||
* @param RequestInterface $request The request to respond to.
|
||||
* @param array|null $args Optional additional arguments.
|
||||
* @return ResponseInterface The response.
|
||||
*/
|
||||
protected function getTarget()
|
||||
protected function getResponseFromTarget(RequestInterface $request, array $args = null)
|
||||
{
|
||||
$unpacker = new HandlerUnpacker();
|
||||
return $unpacker->unpack($this->target);
|
||||
$target = $unpacker->unpack($this->target, $request, $args);
|
||||
if (!is_null($target) && $target instanceof HandlerInterface) {
|
||||
return $target->getResponse($request, $args);
|
||||
}
|
||||
return $target;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,13 +58,15 @@ class PrefixRoute extends BaseRoute implements PrefixRouteInterface
|
|||
$requestPath = $request->getPath();
|
||||
foreach ($this->prefixes as $prefix) {
|
||||
if (strrpos($requestPath, $prefix, -strlen($requestPath)) !== false) {
|
||||
$target = $this->getTarget();
|
||||
return $target->getResponse($request, $args);
|
||||
return $this->getResponseFromTarget($request, $args);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
/* PrefixRouteInterface */
|
||||
|
||||
/**
|
||||
* Returns the path prefixes this maps to a target handler.
|
||||
*
|
||||
|
|
@ -74,14 +76,4 @@ class PrefixRoute extends BaseRoute implements PrefixRouteInterface
|
|||
{
|
||||
return $this->prefixes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the target class this maps to.
|
||||
*
|
||||
* @return string Fully qualified name for a HandlerInterface
|
||||
*/
|
||||
public function getHandler()
|
||||
{
|
||||
return $this->getTarget();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,12 +50,11 @@ class RegexRoute extends BaseRoute
|
|||
{
|
||||
$matched = @preg_match($this->getPattern(), $request->getPath(), $matches);
|
||||
if ($matched) {
|
||||
$target = $this->getTarget();
|
||||
if (is_null($args)) {
|
||||
$args = array();
|
||||
}
|
||||
$args = array_merge($args, $matches);
|
||||
return $target->getResponse($request, $args);
|
||||
return $this->getResponseFromTarget($request, $args);
|
||||
} elseif ($matched === false) {
|
||||
throw new ParseException("Invalid regular expression: " . $this->getPattern());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@
|
|||
namespace pjdietz\WellRESTed\Routes;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
|
||||
use pjdietz\WellRESTed\Interfaces\RequestInterface;
|
||||
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
|
||||
use pjdietz\WellRESTed\Interfaces\Routes\StaticRouteInterface;
|
||||
|
||||
/**
|
||||
|
|
@ -55,25 +57,14 @@ class StaticRoute extends BaseRoute implements StaticRouteInterface
|
|||
*/
|
||||
public function getResponse(RequestInterface $request, array $args = null)
|
||||
{
|
||||
$requestPath = $request->getPath();
|
||||
foreach ($this->paths as $path) {
|
||||
if ($path === $requestPath) {
|
||||
$target = $this->getTarget();
|
||||
return $target->getResponse($request, $args);
|
||||
}
|
||||
if (in_array($request->getPath(), $this->paths)) {
|
||||
return $this->getResponseFromTarget($request, $args);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the target class this maps to.
|
||||
*
|
||||
* @return string Fully qualified name for a HandlerInterface
|
||||
*/
|
||||
public function getHandler()
|
||||
{
|
||||
return $this->getTarget();
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
/* StaticRouteInterface */
|
||||
|
||||
/**
|
||||
* Returns the paths this maps to a target handler.
|
||||
|
|
|
|||
|
|
@ -32,11 +32,9 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testMatchesStaticRoute()
|
||||
{
|
||||
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface");
|
||||
$this->route->getPaths()->willReturn(["/cats/"]);
|
||||
$this->route->getHandler()->willReturn($this->handler->reveal());
|
||||
$this->route->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$this->request->getPath()->willReturn("/cats/");
|
||||
|
||||
|
|
@ -44,16 +42,14 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
|
|||
$table->addRoute($this->route->reveal());
|
||||
$table->getResponse($this->request->reveal());
|
||||
|
||||
$this->route->getHandler()->shouldHaveBeenCalled();
|
||||
$this->route->getResponse(Argument::cetera())->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testMatchesPrefixRoute()
|
||||
{
|
||||
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
|
||||
$this->route->getPrefixes()->willReturn(["/cats/"]);
|
||||
$this->route->getHandler()->willReturn($this->handler->reveal());
|
||||
$this->route->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$this->request->getPath()->willReturn("/cats/molly");
|
||||
|
||||
|
|
@ -61,22 +57,20 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
|
|||
$table->addRoute($this->route->reveal());
|
||||
$table->getResponse($this->request->reveal());
|
||||
|
||||
$this->route->getHandler()->shouldHaveBeenCalled();
|
||||
$this->route->getResponse(Argument::cetera())->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testMatchesBestPrefixRoute()
|
||||
{
|
||||
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$route1 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
|
||||
$route1->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
|
||||
$route1->getPrefixes()->willReturn(["/animals/"]);
|
||||
$route1->getHandler()->willReturn($this->handler->reveal());
|
||||
$route1->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$route2 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
|
||||
$route2->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
|
||||
$route2->getPrefixes()->willReturn(["/animals/cats/"]);
|
||||
$route2->getHandler()->willReturn($this->handler->reveal());
|
||||
$route2->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$this->request->getPath()->willReturn("/animals/cats/molly");
|
||||
|
||||
|
|
@ -85,23 +79,21 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
|
|||
$table->addRoute($route2->reveal());
|
||||
$table->getResponse($this->request->reveal());
|
||||
|
||||
$route1->getHandler()->shouldNotHaveBeenCalled();
|
||||
$route2->getHandler()->shouldHaveBeenCalled();
|
||||
$route1->getResponse(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
$route2->getResponse(Argument::cetera())->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testMatchesStaticRouteBeforePrefixRoute()
|
||||
{
|
||||
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$route1 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
|
||||
$route1->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
|
||||
$route1->getPrefixes()->willReturn(["/animals/cats/"]);
|
||||
$route1->getHandler()->willReturn($this->handler->reveal());
|
||||
$route1->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$route2 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
|
||||
$route2->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface");
|
||||
$route2->getPaths()->willReturn(["/animals/cats/molly"]);
|
||||
$route2->getHandler()->willReturn($this->handler->reveal());
|
||||
$route2->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$this->request->getPath()->willReturn("/animals/cats/molly");
|
||||
|
||||
|
|
@ -110,18 +102,16 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
|
|||
$table->addRoute($route2->reveal());
|
||||
$table->getResponse($this->request->reveal());
|
||||
|
||||
$route1->getHandler()->shouldNotHaveBeenCalled();
|
||||
$route2->getHandler()->shouldHaveBeenCalled();
|
||||
$route1->getResponse(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
$route2->getResponse(Argument::cetera())->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testMatchesPrefixRouteBeforeHandlerRoute()
|
||||
{
|
||||
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$route1 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
|
||||
$route1->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
|
||||
$route1->getPrefixes()->willReturn(["/animals/cats/"]);
|
||||
$route1->getHandler()->willReturn($this->handler->reveal());
|
||||
$route1->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$route2 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
|
||||
$route2->getResponse(Argument::cetera())->willReturn(null);
|
||||
|
|
@ -133,7 +123,7 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
|
|||
$table->addRoute($route2->reveal());
|
||||
$table->getResponse($this->request->reveal());
|
||||
|
||||
$route1->getHandler()->shouldHaveBeenCalled();
|
||||
$route1->getResponse(Argument::cetera())->shouldHaveBeenCalled();
|
||||
$route2->getResponse(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
|
|
@ -161,4 +151,51 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
|
|||
$route2->getResponse(Argument::cetera())->shouldHaveBeenCalled();
|
||||
$route3->getResponse(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testPropagatesArgumentsToStaticRoute()
|
||||
{
|
||||
$this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface");
|
||||
$this->route->getPaths()->willReturn(["/cats/"]);
|
||||
$this->route->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$this->request->getPath()->willReturn("/cats/");
|
||||
|
||||
$args = ["cat" => "molly"];
|
||||
|
||||
$table = new RouteTable();
|
||||
$table->addRoute($this->route->reveal());
|
||||
$table->getResponse($this->request->reveal(), $args);
|
||||
|
||||
$this->route->getResponse($this->request->reveal(), $args)->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testPropagatesArgumentsToPrefixRoute()
|
||||
{
|
||||
$this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
|
||||
$this->route->getPrefixes()->willReturn(["/cats/"]);
|
||||
$this->route->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$this->request->getPath()->willReturn("/cats/");
|
||||
|
||||
$args = ["cat" => "molly"];
|
||||
|
||||
$table = new RouteTable();
|
||||
$table->addRoute($this->route->reveal());
|
||||
$table->getResponse($this->request->reveal(), $args);
|
||||
|
||||
$this->route->getResponse($this->request->reveal(), $args)->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testPropagatesArwgumentsToRoute()
|
||||
{
|
||||
$this->route->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
$this->request->getPath()->willReturn("/cats/");
|
||||
$args = ["cat" => "molly"];
|
||||
|
||||
$table = new RouteTable();
|
||||
$table->addRoute($this->route->reveal());
|
||||
$table->getResponse($this->request->reveal(), $args);
|
||||
|
||||
$this->route->getResponse($this->request->reveal(), $args)->shouldHaveBeenCalled();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,6 +86,19 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
|||
$this->route->getResponse(Argument::cetera())->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testPropagatesArgumentsToRouteTable()
|
||||
{
|
||||
$this->route->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
$this->request->getPath()->willReturn("/cats/");
|
||||
$args = ["cat" => "molly"];
|
||||
|
||||
$router = new Router();
|
||||
$router->addRoute($this->route->reveal());
|
||||
$router->getResponse($this->request->reveal(), $args);
|
||||
|
||||
$this->route->getResponse($this->request->reveal(), $args)->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testRespondsWithErrorResponseForHttpException()
|
||||
{
|
||||
$this->route->getResponse(Argument::cetera())->willThrow(new HttpException());
|
||||
|
|
@ -193,6 +206,27 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
|||
)->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testDispatchesErrorCallable()
|
||||
{
|
||||
$this->request->getPath()->willReturn("/");
|
||||
$this->response->getStatusCode()->willReturn(403);
|
||||
$this->route->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$errorResponse = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\ResponseInterface");
|
||||
$errorResponse->respond()->willReturn();
|
||||
|
||||
$errorCallable = function () use ($errorResponse) {
|
||||
return $errorResponse->reveal();
|
||||
};
|
||||
|
||||
$router = new Router();
|
||||
$router->addRoute($this->route->reveal());
|
||||
$router->setErrorHandlers([403 => $errorCallable]);
|
||||
$result = $router->getResponse($this->request->reveal());
|
||||
|
||||
$this->assertSame($errorResponse->reveal(), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @runInSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
|
|
@ -206,11 +240,9 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
|||
$this->response->getStatusCode()->willReturn(200);
|
||||
$this->response->respond()->willReturn();
|
||||
|
||||
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface");
|
||||
$this->route->getPaths()->willReturn(["/cats/"]);
|
||||
$this->route->getHandler()->willReturn($this->handler->reveal());
|
||||
$this->route->getResponse(Argument::cetera())->willReturn($this->response->reveal());
|
||||
|
||||
$router = new Router();
|
||||
$router->addRoute($this->route->reveal());
|
||||
|
|
|
|||
|
|
@ -10,16 +10,65 @@ use Prophecy\Argument;
|
|||
*/
|
||||
class BaseRouteTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testDispatchesHandlerInstance()
|
||||
public function testDispatchesHandlerTarget()
|
||||
{
|
||||
$request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
|
||||
$request->getPath()->willReturn("/");
|
||||
|
||||
$response = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\ResponseInterface");
|
||||
|
||||
$handler = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
|
||||
$handler->getResponse(Argument::cetera())->willReturn(null);
|
||||
$handler->getResponse(Argument::cetera())->willReturn($response->reveal());
|
||||
|
||||
$route = new StaticRoute("/", $handler->reveal());
|
||||
$route->getResponse($request->reveal());
|
||||
$result = $route->getResponse($request->reveal());
|
||||
|
||||
$this->assertSame($response->reveal(), $result);
|
||||
$handler->getResponse(Argument::cetera())->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testDispatchesResponseTarget()
|
||||
{
|
||||
$request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
|
||||
$request->getPath()->willReturn("/");
|
||||
|
||||
$response = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\ResponseInterface");
|
||||
|
||||
$route = new StaticRoute("/", $response->reveal());
|
||||
$result = $route->getResponse($request->reveal());
|
||||
|
||||
$this->assertSame($response->reveal(), $result);
|
||||
}
|
||||
|
||||
public function testDispatchesNullTarget()
|
||||
{
|
||||
$request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
|
||||
$request->getPath()->willReturn("/");
|
||||
|
||||
$route = new StaticRoute("/", function () { return null; });
|
||||
$result = $route->getResponse($request->reveal());
|
||||
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
public function testPropagatesArgumentsToCallable()
|
||||
{
|
||||
$callableRequest = null;
|
||||
$callableArgs = null;
|
||||
$callable = function ($request, $args) use (&$callableRequest, &$callableArgs) {
|
||||
$callableRequest = $request;
|
||||
$callableArgs = $args;
|
||||
};
|
||||
|
||||
$request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
|
||||
$request->getPath()->willReturn("/");
|
||||
|
||||
$args = ["cat" => "Molly"];
|
||||
|
||||
$route = new StaticRoute("/", $callable);
|
||||
$route->getResponse($request->reveal(), $args);
|
||||
|
||||
$this->assertSame($request->reveal(), $callableRequest);
|
||||
$this->assertSame($args, $callableArgs);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,12 +64,6 @@ class PrefixRouteTest extends \PHPUnit_Framework_TestCase
|
|||
);
|
||||
}
|
||||
|
||||
public function testReturnsHandler()
|
||||
{
|
||||
$route = new PrefixRoute("/cats/", $this->handler->reveal());
|
||||
$this->assertNotNull($route->getHandler());
|
||||
}
|
||||
|
||||
public function testReturnsPrefixes()
|
||||
{
|
||||
$paths = array("/cats/", "/dogs/");
|
||||
|
|
@ -77,6 +71,25 @@ class PrefixRouteTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertEquals($paths, $route->getPrefixes());
|
||||
}
|
||||
|
||||
public function testPropagatesArgumentsToCallable()
|
||||
{
|
||||
$callableRequest = null;
|
||||
$callableArgs = null;
|
||||
$callable = function ($request, $args) use (&$callableRequest, &$callableArgs) {
|
||||
$callableRequest = $request;
|
||||
$callableArgs = $args;
|
||||
};
|
||||
|
||||
$this->request->getPath()->willReturn("/");
|
||||
$args = ["cat" => "Molly"];
|
||||
|
||||
$route = new PrefixRoute("/", $callable);
|
||||
$route->getResponse($this->request->reveal(), $args);
|
||||
|
||||
$this->assertSame($this->request->reveal(), $callableRequest);
|
||||
$this->assertSame($args, $callableArgs);
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
|
|||
return [
|
||||
["~/cat/[0-9]+~", "/cat/2", [0 => "/cat/2"]],
|
||||
["#/dog/.*#", "/dog/his-name-is-bear", [0 => "/dog/his-name-is-bear"]],
|
||||
["~/cat/([0-9+])~", "/cat/2", [
|
||||
["~/cat/([0-9]+)~", "/cat/2", [
|
||||
0 => "/cat/2",
|
||||
1 => "2"
|
||||
]],
|
||||
|
|
@ -97,6 +97,26 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
|
|||
];
|
||||
}
|
||||
|
||||
public function testPropagatesArgumentsToCallable()
|
||||
{
|
||||
$callableRequest = null;
|
||||
$callableArgs = null;
|
||||
$callable = function ($request, $args) use (&$callableRequest, &$callableArgs) {
|
||||
$callableRequest = $request;
|
||||
$callableArgs = $args;
|
||||
};
|
||||
|
||||
$this->request->getPath()->willReturn("/dog/bear");
|
||||
$args = ["cat" => "Molly"];
|
||||
|
||||
$route = new RegexRoute("~/dog/(?<dog>[a-z]+)~", $callable);
|
||||
$route->getResponse($this->request->reveal(), $args);
|
||||
|
||||
$this->assertSame($this->request->reveal(), $callableRequest);
|
||||
$this->assertArraySubset($args, $callableArgs);
|
||||
$this->assertArraySubset(["dog" => "bear"], $callableArgs);
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
|
||||
|
|
|
|||
|
|
@ -56,12 +56,6 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
|
|||
);
|
||||
}
|
||||
|
||||
public function testReturnsHandler()
|
||||
{
|
||||
$route = new StaticRoute("/cats/", $this->handler->reveal());
|
||||
$this->assertNotNull($route->getHandler());
|
||||
}
|
||||
|
||||
public function testReturnsPaths()
|
||||
{
|
||||
$paths = array("/cats/", "/dogs/");
|
||||
|
|
@ -69,6 +63,26 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertEquals($paths, $route->getPaths());
|
||||
}
|
||||
|
||||
public function testPropagatesArgumentsToCallable()
|
||||
{
|
||||
$callableRequest = null;
|
||||
$callableArgs = null;
|
||||
$callable = function ($request, $args) use (&$callableRequest, &$callableArgs) {
|
||||
$callableRequest = $request;
|
||||
$callableArgs = $args;
|
||||
};
|
||||
|
||||
$this->request->getPath()->willReturn("/");
|
||||
|
||||
$args = ["cat" => "Molly"];
|
||||
|
||||
$route = new StaticRoute("/", $callable);
|
||||
$route->getResponse($this->request->reveal(), $args);
|
||||
|
||||
$this->assertSame($this->request->reveal(), $callableRequest);
|
||||
$this->assertSame($args, $callableArgs);
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
|
||||
|
|
|
|||
|
|
@ -134,4 +134,25 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function testPropagatesArgumentsToCallable()
|
||||
{
|
||||
$callableRequest = null;
|
||||
$callableArgs = null;
|
||||
$callable = function ($request, $args) use (&$callableRequest, &$callableArgs) {
|
||||
$callableRequest = $request;
|
||||
$callableArgs = $args;
|
||||
};
|
||||
|
||||
$this->request->getPath()->willReturn("/dog/bear");
|
||||
$args = ["cat" => "Molly"];
|
||||
|
||||
$route = new TemplateRoute("/dog/{dog}", $callable);
|
||||
$route->getResponse($this->request->reveal(), $args);
|
||||
|
||||
$this->assertSame($this->request->reveal(), $callableRequest);
|
||||
$this->assertArraySubset($args, $callableArgs);
|
||||
$this->assertArraySubset(["dog" => "bear"], $callableArgs);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue