Fixed tests

This commit is contained in:
Alejandro Celaya 2017-03-24 21:38:43 +01:00
parent fe7928ae0e
commit d1018b6da7
2 changed files with 14 additions and 48 deletions

View File

@ -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());
}
}

View File

@ -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);