Update tests for Routes

This commit is contained in:
PJ Dietz 2014-07-26 06:15:17 -04:00
parent ea05633dfb
commit 297c4aa2e8
4 changed files with 77 additions and 57 deletions

View File

@ -10,7 +10,7 @@ class BaseRouteTest extends \PHPUnit_Framework_TestCase
* Create a route that will match, but has an incorrect handler assigned. * Create a route that will match, but has an incorrect handler assigned.
* @expectedException \UnexpectedValueException * @expectedException \UnexpectedValueException
*/ */
public function testSinglePathMatch() public function testFailOnHandlerDoesNotImplementInterface()
{ {
$path = "/"; $path = "/";
@ -19,9 +19,8 @@ class BaseRouteTest extends \PHPUnit_Framework_TestCase
->method('getPath') ->method('getPath')
->will($this->returnValue($path)); ->will($this->returnValue($path));
$path = "/";
$route = new StaticRoute($path, __NAMESPACE__ . '\NotAHandler'); $route = new StaticRoute($path, __NAMESPACE__ . '\NotAHandler');
$resp = $route->getResponse($mockRequest); $route->getResponse($mockRequest);
} }
} }

View File

@ -2,81 +2,92 @@
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;
class RegexRouteTest extends \PHPUnit_Framework_TestCase class RegexRouteTest extends \PHPUnit_Framework_TestCase
{ {
public static function setUpBeforeClass()
{
include_once(__DIR__ . "/../src/MockHandler.php");
}
/** /**
* @dataProvider matchingRouteProvider * @dataProvider matchingRouteProvider
*/ */
public function testMatchingRoute($pattern, $path) public function testMatchPatternForRoute($pattern, $path)
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any()) $mockRequest->expects($this->any())
->method('getPath') ->method('getPath')
->will($this->returnValue($path)); ->will($this->returnValue($path));
$route = new RegexRoute($pattern, 'MockHandler'); $route = new RegexRoute($pattern, __NAMESPACE__ . '\RegexRouteTestHandler');
$resp = $route->getResponse($mockRequest); $resp = $route->getResponse($mockRequest);
$this->assertNotNull($resp); $this->assertNotNull($resp);
} }
public function matchingRouteProvider() public function matchingRouteProvider()
{ {
return array( return [
array("~/cat/[0-9]+~", "/cat/2"), ["~/cat/[0-9]+~", "/cat/2"],
array("#/dog/.*#", "/dog/his-name-is-bear") ["#/dog/.*#", "/dog/his-name-is-bear"]
); ];
} }
/** /**
* @dataProvider nonmatchingRouteProvider * @dataProvider mismatchingRouteProvider
*/ */
public function testNonmatchingRoute($pattern, $path) public function testSkipMismatchingPattern($pattern, $path)
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any()) $mockRequest->expects($this->any())
->method('getPath') ->method('getPath')
->will($this->returnValue($path)); ->will($this->returnValue($path));
$route = new RegexRoute($pattern, 'MockHandler'); $route = new RegexRoute($pattern, 'NoClass');
$resp = $route->getResponse($mockRequest); $resp = $route->getResponse($mockRequest);
$this->assertNull($resp); $this->assertNull($resp);
} }
public function nonmatchingRouteProvider() public function mismatchingRouteProvider()
{ {
return array( return [
array("~/cat/[0-9]+~", "/cat/molly"), ["~/cat/[0-9]+~", "/cat/molly"],
array("~/cat/[0-9]+~", "/dog/bear"), ["~/cat/[0-9]+~", "/dog/bear"],
array("#/dog/.*#", "/dog") ["#/dog/.*#", "/dog"]
); ];
} }
/** /**
* @dataProvider invalidRouteProvider * @dataProvider invalidRouteProvider
* @expectedException \pjdietz\WellRESTed\Exceptions\ParseException * @expectedException \pjdietz\WellRESTed\Exceptions\ParseException
*/ */
public function testInvalidRoute($pattern) public function testFailOnInvalidPattern($pattern)
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$route = new RegexRoute($pattern, 'MockHandler'); $route = new RegexRoute($pattern, 'NoClass');
$resp = $route->getResponse($mockRequest); $resp = $route->getResponse($mockRequest);
$this->assertNull($resp); $this->assertNull($resp);
} }
public function invalidRouteProvider() public function invalidRouteProvider()
{ {
return array( return [
array("~/unterminated"), ["~/unterminated"],
array("/nope") ["/nope"]
); ];
} }
} }
/**
* 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();
$resp->setStatusCode(200);
return $resp;
}
}

View File

@ -2,16 +2,13 @@
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;
class StaticRouteTest extends \PHPUnit_Framework_TestCase class StaticRouteTest extends \PHPUnit_Framework_TestCase
{ {
public static function setUpBeforeClass() public function testMatchSinglePath()
{
include_once(__DIR__ . "/../src/MockHandler.php");
}
public function testSinglePathMatch()
{ {
$path = "/"; $path = "/";
@ -20,26 +17,12 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
->method('getPath') ->method('getPath')
->will($this->returnValue($path)); ->will($this->returnValue($path));
$route = new StaticRoute($path, 'MockHandler'); $route = new StaticRoute($path, __NAMESPACE__ . '\StaticRouteTestHandler');
$resp = $route->getResponse($mockRequest); $resp = $route->getResponse($mockRequest);
$this->assertEquals(200, $resp->getStatusCode()); $this->assertEquals(200, $resp->getStatusCode());
} }
public function testSinglePathNoMatch() public function testMatchPathInList()
{
$path = "/";
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue("/not-this-path/"));
$route = new StaticRoute($path, 'HandlerStub');
$resp = $route->getResponse($mockRequest);
$this->assertNull($resp);
}
public function testListPathMatch()
{ {
$path = "/"; $path = "/";
$paths = array($path, "/cats/", "/dogs/"); $paths = array($path, "/cats/", "/dogs/");
@ -49,18 +32,32 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
->method('getPath') ->method('getPath')
->will($this->returnValue($path)); ->will($this->returnValue($path));
$route = new StaticRoute($paths, 'MockHandler'); $route = new StaticRoute($paths, __NAMESPACE__ . '\StaticRouteTestHandler');
$resp = $route->getResponse($mockRequest); $resp = $route->getResponse($mockRequest);
$this->assertEquals(200, $resp->getStatusCode()); $this->assertEquals(200, $resp->getStatusCode());
} }
public function testFailToMatchPath()
{
$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->assertNull($resp);
}
/** /**
* @dataProvider invalidPathsProvider * @dataProvider invalidPathsProvider
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
public function testInvalidPath($path) public function testFailOnInvalidPath($path)
{ {
$route = new StaticRoute($path, 'MockHandler'); new StaticRoute($path, 'NoClass');
} }
public function invalidPathsProvider() public function invalidPathsProvider()
@ -73,3 +70,16 @@ 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)
{
$resp = new Response();
$resp->setStatusCode(200);
return $resp;
}
}

View File

@ -12,7 +12,7 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase
/** /**
* @dataProvider matchingTemplateProvider * @dataProvider matchingTemplateProvider
*/ */
public function testMatchingTemplate($template, $default, $vars, $path, $testName, $expected) public function testMatchTemplate($template, $default, $vars, $path, $testName, $expected)
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any()) $mockRequest->expects($this->any())
@ -44,14 +44,14 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase
/** /**
* @dataProvider nonmatchingTemplateProvider * @dataProvider nonmatchingTemplateProvider
*/ */
public function testNonmatchingTemplate($template, $default, $vars, $path) public function testSkipNonmatchingTemplate($template, $default, $vars, $path)
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any()) $mockRequest->expects($this->any())
->method('getPath') ->method('getPath')
->will($this->returnValue($path)); ->will($this->returnValue($path));
$route = new TemplateRoute($template, __NAMESPACE__ . '\TemplateRouteTestMockHandler', $default, $vars); $route = new TemplateRoute($template, "NoClass", $default, $vars);
$resp = $route->getResponse($mockRequest); $resp = $route->getResponse($mockRequest);
$this->assertNull($resp); $this->assertNull($resp);
} }