From 20a6e7e21010c086108eafd45faed286beb44eda Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 28 Jul 2022 10:33:26 +0200 Subject: [PATCH] Created NotifyNewShortUrlToRedisTest --- .../NotifyNewShortUrlToRedisTest.php | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 module/Core/test/EventDispatcher/RedisPubSub/NotifyNewShortUrlToRedisTest.php diff --git a/module/Core/test/EventDispatcher/RedisPubSub/NotifyNewShortUrlToRedisTest.php b/module/Core/test/EventDispatcher/RedisPubSub/NotifyNewShortUrlToRedisTest.php new file mode 100644 index 00000000..d5fa8b8c --- /dev/null +++ b/module/Core/test/EventDispatcher/RedisPubSub/NotifyNewShortUrlToRedisTest.php @@ -0,0 +1,95 @@ +helper = $this->prophesize(PublishingHelperInterface::class); + $this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class); + $this->em = $this->prophesize(EntityManagerInterface::class); + $this->logger = $this->prophesize(LoggerInterface::class); + } + + /** @test */ + public function doesNothingWhenTheFeatureIsNotEnabled(): void + { + $this->createListener(false)(new ShortUrlCreated('123')); + + $this->em->find(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->helper->publishUpdate(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + /** + * @test + * @dataProvider provideExceptions + */ + public function printsDebugMessageInCaseOfError(Throwable $e): void + { + $shortUrlId = '123'; + $update = Update::forTopicAndPayload(Topic::NEW_SHORT_URL->value, []); + $find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(ShortUrl::withLongUrl('')); + $generateUpdate = $this->updatesGenerator->newShortUrlUpdate(Argument::type(ShortUrl::class))->willReturn( + $update, + ); + $publish = $this->helper->publishUpdate($update)->willThrow($e); + + $this->createListener()(new ShortUrlCreated($shortUrlId)); + + $this->logger->debug( + 'Error while trying to notify {name} with new short URL. {e}', + ['e' => $e, 'name' => 'Redis pub/sub'], + )->shouldHaveBeenCalledOnce(); + $find->shouldHaveBeenCalledOnce(); + $generateUpdate->shouldHaveBeenCalledOnce(); + $publish->shouldHaveBeenCalledOnce(); + } + + public function provideExceptions(): iterable + { + yield [new RuntimeException('RuntimeException Error')]; + yield [new Exception('Exception Error')]; + yield [new DomainException('DomainException Error')]; + } + + private function createListener(bool $enabled = true): NotifyNewShortUrlToRedis + { + return new NotifyNewShortUrlToRedis( + $this->helper->reveal(), + $this->updatesGenerator->reveal(), + $this->em->reveal(), + $this->logger->reveal(), + $enabled, + ); + } +}