From 1c5d95e7279abffc78ae85dd4d3590218b81aca3 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sat, 26 Jul 2014 19:05:38 -0400 Subject: [PATCH] Router::getResponse() now returns null to allow better nesting Runs tests that output responses in process isolation --- src/pjdietz/WellRESTed/Router.php | 11 ++++++-- test/ResponseTest.php | 20 ++++++++++---- test/RouterTest.php | 46 ++++++++++++++++++++++++------- 3 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/pjdietz/WellRESTed/Router.php b/src/pjdietz/WellRESTed/Router.php index ff90dd4..533b381 100644 --- a/src/pjdietz/WellRESTed/Router.php +++ b/src/pjdietz/WellRESTed/Router.php @@ -46,8 +46,7 @@ class Router implements HandlerInterface return $responce; } } - - return $this->getNoRouteResponse($request); + return null; } /** @@ -76,9 +75,15 @@ class Router implements HandlerInterface /** * Dispatch the singleton Request through the router and output the response. + * + * Respond with a 404 Not Found if no route provides a response. */ public function respond() { - $response = $this->getResponse(Request::getRequest()); + $request = Request::getRequest(); + $response = $this->getResponse($request); + if (!$response) { + $response = $this->getNoRouteResponse($request); + } $response->respond(); } diff --git a/test/ResponseTest.php b/test/ResponseTest.php index 0ddb0b6..2c8e9c3 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -168,6 +168,10 @@ class ResponseTest extends \PHPUnit_Framework_TestCase ]; } + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ public function testOutputResponse() { $faker = Factory::create(); @@ -175,13 +179,17 @@ class ResponseTest extends \PHPUnit_Framework_TestCase $resp = new Response(200, $body, ["Content-type" => "text/plain"]); ob_start(); - @$resp->respond(); + $resp->respond(); $captured = ob_get_contents(); ob_end_clean(); $this->assertEquals($body, $captured); } + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ public function testOutputResponseFromFile() { $path = tempnam(sys_get_temp_dir(), "TST"); @@ -197,8 +205,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase $resp->setBodyFilePath($path); ob_start(); - ob_clean(); - @$resp->respond(); + $resp->respond(); $captured = ob_get_contents(); ob_end_clean(); @@ -207,6 +214,10 @@ class ResponseTest extends \PHPUnit_Framework_TestCase $this->assertEquals($captured, $body); } + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ public function testMissingResponseFile() { $path = tempnam(sys_get_temp_dir(), "TST"); @@ -217,8 +228,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase unlink($path); ob_start(); - ob_clean(); - @$resp->respond(); + $resp->respond(); $captured = ob_get_contents(); ob_end_clean(); diff --git a/test/RouterTest.php b/test/RouterTest.php index 3eddfc2..f568efa 100644 --- a/test/RouterTest.php +++ b/test/RouterTest.php @@ -45,7 +45,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase $this->assertEquals(200, $resp->getStatusCode()); } - public function testGetNoRouteResponse() + public function testReturnNullWhenNoRouteMatches() { $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $mockRequest->expects($this->any()) @@ -56,25 +56,51 @@ class RouterTest extends \PHPUnit_Framework_TestCase $router = new Router(); $router->addRoute($route); $resp = $router->getResponse($mockRequest); - $this->assertEquals(404, $resp->getStatusCode()); + $this->assertNull($resp); } - public function testStaticRequest() + public function testNestedRouters() { - $path = "/"; - $original = $_SERVER; - $_SERVER["REQUEST_URI"] = $path; - $_SERVER["HTTP_HOST"] = "localhost"; + $path = "/cats/"; - $route = new StaticRoute($path, __NAMESPACE__ . '\\RouterTestHandler'); + $router1 = new Router(); + $router2 = new Router(); + $router3 = new Router(); + + $router1->addRoute($router2); + $router2->addRoute($router3); + $router3->addRoute(new StaticRoute($path, __NAMESPACE__ . '\\RouterTestHandler')); + + $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); + $mockRequest->expects($this->any()) + ->method('getPath') + ->will($this->returnValue($path)); + + $resp = $router1->getResponse($mockRequest); + $this->assertNotNull($resp); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testStaticRequestDoesNotMatchRouter() + { + $_SERVER["REQUEST_URI"] = "/cats/"; + $_SERVER["HTTP_HOST"] = "localhost"; + $_SERVER["REQUEST_METHOD"] = "GET"; + + $route = new StaticRoute("/dogs/", __NAMESPACE__ . '\\RouterTestHandler'); $router = new Router(); $router->addRoute($route); ob_start(); - @$router->respond(); + $router->respond(); + $captured = ob_get_contents(); ob_end_clean(); - $_SERVER = $original; + $this->assertEquals("No resource at /cats/", $captured); } + } /**