2022-02-20 03:50:21 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware\Mercure;
|
|
|
|
|
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use Mezzio\ProblemDetails\ProblemDetailsResponseFactory;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 15:51:29 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2022-02-20 03:50:21 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Shlinkio\Shlink\Rest\Exception\MercureException;
|
|
|
|
use Shlinkio\Shlink\Rest\Middleware\Mercure\NotConfiguredMercureErrorHandler;
|
|
|
|
|
|
|
|
class NotConfiguredMercureErrorHandlerTest extends TestCase
|
|
|
|
{
|
|
|
|
private NotConfiguredMercureErrorHandler $middleware;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & ProblemDetailsResponseFactory $respFactory;
|
|
|
|
private MockObject & LoggerInterface $logger;
|
|
|
|
private MockObject & RequestHandlerInterface $handler;
|
2022-02-20 03:50:21 -06:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-23 15:51:29 -05:00
|
|
|
$this->respFactory = $this->createMock(ProblemDetailsResponseFactory::class);
|
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
|
|
$this->middleware = new NotConfiguredMercureErrorHandler($this->respFactory, $this->logger);
|
|
|
|
$this->handler = $this->createMock(RequestHandlerInterface::class);
|
2022-02-20 03:50:21 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2022-02-20 03:50:21 -06:00
|
|
|
public function requestHandlerIsInvokedWhenNotErrorOccurs(): void
|
|
|
|
{
|
|
|
|
$req = ServerRequestFactory::fromGlobals();
|
2022-10-23 15:51:29 -05:00
|
|
|
$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');
|
2022-02-20 03:50:21 -06:00
|
|
|
|
2022-10-23 15:51:29 -05:00
|
|
|
$this->middleware->process($req, $this->handler);
|
2022-02-20 03:50:21 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2022-02-20 03:50:21 -06:00
|
|
|
public function exceptionIsParsedToResponse(): void
|
|
|
|
{
|
|
|
|
$req = ServerRequestFactory::fromGlobals();
|
2022-10-23 15:51:29 -05:00
|
|
|
$this->handler->expects($this->once())->method('handle')->with($req)->willThrowException(
|
|
|
|
MercureException::mercureNotConfigured(),
|
|
|
|
);
|
|
|
|
$this->respFactory->expects($this->once())->method('createResponseFromThrowable')->willReturn(new Response());
|
|
|
|
$this->logger->expects($this->once())->method('warning');
|
2022-02-20 03:50:21 -06:00
|
|
|
|
2022-10-23 15:51:29 -05:00
|
|
|
$this->middleware->process($req, $this->handler);
|
2022-02-20 03:50:21 -06:00
|
|
|
}
|
|
|
|
}
|