2016-05-01 17:54:56 +02:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2021-02-08 19:50:17 +01:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Visit;
|
2016-05-01 17:54:56 +02:00
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 21:08:58 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-07-13 12:04:21 +02:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2024-10-16 08:55:38 +02:00
|
|
|
use Shlinkio\Shlink\Core\Config\Options\TrackingOptions;
|
2021-02-09 20:25:28 +01:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
|
2022-09-23 19:03:32 +02:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
2022-09-23 18:05:17 +02:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2021-02-08 19:50:17 +01:00
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitsTracker;
|
2020-02-01 17:34:16 +01:00
|
|
|
|
2016-05-01 17:54:56 +02:00
|
|
|
class VisitsTrackerTest extends TestCase
|
|
|
|
|
{
|
2026-05-22 08:42:47 +02:00
|
|
|
private MockObject&EntityManager $em;
|
|
|
|
|
private MockObject&EventDispatcherInterface $eventDispatcher;
|
2016-07-30 22:55:28 +02:00
|
|
|
|
2022-09-11 12:02:49 +02:00
|
|
|
protected function setUp(): void
|
2016-07-30 22:55:28 +02:00
|
|
|
{
|
2022-10-23 21:08:58 +02:00
|
|
|
$this->em = $this->createMock(EntityManager::class);
|
2026-05-16 11:21:43 +02:00
|
|
|
$this->em->method('wrapInTransaction')->willReturnCallback(static fn (callable $callback) => $callback());
|
2024-03-27 19:14:41 +01:00
|
|
|
|
2022-10-23 21:08:58 +02:00
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
2016-07-30 22:55:28 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test, DataProvider('provideTrackingMethodNames')]
|
2021-02-09 20:34:12 +01:00
|
|
|
public function trackPersistsVisitAndDispatchesEvent(string $method, array $args): void
|
2016-05-01 17:54:56 +02:00
|
|
|
{
|
2024-11-11 08:51:55 +01:00
|
|
|
$this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(Visit::class));
|
2026-05-22 08:42:47 +02:00
|
|
|
$this->eventDispatcher
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('dispatch')
|
|
|
|
|
->with(
|
|
|
|
|
$this->isInstanceOf(UrlVisited::class),
|
|
|
|
|
);
|
2016-07-30 22:55:28 +02:00
|
|
|
|
2024-11-11 08:51:55 +01:00
|
|
|
$result = $this->visitsTracker()->{$method}(...$args);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Visit::class, $result);
|
2016-07-30 22:55:28 +02:00
|
|
|
}
|
2021-02-09 20:34:12 +01:00
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test, DataProvider('provideTrackingMethodNames')]
|
2021-05-16 09:51:52 +02:00
|
|
|
public function trackingIsSkippedCompletelyWhenDisabledFromOptions(string $method, array $args): void
|
|
|
|
|
{
|
2022-10-23 21:08:58 +02:00
|
|
|
$this->em->expects($this->never())->method('persist');
|
|
|
|
|
$this->eventDispatcher->expects($this->never())->method('dispatch');
|
2021-05-16 09:51:52 +02:00
|
|
|
|
2024-11-11 08:51:55 +01:00
|
|
|
$result = $this->visitsTracker(new TrackingOptions(disableTracking: true))->{$method}(...$args);
|
|
|
|
|
|
|
|
|
|
self::assertNull($result);
|
2021-05-16 09:51:52 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 09:32:38 +01:00
|
|
|
public static function provideTrackingMethodNames(): iterable
|
2021-02-09 20:34:12 +01:00
|
|
|
{
|
2024-11-11 08:51:55 +01:00
|
|
|
yield 'track' => ['track', [ShortUrl::createFake(), Visitor::empty()]];
|
|
|
|
|
yield 'trackInvalidShortUrlVisit' => ['trackInvalidShortUrlVisit', [Visitor::empty()]];
|
|
|
|
|
yield 'trackBaseUrlVisit' => ['trackBaseUrlVisit', [Visitor::empty()]];
|
|
|
|
|
yield 'trackRegularNotFoundVisit' => ['trackRegularNotFoundVisit', [Visitor::empty()]];
|
2021-02-09 20:34:12 +01:00
|
|
|
}
|
2021-02-10 20:09:25 +01:00
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test, DataProvider('provideOrphanTrackingMethodNames')]
|
2021-02-10 20:09:25 +01:00
|
|
|
public function orphanVisitsAreNotTrackedWhenDisabled(string $method): void
|
|
|
|
|
{
|
2022-10-23 21:08:58 +02:00
|
|
|
$this->em->expects($this->never())->method('persist');
|
|
|
|
|
$this->eventDispatcher->expects($this->never())->method('dispatch');
|
2021-02-10 20:09:25 +01:00
|
|
|
|
2024-11-11 08:51:55 +01:00
|
|
|
$result = $this->visitsTracker(new TrackingOptions(trackOrphanVisits: false))->{$method}(Visitor::empty());
|
|
|
|
|
|
|
|
|
|
self::assertNull($result);
|
2021-02-10 20:09:25 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 09:32:38 +01:00
|
|
|
public static function provideOrphanTrackingMethodNames(): iterable
|
2021-02-10 20:09:25 +01:00
|
|
|
{
|
|
|
|
|
yield 'trackInvalidShortUrlVisit' => ['trackInvalidShortUrlVisit'];
|
|
|
|
|
yield 'trackBaseUrlVisit' => ['trackBaseUrlVisit'];
|
|
|
|
|
yield 'trackRegularNotFoundVisit' => ['trackRegularNotFoundVisit'];
|
|
|
|
|
}
|
2022-09-17 12:57:04 +02:00
|
|
|
|
2024-10-28 22:27:30 +01:00
|
|
|
private function visitsTracker(TrackingOptions|null $options = null): VisitsTracker
|
2022-09-17 12:57:04 +02:00
|
|
|
{
|
2022-10-23 21:08:58 +02:00
|
|
|
return new VisitsTracker($this->em, $this->eventDispatcher, $options ?? new TrackingOptions());
|
2022-09-17 12:57:04 +02:00
|
|
|
}
|
2016-05-01 17:54:56 +02:00
|
|
|
}
|