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 * pjdietz\WellRESTed\StaticRoute
* *
* @author PJ Dietz <pj@pjdietz.com> * @author PJ Dietz <pj@pjdietz.com>
* @copyright Copyright 2014 by PJ Dietz * @copyright Copyright 2015 by PJ Dietz
* @license MIT * @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. * 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->paths = array($prefixes); $this->paths = array($path);
} elseif (is_array($prefixes)) { } elseif (is_array($path)) {
$this->paths = $prefixes; $this->paths = $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 strings");
} }
} }

View File

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