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