2022-02-20 12:50:21 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware\Mercure;
|
|
|
|
|
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use Mezzio\ProblemDetails\ProblemDetailsResponseFactory;
|
2022-10-23 23:51:29 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2022-02-20 12:50:21 +03: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 20:53:13 +03:00
|
|
|
private MockObject & ProblemDetailsResponseFactory $respFactory;
|
|
|
|
private MockObject & LoggerInterface $logger;
|
|
|
|
private MockObject & RequestHandlerInterface $handler;
|
2022-02-20 12:50:21 +03:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-23 23:51:29 +03: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 12:50:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function requestHandlerIsInvokedWhenNotErrorOccurs(): void
|
|
|
|
{
|
|
|
|
$req = ServerRequestFactory::fromGlobals();
|
2022-10-23 23:51:29 +03: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 12:50:21 +03:00
|
|
|
|
2022-10-23 23:51:29 +03:00
|
|
|
$this->middleware->process($req, $this->handler);
|
2022-02-20 12:50:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function exceptionIsParsedToResponse(): void
|
|
|
|
{
|
|
|
|
$req = ServerRequestFactory::fromGlobals();
|
2022-10-23 23:51:29 +03: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 12:50:21 +03:00
|
|
|
|
2022-10-23 23:51:29 +03:00
|
|
|
$this->middleware->process($req, $this->handler);
|
2022-02-20 12:50:21 +03:00
|
|
|
}
|
|
|
|
}
|