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