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