Update RegexRoute tests

This commit is contained in:
PJ Dietz 2015-02-18 21:21:56 -05:00
parent 1b17ef5d0a
commit 6859bd9707
2 changed files with 36 additions and 48 deletions

View File

@ -4,7 +4,7 @@
* pjdietz\WellRESTed\RegexRout * pjdietz\WellRESTed\RegexRout
* *
* @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,11 +25,11 @@ class RegexRoute extends BaseRoute
* Create a new route mapping a regex pattern to a handler class name. * Create a new route mapping a regex pattern to a handler class name.
* *
* @param string $pattern Regular expression the path must match. * @param string $pattern Regular expression the path must match.
* @param string $targetClassName Fully qualified name to an autoloadable handler class. * @param string $target Fully qualified name to an autoloadable handler class.
*/ */
public function __construct($pattern, $targetClassName) public function __construct($pattern, $target)
{ {
parent::__construct($targetClassName); parent::__construct($target);
$this->pattern = $pattern; $this->pattern = $pattern;
} }

View File

@ -2,42 +2,43 @@
namespace pjdietz\WellRESTed\Test; namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Response;
use pjdietz\WellRESTed\Routes\RegexRoute; use pjdietz\WellRESTed\Routes\RegexRoute;
use Prophecy\Argument;
/**
* @covers pjdietz\WellRESTed\Routes\RegexRoute
*/
class RegexRouteTest extends \PHPUnit_Framework_TestCase class RegexRouteTest extends \PHPUnit_Framework_TestCase
{ {
private $handler;
private $request;
private $response;
/** /**
* @dataProvider matchingRouteProvider * @dataProvider matchingRouteProvider
*/ */
public function testMatchPatternForRoute($pattern, $path, $captures) public function testMatchesPattern($pattern, $path)
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $this->request->getPath()->willReturn($path);
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue($path));
$route = new RegexRoute($pattern, __NAMESPACE__ . '\RegexRouteTestHandler'); $route = new RegexRoute($pattern, $this->handler->reveal());
$resp = $route->getResponse($mockRequest); $resp = $route->getResponse($this->request->reveal());
$this->assertNotNull($resp); $this->assertNotNull($resp);
} }
/** /**
* @dataProvider matchingRouteProvider * @dataProvider matchingRouteProvider
*/ */
public function testExctractCapturesForRoute($pattern, $path, $captures) public function testExtractsCaptures($pattern, $path, $captures)
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $this->request->getPath()->willReturn($path);
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue($path));
$route = new RegexRoute($pattern, __NAMESPACE__ . '\RegexRouteTestHandler'); $route = new RegexRoute($pattern, $this->handler->reveal());
$resp = $route->getResponse($mockRequest); $route->getResponse($this->request->reveal());
$body = json_decode($resp->getBody(), true); $this->handler->getResponse(Argument::any(), Argument::that(
$this->assertEquals($captures, $body); function ($args) use ($captures) {
return $args = $captures;
}))->shouldHaveBeenCalled();
} }
public function matchingRouteProvider() public function matchingRouteProvider()
@ -60,15 +61,12 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
/** /**
* @dataProvider mismatchingRouteProvider * @dataProvider mismatchingRouteProvider
*/ */
public function testSkipMismatchingPattern($pattern, $path) public function testFailsToMatchMismatchingPattern($pattern, $path)
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $this->request->getPath()->willReturn($path);
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue($path));
$route = new RegexRoute($pattern, 'NoClass'); $route = new RegexRoute($pattern, $this->handler->reveal());
$resp = $route->getResponse($mockRequest); $resp = $route->getResponse($this->request->reveal());
$this->assertNull($resp); $this->assertNull($resp);
} }
@ -85,13 +83,10 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
* @dataProvider invalidRouteProvider * @dataProvider invalidRouteProvider
* @expectedException \pjdietz\WellRESTed\Exceptions\ParseException * @expectedException \pjdietz\WellRESTed\Exceptions\ParseException
*/ */
public function testFailOnInvalidPattern($pattern) public function testThrowsExceptionOnInvalidPattern($pattern)
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $route = new RegexRoute($pattern, $this->handler->reveal());
$route->getResponse($this->request->reveal());
$route = new RegexRoute($pattern, 'NoClass');
$resp = $route->getResponse($mockRequest);
$this->assertNull($resp);
} }
public function invalidRouteProvider() public function invalidRouteProvider()
@ -102,18 +97,11 @@ class RegexRouteTest extends \PHPUnit_Framework_TestCase
]; ];
} }
} public function setUp()
/**
* Mini Handler class that allways returns a 200 status code Response.
*/
class RegexRouteTestHandler implements HandlerInterface
{
public function getResponse(RequestInterface $request, array $args = null)
{ {
$resp = new Response(); $this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$resp->setStatusCode(200); $this->response = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\ResponseInterface");
$resp->setBody(json_encode($args)); $this->handler = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
return $resp; $this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
} }
} }