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

100 lines
3.5 KiB
PHP
Raw Normal View History

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);
namespace ShlinkioTest\Shlink\Core\Visit;
2016-05-01 18:54:56 +03:00
use Doctrine\ORM\EntityManager;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2016-05-01 18:54:56 +03:00
use Prophecy\Argument;
2020-11-02 13:50:19 +03:00
use Prophecy\PhpUnit\ProphecyTrait;
2016-07-30 23:55:28 +03:00
use Prophecy\Prophecy\ObjectProphecy;
use Psr\EventDispatcher\EventDispatcherInterface;
2016-07-19 19:01:39 +03:00
use Shlinkio\Shlink\Core\Entity\ShortUrl;
2016-07-30 23:55:28 +03:00
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
use Shlinkio\Shlink\Core\Options\TrackingOptions;
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\VisitsTracker;
2016-05-01 18:54:56 +03:00
class VisitsTrackerTest extends TestCase
{
2020-11-02 13:50:19 +03:00
use ProphecyTrait;
private VisitsTracker $visitsTracker;
private ObjectProphecy $em;
private ObjectProphecy $eventDispatcher;
2016-07-30 23:55:28 +03:00
protected function setUp(): void
2016-07-30 23:55:28 +03:00
{
$this->em = $this->prophesize(EntityManager::class);
$this->eventDispatcher = $this->prophesize(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
{
$persist = $this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->will(function (): void {
});
2016-07-30 23:55:28 +03:00
$this->visitsTracker()->{$method}(...$args);
$persist->shouldHaveBeenCalledOnce();
$this->em->flush()->shouldHaveBeenCalledOnce();
$this->eventDispatcher->dispatch(Argument::type(UrlVisited::class))->shouldHaveBeenCalled();
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
{
$this->visitsTracker(new TrackingOptions(disableTracking: true))->{$method}(...$args);
2021-05-16 10:51:52 +03:00
$this->eventDispatcher->dispatch(Argument::cetera())->shouldNotHaveBeenCalled();
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
$this->em->flush()->shouldNotHaveBeenCalled();
}
2021-02-09 22:34:12 +03:00
public function provideTrackingMethodNames(): iterable
{
yield 'track' => ['track', [ShortUrl::createEmpty(), Visitor::emptyInstance()]];
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->visitsTracker(new TrackingOptions(trackOrphanVisits: false))->{$method}(Visitor::emptyInstance());
$this->eventDispatcher->dispatch(Argument::cetera())->shouldNotHaveBeenCalled();
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
$this->em->flush()->shouldNotHaveBeenCalled();
}
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->reveal(),
$this->eventDispatcher->reveal(),
$options ?? new TrackingOptions(),
);
}
2016-05-01 18:54:56 +03:00
}