2020-04-12 11:01:13 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-07-27 02:38:47 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
2020-04-12 11:01:13 -05:00
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2020-04-12 11:01:13 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-07-26 05:17:37 -05:00
|
|
|
use Shlinkio\Shlink\Common\UpdatePublishing\Update;
|
2022-07-27 02:38:47 -05:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGenerator;
|
2022-07-25 02:02:05 -05:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2021-02-01 15:55:52 -06:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
|
2021-02-01 15:55:52 -06:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2023-01-02 05:28:34 -06:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitsSummary;
|
2022-04-23 11:19:16 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
|
2021-02-11 15:12:38 -06:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer;
|
2020-04-12 11:01:13 -05:00
|
|
|
|
2022-07-27 02:38:47 -05:00
|
|
|
class PublishingUpdatesGeneratorTest extends TestCase
|
2020-04-12 11:01:13 -05:00
|
|
|
{
|
2022-07-27 02:38:47 -05:00
|
|
|
private PublishingUpdatesGenerator $generator;
|
2020-04-12 11:01:13 -05:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2020-04-12 11:01:13 -05:00
|
|
|
{
|
2022-07-27 02:38:47 -05:00
|
|
|
$this->generator = new PublishingUpdatesGenerator(
|
2021-02-11 15:12:38 -06:00
|
|
|
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
|
|
|
new OrphanVisitDataTransformer(),
|
|
|
|
);
|
2020-04-12 11:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideMethod')]
|
2021-02-02 13:21:48 -06:00
|
|
|
public function visitIsProperlySerializedIntoUpdate(string $method, string $expectedTopic, ?string $title): void
|
2020-04-12 11:01:13 -05:00
|
|
|
{
|
2022-12-11 11:24:47 -06:00
|
|
|
$shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData([
|
2021-02-02 13:21:48 -06:00
|
|
|
'customSlug' => 'foo',
|
2023-01-14 08:19:47 -06:00
|
|
|
'longUrl' => 'longUrl',
|
2021-02-02 13:21:48 -06:00
|
|
|
'title' => $title,
|
|
|
|
]));
|
2021-02-07 14:31:12 -06:00
|
|
|
$visit = Visit::forValidShortUrl($shortUrl, Visitor::emptyInstance());
|
2020-04-12 11:01:13 -05:00
|
|
|
|
2022-07-26 05:17:37 -05:00
|
|
|
/** @var Update $update */
|
2020-04-14 13:57:25 -05:00
|
|
|
$update = $this->generator->{$method}($visit);
|
2020-04-12 11:01:13 -05:00
|
|
|
|
2022-07-26 05:17:37 -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(),
|
2023-01-14 08:19:47 -06:00
|
|
|
'longUrl' => 'longUrl',
|
2023-01-21 05:05:54 -06:00
|
|
|
'deviceLongUrls' => $shortUrl->deviceLongUrls(),
|
2020-04-12 11:01:13 -05:00
|
|
|
'dateCreated' => $shortUrl->getDateCreated()->toAtomString(),
|
|
|
|
'visitsCount' => 0,
|
|
|
|
'tags' => [],
|
|
|
|
'meta' => [
|
|
|
|
'validSince' => null,
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => null,
|
|
|
|
],
|
|
|
|
'domain' => null,
|
2021-02-02 13:21:48 -06:00
|
|
|
'title' => $title,
|
2021-05-22 00:35:47 -05:00
|
|
|
'crawlable' => false,
|
2021-10-02 03:31:23 -05:00
|
|
|
'forwardQuery' => true,
|
2023-01-02 05:28:34 -06:00
|
|
|
'visitsSummary' => VisitsSummary::fromTotalAndNonBots(0, 0),
|
2020-04-12 11:01:13 -05:00
|
|
|
],
|
|
|
|
'visit' => [
|
|
|
|
'referer' => '',
|
|
|
|
'userAgent' => '',
|
|
|
|
'visitLocation' => null,
|
|
|
|
'date' => $visit->getDate()->toAtomString(),
|
2021-05-22 08:09:14 -05:00
|
|
|
'potentialBot' => false,
|
2020-04-12 11:01:13 -05:00
|
|
|
],
|
2022-07-26 05:17:37 -05:00
|
|
|
], $update->payload);
|
2020-04-12 11:01:13 -05:00
|
|
|
}
|
2020-04-14 13:57:25 -05:00
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideMethod(): iterable
|
2020-04-14 13:57:25 -05:00
|
|
|
{
|
2021-02-02 13:21:48 -06:00
|
|
|
yield 'newVisitUpdate' => ['newVisitUpdate', 'https://shlink.io/new-visit', 'the cool title'];
|
|
|
|
yield 'newShortUrlVisitUpdate' => ['newShortUrlVisitUpdate', 'https://shlink.io/new-visit/foo', null];
|
2020-04-14 13:57:25 -05:00
|
|
|
}
|
2021-02-11 15:12:38 -06:00
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideOrphanVisits')]
|
2021-02-11 15:12:38 -06:00
|
|
|
public function orphanVisitIsProperlySerializedIntoUpdate(Visit $orphanVisit): void
|
|
|
|
{
|
|
|
|
$update = $this->generator->newOrphanVisitUpdate($orphanVisit);
|
|
|
|
|
2022-07-26 05:17:37 -05:00
|
|
|
self::assertEquals('https://shlink.io/new-orphan-visit', $update->topic);
|
2021-02-11 15:12:38 -06:00
|
|
|
self::assertEquals([
|
|
|
|
'visit' => [
|
|
|
|
'referer' => '',
|
|
|
|
'userAgent' => '',
|
|
|
|
'visitLocation' => null,
|
|
|
|
'date' => $orphanVisit->getDate()->toAtomString(),
|
2021-05-22 08:09:14 -05:00
|
|
|
'potentialBot' => false,
|
2021-02-11 15:12:38 -06:00
|
|
|
'visitedUrl' => $orphanVisit->visitedUrl(),
|
2022-04-23 11:19:16 -05:00
|
|
|
'type' => $orphanVisit->type()->value,
|
2021-02-11 15:12:38 -06:00
|
|
|
],
|
2022-07-26 05:17:37 -05:00
|
|
|
], $update->payload);
|
2021-02-11 15:12:38 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideOrphanVisits(): iterable
|
2021-02-11 15:12:38 -06:00
|
|
|
{
|
|
|
|
$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)];
|
2021-02-11 15:12:38 -06:00
|
|
|
}
|
2022-07-25 02:02:05 -05:00
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2022-07-25 02:02:05 -05:00
|
|
|
public function shortUrlIsProperlySerializedIntoUpdate(): void
|
|
|
|
{
|
2022-12-11 11:24:47 -06:00
|
|
|
$shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData([
|
2022-07-25 02:02:05 -05:00
|
|
|
'customSlug' => 'foo',
|
2023-01-14 08:19:47 -06:00
|
|
|
'longUrl' => 'longUrl',
|
2022-07-25 02:02:05 -05:00
|
|
|
'title' => 'The title',
|
|
|
|
]));
|
|
|
|
|
|
|
|
$update = $this->generator->newShortUrlUpdate($shortUrl);
|
|
|
|
|
2022-07-26 05:17:37 -05:00
|
|
|
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(),
|
2023-01-14 08:19:47 -06:00
|
|
|
'longUrl' => 'longUrl',
|
2023-01-21 05:05:54 -06:00
|
|
|
'deviceLongUrls' => $shortUrl->deviceLongUrls(),
|
2022-07-25 02:02:05 -05:00
|
|
|
'dateCreated' => $shortUrl->getDateCreated()->toAtomString(),
|
|
|
|
'visitsCount' => 0,
|
|
|
|
'tags' => [],
|
|
|
|
'meta' => [
|
|
|
|
'validSince' => null,
|
|
|
|
'validUntil' => null,
|
|
|
|
'maxVisits' => null,
|
|
|
|
],
|
|
|
|
'domain' => null,
|
|
|
|
'title' => $shortUrl->title(),
|
|
|
|
'crawlable' => false,
|
|
|
|
'forwardQuery' => true,
|
2023-01-02 05:28:34 -06:00
|
|
|
'visitsSummary' => VisitsSummary::fromTotalAndNonBots(0, 0),
|
2022-07-26 05:17:37 -05:00
|
|
|
]], $update->payload);
|
2022-07-25 02:02:05 -05:00
|
|
|
}
|
2020-04-12 11:01:13 -05:00
|
|
|
}
|