Migrated OverrideDomainMiddlewareTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 23:00:49 +02:00
parent d61c79da84
commit 083ccd36b7

View File

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
use Laminas\Diactoros\Response; use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
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\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface; use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
@ -22,20 +20,18 @@ use Shlinkio\Shlink\Rest\Middleware\ShortUrl\OverrideDomainMiddleware;
class OverrideDomainMiddlewareTest extends TestCase class OverrideDomainMiddlewareTest extends TestCase
{ {
use ProphecyTrait;
private OverrideDomainMiddleware $middleware; private OverrideDomainMiddleware $middleware;
private ObjectProphecy $domainService; private MockObject $domainService;
private ObjectProphecy $apiKey; private MockObject $apiKey;
private ObjectProphecy $handler; private MockObject $handler;
protected function setUp(): void protected function setUp(): void
{ {
$this->apiKey = $this->prophesize(ApiKey::class); $this->apiKey = $this->createMock(ApiKey::class);
$this->handler = $this->prophesize(RequestHandlerInterface::class); $this->handler = $this->createMock(RequestHandlerInterface::class);
$this->domainService = $this->prophesize(DomainServiceInterface::class); $this->domainService = $this->createMock(DomainServiceInterface::class);
$this->middleware = new OverrideDomainMiddleware($this->domainService->reveal()); $this->middleware = new OverrideDomainMiddleware($this->domainService);
} }
/** @test */ /** @test */
@ -43,16 +39,13 @@ class OverrideDomainMiddlewareTest extends TestCase
{ {
$request = $this->requestWithApiKey(); $request = $this->requestWithApiKey();
$response = new Response(); $response = new Response();
$hasRole = $this->apiKey->hasRole(Role::DOMAIN_SPECIFIC)->willReturn(false); $this->apiKey->expects($this->once())->method('hasRole')->with(Role::DOMAIN_SPECIFIC)->willReturn(false);
$handle = $this->handler->handle($request)->willReturn($response); $this->handler->expects($this->once())->method('handle')->with($request)->willReturn($response);
$getDomain = $this->domainService->getDomain(Argument::cetera()); $this->domainService->expects($this->never())->method('getDomain');
$result = $this->middleware->process($request, $this->handler->reveal()); $result = $this->middleware->process($request, $this->handler);
self::assertSame($response, $result); self::assertSame($response, $result);
$hasRole->shouldHaveBeenCalledOnce();
$handle->shouldHaveBeenCalledOnce();
$getDomain->shouldNotHaveBeenCalled();
} }
/** /**
@ -62,22 +55,19 @@ class OverrideDomainMiddlewareTest extends TestCase
public function overwritesRequestBodyWhenMethodIsPost(Domain $domain, array $body, array $expectedBody): void public function overwritesRequestBodyWhenMethodIsPost(Domain $domain, array $body, array $expectedBody): void
{ {
$request = $this->requestWithApiKey()->withMethod('POST')->withParsedBody($body); $request = $this->requestWithApiKey()->withMethod('POST')->withParsedBody($body);
$hasRole = $this->apiKey->hasRole(Role::DOMAIN_SPECIFIC)->willReturn(true); $this->apiKey->expects($this->once())->method('hasRole')->with(Role::DOMAIN_SPECIFIC)->willReturn(true);
$getRoleMeta = $this->apiKey->getRoleMeta(Role::DOMAIN_SPECIFIC)->willReturn(['domain_id' => '123']); $this->apiKey->expects($this->once())->method('getRoleMeta')->with(Role::DOMAIN_SPECIFIC)->willReturn(
$getDomain = $this->domainService->getDomain('123')->willReturn($domain); ['domain_id' => '123'],
$handle = $this->handler->handle(Argument::that( );
$this->domainService->expects($this->once())->method('getDomain')->with('123')->willReturn($domain);
$this->handler->expects($this->once())->method('handle')->with($this->callback(
function (ServerRequestInterface $req) use ($expectedBody): bool { function (ServerRequestInterface $req) use ($expectedBody): bool {
Assert::assertEquals($req->getParsedBody(), $expectedBody); Assert::assertEquals($req->getParsedBody(), $expectedBody);
return true; return true;
}, },
))->willReturn(new Response()); ))->willReturn(new Response());
$this->middleware->process($request, $this->handler->reveal()); $this->middleware->process($request, $this->handler);
$hasRole->shouldHaveBeenCalledOnce();
$getRoleMeta->shouldHaveBeenCalledOnce();
$getDomain->shouldHaveBeenCalledOnce();
$handle->shouldHaveBeenCalledOnce();
} }
public function provideBodies(): iterable public function provideBodies(): iterable
@ -112,22 +102,19 @@ class OverrideDomainMiddlewareTest extends TestCase
{ {
$domain = Domain::withAuthority('something.com'); $domain = Domain::withAuthority('something.com');
$request = $this->requestWithApiKey()->withMethod($method); $request = $this->requestWithApiKey()->withMethod($method);
$hasRole = $this->apiKey->hasRole(Role::DOMAIN_SPECIFIC)->willReturn(true); $this->apiKey->expects($this->once())->method('hasRole')->with(Role::DOMAIN_SPECIFIC)->willReturn(true);
$getRoleMeta = $this->apiKey->getRoleMeta(Role::DOMAIN_SPECIFIC)->willReturn(['domain_id' => '123']); $this->apiKey->expects($this->once())->method('getRoleMeta')->with(Role::DOMAIN_SPECIFIC)->willReturn(
$getDomain = $this->domainService->getDomain('123')->willReturn($domain); ['domain_id' => '123'],
$handle = $this->handler->handle(Argument::that( );
$this->domainService->expects($this->once())->method('getDomain')->with('123')->willReturn($domain);
$this->handler->expects($this->once())->method('handle')->with($this->callback(
function (ServerRequestInterface $req): bool { function (ServerRequestInterface $req): bool {
Assert::assertEquals($req->getAttribute(ShortUrlInputFilter::DOMAIN), 'something.com'); Assert::assertEquals($req->getAttribute(ShortUrlInputFilter::DOMAIN), 'something.com');
return true; return true;
}, },
))->willReturn(new Response()); ))->willReturn(new Response());
$this->middleware->process($request, $this->handler->reveal()); $this->middleware->process($request, $this->handler);
$hasRole->shouldHaveBeenCalledOnce();
$getRoleMeta->shouldHaveBeenCalledOnce();
$getDomain->shouldHaveBeenCalledOnce();
$handle->shouldHaveBeenCalledOnce();
} }
public function provideMethods(): iterable public function provideMethods(): iterable
@ -140,6 +127,6 @@ class OverrideDomainMiddlewareTest extends TestCase
private function requestWithApiKey(): ServerRequestInterface private function requestWithApiKey(): ServerRequestInterface
{ {
return ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $this->apiKey->reveal()); return ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $this->apiKey);
} }
} }