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