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

148 lines
5.2 KiB
PHP
Raw Normal View History

2020-04-12 11:01:13 -05:00
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
2020-04-12 11:01:13 -05:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\UpdatePublishing\Update;
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGenerator;
2022-07-25 02:02:05 -05:00
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\VisitsSummary;
2022-04-23 11:19:16 -05:00
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer;
2020-04-12 11:01:13 -05:00
class PublishingUpdatesGeneratorTest extends TestCase
2020-04-12 11:01:13 -05:00
{
private PublishingUpdatesGenerator $generator;
2020-04-12 11:01:13 -05:00
protected function setUp(): void
2020-04-12 11:01:13 -05:00
{
$this->generator = new PublishingUpdatesGenerator(
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
new OrphanVisitDataTransformer(),
);
2020-04-12 11:01:13 -05:00
}
/**
* @test
* @dataProvider provideMethod
*/
public function visitIsProperlySerializedIntoUpdate(string $method, string $expectedTopic, ?string $title): void
2020-04-12 11:01:13 -05:00
{
$shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData([
'customSlug' => 'foo',
'longUrl' => '',
'title' => $title,
]));
$visit = Visit::forValidShortUrl($shortUrl, Visitor::emptyInstance());
2020-04-12 11:01:13 -05:00
/** @var Update $update */
$update = $this->generator->{$method}($visit);
2020-04-12 11:01:13 -05:00
self::assertEquals($expectedTopic, $update->topic);
2020-10-03 17:35:14 -05:00
self::assertEquals([
2020-04-12 11:01:13 -05:00
'shortUrl' => [
'shortCode' => $shortUrl->getShortCode(),
'shortUrl' => 'http:/' . $shortUrl->getShortCode(),
'longUrl' => '',
'dateCreated' => $shortUrl->getDateCreated()->toAtomString(),
'visitsCount' => 0,
'tags' => [],
'meta' => [
'validSince' => null,
'validUntil' => null,
'maxVisits' => null,
],
'domain' => null,
'title' => $title,
2021-05-22 00:35:47 -05:00
'crawlable' => false,
'forwardQuery' => true,
'visitsSummary' => VisitsSummary::fromTotalAndNonBots(0, 0),
2020-04-12 11:01:13 -05:00
],
'visit' => [
'referer' => '',
'userAgent' => '',
'visitLocation' => null,
'date' => $visit->getDate()->toAtomString(),
'potentialBot' => false,
2020-04-12 11:01:13 -05:00
],
], $update->payload);
2020-04-12 11:01:13 -05:00
}
public function provideMethod(): iterable
{
yield 'newVisitUpdate' => ['newVisitUpdate', 'https://shlink.io/new-visit', 'the cool title'];
yield 'newShortUrlVisitUpdate' => ['newShortUrlVisitUpdate', 'https://shlink.io/new-visit/foo', null];
}
/**
* @test
* @dataProvider provideOrphanVisits
*/
public function orphanVisitIsProperlySerializedIntoUpdate(Visit $orphanVisit): void
{
$update = $this->generator->newOrphanVisitUpdate($orphanVisit);
self::assertEquals('https://shlink.io/new-orphan-visit', $update->topic);
self::assertEquals([
'visit' => [
'referer' => '',
'userAgent' => '',
'visitLocation' => null,
'date' => $orphanVisit->getDate()->toAtomString(),
'potentialBot' => false,
'visitedUrl' => $orphanVisit->visitedUrl(),
2022-04-23 11:19:16 -05:00
'type' => $orphanVisit->type()->value,
],
], $update->payload);
}
public function provideOrphanVisits(): iterable
{
$visitor = Visitor::emptyInstance();
2022-04-23 11:19:16 -05:00
yield VisitType::REGULAR_404->value => [Visit::forRegularNotFound($visitor)];
yield VisitType::INVALID_SHORT_URL->value => [Visit::forInvalidShortUrl($visitor)];
yield VisitType::BASE_URL->value => [Visit::forBasePath($visitor)];
}
2022-07-25 02:02:05 -05:00
/** @test */
public function shortUrlIsProperlySerializedIntoUpdate(): void
{
$shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData([
2022-07-25 02:02:05 -05:00
'customSlug' => 'foo',
'longUrl' => '',
'title' => 'The title',
]));
$update = $this->generator->newShortUrlUpdate($shortUrl);
self::assertEquals(Topic::NEW_SHORT_URL->value, $update->topic);
2022-07-25 02:02:05 -05:00
self::assertEquals(['shortUrl' => [
'shortCode' => $shortUrl->getShortCode(),
'shortUrl' => 'http:/' . $shortUrl->getShortCode(),
'longUrl' => '',
'dateCreated' => $shortUrl->getDateCreated()->toAtomString(),
'visitsCount' => 0,
'tags' => [],
'meta' => [
'validSince' => null,
'validUntil' => null,
'maxVisits' => null,
],
'domain' => null,
'title' => $shortUrl->title(),
'crawlable' => false,
'forwardQuery' => true,
'visitsSummary' => VisitsSummary::fromTotalAndNonBots(0, 0),
]], $update->payload);
2022-07-25 02:02:05 -05:00
}
2020-04-12 11:01:13 -05:00
}