Router::getResponse() now returns null to allow better nesting

Runs tests that output responses in process isolation
This commit is contained in:
PJ Dietz 2014-07-26 19:05:38 -04:00
parent 98c4ac0eb8
commit 1c5d95e727
3 changed files with 59 additions and 18 deletions

View File

@ -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();
}

View File

@ -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();

View File

@ -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);
}
}
/**