Router::getResponse() now returns null to allow better nesting
Runs tests that output responses in process isolation
This commit is contained in:
parent
98c4ac0eb8
commit
1c5d95e727
|
|
@ -46,8 +46,7 @@ class Router implements HandlerInterface
|
||||||
return $responce;
|
return $responce;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
return $this->getNoRouteResponse($request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -76,9 +75,15 @@ class Router implements HandlerInterface
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatch the singleton Request through the router and output the response.
|
* 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() {
|
public function respond() {
|
||||||
$response = $this->getResponse(Request::getRequest());
|
$request = Request::getRequest();
|
||||||
|
$response = $this->getResponse($request);
|
||||||
|
if (!$response) {
|
||||||
|
$response = $this->getNoRouteResponse($request);
|
||||||
|
}
|
||||||
$response->respond();
|
$response->respond();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -168,6 +168,10 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
* @preserveGlobalState disabled
|
||||||
|
*/
|
||||||
public function testOutputResponse()
|
public function testOutputResponse()
|
||||||
{
|
{
|
||||||
$faker = Factory::create();
|
$faker = Factory::create();
|
||||||
|
|
@ -175,13 +179,17 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
$resp = new Response(200, $body, ["Content-type" => "text/plain"]);
|
$resp = new Response(200, $body, ["Content-type" => "text/plain"]);
|
||||||
ob_start();
|
ob_start();
|
||||||
@$resp->respond();
|
$resp->respond();
|
||||||
$captured = ob_get_contents();
|
$captured = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
$this->assertEquals($body, $captured);
|
$this->assertEquals($body, $captured);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
* @preserveGlobalState disabled
|
||||||
|
*/
|
||||||
public function testOutputResponseFromFile()
|
public function testOutputResponseFromFile()
|
||||||
{
|
{
|
||||||
$path = tempnam(sys_get_temp_dir(), "TST");
|
$path = tempnam(sys_get_temp_dir(), "TST");
|
||||||
|
|
@ -197,8 +205,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
||||||
$resp->setBodyFilePath($path);
|
$resp->setBodyFilePath($path);
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
ob_clean();
|
$resp->respond();
|
||||||
@$resp->respond();
|
|
||||||
$captured = ob_get_contents();
|
$captured = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
|
|
@ -207,6 +214,10 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals($captured, $body);
|
$this->assertEquals($captured, $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
* @preserveGlobalState disabled
|
||||||
|
*/
|
||||||
public function testMissingResponseFile()
|
public function testMissingResponseFile()
|
||||||
{
|
{
|
||||||
$path = tempnam(sys_get_temp_dir(), "TST");
|
$path = tempnam(sys_get_temp_dir(), "TST");
|
||||||
|
|
@ -217,8 +228,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
||||||
unlink($path);
|
unlink($path);
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
ob_clean();
|
$resp->respond();
|
||||||
@$resp->respond();
|
|
||||||
$captured = ob_get_contents();
|
$captured = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals(200, $resp->getStatusCode());
|
$this->assertEquals(200, $resp->getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetNoRouteResponse()
|
public function testReturnNullWhenNoRouteMatches()
|
||||||
{
|
{
|
||||||
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
|
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
|
||||||
$mockRequest->expects($this->any())
|
$mockRequest->expects($this->any())
|
||||||
|
|
@ -56,25 +56,51 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
$router = new Router();
|
$router = new Router();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
$resp = $router->getResponse($mockRequest);
|
$resp = $router->getResponse($mockRequest);
|
||||||
$this->assertEquals(404, $resp->getStatusCode());
|
$this->assertNull($resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testStaticRequest()
|
public function testNestedRouters()
|
||||||
{
|
{
|
||||||
$path = "/";
|
$path = "/cats/";
|
||||||
$original = $_SERVER;
|
|
||||||
$_SERVER["REQUEST_URI"] = $path;
|
|
||||||
$_SERVER["HTTP_HOST"] = "localhost";
|
|
||||||
|
|
||||||
$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 = new Router();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
ob_start();
|
ob_start();
|
||||||
@$router->respond();
|
$router->respond();
|
||||||
|
$captured = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
$_SERVER = $original;
|
$this->assertEquals("No resource at /cats/", $captured);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue