diff --git a/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php new file mode 100644 index 00000000..e64fab2c --- /dev/null +++ b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php @@ -0,0 +1,127 @@ +helper = $this->prophesize(RabbitMqPublishingHelperInterface::class); + $this->em = $this->prophesize(EntityManagerInterface::class); + $this->logger = $this->prophesize(LoggerInterface::class); + + $this->listener = new NotifyNewShortUrlToRabbitMq( + $this->helper->reveal(), + $this->em->reveal(), + $this->logger->reveal(), + new ShortUrlDataTransformer(new ShortUrlStringifier([])), + true, + ); + } + + /** @test */ + public function doesNothingWhenTheFeatureIsNotEnabled(): void + { + $listener = new NotifyNewShortUrlToRabbitMq( + $this->helper->reveal(), + $this->em->reveal(), + $this->logger->reveal(), + new ShortUrlDataTransformer(new ShortUrlStringifier([])), + false, + ); + + $listener(new ShortUrlCreated('123')); + + $this->em->find(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->helper->publishPayloadInQueue(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + /** @test */ + public function notificationsAreNotSentWhenShortUrlCannotBeFound(): void + { + $shortUrlId = '123'; + $find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(null); + $logWarning = $this->logger->warning( + 'Tried to notify RabbitMQ for new short URL with id "{shortUrlId}", but it does not exist.', + ['shortUrlId' => $shortUrlId], + ); + + ($this->listener)(new ShortUrlCreated($shortUrlId)); + + $find->shouldHaveBeenCalledOnce(); + $logWarning->shouldHaveBeenCalledOnce(); + $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->helper->publishPayloadInQueue(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + /** @test */ + public function expectedChannelIsNotified(): void + { + $shortUrlId = '123'; + $find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(ShortUrl::withLongUrl('')); + + ($this->listener)(new ShortUrlCreated($shortUrlId)); + + $find->shouldHaveBeenCalledOnce(); + $this->helper->publishPayloadInQueue( + Argument::type('array'), + 'https://shlink.io/new-short-url', + )->shouldHaveBeenCalledOnce(); + $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + /** + * @test + * @dataProvider provideExceptions + */ + public function printsDebugMessageInCaseOfError(Throwable $e): void + { + $shortUrlId = '123'; + $find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(ShortUrl::withLongUrl('')); + $publish = $this->helper->publishPayloadInQueue(Argument::cetera())->willThrow($e); + + ($this->listener)(new ShortUrlCreated($shortUrlId)); + + $this->logger->debug( + 'Error while trying to notify RabbitMQ with new short URL. {e}', + ['e' => $e], + )->shouldHaveBeenCalledOnce(); + $find->shouldHaveBeenCalledOnce(); + $publish->shouldHaveBeenCalledOnce(); + } + + public function provideExceptions(): iterable + { + yield [new RuntimeException('RuntimeException Error')]; + yield [new Exception('Exception Error')]; + yield [new DomainException('DomainException Error')]; + } +} diff --git a/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php b/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php index 942a7bdc..18a99425 100644 --- a/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php +++ b/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php @@ -34,7 +34,6 @@ class NotifyVisitToRabbitMqTest extends TestCase private ObjectProphecy $helper; private ObjectProphecy $em; private ObjectProphecy $logger; - private ObjectProphecy $orphanVisitTransformer; protected function setUp(): void { @@ -135,7 +134,7 @@ class NotifyVisitToRabbitMqTest extends TestCase { $visitId = '123'; $findVisit = $this->em->find(Visit::class, $visitId)->willReturn(Visit::forBasePath(Visitor::emptyInstance())); - $channel = $this->helper->publishPayloadInQueue(Argument::cetera())->willThrow($e); + $publish = $this->helper->publishPayloadInQueue(Argument::cetera())->willThrow($e); ($this->listener)(new VisitLocated($visitId)); @@ -144,7 +143,7 @@ class NotifyVisitToRabbitMqTest extends TestCase ['e' => $e], )->shouldHaveBeenCalledOnce(); $findVisit->shouldHaveBeenCalledOnce(); - $channel->shouldHaveBeenCalledOnce(); + $publish->shouldHaveBeenCalledOnce(); } public function provideExceptions(): iterable