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.
* @expectedException \UnexpectedValueException
*/
public function testSinglePathMatch()
public function testFailOnHandlerDoesNotImplementInterface()
{
$path = "/";
@ -19,9 +19,8 @@ class BaseRouteTest extends \PHPUnit_Framework_TestCase
->method('getPath')
->will($this->returnValue($path));
$path = "/";
$route = new StaticRoute($path, __NAMESPACE__ . '\NotAHandler');
$resp = $route->getResponse($mockRequest);
$route->getResponse($mockRequest);
}
}

View File

@ -2,81 +2,92 @@
namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Response;
use pjdietz\WellRESTed\Routes\RegexRoute;
class RegexRouteTest extends \PHPUnit_Framework_TestCase
{
public static function setUpBeforeClass()
{
include_once(__DIR__ . "/../src/MockHandler.php");
}
/**
* @dataProvider matchingRouteProvider
*/
public function testMatchingRoute($pattern, $path)
public function testMatchPatternForRoute($pattern, $path)
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue($path));
$route = new RegexRoute($pattern, 'MockHandler');
$route = new RegexRoute($pattern, __NAMESPACE__ . '\RegexRouteTestHandler');
$resp = $route->getResponse($mockRequest);
$this->assertNotNull($resp);
}
public function matchingRouteProvider()
{
return array(
array("~/cat/[0-9]+~", "/cat/2"),
array("#/dog/.*#", "/dog/his-name-is-bear")
);
return [
["~/cat/[0-9]+~", "/cat/2"],
["#/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->expects($this->any())
->method('getPath')
->will($this->returnValue($path));
$route = new RegexRoute($pattern, 'MockHandler');
$route = new RegexRoute($pattern, 'NoClass');
$resp = $route->getResponse($mockRequest);
$this->assertNull($resp);
}
public function nonmatchingRouteProvider()
public function mismatchingRouteProvider()
{
return array(
array("~/cat/[0-9]+~", "/cat/molly"),
array("~/cat/[0-9]+~", "/dog/bear"),
array("#/dog/.*#", "/dog")
);
return [
["~/cat/[0-9]+~", "/cat/molly"],
["~/cat/[0-9]+~", "/dog/bear"],
["#/dog/.*#", "/dog"]
];
}
/**
* @dataProvider invalidRouteProvider
* @expectedException \pjdietz\WellRESTed\Exceptions\ParseException
*/
public function testInvalidRoute($pattern)
public function testFailOnInvalidPattern($pattern)
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$route = new RegexRoute($pattern, 'MockHandler');
$route = new RegexRoute($pattern, 'NoClass');
$resp = $route->getResponse($mockRequest);
$this->assertNull($resp);
}
public function invalidRouteProvider()
{
return array(
array("~/unterminated"),
array("/nope")
);
return [
["~/unterminated"],
["/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;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Response;
use pjdietz\WellRESTed\Routes\StaticRoute;
class StaticRouteTest extends \PHPUnit_Framework_TestCase
{
public static function setUpBeforeClass()
{
include_once(__DIR__ . "/../src/MockHandler.php");
}
public function testSinglePathMatch()
public function testMatchSinglePath()
{
$path = "/";
@ -20,26 +17,12 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
->method('getPath')
->will($this->returnValue($path));
$route = new StaticRoute($path, 'MockHandler');
$route = new StaticRoute($path, __NAMESPACE__ . '\StaticRouteTestHandler');
$resp = $route->getResponse($mockRequest);
$this->assertEquals(200, $resp->getStatusCode());
}
public function testSinglePathNoMatch()
{
$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()
public function testMatchPathInList()
{
$path = "/";
$paths = array($path, "/cats/", "/dogs/");
@ -49,18 +32,32 @@ class StaticRouteTest extends \PHPUnit_Framework_TestCase
->method('getPath')
->will($this->returnValue($path));
$route = new StaticRoute($paths, 'MockHandler');
$route = new StaticRoute($paths, __NAMESPACE__ . '\StaticRouteTestHandler');
$resp = $route->getResponse($mockRequest);
$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
* @expectedException \InvalidArgumentException
*/
public function testInvalidPath($path)
public function testFailOnInvalidPath($path)
{
$route = new StaticRoute($path, 'MockHandler');
new StaticRoute($path, 'NoClass');
}
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
*/
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->expects($this->any())
@ -44,14 +44,14 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider nonmatchingTemplateProvider
*/
public function testNonmatchingTemplate($template, $default, $vars, $path)
public function testSkipNonmatchingTemplate($template, $default, $vars, $path)
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue($path));
$route = new TemplateRoute($template, __NAMESPACE__ . '\TemplateRouteTestMockHandler', $default, $vars);
$route = new TemplateRoute($template, "NoClass", $default, $vars);
$resp = $route->getResponse($mockRequest);
$this->assertNull($resp);
}