Update PrefixRoute tests

This commit is contained in:
PJ Dietz 2015-02-18 20:47:45 -05:00
parent 38aaf26943
commit 63fd00fff0
2 changed files with 54 additions and 62 deletions

View File

@ -25,19 +25,19 @@ class PrefixRoute extends BaseRoute implements PrefixRouteInterface
/** /**
* Create a new StaticRoute for a given path or paths and a handler class. * Create a new StaticRoute for a given path or paths and a handler class.
* *
* @param string|array $prefixes Path or list of paths the request must match * @param string|array $path Path or list of paths the request must match
* @param string $targetClassName Fully qualified name to an autoloadable handler class. * @param string $target Fully qualified name to an autoloadable handler class.
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function __construct($prefixes, $targetClassName) public function __construct($path, $target)
{ {
parent::__construct($targetClassName); parent::__construct($target);
if (is_string($prefixes)) { if (is_string($path)) {
$this->prefixes = array($prefixes); $this->prefixes = array($path);
} elseif (is_array($prefixes)) { } elseif (is_array($path)) {
$this->prefixes = $prefixes; $this->prefixes = $path;
} else { } else {
throw new InvalidArgumentException("$prefixes must be a string or array of string"); throw new InvalidArgumentException("$path must be a string or array of string");
} }
} }

View File

@ -2,63 +2,47 @@
namespace pjdietz\WellRESTed\Test; namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Response;
use pjdietz\WellRESTed\Routes\PrefixRoute; use pjdietz\WellRESTed\Routes\PrefixRoute;
use Prophecy\Argument;
/**
* @covers pjdietz\WellRESTed\Routes\PrefixRoute
*/
class PrefixRouteTest extends \PHPUnit_Framework_TestCase class PrefixRouteTest extends \PHPUnit_Framework_TestCase
{ {
public function testMatchSinglePathExactly() private $handler;
private $request;
private $response;
public function testMatchesSinglePathExactly()
{ {
$path = "/"; $this->request->getPath()->willReturn("/cats/");
$route = new PrefixRoute("/cats/", $this->handler->reveal());
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $resp = $route->getResponse($this->request->reveal());
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue($path));
$route = new PrefixRoute($path, __NAMESPACE__ . '\PrefixRouteTestHandler');
$resp = $route->getResponse($mockRequest);
$this->assertNotNull($resp); $this->assertNotNull($resp);
} }
public function testMatchSinglePathWithPrefix() public function testMatchesSinglePathWithPrefix()
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $this->request->getPath()->willReturn("/cats/molly");
$mockRequest->expects($this->any()) $route = new PrefixRoute("/cats/", $this->handler->reveal());
->method('getPath') $resp = $route->getResponse($this->request->reveal());
->will($this->returnValue("/cats/"));
$route = new PrefixRoute("/", __NAMESPACE__ . '\PrefixRouteTestHandler');
$resp = $route->getResponse($mockRequest);
$this->assertNotNull($resp); $this->assertNotNull($resp);
} }
public function testMatchPathInList() public function testMatchesPathInList()
{ {
$paths = array("/cats/", "/dogs/"); $this->request->getPath()->willReturn("/cats/molly");
$route = new PrefixRoute(array("/cats/", "/dogs/"), $this->handler->reveal());
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $resp = $route->getResponse($this->request->reveal());
$mockRequest->expects($this->any()) $this->assertNotNull($resp);
->method('getPath')
->will($this->returnValue("/cats/"));
$route = new PrefixRoute($paths, __NAMESPACE__ . '\StaticRouteTestHandler');
$resp = $route->getResponse($mockRequest);
$this->assertEquals(200, $resp->getStatusCode());
} }
public function testFailToMatchPath() public function testFailsToMatchPath()
{ {
$path = "/cat/"; $this->request->getPath()->willReturn("/dogs/");
$route = new PrefixRoute("/cats/", $this->handler->reveal());
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $resp = $route->getResponse($this->request->reveal());
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue("/not-this-path/"));
$route = new PrefixRoute($path, 'NoClass');
$resp = $route->getResponse($mockRequest);
$this->assertNull($resp); $this->assertNull($resp);
} }
@ -66,9 +50,9 @@ class PrefixRouteTest extends \PHPUnit_Framework_TestCase
* @dataProvider invalidPathsProvider * @dataProvider invalidPathsProvider
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
public function testFailOnInvalidPath($path) public function testThrowsExceptionOnInvalidPath($path)
{ {
new PrefixRoute($path, 'NoClass'); new PrefixRoute($path, "\\NoClass");
} }
public function invalidPathsProvider() public function invalidPathsProvider()
@ -79,17 +63,25 @@ class PrefixRouteTest extends \PHPUnit_Framework_TestCase
array(null) array(null)
); );
} }
}
/** public function testReturnsHandler()
* Mini Handler class that allways returns a 200 status code Response.
*/
class PrefixRouteTestHandler implements HandlerInterface
{
public function getResponse(\pjdietz\WellRESTed\Interfaces\RequestInterface $request, array $args = null)
{ {
$resp = new Response(); $route = new PrefixRoute("/cats/", $this->handler->reveal());
$resp->setStatusCode(200); $this->assertNotNull($route->getHandler());
return $resp; }
public function testReturnsPrefixes()
{
$paths = array("/cats/", "/dogs/");
$route = new PrefixRoute($paths, $this->handler->reveal());
$this->assertEquals($paths, $route->getPrefixes());
}
public function setUp()
{
$this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$this->response = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\ResponseInterface");
$this->handler = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
} }
} }