From 73ae754aa76b973336dcffe4727c8cf367f42399 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 28 Jul 2022 10:36:52 +0200 Subject: [PATCH] Created NotifyVisitToRedisTest --- .../RedisPubSub/NotifyVisitToRedisTest.php | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 module/Core/test/EventDispatcher/RedisPubSub/NotifyVisitToRedisTest.php diff --git a/module/Core/test/EventDispatcher/RedisPubSub/NotifyVisitToRedisTest.php b/module/Core/test/EventDispatcher/RedisPubSub/NotifyVisitToRedisTest.php new file mode 100644 index 00000000..3beaa838 --- /dev/null +++ b/module/Core/test/EventDispatcher/RedisPubSub/NotifyVisitToRedisTest.php @@ -0,0 +1,94 @@ +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 VisitLocated('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 + { + $visitId = '123'; + $findVisit = $this->em->find(Visit::class, $visitId)->willReturn(Visit::forBasePath(Visitor::emptyInstance())); + $generateUpdate = $this->updatesGenerator->newOrphanVisitUpdate(Argument::type(Visit::class))->willReturn( + Update::forTopicAndPayload('', []), + ); + $publish = $this->helper->publishUpdate(Argument::cetera())->willThrow($e); + + $this->createListener()(new VisitLocated($visitId)); + + $this->logger->debug( + 'Error while trying to notify {name} with new visit. {e}', + ['e' => $e, 'name' => 'Redis pub/sub'], + )->shouldHaveBeenCalledOnce(); + $findVisit->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): NotifyVisitToRedis + { + return new NotifyVisitToRedis( + $this->helper->reveal(), + $this->updatesGenerator->reveal(), + $this->em->reveal(), + $this->logger->reveal(), + $enabled, + ); + } +}