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;
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-05-01 17:54:56 +02:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 11:50:19 +01:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2016-07-30 22:55:28 +02:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-07-13 12:04:21 +02:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2016-07-19 18:01:39 +02:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-07-30 22:55:28 +02:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2021-02-09 20:25:28 +01:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
|
2018-10-18 20:19:22 +02:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2021-05-16 09:30:04 +02:00
|
|
|
use Shlinkio\Shlink\Core\Options\TrackingOptions;
|
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
|
|
|
|
{
|
2020-11-02 11:50:19 +01:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-29 22:48:40 +01:00
|
|
|
private VisitsTracker $visitsTracker;
|
|
|
|
private ObjectProphecy $em;
|
|
|
|
private ObjectProphecy $eventDispatcher;
|
2021-05-16 09:30:04 +02:00
|
|
|
private TrackingOptions $options;
|
2016-07-30 22:55:28 +02:00
|
|
|
|
2019-02-16 10:53:45 +01:00
|
|
|
public function setUp(): void
|
2016-07-30 22:55:28 +02:00
|
|
|
{
|
|
|
|
$this->em = $this->prophesize(EntityManager::class);
|
2021-04-07 11:33:23 +02:00
|
|
|
|
2019-07-13 12:04:21 +02:00
|
|
|
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
|
2021-05-16 09:30:04 +02:00
|
|
|
$this->options = new TrackingOptions();
|
2019-07-13 12:04:21 +02:00
|
|
|
|
2021-02-10 20:09:25 +01:00
|
|
|
$this->visitsTracker = new VisitsTracker($this->em->reveal(), $this->eventDispatcher->reveal(), $this->options);
|
2016-07-30 22:55:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-09 20:34:12 +01:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideTrackingMethodNames
|
|
|
|
*/
|
|
|
|
public function trackPersistsVisitAndDispatchesEvent(string $method, array $args): void
|
2016-05-01 17:54:56 +02:00
|
|
|
{
|
2021-04-07 11:33:23 +02:00
|
|
|
$persist = $this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->will(function (): void {
|
|
|
|
});
|
2016-07-30 22:55:28 +02:00
|
|
|
|
2021-02-09 20:34:12 +01:00
|
|
|
$this->visitsTracker->{$method}(...$args);
|
2019-07-13 12:04:21 +02:00
|
|
|
|
2021-04-07 11:33:23 +02:00
|
|
|
$persist->shouldHaveBeenCalledOnce();
|
|
|
|
$this->em->flush()->shouldHaveBeenCalledOnce();
|
2021-02-09 20:25:28 +01:00
|
|
|
$this->eventDispatcher->dispatch(Argument::type(UrlVisited::class))->shouldHaveBeenCalled();
|
2016-07-30 22:55:28 +02:00
|
|
|
}
|
2021-02-09 20:34:12 +01:00
|
|
|
|
2021-05-16 09:51:52 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideTrackingMethodNames
|
|
|
|
*/
|
|
|
|
public function trackingIsSkippedCompletelyWhenDisabledFromOptions(string $method, array $args): void
|
|
|
|
{
|
|
|
|
$this->options->disableTracking = true;
|
|
|
|
|
|
|
|
$this->visitsTracker->{$method}(...$args);
|
|
|
|
|
|
|
|
$this->eventDispatcher->dispatch(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
|
|
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
|
|
$this->em->flush()->shouldNotHaveBeenCalled();
|
|
|
|
}
|
|
|
|
|
2021-02-09 20:34:12 +01: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()]];
|
|
|
|
}
|
2021-02-10 20:09:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideOrphanTrackingMethodNames
|
|
|
|
*/
|
|
|
|
public function orphanVisitsAreNotTrackedWhenDisabled(string $method): void
|
|
|
|
{
|
|
|
|
$this->options->trackOrphanVisits = false;
|
|
|
|
|
|
|
|
$this->visitsTracker->{$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'];
|
|
|
|
}
|
2016-05-01 17:54:56 +02:00
|
|
|
}
|