Add tests for Router
This commit is contained in:
parent
efd1843603
commit
b601c38d36
|
|
@ -39,11 +39,6 @@ class Router implements HandlerInterface
|
|||
*/
|
||||
public function getResponse(RequestInterface $request, array $args = null)
|
||||
{
|
||||
// Use the singleton if the caller did not pass a request.
|
||||
if (is_null($request)) {
|
||||
$request = Request::getRequest();
|
||||
}
|
||||
|
||||
foreach ($this->routes as $route) {
|
||||
/** @var HandlerInterface $route */
|
||||
$responce = $route->getResponse($request, $args);
|
||||
|
|
@ -100,18 +95,4 @@ class Router implements HandlerInterface
|
|||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a response indicating a 500 Internal Server Error
|
||||
*
|
||||
* @param RequestInterface $request
|
||||
* @param string $message Optional additional message.
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function getInternalServerErrorResponse(RequestInterface $request, $message = '')
|
||||
{
|
||||
$response = new Response(500);
|
||||
$response->setBody('Server error at ' . $request->getPath() . "\n" . $message);
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
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 = "/";
|
||||
|
||||
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
|
||||
$mockRequest->expects($this->any())
|
||||
->method('getPath')
|
||||
->will($this->returnValue($path));
|
||||
|
||||
$route = new StaticRoute($path, 'MockHandler');
|
||||
$router = new Router();
|
||||
$router->addRoute($route);
|
||||
$resp = $router->getResponse($mockRequest);
|
||||
$this->assertNotNull($resp);
|
||||
|
||||
}
|
||||
|
||||
public function testAddRoutes()
|
||||
{
|
||||
$path = "/";
|
||||
|
||||
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
|
||||
$mockRequest->expects($this->any())
|
||||
->method('getPath')
|
||||
->will($this->returnValue($path));
|
||||
|
||||
$routes = array();
|
||||
$routes[] = new StaticRoute("/not-this", 'MockHandler');
|
||||
$routes[] = new StaticRoute("/or-this", 'MockHandler');
|
||||
|
||||
$router = new Router();
|
||||
$router->addRoutes($routes);
|
||||
$resp = $router->getResponse($mockRequest);
|
||||
$this->assertEquals(404, $resp->getStatusCode());
|
||||
|
||||
}
|
||||
|
||||
public function testStaticRequest()
|
||||
{
|
||||
$path = "/";
|
||||
$original = $_SERVER;
|
||||
$_SERVER["REQUEST_URI"] = $path;
|
||||
$_SERVER["HTTP_HOST"] = "localhost";
|
||||
|
||||
$route = new StaticRoute($path, 'MockHandler');
|
||||
$router = new Router();
|
||||
$router->addRoute($route);
|
||||
ob_start();
|
||||
@$router->respond();
|
||||
ob_end_clean();
|
||||
|
||||
$_SERVER = $original;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue