From d1018b6da7428e82dd96d80906f9a3e4ae5df497 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 24 Mar 2017 21:38:43 +0100 Subject: [PATCH] Fixed tests --- .../ErrorHandler/JsonErrorHandlerTest.php | 47 +++---------------- .../CheckAuthenticationMiddlewareTest.php | 15 +++--- 2 files changed, 14 insertions(+), 48 deletions(-) diff --git a/module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php b/module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php index 1e410d67..75d4ad0e 100644 --- a/module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php +++ b/module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php @@ -5,7 +5,6 @@ use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Rest\ErrorHandler\JsonErrorHandler; use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequestFactory; -use Zend\Expressive\Router\RouteResult; class JsonErrorHandlerTest extends TestCase { @@ -22,58 +21,24 @@ class JsonErrorHandlerTest extends TestCase /** * @test */ - public function noMatchedRouteReturnsNotFoundResponse() + public function noErrorStatusReturnsInternalServerError() { - $response = $this->errorHandler->__invoke(ServerRequestFactory::fromGlobals(), new Response()); + $response = $this->errorHandler->__invoke(null, ServerRequestFactory::fromGlobals(), new Response()); $this->assertInstanceOf(Response\JsonResponse::class, $response); - $this->assertEquals(404, $response->getStatusCode()); + $this->assertEquals(500, $response->getStatusCode()); } /** * @test */ - public function matchedRouteWithErrorReturnsMethodNotAllowedResponse() + public function errorStatusReturnsThatStatus() { $response = $this->errorHandler->__invoke( + null, ServerRequestFactory::fromGlobals(), - (new Response())->withStatus(405), - 405 + (new Response())->withStatus(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()); - } } diff --git a/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php b/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php index fca26c2e..4f472726 100644 --- a/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php +++ b/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php @@ -7,6 +7,7 @@ use Shlinkio\Shlink\Rest\Authentication\JWTService; use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware; use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequestFactory; +use Zend\Expressive\Router\Route; use Zend\Expressive\Router\RouteResult; use Zend\I18n\Translator\Translator; @@ -55,7 +56,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase $request = ServerRequestFactory::fromGlobals()->withAttribute( RouteResult::class, - RouteResult::fromRouteMatch('rest-authenticate', 'foo', []) + RouteResult::fromRoute(new Route('foo', '', Route::HTTP_METHOD_ANY, 'rest-authenticate'), []) ); $response = new Response(); $isCalled = false; @@ -67,7 +68,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase $request = ServerRequestFactory::fromGlobals()->withAttribute( RouteResult::class, - RouteResult::fromRouteMatch('bar', 'foo', []) + RouteResult::fromRoute(new Route('bar', 'foo'), []) )->withMethod('OPTIONS'); $response = new Response(); $isCalled = false; @@ -85,7 +86,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase { $request = ServerRequestFactory::fromGlobals()->withAttribute( RouteResult::class, - RouteResult::fromRouteMatch('bar', 'foo', []) + RouteResult::fromRoute(new Route('bar', 'foo'), []) ); $response = $this->middleware->__invoke($request, new Response()); $this->assertEquals(401, $response->getStatusCode()); @@ -99,7 +100,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase $authToken = 'ABC-abc'; $request = ServerRequestFactory::fromGlobals()->withAttribute( RouteResult::class, - RouteResult::fromRouteMatch('bar', 'foo', []) + RouteResult::fromRoute(new Route('bar', 'foo'), []) )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken); $response = $this->middleware->__invoke($request, new Response()); @@ -115,7 +116,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase $authToken = 'ABC-abc'; $request = ServerRequestFactory::fromGlobals()->withAttribute( RouteResult::class, - RouteResult::fromRouteMatch('bar', 'foo', []) + RouteResult::fromRoute(new Route('bar', 'foo'), []) )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Basic ' . $authToken); $response = $this->middleware->__invoke($request, new Response()); @@ -133,7 +134,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase $authToken = 'ABC-abc'; $request = ServerRequestFactory::fromGlobals()->withAttribute( RouteResult::class, - RouteResult::fromRouteMatch('bar', 'foo', []) + RouteResult::fromRoute(new Route('bar', 'foo'), []) )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Bearer ' . $authToken); $this->jwtService->verify($authToken)->willReturn(false)->shouldBeCalledTimes(1); @@ -149,7 +150,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase $authToken = 'ABC-abc'; $request = ServerRequestFactory::fromGlobals()->withAttribute( RouteResult::class, - RouteResult::fromRouteMatch('bar', 'foo', []) + RouteResult::fromRoute(new Route('bar', 'foo'), []) )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'bearer ' . $authToken); $this->jwtService->verify($authToken)->willReturn(true)->shouldBeCalledTimes(1); $this->jwtService->refresh($authToken)->willReturn($authToken)->shouldBeCalledTimes(1);