diff --git a/test/RouterTest.php b/test/RouterTest.php index 7875cb2..3eddfc2 100644 --- a/test/RouterTest.php +++ b/test/RouterTest.php @@ -2,16 +2,14 @@ namespace pjdietz\WellRESTed\Test; +use pjdietz\WellRESTed\Interfaces\HandlerInterface; +use pjdietz\WellRESTed\Interfaces\RequestInterface; +use pjdietz\WellRESTed\Response; use pjdietz\WellRESTed\Router; use pjdietz\WellRESTed\Routes\StaticRoute; class RouterTest extends \PHPUnit_Framework_TestCase { - public static function setUpBeforeClass() - { - include_once(__DIR__ . "/src/MockHandler.php"); - } - public function testAddRoute() { $path = "/"; @@ -21,12 +19,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase ->method('getPath') ->will($this->returnValue($path)); - $route = new StaticRoute($path, 'MockHandler'); + $route = new StaticRoute($path, __NAMESPACE__ . '\\RouterTestHandler'); $router = new Router(); $router->addRoute($route); $resp = $router->getResponse($mockRequest); $this->assertNotNull($resp); - } public function testAddRoutes() @@ -39,14 +36,27 @@ class RouterTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue($path)); $routes = array(); - $routes[] = new StaticRoute("/not-this", 'MockHandler'); - $routes[] = new StaticRoute("/or-this", 'MockHandler'); + $routes[] = new StaticRoute("/", __NAMESPACE__ . '\\RouterTestHandler'); + $routes[] = new StaticRoute("/another/", __NAMESPACE__ . '\\RouterTestHandler'); $router = new Router(); $router->addRoutes($routes); $resp = $router->getResponse($mockRequest); - $this->assertEquals(404, $resp->getStatusCode()); + $this->assertEquals(200, $resp->getStatusCode()); + } + public function testGetNoRouteResponse() + { + $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue("/dog/")); + + $route = new StaticRoute("/cat/", __NAMESPACE__ . '\\RouterTestHandler'); + $router = new Router(); + $router->addRoute($route); + $resp = $router->getResponse($mockRequest); + $this->assertEquals(404, $resp->getStatusCode()); } public function testStaticRequest() @@ -56,7 +66,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase $_SERVER["REQUEST_URI"] = $path; $_SERVER["HTTP_HOST"] = "localhost"; - $route = new StaticRoute($path, 'MockHandler'); + $route = new StaticRoute($path, __NAMESPACE__ . '\\RouterTestHandler'); $router = new Router(); $router->addRoute($route); ob_start(); @@ -65,5 +75,17 @@ class RouterTest extends \PHPUnit_Framework_TestCase $_SERVER = $original; } - +} + +/** + * Mini Handler class that allways returns a 200 status code Response. + */ +class RouterTestHandler implements HandlerInterface +{ + public function getResponse(RequestInterface $request, array $args = null) + { + $resp = new Response(); + $resp->setStatusCode(200); + return $resp; + } }