From 75088499b8bef40744bdf383a8bb3613b7455a9d Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sat, 2 Aug 2014 17:42:16 -0400 Subject: [PATCH] Ensure $args propagate from Router to Handlers --- src/pjdietz/WellRESTed/Router.php | 4 ++-- test/RouterTest.php | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/pjdietz/WellRESTed/Router.php b/src/pjdietz/WellRESTed/Router.php index 0bc8bb0..34bd164 100644 --- a/src/pjdietz/WellRESTed/Router.php +++ b/src/pjdietz/WellRESTed/Router.php @@ -78,10 +78,10 @@ class Router implements HandlerInterface * * Respond with a 404 Not Found if no route provides a response. */ - public function respond() + public function respond($args = null) { $request = Request::getRequest(); - $response = $this->getResponse($request); + $response = $this->getResponse($request, $args); if (!$response) { $response = $this->getNoRouteResponse($request); } diff --git a/test/RouterTest.php b/test/RouterTest.php index a2bd809..5278136 100644 --- a/test/RouterTest.php +++ b/test/RouterTest.php @@ -4,7 +4,6 @@ namespace pjdietz\WellRESTed\Test; use pjdietz\WellRESTed\Interfaces\HandlerInterface; use pjdietz\WellRESTed\Interfaces\RequestInterface; -use pjdietz\WellRESTed\Interfaces\ResponseInterface; use pjdietz\WellRESTed\Response; use pjdietz\WellRESTed\Router; use pjdietz\WellRESTed\Routes\StaticRoute; @@ -134,6 +133,25 @@ class RouterTest extends \PHPUnit_Framework_TestCase ]; } + public function testInjection() + { + $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue("/2/3")); + + $dependencies = [ + "add" => function ($a, $b) { + return $a + $b; + } + ]; + + $router = new Router(); + $router->addRoute(new TemplateRoute("/{a}/{b}", __NAMESPACE__ . "\\InjectionHandler")); + $resp = $router->getResponse($mockRequest, $dependencies); + $this->assertEquals("5", $resp->getBody()); + } + } /** @@ -184,3 +202,14 @@ class NotFoundHandler implements HandlerInterface return $response; } } + +class InjectionHandler implements HandlerInterface +{ + public function getResponse(RequestInterface $request, array $args = null) + { + $response = new Response(200); + $body = $args["add"]($args["a"], $args["b"]); + $response->setBody($body); + return $response; + } +}