Created NotifyNewShortUrlToMercureTest

This commit is contained in:
Alejandro Celaya 2022-07-25 09:30:25 +02:00
parent be1ce06c00
commit 019bd4dec8
3 changed files with 107 additions and 4 deletions

View File

@ -38,9 +38,7 @@ class NotifyNewShortUrlToMercure
try {
$this->hub->publish($this->updatesGenerator->newShortUrlUpdate($shortUrl));
} catch (Throwable $e) {
$this->logger->debug('Error while trying to notify mercure hub with new short URL. {e}', [
'e' => $e,
]);
$this->logger->debug('Error while trying to notify mercure hub with new short URL. {e}', ['e' => $e]);
}
}
}

View File

@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\EventDispatcher\Mercure;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
use Shlinkio\Shlink\Core\EventDispatcher\Mercure\NotifyNewShortUrlToMercure;
use Shlinkio\Shlink\Core\Mercure\MercureUpdatesGeneratorInterface;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
class NotifyNewShortUrlToMercureTest extends TestCase
{
use ProphecyTrait;
private NotifyNewShortUrlToMercure $listener;
private ObjectProphecy $hub;
private ObjectProphecy $updatesGenerator;
private ObjectProphecy $em;
private ObjectProphecy $logger;
protected function setUp(): void
{
$this->hub = $this->prophesize(HubInterface::class);
$this->updatesGenerator = $this->prophesize(MercureUpdatesGeneratorInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->logger = $this->prophesize(LoggerInterface::class);
$this->listener = new NotifyNewShortUrlToMercure(
$this->hub->reveal(),
$this->updatesGenerator->reveal(),
$this->em->reveal(),
$this->logger->reveal(),
);
}
/** @test */
public function messageIsLoggedWhenShortUrlIsNotFound(): void
{
$find = $this->em->find(ShortUrl::class, '123')->willReturn(null);
($this->listener)(new ShortUrlCreated('123'));
$find->shouldHaveBeenCalledOnce();
$this->logger->warning(
'Tried to notify Mercure for new short URL with id "{shortUrlId}", but it does not exist.',
['shortUrlId' => '123'],
)->shouldHaveBeenCalledOnce();
$this->hub->publish(Argument::cetera())->shouldNotHaveBeenCalled();
$this->updatesGenerator->newShortUrlUpdate(Argument::cetera())->shouldNotHaveBeenCalled();
$this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled();
}
/** @test */
public function expectedNotificationIsPublished(): void
{
$shortUrl = ShortUrl::withLongUrl('');
$update = new Update([]);
$find = $this->em->find(ShortUrl::class, '123')->willReturn($shortUrl);
$newUpdate = $this->updatesGenerator->newShortUrlUpdate($shortUrl)->willReturn($update);
$publish = $this->hub->publish($update)->willReturn('');
($this->listener)(new ShortUrlCreated('123'));
$find->shouldHaveBeenCalledOnce();
$newUpdate->shouldHaveBeenCalledOnce();
$publish->shouldHaveBeenCalledOnce();
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
$this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled();
}
/** @test */
public function messageIsPrintedIfPublishingFails(): void
{
$shortUrl = ShortUrl::withLongUrl('');
$update = new Update([]);
$e = new Exception('Error');
$find = $this->em->find(ShortUrl::class, '123')->willReturn($shortUrl);
$newUpdate = $this->updatesGenerator->newShortUrlUpdate($shortUrl)->willReturn($update);
$publish = $this->hub->publish($update)->willThrow($e);
($this->listener)(new ShortUrlCreated('123'));
$find->shouldHaveBeenCalledOnce();
$newUpdate->shouldHaveBeenCalledOnce();
$publish->shouldHaveBeenCalledOnce();
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
$this->logger->debug(
'Error while trying to notify mercure hub with new short URL. {e}',
['e' => $e],
)->shouldHaveBeenCalledOnce();
}
}

View File

@ -17,6 +17,7 @@ use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelperInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq;
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
use Throwable;
@ -93,7 +94,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
$find->shouldHaveBeenCalledOnce();
$this->helper->publishPayloadInQueue(
Argument::type('array'),
'https://shlink.io/new-short-url',
Topic::NEW_SHORT_URL->value,
)->shouldHaveBeenCalledOnce();
$this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled();
}