Update StaticRoute tests to use Prophecy

This commit is contained in:
PJ Dietz 2015-02-18 20:39:39 -05:00
parent 9498542f30
commit 38aaf26943
2 changed files with 53 additions and 59 deletions

View File

@ -4,7 +4,7 @@
* pjdietz\WellRESTed\StaticRoute
*
* @author PJ Dietz <pj@pjdietz.com>
* @copyright Copyright 2014 by PJ Dietz
* @copyright Copyright 2015 by PJ Dietz
* @license MIT
*/
@ -25,19 +25,19 @@ class StaticRoute extends BaseRoute implements StaticRouteInterface
/**
* 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 $targetClassName Fully qualified name to an autoloadable handler class.
* @param string|array $path Path or list of paths the request must match
* @param string $target Fully qualified name to an autoloadable handler class.
* @throws \InvalidArgumentException
*/
public function __construct($prefixes, $targetClassName)
public function __construct($path, $target)
{
parent::__construct($targetClassName);
if (is_string($prefixes)) {
$this->paths = array($prefixes);
} elseif (is_array($prefixes)) {
$this->paths = $prefixes;
parent::__construct($target);
if (is_string($path)) {
$this->paths = array($path);
} elseif (is_array($path)) {
$this->paths = $path;
} else {
throw new InvalidArgumentException("$prefixes must be a string or array of string");
throw new InvalidArgumentException("$path must be a string or array of strings");
}
}

View File

@ -2,52 +2,39 @@
namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Response;
use pjdietz\WellRESTed\Routes\StaticRoute;
use Prophecy\Argument;
/**
* @covers pjdietz\WellRESTed\Routes\StaticRoute
*/
class StaticRouteTest extends \PHPUnit_Framework_TestCase
{
public function testMatchSinglePath()
private $handler;
private $request;
private $response;
public function testMatchesSinglePath()
{
$path = "/";
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue($path));
$route = new StaticRoute($path, __NAMESPACE__ . '\StaticRouteTestHandler');
$resp = $route->getResponse($mockRequest);
$this->assertEquals(200, $resp->getStatusCode());
$this->request->getPath()->willReturn("/cats/");
$route = new StaticRoute("/cats/", $this->handler->reveal());
$resp = $route->getResponse($this->request->reveal());
$this->assertNotNull($resp);
}
public function testMatchPathInList()
public function testMatchesPathInList()
{
$path = "/";
$paths = array($path, "/cats/", "/dogs/");
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue($path));
$route = new StaticRoute($paths, __NAMESPACE__ . '\StaticRouteTestHandler');
$resp = $route->getResponse($mockRequest);
$this->assertEquals(200, $resp->getStatusCode());
$this->request->getPath()->willReturn("/cats/");
$route = new StaticRoute(array("/cats/", "/dogs/"), $this->handler->reveal());
$resp = $route->getResponse($this->request->reveal());
$this->assertNotNull($resp);
}
public function testFailToMatchPath()
public function testFailsToMatchPath()
{
$path = "/";
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue("/not-this-path/"));
$route = new StaticRoute($path, 'NoClass');
$resp = $route->getResponse($mockRequest);
$this->request->getPath()->willReturn("/dogs/");
$route = new StaticRoute("/cats/", $this->handler->reveal());
$resp = $route->getResponse($this->request->reveal());
$this->assertNull($resp);
}
@ -55,9 +42,9 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
* @dataProvider invalidPathsProvider
* @expectedException \InvalidArgumentException
*/
public function testFailOnInvalidPath($path)
public function testThrowsExceptionOnInvalidPath($path)
{
new StaticRoute($path, 'NoClass');
new StaticRoute($path, "\\NoClass");
}
public function invalidPathsProvider()
@ -69,17 +56,24 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
);
}
}
/**
* Mini Handler class that allways returns a 200 status code Response.
*/
class StaticRouteTestHandler implements HandlerInterface
{
public function getResponse(\pjdietz\WellRESTed\Interfaces\RequestInterface $request, array $args = null)
public function testReturnsHandler()
{
$resp = new Response();
$resp->setStatusCode(200);
return $resp;
$route = new StaticRoute("/cats/", $this->handler->reveal());
$this->assertNotNull($route->getHandler());
}
public function testReturnsPaths()
{
$paths = array("/cats/", "/dogs/");
$route = new StaticRoute($paths, $this->handler->reveal());
$this->assertEquals($paths, $route->getPaths());
}
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());
}
}