diff --git a/.idea/php.xml b/.idea/php.xml index f6e673a..a7994be 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -1,3 +1,8 @@ - - + + + + + + + \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 017916a..ccb135b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -20,8 +20,11 @@ - - ./test/ + + ./test/tests/unit/ + + + ./test/tests/integration/ diff --git a/src/pjdietz/WellRESTed/Router.php b/src/pjdietz/WellRESTed/Router.php index 0f4d202..45a0192 100644 --- a/src/pjdietz/WellRESTed/Router.php +++ b/src/pjdietz/WellRESTed/Router.php @@ -136,7 +136,7 @@ class Router implements HandlerInterface public function getResponse(RequestInterface $request, array $args = null) { $response = $this->tryResponse($this->routeTable, $request, $args); - if ($response) { + if ($response && $response instanceof ResponseInterface) { // Check if the router has an error handler for this status code. $status = $response->getStatusCode(); $errorResponse = $this->getErrorResponse($status, $request, $args, $response); diff --git a/test/src/MockHandler.php b/test/src/MockHandler.php deleted file mode 100644 index e39712d..0000000 --- a/test/src/MockHandler.php +++ /dev/null @@ -1,17 +0,0 @@ -setStatusCode(200); - return $resp; - } -} diff --git a/test/tests/integration/RouterTest.php b/test/tests/integration/RouterTest.php new file mode 100644 index 0000000..79edcf3 --- /dev/null +++ b/test/tests/integration/RouterTest.php @@ -0,0 +1,75 @@ +request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); + $this->request->getPath()->willReturn("/"); + $this->request->getMethod()->willReturn("GET"); + $this->response = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\ResponseInterface"); + $this->response->getStatusCode()->willReturn(200); + $this->response->getBody()->willReturn("Hello, world!"); + } + + public function testDispatchesCallable() + { + $response = $this->response; + + $router = new Router(); + $router->add("/", function () use ($response) { + return $response->reveal(); + }); + + $result = $router->getResponse($this->request->reveal()); + $this->assertSame($response->reveal(), $result); + } + + public function testDispatchesCallableWithArguments() + { + $response = $this->response; + $args = ["cat" => "molly"]; + + $router = new Router(); + $router->add("/", function ($rqst, $args) use ($response) { + $response->getBody()->willReturn($args["cat"]); + return $response->reveal(); + }); + + $result = $router->getResponse($this->request->reveal(), $args); + $this->assertEquals("molly", $result->getBody()); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testStopsDispatchingCallablesAfterFirstNonNull() + { + $router = new Router(); + $router->add("/cats/{cat}", function () { + echo "Hello, cat!"; + return true; + }); + $router->add("/cats/{cat}", function () { + echo "Hello, cat!"; + }); + + $this->request->getPath()->willReturn("/cats/molly"); + + ob_start(); + $router->getResponse($this->request->reveal()); + $captured = ob_get_contents(); + ob_end_clean(); + + $this->assertEquals("Hello, cat!", $captured); + } +} diff --git a/test/ApacheRequestHeadersTest.php b/test/tests/unit/ApacheRequestHeadersTest.php similarity index 100% rename from test/ApacheRequestHeadersTest.php rename to test/tests/unit/ApacheRequestHeadersTest.php diff --git a/test/ClientTest.php b/test/tests/unit/ClientTest.php similarity index 94% rename from test/ClientTest.php rename to test/tests/unit/ClientTest.php index 93af958..0cfc012 100644 --- a/test/ClientTest.php +++ b/test/tests/unit/ClientTest.php @@ -19,7 +19,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $host = "localhost"; $port = $this->getRandomNumberInRange(getenv("PORT")); - $script = realpath(__DIR__ . "/sham-routers/method.php"); + $script = realpath(__DIR__ . "/../../sham-routers/method.php"); $server = new ShamServer($host, $port, $script); @@ -57,7 +57,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $host = "localhost"; $port = $this->getRandomNumberInRange(getenv("PORT")); - $script = realpath(__DIR__ . "/sham-routers/headers.php"); + $script = realpath(__DIR__ . "/../../sham-routers/headers.php"); $server = new ShamServer($host, $port, $script); @@ -92,7 +92,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $host = "localhost"; $port = $this->getRandomNumberInRange(getenv("PORT")); - $script = realpath(__DIR__ . "/sham-routers/body.php"); + $script = realpath(__DIR__ . "/../../sham-routers/body.php"); $server = new ShamServer($host, $port, $script); $rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); @@ -125,7 +125,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $host = "localhost"; $port = $this->getRandomNumberInRange(getenv("PORT")); - $script = realpath(__DIR__ . "/sham-routers/formFields.php"); + $script = realpath(__DIR__ . "/../../sham-routers/formFields.php"); $server = new ShamServer($host, $port, $script); $rqst = new Request("http://$host:$port"); @@ -158,7 +158,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $host = "localhost"; $port = $this->getRandomNumberInRange(getenv("PORT")); - $script = realpath(__DIR__ . "/sham-routers/headers.php"); + $script = realpath(__DIR__ . "/../../sham-routers/headers.php"); $server = new ShamServer($host, $port, $script); $rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); @@ -181,7 +181,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $host = "localhost"; $port = $this->getRandomNumberInRange(getenv("PORT")); - $script = realpath(__DIR__ . "/sham-routers/headers.php"); + $script = realpath(__DIR__ . "/../../sham-routers/headers.php"); $server = new ShamServer($host, $port, $script); $rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); diff --git a/test/HandlerTest.php b/test/tests/unit/HandlerTest.php similarity index 100% rename from test/HandlerTest.php rename to test/tests/unit/HandlerTest.php diff --git a/test/HandlerUnpackerTest.php b/test/tests/unit/HandlerUnpackerTest.php similarity index 100% rename from test/HandlerUnpackerTest.php rename to test/tests/unit/HandlerUnpackerTest.php diff --git a/test/MessageTest.php b/test/tests/unit/MessageTest.php similarity index 100% rename from test/MessageTest.php rename to test/tests/unit/MessageTest.php diff --git a/test/RequestTest.php b/test/tests/unit/RequestTest.php similarity index 100% rename from test/RequestTest.php rename to test/tests/unit/RequestTest.php diff --git a/test/ResponseTest.php b/test/tests/unit/ResponseTest.php similarity index 100% rename from test/ResponseTest.php rename to test/tests/unit/ResponseTest.php diff --git a/test/RouteBuilderTest.php b/test/tests/unit/RouteBuilderTest.php similarity index 100% rename from test/RouteBuilderTest.php rename to test/tests/unit/RouteBuilderTest.php diff --git a/test/RouteTableTest.php b/test/tests/unit/RouteTableTest.php similarity index 100% rename from test/RouteTableTest.php rename to test/tests/unit/RouteTableTest.php diff --git a/test/RouterTest.php b/test/tests/unit/RouterTest.php similarity index 100% rename from test/RouterTest.php rename to test/tests/unit/RouterTest.php diff --git a/test/Routes/BaseRouteTest.php b/test/tests/unit/Routes/BaseRouteTest.php similarity index 100% rename from test/Routes/BaseRouteTest.php rename to test/tests/unit/Routes/BaseRouteTest.php diff --git a/test/Routes/PrefixRouteTest.php b/test/tests/unit/Routes/PrefixRouteTest.php similarity index 100% rename from test/Routes/PrefixRouteTest.php rename to test/tests/unit/Routes/PrefixRouteTest.php diff --git a/test/Routes/RegexRouteTest.php b/test/tests/unit/Routes/RegexRouteTest.php similarity index 100% rename from test/Routes/RegexRouteTest.php rename to test/tests/unit/Routes/RegexRouteTest.php diff --git a/test/Routes/RouteFactoryTest.php b/test/tests/unit/Routes/RouteFactoryTest.php similarity index 100% rename from test/Routes/RouteFactoryTest.php rename to test/tests/unit/Routes/RouteFactoryTest.php diff --git a/test/Routes/StaticRouteTest.php b/test/tests/unit/Routes/StaticRouteTest.php similarity index 100% rename from test/Routes/StaticRouteTest.php rename to test/tests/unit/Routes/StaticRouteTest.php diff --git a/test/Routes/TemplateRouteTest.php b/test/tests/unit/Routes/TemplateRouteTest.php similarity index 100% rename from test/Routes/TemplateRouteTest.php rename to test/tests/unit/Routes/TemplateRouteTest.php