Created NotConfiguredMercureErrorHandlerTest

This commit is contained in:
Alejandro Celaya 2022-02-20 10:50:21 +01:00
parent 6983f9b2bf
commit 5a25211371
3 changed files with 63 additions and 19 deletions

View file

@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
### Changed
* [#1359](https://github.com/shlinkio/shlink/issues/1359) Hidden database commands.
* [#1385](https://github.com/shlinkio/shlink/issues/1385) Prevented a big error message from being logged when using Shlink without mercure.
### Deprecated
* [#1340](https://github.com/shlinkio/shlink/issues/1340) Deprecated webhooks. New events will only be added to other real-time updates approaches, and webhooks will be completely removed in Shlink 4.0.0.

View file

@ -11,7 +11,6 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use RuntimeException;
use Shlinkio\Shlink\Common\Mercure\JwtProviderInterface;
use Shlinkio\Shlink\Rest\Action\MercureInfoAction;
use Shlinkio\Shlink\Rest\Exception\MercureException;
@ -49,24 +48,6 @@ class MercureInfoActionTest extends TestCase
yield 'host is null' => [['public_hub_url' => null]];
}
/**
* @test
* @dataProvider provideValidConfigs
*/
public function throwsExceptionWhenBuildingTokenFails(array $mercureConfig): void
{
$buildToken = $this->provider->buildSubscriptionToken(Argument::any())->willThrow(
new RuntimeException('Error'),
);
$action = new MercureInfoAction($this->provider->reveal(), $mercureConfig);
$this->expectException(MercureException::class);
$buildToken->shouldBeCalledOnce();
$action->handle(ServerRequestFactory::fromGlobals());
}
public function provideValidConfigs(): iterable
{
yield 'days not defined' => [['public_hub_url' => 'http://foobar.com']];

View file

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware\Mercure;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use Mezzio\ProblemDetails\ProblemDetailsResponseFactory;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
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
{
use ProphecyTrait;
private NotConfiguredMercureErrorHandler $middleware;
private ObjectProphecy $respFactory;
private ObjectProphecy $logger;
private ObjectProphecy $handler;
protected function setUp(): void
{
$this->respFactory = $this->prophesize(ProblemDetailsResponseFactory::class);
$this->logger = $this->prophesize(LoggerInterface::class);
$this->middleware = new NotConfiguredMercureErrorHandler($this->respFactory->reveal(), $this->logger->reveal());
$this->handler = $this->prophesize(RequestHandlerInterface::class);
}
/** @test */
public function requestHandlerIsInvokedWhenNotErrorOccurs(): void
{
$req = ServerRequestFactory::fromGlobals();
$handle = $this->handler->handle($req)->willReturn(new Response());
$this->middleware->process($req, $this->handler->reveal());
$handle->shouldHaveBeenCalledOnce();
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
$this->respFactory->createResponseFromThrowable(Argument::cetera())->shouldNotHaveBeenCalled();
}
/** @test */
public function exceptionIsParsedToResponse(): void
{
$req = ServerRequestFactory::fromGlobals();
$handle = $this->handler->handle($req)->willThrow(MercureException::mercureNotConfigured());
$createResp = $this->respFactory->createResponseFromThrowable(Argument::cetera())->willReturn(new Response());
$this->middleware->process($req, $this->handler->reveal());
$handle->shouldHaveBeenCalledOnce();
$createResp->shouldHaveBeenCalledOnce();
$this->logger->warning(Argument::cetera())->shouldHaveBeenCalledOnce();
}
}