Update RouterTest

This commit is contained in:
PJ Dietz 2014-07-26 05:48:54 -04:00
parent 4d31e2e977
commit ea05633dfb
1 changed files with 34 additions and 12 deletions

View File

@ -2,16 +2,14 @@
namespace pjdietz\WellRESTed\Test; 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\Router;
use pjdietz\WellRESTed\Routes\StaticRoute; use pjdietz\WellRESTed\Routes\StaticRoute;
class RouterTest extends \PHPUnit_Framework_TestCase class RouterTest extends \PHPUnit_Framework_TestCase
{ {
public static function setUpBeforeClass()
{
include_once(__DIR__ . "/src/MockHandler.php");
}
public function testAddRoute() public function testAddRoute()
{ {
$path = "/"; $path = "/";
@ -21,12 +19,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase
->method('getPath') ->method('getPath')
->will($this->returnValue($path)); ->will($this->returnValue($path));
$route = new StaticRoute($path, 'MockHandler'); $route = new StaticRoute($path, __NAMESPACE__ . '\\RouterTestHandler');
$router = new Router(); $router = new Router();
$router->addRoute($route); $router->addRoute($route);
$resp = $router->getResponse($mockRequest); $resp = $router->getResponse($mockRequest);
$this->assertNotNull($resp); $this->assertNotNull($resp);
} }
public function testAddRoutes() public function testAddRoutes()
@ -39,14 +36,27 @@ class RouterTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue($path)); ->will($this->returnValue($path));
$routes = array(); $routes = array();
$routes[] = new StaticRoute("/not-this", 'MockHandler'); $routes[] = new StaticRoute("/", __NAMESPACE__ . '\\RouterTestHandler');
$routes[] = new StaticRoute("/or-this", 'MockHandler'); $routes[] = new StaticRoute("/another/", __NAMESPACE__ . '\\RouterTestHandler');
$router = new Router(); $router = new Router();
$router->addRoutes($routes); $router->addRoutes($routes);
$resp = $router->getResponse($mockRequest); $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() public function testStaticRequest()
@ -56,7 +66,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$_SERVER["REQUEST_URI"] = $path; $_SERVER["REQUEST_URI"] = $path;
$_SERVER["HTTP_HOST"] = "localhost"; $_SERVER["HTTP_HOST"] = "localhost";
$route = new StaticRoute($path, 'MockHandler'); $route = new StaticRoute($path, __NAMESPACE__ . '\\RouterTestHandler');
$router = new Router(); $router = new Router();
$router->addRoute($route); $router->addRoute($route);
ob_start(); ob_start();
@ -65,5 +75,17 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$_SERVER = $original; $_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;
}
} }