From 41939b790da5ba8793dd89cc3ead6d83e13637a7 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Jul 2016 23:17:13 +0200 Subject: [PATCH] Added more Rest module tests --- module/Rest/test/ConfigProviderTest.php | 33 ++++++++ .../ErrorHandler/JsonErrorHandlerTest.php | 79 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 module/Rest/test/ConfigProviderTest.php create mode 100644 module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php diff --git a/module/Rest/test/ConfigProviderTest.php b/module/Rest/test/ConfigProviderTest.php new file mode 100644 index 00000000..377c845f --- /dev/null +++ b/module/Rest/test/ConfigProviderTest.php @@ -0,0 +1,33 @@ +configProvider = new ConfigProvider(); + } + + /** + * @test + */ + public function properConfigIsReturned() + { + $config = $this->configProvider->__invoke(); + + $this->assertArrayHasKey('error_handler', $config); + $this->assertArrayHasKey('middleware_pipeline', $config); + $this->assertArrayHasKey('rest', $config); + $this->assertArrayHasKey('routes', $config); + $this->assertArrayHasKey('services', $config); + $this->assertArrayHasKey('translator', $config); + } +} diff --git a/module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php b/module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php new file mode 100644 index 00000000..ea1eee80 --- /dev/null +++ b/module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php @@ -0,0 +1,79 @@ +errorHandler = new JsonErrorHandler(); + } + + /** + * @test + */ + public function noMatchedRouteReturnsNotFoundResponse() + { + $response = $this->errorHandler->__invoke(ServerRequestFactory::fromGlobals(), new Response()); + $this->assertInstanceOf(Response\JsonResponse::class, $response); + $this->assertEquals(404, $response->getStatusCode()); + } + + /** + * @test + */ + public function matchedRouteWithErrorReturnsMethodNotAllowedResponse() + { + $response = $this->errorHandler->__invoke( + ServerRequestFactory::fromGlobals(), + (new Response())->withStatus(405), + 405 + ); + $this->assertInstanceOf(Response\JsonResponse::class, $response); + $this->assertEquals(405, $response->getStatusCode()); + } + + /** + * @test + */ + public function responseWithErrorKeepsStatus() + { + $response = $this->errorHandler->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute( + RouteResult::class, + RouteResult::fromRouteMatch('foo', 'bar', []) + ), + (new Response())->withStatus(401), + 401 + ); + $this->assertInstanceOf(Response\JsonResponse::class, $response); + $this->assertEquals(401, $response->getStatusCode()); + } + + /** + * @test + */ + public function responseWithoutErrorReturnsStatus500() + { + $response = $this->errorHandler->__invoke( + ServerRequestFactory::fromGlobals()->withAttribute( + RouteResult::class, + RouteResult::fromRouteMatch('foo', 'bar', []) + ), + (new Response())->withStatus(200), + 'Some error' + ); + $this->assertInstanceOf(Response\JsonResponse::class, $response); + $this->assertEquals(500, $response->getStatusCode()); + } +}