2022-07-28 03:36:52 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\EventDispatcher\RedisPubSub;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use DomainException;
|
|
|
|
use Exception;
|
2022-10-22 13:04:12 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2022-07-28 03:36:52 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use RuntimeException;
|
|
|
|
use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface;
|
|
|
|
use Shlinkio\Shlink\Common\UpdatePublishing\Update;
|
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
|
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\RedisPubSub\NotifyVisitToRedis;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2022-07-28 03:36:52 -05:00
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class NotifyVisitToRedisTest extends TestCase
|
|
|
|
{
|
2022-10-24 12:59:03 -05:00
|
|
|
private MockObject & PublishingHelperInterface $helper;
|
|
|
|
private MockObject & PublishingUpdatesGeneratorInterface $updatesGenerator;
|
|
|
|
private MockObject & EntityManagerInterface $em;
|
|
|
|
private MockObject & LoggerInterface $logger;
|
2022-07-28 03:36:52 -05:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-22 13:04:12 -05:00
|
|
|
$this->helper = $this->createMock(PublishingHelperInterface::class);
|
|
|
|
$this->updatesGenerator = $this->createMock(PublishingUpdatesGeneratorInterface::class);
|
|
|
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2022-07-28 03:36:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function doesNothingWhenTheFeatureIsNotEnabled(): void
|
|
|
|
{
|
2022-10-22 13:04:12 -05:00
|
|
|
$this->helper->expects($this->never())->method('publishUpdate');
|
|
|
|
$this->em->expects($this->never())->method('find');
|
|
|
|
$this->logger->expects($this->never())->method('warning');
|
|
|
|
$this->logger->expects($this->never())->method('debug');
|
2022-07-28 03:36:52 -05:00
|
|
|
|
2022-10-22 13:04:12 -05:00
|
|
|
$this->createListener(false)(new VisitLocated('123'));
|
2022-07-28 03:36:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideExceptions
|
|
|
|
*/
|
|
|
|
public function printsDebugMessageInCaseOfError(Throwable $e): void
|
|
|
|
{
|
|
|
|
$visitId = '123';
|
2022-10-23 11:15:57 -05:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, $visitId)->willReturn(
|
|
|
|
Visit::forBasePath(Visitor::emptyInstance()),
|
|
|
|
);
|
2022-10-22 13:04:12 -05:00
|
|
|
$this->updatesGenerator->expects($this->once())->method('newOrphanVisitUpdate')->with(
|
|
|
|
$this->isInstanceOf(Visit::class),
|
|
|
|
)->willReturn(Update::forTopicAndPayload('', []));
|
|
|
|
$this->helper->expects($this->once())->method('publishUpdate')->withAnyParameters()->willThrowException($e);
|
|
|
|
$this->logger->expects($this->once())->method('debug')->with(
|
2022-10-23 11:15:57 -05:00
|
|
|
'Error while trying to notify {name} with new visit. {e}',
|
|
|
|
['e' => $e, 'name' => 'Redis pub/sub'],
|
2022-07-28 03:36:52 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->createListener()(new VisitLocated($visitId));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2022-10-22 13:04:12 -05:00
|
|
|
return new NotifyVisitToRedis($this->helper, $this->updatesGenerator, $this->em, $this->logger, $enabled);
|
2022-07-28 03:36:52 -05:00
|
|
|
}
|
|
|
|
}
|