shlink/module/Core/test/EventDispatcher/NotifyVisitToMercureTest.php

168 lines
7 KiB
PHP
Raw Normal View History

2020-04-12 18:50:25 +03:00
<?php
declare(strict_types=1);
2020-04-12 19:01:13 +03:00
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
2020-04-12 18:50:25 +03:00
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
2020-11-02 13:50:19 +03:00
use Prophecy\PhpUnit\ProphecyTrait;
2020-04-12 18:50:25 +03:00
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
2020-04-12 18:50:25 +03:00
use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToMercure;
use Shlinkio\Shlink\Core\Mercure\MercureUpdatesGeneratorInterface;
use Shlinkio\Shlink\Core\Model\Visitor;
2021-04-02 10:46:02 +03:00
use Symfony\Component\Mercure\HubInterface;
2020-04-12 18:50:25 +03:00
use Symfony\Component\Mercure\Update;
class NotifyVisitToMercureTest extends TestCase
{
2020-11-02 13:50:19 +03:00
use ProphecyTrait;
2020-04-12 18:50:25 +03:00
private NotifyVisitToMercure $listener;
2021-04-02 10:46:02 +03:00
private ObjectProphecy $hub;
2020-04-12 18:50:25 +03:00
private ObjectProphecy $updatesGenerator;
private ObjectProphecy $em;
private ObjectProphecy $logger;
public function setUp(): void
{
2021-04-02 10:46:02 +03:00
$this->hub = $this->prophesize(HubInterface::class);
2020-04-12 18:50:25 +03:00
$this->updatesGenerator = $this->prophesize(MercureUpdatesGeneratorInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->logger = $this->prophesize(LoggerInterface::class);
$this->listener = new NotifyVisitToMercure(
2021-04-02 10:46:02 +03:00
$this->hub->reveal(),
2020-04-12 18:50:25 +03:00
$this->updatesGenerator->reveal(),
$this->em->reveal(),
$this->logger->reveal(),
);
}
/** @test */
public function notificationsAreNotSentWhenVisitCannotBeFound(): void
2020-04-12 18:50:25 +03:00
{
$visitId = '123';
$findVisit = $this->em->find(Visit::class, $visitId)->willReturn(null);
$logWarning = $this->logger->warning(
'Tried to notify mercure for visit with id "{visitId}", but it does not exist.',
['visitId' => $visitId],
);
$logDebug = $this->logger->debug(Argument::cetera());
$buildNewShortUrlVisitUpdate = $this->updatesGenerator->newShortUrlVisitUpdate(
Argument::type(Visit::class),
2020-04-12 18:50:25 +03:00
);
$buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate(Argument::type(Visit::class));
$buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate(Argument::type(Visit::class));
2021-04-02 10:46:02 +03:00
$publish = $this->hub->publish(Argument::type(Update::class));
2020-04-12 18:50:25 +03:00
($this->listener)(new VisitLocated($visitId));
$findVisit->shouldHaveBeenCalledOnce();
$logWarning->shouldHaveBeenCalledOnce();
$logDebug->shouldNotHaveBeenCalled();
$buildNewShortUrlVisitUpdate->shouldNotHaveBeenCalled();
2020-04-12 18:50:25 +03:00
$buildNewVisitUpdate->shouldNotHaveBeenCalled();
$buildNewOrphanVisitUpdate->shouldNotHaveBeenCalled();
2020-04-12 18:50:25 +03:00
$publish->shouldNotHaveBeenCalled();
}
/** @test */
public function notificationsAreSentWhenVisitIsFound(): void
2020-04-12 18:50:25 +03:00
{
$visitId = '123';
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance());
2020-04-12 18:50:25 +03:00
$update = new Update('', '');
$findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit);
$logWarning = $this->logger->warning(Argument::cetera());
$logDebug = $this->logger->debug(Argument::cetera());
$buildNewShortUrlVisitUpdate = $this->updatesGenerator->newShortUrlVisitUpdate($visit)->willReturn($update);
$buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate($visit)->willReturn($update);
2020-04-12 18:50:25 +03:00
$buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate($visit)->willReturn($update);
2021-04-02 10:46:02 +03:00
$publish = $this->hub->publish($update);
2020-04-12 18:50:25 +03:00
($this->listener)(new VisitLocated($visitId));
$findVisit->shouldHaveBeenCalledOnce();
$logWarning->shouldNotHaveBeenCalled();
$logDebug->shouldNotHaveBeenCalled();
$buildNewShortUrlVisitUpdate->shouldHaveBeenCalledOnce();
2020-04-12 18:50:25 +03:00
$buildNewVisitUpdate->shouldHaveBeenCalledOnce();
$buildNewOrphanVisitUpdate->shouldNotHaveBeenCalled();
$publish->shouldHaveBeenCalledTimes(2);
2020-04-12 18:50:25 +03:00
}
/** @test */
public function debugIsLoggedWhenExceptionIsThrown(): void
{
$visitId = '123';
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance());
2020-04-12 18:50:25 +03:00
$update = new Update('', '');
$e = new RuntimeException('Error');
$findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit);
$logWarning = $this->logger->warning(Argument::cetera());
$logDebug = $this->logger->debug('Error while trying to notify mercure hub with new visit. {e}', [
'e' => $e,
]);
$buildNewShortUrlVisitUpdate = $this->updatesGenerator->newShortUrlVisitUpdate($visit)->willReturn($update);
$buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate($visit)->willReturn($update);
2020-04-12 18:50:25 +03:00
$buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate($visit)->willReturn($update);
2021-04-02 10:46:02 +03:00
$publish = $this->hub->publish($update)->willThrow($e);
2020-04-12 18:50:25 +03:00
($this->listener)(new VisitLocated($visitId));
$findVisit->shouldHaveBeenCalledOnce();
$logWarning->shouldNotHaveBeenCalled();
$logDebug->shouldHaveBeenCalledOnce();
$buildNewShortUrlVisitUpdate->shouldHaveBeenCalledOnce();
$buildNewVisitUpdate->shouldHaveBeenCalledOnce();
$buildNewOrphanVisitUpdate->shouldNotHaveBeenCalled();
$publish->shouldHaveBeenCalledOnce();
}
/**
* @test
* @dataProvider provideOrphanVisits
*/
public function notificationsAreSentForOrphanVisits(Visit $visit): void
{
$visitId = '123';
$update = new Update('', '');
$findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit);
$logWarning = $this->logger->warning(Argument::cetera());
$logDebug = $this->logger->debug(Argument::cetera());
$buildNewShortUrlVisitUpdate = $this->updatesGenerator->newShortUrlVisitUpdate($visit)->willReturn($update);
$buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate($visit)->willReturn($update);
$buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate($visit)->willReturn($update);
2021-04-02 10:46:02 +03:00
$publish = $this->hub->publish($update);
($this->listener)(new VisitLocated($visitId));
$findVisit->shouldHaveBeenCalledOnce();
$logWarning->shouldNotHaveBeenCalled();
$logDebug->shouldNotHaveBeenCalled();
$buildNewShortUrlVisitUpdate->shouldNotHaveBeenCalled();
$buildNewVisitUpdate->shouldNotHaveBeenCalled();
$buildNewOrphanVisitUpdate->shouldHaveBeenCalledOnce();
2020-04-12 18:50:25 +03:00
$publish->shouldHaveBeenCalledOnce();
}
public function provideOrphanVisits(): iterable
{
$visitor = Visitor::emptyInstance();
yield Visit::TYPE_REGULAR_404 => [Visit::forRegularNotFound($visitor)];
yield Visit::TYPE_INVALID_SHORT_URL => [Visit::forInvalidShortUrl($visitor)];
yield Visit::TYPE_BASE_URL => [Visit::forBasePath($visitor)];
}
2020-04-12 18:50:25 +03:00
}