Update BaseRoute to use HandlerUnpacker

This commit is contained in:
PJ Dietz 2015-02-19 19:59:33 -05:00
parent 5dc5cdab06
commit 4deac492dd
2 changed files with 16 additions and 70 deletions

View File

@ -10,6 +10,7 @@
namespace pjdietz\WellRESTed\Routes; namespace pjdietz\WellRESTed\Routes;
use pjdietz\WellRESTed\HandlerUnpacker;
use pjdietz\WellRESTed\Interfaces\HandlerInterface; use pjdietz\WellRESTed\Interfaces\HandlerInterface;
/** /**
@ -17,18 +18,18 @@ use pjdietz\WellRESTed\Interfaces\HandlerInterface;
*/ */
abstract class BaseRoute implements HandlerInterface abstract class BaseRoute implements HandlerInterface
{ {
/** @var callable|string|HandlerInterface HandlerInterface to dispatch */ /** @var callable|string|HandlerInterface Handler to dispatch */
private $target; 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: * $target may be:
* - A callable expecting no arguments that returns a HandlerInterface * - A callable expecting no arguments that returns a HandlerInterface
* - A string containing the fully qualified class of 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) 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 * @throws \UnexpectedValueException
* @return HandlerInterface * @return HandlerInterface
*/ */
protected function getTarget() protected function getTarget()
{ {
if (is_callable($this->target)) { $unpacker = new HandlerUnpacker();
$callable = $this->target; return $unpacker->unpack($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");
}
} }
} }

View File

@ -2,67 +2,24 @@
namespace pjdietz\WellRESTed\Test; namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Routes\StaticRoute; use pjdietz\WellRESTed\Routes\StaticRoute;
use Prophecy\Argument;
/** /**
* @covers pjdietz\WellRESTed\Routes\BaseRoute * @covers pjdietz\WellRESTed\Routes\BaseRoute
*/ */
class BaseRouteTest extends \PHPUnit_Framework_TestCase 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() public function testDispatchesHandlerInstance()
{ {
$target = new ValidHandler(); $request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$request->getPath()->willReturn("/");
$route = new StaticRoute($this->path, $target); $handler = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
$route->getResponse($this->request->reveal()); $handler->getResponse(Argument::cetera())->willReturn(null);
}
/** $route = new StaticRoute("/", $handler->reveal());
* @expectedException \UnexpectedValueException $route->getResponse($request->reveal());
*/ $handler->getResponse(Argument::cetera())->shouldHaveBeenCalled();
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;
} }
} }