Migrated NotifyNewShortUrlToRedisTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-22 19:59:32 +02:00
parent 739433ba8b
commit d0393799d2

View File

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Core\EventDispatcher\RedisPubSub;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use DomainException; use DomainException;
use Exception; use Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use RuntimeException; use RuntimeException;
use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface; use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface;
@ -24,30 +22,28 @@ use Throwable;
class NotifyNewShortUrlToRedisTest extends TestCase class NotifyNewShortUrlToRedisTest extends TestCase
{ {
use ProphecyTrait; private MockObject $helper;
private MockObject $updatesGenerator;
private ObjectProphecy $helper; private MockObject $em;
private ObjectProphecy $updatesGenerator; private MockObject $logger;
private ObjectProphecy $em;
private ObjectProphecy $logger;
protected function setUp(): void protected function setUp(): void
{ {
$this->helper = $this->prophesize(PublishingHelperInterface::class); $this->helper = $this->createMock(PublishingHelperInterface::class);
$this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class); $this->updatesGenerator = $this->createMock(PublishingUpdatesGeneratorInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class); $this->em = $this->createMock(EntityManagerInterface::class);
$this->logger = $this->prophesize(LoggerInterface::class); $this->logger = $this->createMock(LoggerInterface::class);
} }
/** @test */ /** @test */
public function doesNothingWhenTheFeatureIsNotEnabled(): void public function doesNothingWhenTheFeatureIsNotEnabled(): void
{ {
$this->createListener(false)(new ShortUrlCreated('123')); $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');
$this->em->find(Argument::cetera())->shouldNotHaveBeenCalled(); $this->createListener(false)(new ShortUrlCreated('123'));
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
$this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled();
$this->helper->publishUpdate(Argument::cetera())->shouldNotHaveBeenCalled();
} }
/** /**
@ -58,21 +54,22 @@ class NotifyNewShortUrlToRedisTest extends TestCase
{ {
$shortUrlId = '123'; $shortUrlId = '123';
$update = Update::forTopicAndPayload(Topic::NEW_SHORT_URL->value, []); $update = Update::forTopicAndPayload(Topic::NEW_SHORT_URL->value, []);
$find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(ShortUrl::withLongUrl('')); $this->em->expects($this->once())->method('find')->with(
$generateUpdate = $this->updatesGenerator->newShortUrlUpdate(Argument::type(ShortUrl::class))->willReturn( $this->equalTo(ShortUrl::class),
$update, $this->equalTo($shortUrlId),
)->willReturn(ShortUrl::withLongUrl(''));
$this->updatesGenerator->expects($this->once())->method('newShortUrlUpdate')->with(
$this->isInstanceOf(ShortUrl::class),
)->willReturn($update);
$this->helper->expects($this->once())->method('publishUpdate')->with(
$this->equalTo($update),
)->willThrowException($e);
$this->logger->expects($this->once())->method('debug')->with(
$this->equalTo('Error while trying to notify {name} with new short URL. {e}'),
$this->equalTo(['e' => $e, 'name' => 'Redis pub/sub']),
); );
$publish = $this->helper->publishUpdate($update)->willThrow($e);
$this->createListener()(new ShortUrlCreated($shortUrlId)); $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 public function provideExceptions(): iterable
@ -84,12 +81,6 @@ class NotifyNewShortUrlToRedisTest extends TestCase
private function createListener(bool $enabled = true): NotifyNewShortUrlToRedis private function createListener(bool $enabled = true): NotifyNewShortUrlToRedis
{ {
return new NotifyNewShortUrlToRedis( return new NotifyNewShortUrlToRedis($this->helper, $this->updatesGenerator, $this->em, $this->logger, $enabled);
$this->helper->reveal(),
$this->updatesGenerator->reveal(),
$this->em->reveal(),
$this->logger->reveal(),
$enabled,
);
} }
} }