shlink/module/Core/test/Visit/VisitsTrackerTest.php

92 lines
3.3 KiB
PHP
Raw Normal View History

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);
namespace ShlinkioTest\Shlink\Core\Visit;
2016-05-01 10:54:56 -05:00
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\MockObject\MockObject;
2017-03-24 14:34:18 -05:00
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
use Shlinkio\Shlink\Core\Options\TrackingOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\VisitsTracker;
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
protected function setUp(): void
2016-07-30 15:55:28 -05:00
{
$this->em = $this->createMock(EntityManager::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
2016-07-30 15:55:28 -05:00
}
2021-02-09 13:34:12 -06:00
/**
* @test
* @dataProvider provideTrackingMethodNames
*/
public function trackPersistsVisitAndDispatchesEvent(string $method, array $args): void
2016-05-01 10:54:56 -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),
);
$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
$this->visitsTracker()->{$method}(...$args);
2016-07-30 15:55:28 -05:00
}
2021-02-09 13:34:12 -06:00
2021-05-16 02:51:52 -05:00
/**
* @test
* @dataProvider provideTrackingMethodNames
*/
public function trackingIsSkippedCompletelyWhenDisabledFromOptions(string $method, array $args): void
{
$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
$this->visitsTracker(new TrackingOptions(disableTracking: true))->{$method}(...$args);
2021-05-16 02:51:52 -05:00
}
2021-02-09 13:34:12 -06:00
public function provideTrackingMethodNames(): iterable
{
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()]];
}
/**
* @test
* @dataProvider provideOrphanTrackingMethodNames
*/
public function orphanVisitsAreNotTrackedWhenDisabled(string $method): void
{
$this->em->expects($this->never())->method('persist');
$this->em->expects($this->never())->method('flush');
$this->eventDispatcher->expects($this->never())->method('dispatch');
$this->visitsTracker(new TrackingOptions(trackOrphanVisits: false))->{$method}(Visitor::emptyInstance());
}
public function provideOrphanTrackingMethodNames(): iterable
{
yield 'trackInvalidShortUrlVisit' => ['trackInvalidShortUrlVisit'];
yield 'trackBaseUrlVisit' => ['trackBaseUrlVisit'];
yield 'trackRegularNotFoundVisit' => ['trackRegularNotFoundVisit'];
}
private function visitsTracker(?TrackingOptions $options = null): VisitsTracker
{
return new VisitsTracker($this->em, $this->eventDispatcher, $options ?? new TrackingOptions());
}
2016-05-01 10:54:56 -05:00
}