Migrated NotifyVisitToMercureTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-22 10:03:05 +02:00
parent 16a951b938
commit 8298f9d491

View File

@ -5,10 +5,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\EventDispatcher\Mercure;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface;
@ -23,55 +21,41 @@ use Shlinkio\Shlink\Core\Visit\Model\VisitType;
class NotifyVisitToMercureTest extends TestCase
{
use ProphecyTrait;
private NotifyVisitToMercure $listener;
private ObjectProphecy $helper;
private ObjectProphecy $updatesGenerator;
private ObjectProphecy $em;
private ObjectProphecy $logger;
private MockObject $helper;
private MockObject $updatesGenerator;
private MockObject $em;
private MockObject $logger;
protected function setUp(): void
{
$this->helper = $this->prophesize(PublishingHelperInterface::class);
$this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->logger = $this->prophesize(LoggerInterface::class);
$this->helper = $this->createMock(PublishingHelperInterface::class);
$this->updatesGenerator = $this->createMock(PublishingUpdatesGeneratorInterface::class);
$this->em = $this->createMock(EntityManagerInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->listener = new NotifyVisitToMercure(
$this->helper->reveal(),
$this->updatesGenerator->reveal(),
$this->em->reveal(),
$this->logger->reveal(),
);
$this->listener = new NotifyVisitToMercure($this->helper, $this->updatesGenerator, $this->em, $this->logger);
}
/** @test */
public function notificationsAreNotSentWhenVisitCannotBeFound(): void
{
$visitId = '123';
$findVisit = $this->em->find(Visit::class, $visitId)->willReturn(null);
$logWarning = $this->logger->warning(
'Tried to notify {name} for visit with id "{visitId}", but it does not exist.',
['visitId' => $visitId, 'name' => 'Mercure'],
$this->em->expects($this->once())->method('find')->with(
$this->equalTo(Visit::class),
$this->equalTo($visitId),
)->willReturn(null);
$this->logger->expects($this->once())->method('warning')->with(
$this->equalTo('Tried to notify {name} for visit with id "{visitId}", but it does not exist.'),
$this->equalTo(['visitId' => $visitId, 'name' => 'Mercure']),
);
$logDebug = $this->logger->debug(Argument::cetera());
$buildNewShortUrlVisitUpdate = $this->updatesGenerator->newShortUrlVisitUpdate(
Argument::type(Visit::class),
);
$buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate(Argument::type(Visit::class));
$buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate(Argument::type(Visit::class));
$publish = $this->helper->publishUpdate(Argument::type(Update::class));
$this->logger->expects($this->never())->method('debug');
$this->updatesGenerator->expects($this->never())->method('newShortUrlVisitUpdate');
$this->updatesGenerator->expects($this->never())->method('newOrphanVisitUpdate');
$this->updatesGenerator->expects($this->never())->method('newVisitUpdate');
$this->helper->expects($this->never())->method('publishUpdate');
($this->listener)(new VisitLocated($visitId));
$findVisit->shouldHaveBeenCalledOnce();
$logWarning->shouldHaveBeenCalledOnce();
$logDebug->shouldNotHaveBeenCalled();
$buildNewShortUrlVisitUpdate->shouldNotHaveBeenCalled();
$buildNewVisitUpdate->shouldNotHaveBeenCalled();
$buildNewOrphanVisitUpdate->shouldNotHaveBeenCalled();
$publish->shouldNotHaveBeenCalled();
}
/** @test */
@ -81,23 +65,22 @@ class NotifyVisitToMercureTest extends TestCase
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance());
$update = Update::forTopicAndPayload('', []);
$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);
$publish = $this->helper->publishUpdate($update);
$this->em->expects($this->once())->method('find')->with(
$this->equalTo(Visit::class),
$this->equalTo($visitId),
)->willReturn($visit);
$this->logger->expects($this->never())->method('warning');
$this->logger->expects($this->never())->method('debug');
$this->updatesGenerator->expects($this->once())->method('newShortUrlVisitUpdate')->with(
$this->equalTo($visit),
)->willReturn($update);
$this->updatesGenerator->expects($this->never())->method('newOrphanVisitUpdate');
$this->updatesGenerator->expects($this->once())->method('newVisitUpdate')->with(
$this->equalTo($visit),
)->willReturn($update);
$this->helper->expects($this->exactly(2))->method('publishUpdate')->with($this->equalTo($update));
($this->listener)(new VisitLocated($visitId));
$findVisit->shouldHaveBeenCalledOnce();
$logWarning->shouldNotHaveBeenCalled();
$logDebug->shouldNotHaveBeenCalled();
$buildNewShortUrlVisitUpdate->shouldHaveBeenCalledOnce();
$buildNewVisitUpdate->shouldHaveBeenCalledOnce();
$buildNewOrphanVisitUpdate->shouldNotHaveBeenCalled();
$publish->shouldHaveBeenCalledTimes(2);
}
/** @test */
@ -108,26 +91,27 @@ class NotifyVisitToMercureTest extends TestCase
$update = Update::forTopicAndPayload('', []);
$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 {name} with new visit. {e}', [
'e' => $e,
'name' => 'Mercure',
]);
$buildNewShortUrlVisitUpdate = $this->updatesGenerator->newShortUrlVisitUpdate($visit)->willReturn($update);
$buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate($visit)->willReturn($update);
$buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate($visit)->willReturn($update);
$publish = $this->helper->publishUpdate($update)->willThrow($e);
$this->em->expects($this->once())->method('find')->with(
$this->equalTo(Visit::class),
$this->equalTo($visitId),
)->willReturn($visit);
$this->logger->expects($this->never())->method('warning');
$this->logger->expects($this->once())->method('debug')->with(
$this->equalTo('Error while trying to notify {name} with new visit. {e}'),
$this->equalTo(['e' => $e, 'name' => 'Mercure']),
);
$this->updatesGenerator->expects($this->once())->method('newShortUrlVisitUpdate')->with(
$this->equalTo($visit),
)->willReturn($update);
$this->updatesGenerator->expects($this->never())->method('newOrphanVisitUpdate');
$this->updatesGenerator->expects($this->once())->method('newVisitUpdate')->with(
$this->equalTo($visit),
)->willReturn($update);
$this->helper->expects($this->once())->method('publishUpdate')->with(
$this->equalTo($update),
)->willThrowException($e);
($this->listener)(new VisitLocated($visitId));
$findVisit->shouldHaveBeenCalledOnce();
$logWarning->shouldNotHaveBeenCalled();
$logDebug->shouldHaveBeenCalledOnce();
$buildNewShortUrlVisitUpdate->shouldHaveBeenCalledOnce();
$buildNewVisitUpdate->shouldHaveBeenCalledOnce();
$buildNewOrphanVisitUpdate->shouldNotHaveBeenCalled();
$publish->shouldHaveBeenCalledOnce();
}
/**
@ -139,23 +123,20 @@ class NotifyVisitToMercureTest extends TestCase
$visitId = '123';
$update = Update::forTopicAndPayload('', []);
$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);
$publish = $this->helper->publishUpdate($update);
$this->em->expects($this->once())->method('find')->with(
$this->equalTo(Visit::class),
$this->equalTo($visitId),
)->willReturn($visit);
$this->logger->expects($this->never())->method('warning');
$this->logger->expects($this->never())->method('debug');
$this->updatesGenerator->expects($this->never())->method('newShortUrlVisitUpdate');
$this->updatesGenerator->expects($this->once())->method('newOrphanVisitUpdate')->with(
$this->equalTo($visit),
)->willReturn($update);
$this->updatesGenerator->expects($this->never())->method('newVisitUpdate');
$this->helper->expects($this->once())->method('publishUpdate')->with($this->equalTo($update));
($this->listener)(new VisitLocated($visitId));
$findVisit->shouldHaveBeenCalledOnce();
$logWarning->shouldNotHaveBeenCalled();
$logDebug->shouldNotHaveBeenCalled();
$buildNewShortUrlVisitUpdate->shouldNotHaveBeenCalled();
$buildNewVisitUpdate->shouldNotHaveBeenCalled();
$buildNewOrphanVisitUpdate->shouldHaveBeenCalledOnce();
$publish->shouldHaveBeenCalledOnce();
}
public function provideOrphanVisits(): iterable