mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-23 07:33:58 -06:00
Migrated AuthenticationMiddlewareTest to use PHPUnit mocks
This commit is contained in:
parent
b1f814e118
commit
3433899577
@ -10,10 +10,8 @@ use Laminas\Diactoros\ServerRequest;
|
|||||||
use Laminas\Diactoros\ServerRequestFactory;
|
use Laminas\Diactoros\ServerRequestFactory;
|
||||||
use Mezzio\Router\Route;
|
use Mezzio\Router\Route;
|
||||||
use Mezzio\Router\RouteResult;
|
use Mezzio\Router\RouteResult;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\MiddlewareInterface;
|
use Psr\Http\Server\MiddlewareInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
@ -29,21 +27,19 @@ use function Laminas\Stratigility\middleware;
|
|||||||
|
|
||||||
class AuthenticationMiddlewareTest extends TestCase
|
class AuthenticationMiddlewareTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private AuthenticationMiddleware $middleware;
|
private AuthenticationMiddleware $middleware;
|
||||||
private ObjectProphecy $apiKeyService;
|
private MockObject $apiKeyService;
|
||||||
private ObjectProphecy $handler;
|
private MockObject $handler;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class);
|
$this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class);
|
||||||
$this->middleware = new AuthenticationMiddleware(
|
$this->middleware = new AuthenticationMiddleware(
|
||||||
$this->apiKeyService->reveal(),
|
$this->apiKeyService,
|
||||||
[HealthAction::class],
|
[HealthAction::class],
|
||||||
['with_query_api_key'],
|
['with_query_api_key'],
|
||||||
);
|
);
|
||||||
$this->handler = $this->prophesize(RequestHandlerInterface::class);
|
$this->handler = $this->createMock(RequestHandlerInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,13 +48,10 @@ class AuthenticationMiddlewareTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function someSituationsFallbackToNextMiddleware(ServerRequestInterface $request): void
|
public function someSituationsFallbackToNextMiddleware(ServerRequestInterface $request): void
|
||||||
{
|
{
|
||||||
$handle = $this->handler->handle($request)->willReturn(new Response());
|
$this->handler->expects($this->once())->method('handle')->with($request)->willReturn(new Response());
|
||||||
$checkApiKey = $this->apiKeyService->check(Argument::any());
|
$this->apiKeyService->expects($this->never())->method('check');
|
||||||
|
|
||||||
$this->middleware->process($request, $this->handler->reveal());
|
$this->middleware->process($request, $this->handler);
|
||||||
|
|
||||||
$handle->shouldHaveBeenCalledOnce();
|
|
||||||
$checkApiKey->shouldNotHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideRequestsWithoutAuth(): iterable
|
public function provideRequestsWithoutAuth(): iterable
|
||||||
@ -90,12 +83,12 @@ class AuthenticationMiddlewareTest extends TestCase
|
|||||||
ServerRequestInterface $request,
|
ServerRequestInterface $request,
|
||||||
string $expectedMessage,
|
string $expectedMessage,
|
||||||
): void {
|
): void {
|
||||||
$this->apiKeyService->check(Argument::any())->shouldNotBeCalled();
|
$this->apiKeyService->expects($this->never())->method('check');
|
||||||
$this->handler->handle($request)->shouldNotBeCalled();
|
$this->handler->expects($this->never())->method('handle');
|
||||||
$this->expectException(MissingAuthenticationException::class);
|
$this->expectException(MissingAuthenticationException::class);
|
||||||
$this->expectExceptionMessage($expectedMessage);
|
$this->expectExceptionMessage($expectedMessage);
|
||||||
|
|
||||||
$this->middleware->process($request, $this->handler->reveal());
|
$this->middleware->process($request, $this->handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideRequestsWithoutApiKey(): iterable
|
public function provideRequestsWithoutApiKey(): iterable
|
||||||
@ -127,12 +120,14 @@ class AuthenticationMiddlewareTest extends TestCase
|
|||||||
)
|
)
|
||||||
->withHeader('X-Api-Key', $apiKey);
|
->withHeader('X-Api-Key', $apiKey);
|
||||||
|
|
||||||
$this->apiKeyService->check($apiKey)->willReturn(new ApiKeyCheckResult())->shouldBeCalledOnce();
|
$this->apiKeyService->expects($this->once())->method('check')->with($apiKey)->willReturn(
|
||||||
$this->handler->handle($request)->shouldNotBeCalled();
|
new ApiKeyCheckResult(),
|
||||||
|
);
|
||||||
|
$this->handler->expects($this->never())->method('handle');
|
||||||
$this->expectException(VerifyAuthenticationException::class);
|
$this->expectException(VerifyAuthenticationException::class);
|
||||||
$this->expectExceptionMessage('Provided API key does not exist or is invalid');
|
$this->expectExceptionMessage('Provided API key does not exist or is invalid');
|
||||||
|
|
||||||
$this->middleware->process($request, $this->handler->reveal());
|
$this->middleware->process($request, $this->handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@ -147,13 +142,14 @@ class AuthenticationMiddlewareTest extends TestCase
|
|||||||
)
|
)
|
||||||
->withHeader('X-Api-Key', $key);
|
->withHeader('X-Api-Key', $key);
|
||||||
|
|
||||||
$handle = $this->handler->handle($request->withAttribute(ApiKey::class, $apiKey))->willReturn(new Response());
|
$this->handler->expects($this->once())->method('handle')->with(
|
||||||
$checkApiKey = $this->apiKeyService->check($key)->willReturn(new ApiKeyCheckResult($apiKey));
|
$request->withAttribute(ApiKey::class, $apiKey),
|
||||||
|
)->willReturn(new Response());
|
||||||
|
$this->apiKeyService->expects($this->once())->method('check')->with($key)->willReturn(
|
||||||
|
new ApiKeyCheckResult($apiKey),
|
||||||
|
);
|
||||||
|
|
||||||
$this->middleware->process($request, $this->handler->reveal());
|
$this->middleware->process($request, $this->handler);
|
||||||
|
|
||||||
$handle->shouldHaveBeenCalledOnce();
|
|
||||||
$checkApiKey->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getDummyMiddleware(): MiddlewareInterface
|
private function getDummyMiddleware(): MiddlewareInterface
|
||||||
|
Loading…
Reference in New Issue
Block a user