Migrated NotConfiguredMercureErrorHandlerTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:51:29 +02:00
parent db85915c2f
commit 674a4416cf

View file

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\Middleware\Mercure;
use Laminas\Diactoros\Response; use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\ServerRequestFactory;
use Mezzio\ProblemDetails\ProblemDetailsResponseFactory; use Mezzio\ProblemDetails\ProblemDetailsResponseFactory;
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\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Rest\Exception\MercureException; use Shlinkio\Shlink\Rest\Exception\MercureException;
@ -18,45 +16,40 @@ use Shlinkio\Shlink\Rest\Middleware\Mercure\NotConfiguredMercureErrorHandler;
class NotConfiguredMercureErrorHandlerTest extends TestCase class NotConfiguredMercureErrorHandlerTest extends TestCase
{ {
use ProphecyTrait;
private NotConfiguredMercureErrorHandler $middleware; private NotConfiguredMercureErrorHandler $middleware;
private ObjectProphecy $respFactory; private MockObject $respFactory;
private ObjectProphecy $logger; private MockObject $logger;
private ObjectProphecy $handler; private MockObject $handler;
protected function setUp(): void protected function setUp(): void
{ {
$this->respFactory = $this->prophesize(ProblemDetailsResponseFactory::class); $this->respFactory = $this->createMock(ProblemDetailsResponseFactory::class);
$this->logger = $this->prophesize(LoggerInterface::class); $this->logger = $this->createMock(LoggerInterface::class);
$this->middleware = new NotConfiguredMercureErrorHandler($this->respFactory->reveal(), $this->logger->reveal()); $this->middleware = new NotConfiguredMercureErrorHandler($this->respFactory, $this->logger);
$this->handler = $this->prophesize(RequestHandlerInterface::class); $this->handler = $this->createMock(RequestHandlerInterface::class);
} }
/** @test */ /** @test */
public function requestHandlerIsInvokedWhenNotErrorOccurs(): void public function requestHandlerIsInvokedWhenNotErrorOccurs(): void
{ {
$req = ServerRequestFactory::fromGlobals(); $req = ServerRequestFactory::fromGlobals();
$handle = $this->handler->handle($req)->willReturn(new Response()); $this->handler->expects($this->once())->method('handle')->with($req)->willReturn(new Response());
$this->respFactory->expects($this->never())->method('createResponseFromThrowable');
$this->logger->expects($this->never())->method('warning');
$this->middleware->process($req, $this->handler->reveal()); $this->middleware->process($req, $this->handler);
$handle->shouldHaveBeenCalledOnce();
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
$this->respFactory->createResponseFromThrowable(Argument::cetera())->shouldNotHaveBeenCalled();
} }
/** @test */ /** @test */
public function exceptionIsParsedToResponse(): void public function exceptionIsParsedToResponse(): void
{ {
$req = ServerRequestFactory::fromGlobals(); $req = ServerRequestFactory::fromGlobals();
$handle = $this->handler->handle($req)->willThrow(MercureException::mercureNotConfigured()); $this->handler->expects($this->once())->method('handle')->with($req)->willThrowException(
$createResp = $this->respFactory->createResponseFromThrowable(Argument::cetera())->willReturn(new Response()); MercureException::mercureNotConfigured(),
);
$this->respFactory->expects($this->once())->method('createResponseFromThrowable')->willReturn(new Response());
$this->logger->expects($this->once())->method('warning');
$this->middleware->process($req, $this->handler->reveal()); $this->middleware->process($req, $this->handler);
$handle->shouldHaveBeenCalledOnce();
$createResp->shouldHaveBeenCalledOnce();
$this->logger->warning(Argument::cetera())->shouldHaveBeenCalledOnce();
} }
} }