Propagate $request and $args to route and errorHandler callables

This commit is contained in:
PJ Dietz 2015-02-22 12:01:21 -05:00
parent d785e21fee
commit 812012bdbf
15 changed files with 280 additions and 114 deletions

View File

@ -12,13 +12,6 @@ namespace pjdietz\WellRESTed\Interfaces\Routes;
interface PrefixRouteInterface 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. * Returns the path prefixes this maps to a target handler.
* *

View File

@ -12,13 +12,6 @@ namespace pjdietz\WellRESTed\Interfaces\Routes;
interface StaticRouteInterface 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. * Returns the paths this maps to a target handler.
* *

View File

@ -49,18 +49,16 @@ class RouteTable implements HandlerInterface
{ {
$response = null; $response = null;
$path = $request->getPath(); // First check if there is a static route.
$response = $this->getStaticResponse($request, $args);
// First check if there is a handler for this exact path. if ($response) {
$handler = $this->getStaticHandler($path); return $response;
if ($handler) {
return $handler->getResponse($request, $args);
} }
// Check prefix routes for any routes that match. Use the longest matching prefix. // Check prefix routes for any routes that match. Use the longest matching prefix.
$handler = $this->getPrefixHandler($path); $response = $this->getPrefixResponse($request, $args);
if ($handler) { if ($response) {
return $handler->getResponse($request, $args); return $response;
} }
// Try each of the routes. // 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 * @param RequestInterface $request
* @return HandlerInterface|null * @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])) { if (isset($this->staticRoutes[$path])) {
$route = $this->staticRoutes[$path]; $route = $this->staticRoutes[$path];
return $route->getHandler(); return $route->getResponse($request, $args);
} }
return null; return null;
} }
@ -93,11 +93,14 @@ class RouteTable implements HandlerInterface
/** /**
* Returning the best-matching prefix handler, or null if none match. * Returning the best-matching prefix handler, or null if none match.
* *
* @param $path string The request's path * @param RequestInterface $request
* @return HandlerInterface|null * @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. // Find all prefixes that match the start of this path.
$prefixes = array_keys($this->prefixRoutes); $prefixes = array_keys($this->prefixRoutes);
$matches = array_filter( $matches = array_filter(
@ -115,9 +118,8 @@ class RouteTable implements HandlerInterface
}; };
usort($matches, $compareByLength); usort($matches, $compareByLength);
} }
// Instantiate and return the handler identified as the best match.
$route = $this->prefixRoutes[$matches[0]]; $route = $this->prefixRoutes[$matches[0]];
return $route->getHandler(); return $route->getResponse($request, $args);
} }
return null; return null;
} }

View File

@ -103,7 +103,7 @@ class Router implements HandlerInterface
* Add a custom error handler. * Add a custom error handler.
* *
* @param integer $statusCode The error status code * @param integer $statusCode The error status code
* @param callable|string|HandlerInterface $errorHandler * @param mixed $errorHandler
*/ */
public function setErrorHandler($statusCode, $errorHandler) public function setErrorHandler($statusCode, $errorHandler)
{ {
@ -168,14 +168,17 @@ class Router implements HandlerInterface
private function getErrorResponse($status, $request, $args = null, $response = null) private function getErrorResponse($status, $request, $args = null, $response = null)
{ {
if (isset($this->errorHandlers[$status])) { if (isset($this->errorHandlers[$status])) {
$unpacker = new HandlerUnpacker();
$errorHandler = $unpacker->unpack($this->errorHandlers[$status]);
// Pass the response triggering this along to the error handler. // Pass the response triggering this along to the error handler.
$errorArgs = array("response" => $response); $errorArgs = array("response" => $response);
if ($args) { if ($args) {
$errorArgs = array_merge($args, $errorArgs); $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; return null;
} }

View File

@ -12,6 +12,8 @@ namespace pjdietz\WellRESTed\Routes;
use pjdietz\WellRESTed\HandlerUnpacker; use pjdietz\WellRESTed\HandlerUnpacker;
use pjdietz\WellRESTed\Interfaces\HandlerInterface; use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
/** /**
* Base class for Routes. * 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 * @param RequestInterface $request The request to respond to.
* @return HandlerInterface * @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(); $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;
} }
} }

View File

@ -58,13 +58,15 @@ class PrefixRoute extends BaseRoute implements PrefixRouteInterface
$requestPath = $request->getPath(); $requestPath = $request->getPath();
foreach ($this->prefixes as $prefix) { foreach ($this->prefixes as $prefix) {
if (strrpos($requestPath, $prefix, -strlen($requestPath)) !== false) { if (strrpos($requestPath, $prefix, -strlen($requestPath)) !== false) {
$target = $this->getTarget(); return $this->getResponseFromTarget($request, $args);
return $target->getResponse($request, $args);
} }
} }
return null; return null;
} }
// ------------------------------------------------------------------------
/* PrefixRouteInterface */
/** /**
* Returns the path prefixes this maps to a target handler. * Returns the path prefixes this maps to a target handler.
* *
@ -74,14 +76,4 @@ class PrefixRoute extends BaseRoute implements PrefixRouteInterface
{ {
return $this->prefixes; return $this->prefixes;
} }
/**
* Returns the target class this maps to.
*
* @return string Fully qualified name for a HandlerInterface
*/
public function getHandler()
{
return $this->getTarget();
}
} }

View File

@ -50,12 +50,11 @@ class RegexRoute extends BaseRoute
{ {
$matched = @preg_match($this->getPattern(), $request->getPath(), $matches); $matched = @preg_match($this->getPattern(), $request->getPath(), $matches);
if ($matched) { if ($matched) {
$target = $this->getTarget();
if (is_null($args)) { if (is_null($args)) {
$args = array(); $args = array();
} }
$args = array_merge($args, $matches); $args = array_merge($args, $matches);
return $target->getResponse($request, $args); return $this->getResponseFromTarget($request, $args);
} elseif ($matched === false) { } elseif ($matched === false) {
throw new ParseException("Invalid regular expression: " . $this->getPattern()); throw new ParseException("Invalid regular expression: " . $this->getPattern());
} }

View File

@ -11,7 +11,9 @@
namespace pjdietz\WellRESTed\Routes; namespace pjdietz\WellRESTed\Routes;
use InvalidArgumentException; use InvalidArgumentException;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface; use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
use pjdietz\WellRESTed\Interfaces\Routes\StaticRouteInterface; use pjdietz\WellRESTed\Interfaces\Routes\StaticRouteInterface;
/** /**
@ -55,25 +57,14 @@ class StaticRoute extends BaseRoute implements StaticRouteInterface
*/ */
public function getResponse(RequestInterface $request, array $args = null) public function getResponse(RequestInterface $request, array $args = null)
{ {
$requestPath = $request->getPath(); if (in_array($request->getPath(), $this->paths)) {
foreach ($this->paths as $path) { return $this->getResponseFromTarget($request, $args);
if ($path === $requestPath) {
$target = $this->getTarget();
return $target->getResponse($request, $args);
}
} }
return null; return null;
} }
/** // ------------------------------------------------------------------------
* Returns the target class this maps to. /* StaticRouteInterface */
*
* @return string Fully qualified name for a HandlerInterface
*/
public function getHandler()
{
return $this->getTarget();
}
/** /**
* Returns the paths this maps to a target handler. * Returns the paths this maps to a target handler.

View File

@ -32,11 +32,9 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
public function testMatchesStaticRoute() public function testMatchesStaticRoute()
{ {
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
$this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface"); $this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface");
$this->route->getPaths()->willReturn(["/cats/"]); $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/"); $this->request->getPath()->willReturn("/cats/");
@ -44,16 +42,14 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
$table->addRoute($this->route->reveal()); $table->addRoute($this->route->reveal());
$table->getResponse($this->request->reveal()); $table->getResponse($this->request->reveal());
$this->route->getHandler()->shouldHaveBeenCalled(); $this->route->getResponse(Argument::cetera())->shouldHaveBeenCalled();
} }
public function testMatchesPrefixRoute() public function testMatchesPrefixRoute()
{ {
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
$this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface"); $this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
$this->route->getPrefixes()->willReturn(["/cats/"]); $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"); $this->request->getPath()->willReturn("/cats/molly");
@ -61,22 +57,20 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
$table->addRoute($this->route->reveal()); $table->addRoute($this->route->reveal());
$table->getResponse($this->request->reveal()); $table->getResponse($this->request->reveal());
$this->route->getHandler()->shouldHaveBeenCalled(); $this->route->getResponse(Argument::cetera())->shouldHaveBeenCalled();
} }
public function testMatchesBestPrefixRoute() public function testMatchesBestPrefixRoute()
{ {
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
$route1 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface"); $route1 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
$route1->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface"); $route1->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
$route1->getPrefixes()->willReturn(["/animals/"]); $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 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
$route2->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface"); $route2->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
$route2->getPrefixes()->willReturn(["/animals/cats/"]); $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"); $this->request->getPath()->willReturn("/animals/cats/molly");
@ -85,23 +79,21 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
$table->addRoute($route2->reveal()); $table->addRoute($route2->reveal());
$table->getResponse($this->request->reveal()); $table->getResponse($this->request->reveal());
$route1->getHandler()->shouldNotHaveBeenCalled(); $route1->getResponse(Argument::cetera())->shouldNotHaveBeenCalled();
$route2->getHandler()->shouldHaveBeenCalled(); $route2->getResponse(Argument::cetera())->shouldHaveBeenCalled();
} }
public function testMatchesStaticRouteBeforePrefixRoute() public function testMatchesStaticRouteBeforePrefixRoute()
{ {
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
$route1 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface"); $route1 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
$route1->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface"); $route1->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
$route1->getPrefixes()->willReturn(["/animals/cats/"]); $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 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
$route2->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface"); $route2->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface");
$route2->getPaths()->willReturn(["/animals/cats/molly"]); $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"); $this->request->getPath()->willReturn("/animals/cats/molly");
@ -110,18 +102,16 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
$table->addRoute($route2->reveal()); $table->addRoute($route2->reveal());
$table->getResponse($this->request->reveal()); $table->getResponse($this->request->reveal());
$route1->getHandler()->shouldNotHaveBeenCalled(); $route1->getResponse(Argument::cetera())->shouldNotHaveBeenCalled();
$route2->getHandler()->shouldHaveBeenCalled(); $route2->getResponse(Argument::cetera())->shouldHaveBeenCalled();
} }
public function testMatchesPrefixRouteBeforeHandlerRoute() public function testMatchesPrefixRouteBeforeHandlerRoute()
{ {
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
$route1 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface"); $route1 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
$route1->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface"); $route1->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\PrefixRouteInterface");
$route1->getPrefixes()->willReturn(["/animals/cats/"]); $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 = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
$route2->getResponse(Argument::cetera())->willReturn(null); $route2->getResponse(Argument::cetera())->willReturn(null);
@ -133,7 +123,7 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
$table->addRoute($route2->reveal()); $table->addRoute($route2->reveal());
$table->getResponse($this->request->reveal()); $table->getResponse($this->request->reveal());
$route1->getHandler()->shouldHaveBeenCalled(); $route1->getResponse(Argument::cetera())->shouldHaveBeenCalled();
$route2->getResponse(Argument::cetera())->shouldNotHaveBeenCalled(); $route2->getResponse(Argument::cetera())->shouldNotHaveBeenCalled();
} }
@ -161,4 +151,51 @@ class RouteTableTest extends \PHPUnit_Framework_TestCase
$route2->getResponse(Argument::cetera())->shouldHaveBeenCalled(); $route2->getResponse(Argument::cetera())->shouldHaveBeenCalled();
$route3->getResponse(Argument::cetera())->shouldNotHaveBeenCalled(); $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();
}
} }

View File

@ -86,6 +86,19 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->route->getResponse(Argument::cetera())->shouldHaveBeenCalled(); $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() public function testRespondsWithErrorResponseForHttpException()
{ {
$this->route->getResponse(Argument::cetera())->willThrow(new HttpException()); $this->route->getResponse(Argument::cetera())->willThrow(new HttpException());
@ -193,6 +206,27 @@ class RouterTest extends \PHPUnit_Framework_TestCase
)->shouldHaveBeenCalled(); )->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 * @runInSeparateProcess
* @preserveGlobalState disabled * @preserveGlobalState disabled
@ -206,11 +240,9 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->response->getStatusCode()->willReturn(200); $this->response->getStatusCode()->willReturn(200);
$this->response->respond()->willReturn(); $this->response->respond()->willReturn();
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
$this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface"); $this->route->willImplement("\\pjdietz\\WellRESTed\\Interfaces\\Routes\\StaticRouteInterface");
$this->route->getPaths()->willReturn(["/cats/"]); $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 = new Router();
$router->addRoute($this->route->reveal()); $router->addRoute($this->route->reveal());

View File

@ -10,16 +10,65 @@ use Prophecy\Argument;
*/ */
class BaseRouteTest extends \PHPUnit_Framework_TestCase class BaseRouteTest extends \PHPUnit_Framework_TestCase
{ {
public function testDispatchesHandlerInstance() public function testDispatchesHandlerTarget()
{ {
$request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); $request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$request->getPath()->willReturn("/"); $request->getPath()->willReturn("/");
$response = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\ResponseInterface");
$handler = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface"); $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 = new StaticRoute("/", $handler->reveal());
$route->getResponse($request->reveal()); $result = $route->getResponse($request->reveal());
$this->assertSame($response->reveal(), $result);
$handler->getResponse(Argument::cetera())->shouldHaveBeenCalled(); $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);
}
} }

View File

@ -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() public function testReturnsPrefixes()
{ {
$paths = array("/cats/", "/dogs/"); $paths = array("/cats/", "/dogs/");
@ -77,6 +71,25 @@ class PrefixRouteTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($paths, $route->getPrefixes()); $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() public function setUp()
{ {
$this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); $this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");

View File

@ -46,7 +46,7 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
return [ return [
["~/cat/[0-9]+~", "/cat/2", [0 => "/cat/2"]], ["~/cat/[0-9]+~", "/cat/2", [0 => "/cat/2"]],
["#/dog/.*#", "/dog/his-name-is-bear", [0 => "/dog/his-name-is-bear"]], ["#/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", 0 => "/cat/2",
1 => "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() public function setUp()
{ {
$this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); $this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");

View File

@ -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() public function testReturnsPaths()
{ {
$paths = array("/cats/", "/dogs/"); $paths = array("/cats/", "/dogs/");
@ -69,6 +63,26 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($paths, $route->getPaths()); $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() public function setUp()
{ {
$this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); $this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");

View File

@ -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);
}
} }