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

View File

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