From b601c38d369dd4574c321d7aba711ac74cdc21b7 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Mon, 14 Jul 2014 00:19:04 -0400 Subject: [PATCH] Add tests for Router --- src/pjdietz/WellRESTed/Router.php | 19 -------- test/RouterTest.php | 72 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 test/RouterTest.php diff --git a/src/pjdietz/WellRESTed/Router.php b/src/pjdietz/WellRESTed/Router.php index f971e9b..ff90dd4 100644 --- a/src/pjdietz/WellRESTed/Router.php +++ b/src/pjdietz/WellRESTed/Router.php @@ -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; - } - } diff --git a/test/RouterTest.php b/test/RouterTest.php new file mode 100644 index 0000000..093a1d8 --- /dev/null +++ b/test/RouterTest.php @@ -0,0 +1,72 @@ +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; + } + +}