Extended NotifyVisitToRabbitMqTest covering legacy and non-legacy use-cases

This commit is contained in:
Alejandro Celaya 2022-07-25 12:30:28 +02:00
parent 6ce2049935
commit 19b0f0d7dc

View file

@ -7,6 +7,7 @@ namespace ShlinkioTest\Shlink\Core\EventDispatcher\RabbitMq;
use Doctrine\ORM\EntityManagerInterface;
use DomainException;
use Exception;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
@ -37,12 +38,14 @@ class NotifyVisitToRabbitMqTest extends TestCase
private ObjectProphecy $helper;
private ObjectProphecy $em;
private ObjectProphecy $logger;
private RabbitMqOptions $options;
protected function setUp(): void
{
$this->helper = $this->prophesize(RabbitMqPublishingHelperInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->logger = $this->prophesize(LoggerInterface::class);
$this->options = new RabbitMqOptions(['enabled' => true, 'legacy_visits_publishing' => true]);
$this->listener = new NotifyVisitToRabbitMq(
$this->helper->reveal(),
@ -50,23 +53,16 @@ class NotifyVisitToRabbitMqTest extends TestCase
$this->logger->reveal(),
new OrphanVisitDataTransformer(),
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
new RabbitMqOptions(['enabled' => true, 'legacy_visits_publishing' => true]),
$this->options,
);
}
/** @test */
public function doesNothingWhenTheFeatureIsNotEnabled(): void
{
$listener = new NotifyVisitToRabbitMq(
$this->helper->reveal(),
$this->em->reveal(),
$this->logger->reveal(),
new OrphanVisitDataTransformer(),
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
new RabbitMqOptions(['enabled' => false, 'legacy_visits_publishing' => true]),
);
$this->options->enabled = false;
$listener(new VisitLocated('123'));
($this->listener)(new VisitLocated('123'));
$this->em->find(Argument::cetera())->shouldNotHaveBeenCalled();
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
@ -157,4 +153,79 @@ class NotifyVisitToRabbitMqTest extends TestCase
yield [new Exception('Exception Error')];
yield [new DomainException('DomainException Error')];
}
/**
* @test
* @dataProvider provideLegacyPayloads
*/
public function expectedPayloadIsPublishedDependingOnConfig(
bool $legacy,
Visit $visit,
callable $assertPayload,
): void {
$this->options->legacyVisitsPublishing = $legacy;
$visitId = '123';
$findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit);
($this->listener)(new VisitLocated($visitId));
$findVisit->shouldHaveBeenCalledOnce();
$this->helper->publishPayloadInQueue(Argument::that($assertPayload), Argument::type('string'))
->shouldHaveBeenCalled();
}
public function provideLegacyPayloads(): iterable
{
yield 'non-legacy non-orphan visit' => [
true,
$visit = Visit::forValidShortUrl(ShortUrl::withLongUrl(''), Visitor::emptyInstance()),
function (array $payload) use ($visit): bool {
Assert::assertEquals($payload, $visit->jsonSerialize());
Assert::assertArrayNotHasKey('visitedUrl', $payload);
Assert::assertArrayNotHasKey('type', $payload);
Assert::assertArrayNotHasKey('visit', $payload);
Assert::assertArrayNotHasKey('shortUrl', $payload);
return true;
},
];
yield 'non-legacy orphan visit' => [
true,
Visit::forBasePath(Visitor::emptyInstance()),
function (array $payload): bool {
Assert::assertArrayHasKey('visitedUrl', $payload);
Assert::assertArrayHasKey('type', $payload);
return true;
},
];
yield 'legacy non-orphan visit' => [
false,
$visit = Visit::forValidShortUrl(ShortUrl::withLongUrl(''), Visitor::emptyInstance()),
function (array $payload) use ($visit): bool {
Assert::assertArrayHasKey('visit', $payload);
Assert::assertArrayHasKey('shortUrl', $payload);
Assert::assertIsArray($payload['visit']);
Assert::assertEquals($payload['visit'], $visit->jsonSerialize());
Assert::assertArrayNotHasKey('visitedUrl', ['visit']);
Assert::assertArrayNotHasKey('type', ['visit']);
return true;
},
];
yield 'legacy orphan visit' => [
false,
Visit::forBasePath(Visitor::emptyInstance()),
function (array $payload): bool {
Assert::assertArrayHasKey('visit', $payload);
Assert::assertArrayNotHasKey('shortUrl', $payload);
Assert::assertIsArray($payload['visit']);
Assert::assertArrayHasKey('visitedUrl', $payload['visit']);
Assert::assertArrayHasKey('type', $payload['visit']);
return true;
},
];
}
}