From 019bd4dec8c745b51ded798729d6a71e548977ca Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 25 Jul 2022 09:30:25 +0200 Subject: [PATCH] Created NotifyNewShortUrlToMercureTest --- .../Mercure/NotifyNewShortUrlToMercure.php | 4 +- .../NotifyNewShortUrlToMercureTest.php | 104 ++++++++++++++++++ .../NotifyNewShortUrlToRabbitMqTest.php | 3 +- 3 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 module/Core/test/EventDispatcher/Mercure/NotifyNewShortUrlToMercureTest.php diff --git a/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php b/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php index 46b2f38b..8e93d88b 100644 --- a/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php +++ b/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php @@ -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]); } } } diff --git a/module/Core/test/EventDispatcher/Mercure/NotifyNewShortUrlToMercureTest.php b/module/Core/test/EventDispatcher/Mercure/NotifyNewShortUrlToMercureTest.php new file mode 100644 index 00000000..6bc2d527 --- /dev/null +++ b/module/Core/test/EventDispatcher/Mercure/NotifyNewShortUrlToMercureTest.php @@ -0,0 +1,104 @@ +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(); + } +} diff --git a/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php index e64fab2c..51b557af 100644 --- a/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php +++ b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php @@ -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(); }