From 4deac492dd0ec67d323769b63c0fdb311eae6b46 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 19 Feb 2015 19:59:33 -0500 Subject: [PATCH] Update BaseRoute to use HandlerUnpacker --- src/pjdietz/WellRESTed/Routes/BaseRoute.php | 27 +++------- test/Routes/BaseRouteTest.php | 59 +++------------------ 2 files changed, 16 insertions(+), 70 deletions(-) diff --git a/src/pjdietz/WellRESTed/Routes/BaseRoute.php b/src/pjdietz/WellRESTed/Routes/BaseRoute.php index 1ba8c63..451ab21 100644 --- a/src/pjdietz/WellRESTed/Routes/BaseRoute.php +++ b/src/pjdietz/WellRESTed/Routes/BaseRoute.php @@ -10,6 +10,7 @@ namespace pjdietz\WellRESTed\Routes; +use pjdietz\WellRESTed\HandlerUnpacker; use pjdietz\WellRESTed\Interfaces\HandlerInterface; /** @@ -17,18 +18,18 @@ use pjdietz\WellRESTed\Interfaces\HandlerInterface; */ abstract class BaseRoute implements HandlerInterface { - /** @var callable|string|HandlerInterface HandlerInterface to dispatch */ + /** @var callable|string|HandlerInterface Handler to dispatch */ private $target; /** - * Create a new route that will dispatch an instance of the given handelr class. + * Create a new route that will dispatch an instance of the given handler. * * $target may be: * - A callable expecting no arguments that returns a HandlerInterface * - A string containing the fully qualified class of a HandlerInterface - * - A HandlerInterface + * - A HandlerInterface instance * - * @param callable|string|HandlerInterface $target HandlerInterface to dispatch + * @param mixed $target Handler to dispatch */ public function __construct($target) { @@ -36,26 +37,14 @@ abstract class BaseRoute implements HandlerInterface } /** - * Instantiate and return an instance of the assigned HandlerInterface + * Return an instance of the assigned handler * * @throws \UnexpectedValueException * @return HandlerInterface */ protected function getTarget() { - if (is_callable($this->target)) { - $callable = $this->target; - $target = $callable(); - } elseif (is_string($this->target)) { - $className = $this->target; - $target = new $className(); - } else { - $target = $this->target; - } - if ($target instanceof HandlerInterface) { - return $target; - } else { - throw new \UnexpectedValueException("Target class must implement HandlerInterface"); - } + $unpacker = new HandlerUnpacker(); + return $unpacker->unpack($this->target); } } diff --git a/test/Routes/BaseRouteTest.php b/test/Routes/BaseRouteTest.php index c35a345..696f9c8 100644 --- a/test/Routes/BaseRouteTest.php +++ b/test/Routes/BaseRouteTest.php @@ -2,67 +2,24 @@ namespace pjdietz\WellRESTed\Test; -use pjdietz\WellRESTed\Interfaces\HandlerInterface; -use pjdietz\WellRESTed\Interfaces\RequestInterface; use pjdietz\WellRESTed\Routes\StaticRoute; +use Prophecy\Argument; /** * @covers pjdietz\WellRESTed\Routes\BaseRoute */ class BaseRouteTest extends \PHPUnit_Framework_TestCase { - private $path = "/"; - private $request; - - public function testDispatchesHandlerFromCallable() - { - $target = function () { - $handler = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface"); - return $handler->reveal(); - }; - - $route = new StaticRoute($this->path, $target); - $route->getResponse($this->request->reveal()); - } - - public function testDispatchesHandlerFromString() - { - $target = __NAMESPACE__ . "\\ValidHandler"; - - $route = new StaticRoute($this->path, $target); - $route->getResponse($this->request->reveal()); - } - public function testDispatchesHandlerInstance() { - $target = new ValidHandler(); + $request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); + $request->getPath()->willReturn("/"); - $route = new StaticRoute($this->path, $target); - $route->getResponse($this->request->reveal()); - } + $handler = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface"); + $handler->getResponse(Argument::cetera())->willReturn(null); - /** - * @expectedException \UnexpectedValueException - */ - public function testThrowsExceptionWhenHandlerDoesNotImplementInterface() - { - $target = "\\stdClass"; - - $route = new StaticRoute($this->path, $target); - $route->getResponse($this->request->reveal()); - } - - public function setUp() - { - $this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); - $this->request->getPath()->willReturn($this->path); - } -} - -class ValidHandler implements HandlerInterface -{ - public function getResponse(RequestInterface $request, array $args = null) - { - return null; + $route = new StaticRoute("/", $handler->reveal()); + $route->getResponse($request->reveal()); + $handler->getResponse(Argument::cetera())->shouldHaveBeenCalled(); } }