mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 17:23:12 +08:00
Wrap JSON serialization for any kind of visit in Visit entity itself
This commit is contained in:
@@ -68,7 +68,6 @@ return [
|
||||
Visit\Geolocation\VisitLocator::class => ConfigAbstractFactory::class,
|
||||
Visit\Geolocation\VisitToLocationHelper::class => ConfigAbstractFactory::class,
|
||||
Visit\VisitsStatsHelper::class => ConfigAbstractFactory::class,
|
||||
Visit\Transformer\OrphanVisitDataTransformer::class => InvokableFactory::class,
|
||||
Visit\Repository\VisitLocationRepository::class => [
|
||||
EntityRepositoryFactory::class,
|
||||
Visit\Entity\Visit::class,
|
||||
@@ -199,10 +198,7 @@ return [
|
||||
],
|
||||
ShortUrl\Middleware\TrimTrailingSlashMiddleware::class => [Options\UrlShortenerOptions::class],
|
||||
|
||||
EventDispatcher\PublishingUpdatesGenerator::class => [
|
||||
ShortUrl\Transformer\ShortUrlDataTransformer::class,
|
||||
Visit\Transformer\OrphanVisitDataTransformer::class,
|
||||
],
|
||||
EventDispatcher\PublishingUpdatesGenerator::class => [ShortUrl\Transformer\ShortUrlDataTransformer::class],
|
||||
|
||||
Importer\ImportedLinksProcessor::class => [
|
||||
'em',
|
||||
|
||||
@@ -9,12 +9,10 @@ use Shlinkio\Shlink\Common\UpdatePublishing\Update;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||
|
||||
final class PublishingUpdatesGenerator implements PublishingUpdatesGeneratorInterface
|
||||
final readonly class PublishingUpdatesGenerator implements PublishingUpdatesGeneratorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DataTransformerInterface $shortUrlTransformer,
|
||||
private readonly DataTransformerInterface $orphanVisitTransformer,
|
||||
) {
|
||||
public function __construct(private DataTransformerInterface $shortUrlTransformer)
|
||||
{
|
||||
}
|
||||
|
||||
public function newVisitUpdate(Visit $visit): Update
|
||||
@@ -28,7 +26,7 @@ final class PublishingUpdatesGenerator implements PublishingUpdatesGeneratorInte
|
||||
public function newOrphanVisitUpdate(Visit $visit): Update
|
||||
{
|
||||
return Update::forTopicAndPayload(Topic::NEW_ORPHAN_VISIT->value, [
|
||||
'visit' => $this->orphanVisitTransformer->transform($visit),
|
||||
'visit' => $visit->jsonSerialize(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Visit\Transformer;
|
||||
|
||||
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||
|
||||
class OrphanVisitDataTransformer implements DataTransformerInterface
|
||||
{
|
||||
/**
|
||||
* @param Visit $visit
|
||||
*/
|
||||
public function transform($visit): array // phpcs:ignore
|
||||
{
|
||||
return $visit->jsonSerialize();
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitsSummary;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
|
||||
use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer;
|
||||
|
||||
class PublishingUpdatesGeneratorTest extends TestCase
|
||||
{
|
||||
@@ -28,7 +27,6 @@ class PublishingUpdatesGeneratorTest extends TestCase
|
||||
{
|
||||
$this->generator = new PublishingUpdatesGenerator(
|
||||
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
||||
new OrphanVisitDataTransformer(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,18 @@ declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\Visit\Entity;
|
||||
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use Laminas\Diactoros\Uri;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Common\Util\IpAddress;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
|
||||
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||
|
||||
class VisitTest extends TestCase
|
||||
{
|
||||
@@ -40,6 +45,62 @@ class VisitTest extends TestCase
|
||||
yield 'Guzzle' => ['guzzlehttp', true];
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideOrphanVisits')]
|
||||
public function isProperlyJsonSerializedWhenOrphan(Visit $visit, array $expectedResult): void
|
||||
{
|
||||
self::assertEquals($expectedResult, $visit->jsonSerialize());
|
||||
}
|
||||
|
||||
public static function provideOrphanVisits(): iterable
|
||||
{
|
||||
yield 'base path visit' => [
|
||||
$visit = Visit::forBasePath(Visitor::emptyInstance()),
|
||||
[
|
||||
'referer' => '',
|
||||
'date' => $visit->getDate()->toAtomString(),
|
||||
'userAgent' => '',
|
||||
'visitLocation' => null,
|
||||
'potentialBot' => false,
|
||||
'visitedUrl' => '',
|
||||
'type' => VisitType::BASE_URL->value,
|
||||
],
|
||||
];
|
||||
yield 'invalid short url visit' => [
|
||||
$visit = Visit::forInvalidShortUrl(Visitor::fromRequest(
|
||||
ServerRequestFactory::fromGlobals()->withHeader('User-Agent', 'foo')
|
||||
->withHeader('Referer', 'bar')
|
||||
->withUri(new Uri('https://example.com/foo')),
|
||||
)),
|
||||
[
|
||||
'referer' => 'bar',
|
||||
'date' => $visit->getDate()->toAtomString(),
|
||||
'userAgent' => 'foo',
|
||||
'visitLocation' => null,
|
||||
'potentialBot' => false,
|
||||
'visitedUrl' => 'https://example.com/foo',
|
||||
'type' => VisitType::INVALID_SHORT_URL->value,
|
||||
],
|
||||
];
|
||||
yield 'regular 404 visit' => [
|
||||
$visit = Visit::forRegularNotFound(
|
||||
Visitor::fromRequest(
|
||||
ServerRequestFactory::fromGlobals()->withHeader('User-Agent', 'user-agent')
|
||||
->withHeader('Referer', 'referer')
|
||||
->withUri(new Uri('https://s.test/foo/bar')),
|
||||
),
|
||||
)->locate($location = VisitLocation::fromGeolocation(Location::emptyInstance())),
|
||||
[
|
||||
'referer' => 'referer',
|
||||
'date' => $visit->getDate()->toAtomString(),
|
||||
'userAgent' => 'user-agent',
|
||||
'visitLocation' => $location,
|
||||
'potentialBot' => false,
|
||||
'visitedUrl' => 'https://s.test/foo/bar',
|
||||
'type' => VisitType::REGULAR_404->value,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideAddresses')]
|
||||
public function addressIsAnonymizedWhenRequested(bool $anonymize, ?string $address, ?string $expectedAddress): void
|
||||
{
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\Visit\Transformer;
|
||||
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use Laminas\Diactoros\Uri;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
|
||||
use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer;
|
||||
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||
|
||||
class OrphanVisitDataTransformerTest extends TestCase
|
||||
{
|
||||
private OrphanVisitDataTransformer $transformer;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->transformer = new OrphanVisitDataTransformer();
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideVisits')]
|
||||
public function visitsAreParsedAsExpected(Visit $visit, array $expectedResult): void
|
||||
{
|
||||
$result = $this->transformer->transform($visit);
|
||||
|
||||
self::assertEquals($expectedResult, $result);
|
||||
}
|
||||
|
||||
public static function provideVisits(): iterable
|
||||
{
|
||||
yield 'base path visit' => [
|
||||
$visit = Visit::forBasePath(Visitor::emptyInstance()),
|
||||
[
|
||||
'referer' => '',
|
||||
'date' => $visit->getDate()->toAtomString(),
|
||||
'userAgent' => '',
|
||||
'visitLocation' => null,
|
||||
'potentialBot' => false,
|
||||
'visitedUrl' => '',
|
||||
'type' => VisitType::BASE_URL->value,
|
||||
],
|
||||
];
|
||||
yield 'invalid short url visit' => [
|
||||
$visit = Visit::forInvalidShortUrl(Visitor::fromRequest(
|
||||
ServerRequestFactory::fromGlobals()->withHeader('User-Agent', 'foo')
|
||||
->withHeader('Referer', 'bar')
|
||||
->withUri(new Uri('https://example.com/foo')),
|
||||
)),
|
||||
[
|
||||
'referer' => 'bar',
|
||||
'date' => $visit->getDate()->toAtomString(),
|
||||
'userAgent' => 'foo',
|
||||
'visitLocation' => null,
|
||||
'potentialBot' => false,
|
||||
'visitedUrl' => 'https://example.com/foo',
|
||||
'type' => VisitType::INVALID_SHORT_URL->value,
|
||||
],
|
||||
];
|
||||
yield 'regular 404 visit' => [
|
||||
$visit = Visit::forRegularNotFound(
|
||||
Visitor::fromRequest(
|
||||
ServerRequestFactory::fromGlobals()->withHeader('User-Agent', 'user-agent')
|
||||
->withHeader('Referer', 'referer')
|
||||
->withUri(new Uri('https://s.test/foo/bar')),
|
||||
),
|
||||
)->locate($location = VisitLocation::fromGeolocation(Location::emptyInstance())),
|
||||
[
|
||||
'referer' => 'referer',
|
||||
'date' => $visit->getDate()->toAtomString(),
|
||||
'userAgent' => 'user-agent',
|
||||
'visitLocation' => $location,
|
||||
'potentialBot' => false,
|
||||
'visitedUrl' => 'https://s.test/foo/bar',
|
||||
'type' => VisitType::REGULAR_404->value,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user