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);
|
|
|
|
|
2016-07-19 18:01:39 +02:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Service;
|
2016-05-01 17:54:56 +02:00
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
2020-01-01 21:11:53 +01:00
|
|
|
use Laminas\Stdlib\ArrayUtils;
|
2018-11-28 20:39:08 +01:00
|
|
|
use PHPUnit\Framework\Assert;
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-05-01 17:54:56 +02:00
|
|
|
use Prophecy\Argument;
|
2016-07-30 22:55:28 +02:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-07-13 12:04:21 +02:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2018-11-27 21:09:27 +01:00
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
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;
|
2019-07-13 12:04:21 +02:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited;
|
2020-02-01 17:34:16 +01:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
2018-10-18 20:19:22 +02:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2018-11-27 21:09:27 +01:00
|
|
|
use Shlinkio\Shlink\Core\Model\VisitsParams;
|
2020-02-01 17:34:16 +01:00
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
|
2016-07-30 22:55:28 +02:00
|
|
|
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
2016-07-19 18:01:39 +02:00
|
|
|
use Shlinkio\Shlink\Core\Service\VisitsTracker;
|
2016-05-01 17:54:56 +02:00
|
|
|
|
2020-02-01 17:34:16 +01:00
|
|
|
use function Functional\map;
|
|
|
|
use function range;
|
|
|
|
|
2016-05-01 17:54:56 +02:00
|
|
|
class VisitsTrackerTest extends TestCase
|
|
|
|
{
|
2019-12-29 22:48:40 +01:00
|
|
|
private VisitsTracker $visitsTracker;
|
|
|
|
private ObjectProphecy $em;
|
|
|
|
private ObjectProphecy $eventDispatcher;
|
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);
|
2019-07-13 12:04:21 +02:00
|
|
|
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
|
|
|
|
|
|
|
|
$this->visitsTracker = new VisitsTracker($this->em->reveal(), $this->eventDispatcher->reveal());
|
2016-07-30 22:55:28 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2019-07-13 12:04:21 +02:00
|
|
|
public function trackPersistsVisit(): void
|
2016-05-01 17:54:56 +02:00
|
|
|
{
|
|
|
|
$shortCode = '123ABC';
|
|
|
|
|
2019-12-29 23:16:55 +01:00
|
|
|
$this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->shouldBeCalledOnce();
|
2019-11-20 20:03:06 +01:00
|
|
|
$this->em->flush()->shouldBeCalledOnce();
|
2016-07-30 22:55:28 +02:00
|
|
|
|
2020-02-01 13:03:48 +01:00
|
|
|
$this->visitsTracker->track(new ShortUrl($shortCode), Visitor::emptyInstance());
|
2019-07-13 12:04:21 +02:00
|
|
|
|
|
|
|
$this->eventDispatcher->dispatch(Argument::type(ShortUrlVisited::class))->shouldHaveBeenCalled();
|
2016-07-30 22:55:28 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2019-07-13 12:04:21 +02:00
|
|
|
public function trackedIpAddressGetsObfuscated(): void
|
2016-08-09 09:13:39 +02:00
|
|
|
{
|
|
|
|
$shortCode = '123ABC';
|
|
|
|
|
2018-11-28 20:39:08 +01:00
|
|
|
$this->em->persist(Argument::any())->will(function ($args) {
|
2016-08-09 09:13:39 +02:00
|
|
|
/** @var Visit $visit */
|
|
|
|
$visit = $args[0];
|
2018-11-28 20:39:08 +01:00
|
|
|
Assert::assertEquals('4.3.2.0', $visit->getRemoteAddr());
|
2019-07-13 12:04:21 +02:00
|
|
|
$visit->setId('1');
|
|
|
|
return $visit;
|
2019-11-20 20:03:06 +01:00
|
|
|
})->shouldBeCalledOnce();
|
|
|
|
$this->em->flush()->shouldBeCalledOnce();
|
2016-08-09 09:13:39 +02:00
|
|
|
|
2020-02-01 13:03:48 +01:00
|
|
|
$this->visitsTracker->track(new ShortUrl($shortCode), new Visitor('', '', '4.3.2.1'));
|
2019-07-13 12:04:21 +02:00
|
|
|
|
|
|
|
$this->eventDispatcher->dispatch(Argument::type(ShortUrlVisited::class))->shouldHaveBeenCalled();
|
2016-08-09 09:13:39 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2019-07-13 12:04:21 +02:00
|
|
|
public function infoReturnsVisitsForCertainShortCode(): void
|
2016-07-30 22:55:28 +02:00
|
|
|
{
|
|
|
|
$shortCode = '123ABC';
|
2020-02-01 17:34:16 +01:00
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
|
|
|
$count = $repo->shortCodeIsInUse($shortCode, null)->willReturn(true);
|
2018-11-11 13:18:21 +01:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledOnce();
|
2016-07-30 22:55:28 +02:00
|
|
|
|
2020-02-01 17:34:16 +01:00
|
|
|
$list = map(range(0, 1), fn () => new Visit(new ShortUrl(''), Visitor::emptyInstance()));
|
2016-07-30 22:55:28 +02:00
|
|
|
$repo2 = $this->prophesize(VisitRepository::class);
|
2020-02-01 17:34:16 +01:00
|
|
|
$repo2->findVisitsByShortCode($shortCode, null, Argument::type(DateRange::class), 1, 0)->willReturn($list);
|
|
|
|
$repo2->countVisitsByShortCode($shortCode, null, Argument::type(DateRange::class))->willReturn(1);
|
2018-11-11 13:18:21 +01:00
|
|
|
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
|
2016-05-01 17:54:56 +02:00
|
|
|
|
2020-02-01 17:34:16 +01:00
|
|
|
$paginator = $this->visitsTracker->info(new ShortUrlIdentifier($shortCode), new VisitsParams());
|
2018-11-28 20:39:08 +01:00
|
|
|
|
|
|
|
$this->assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentItems()));
|
|
|
|
$count->shouldHaveBeenCalledOnce();
|
2016-05-01 17:54:56 +02:00
|
|
|
}
|
2020-02-01 17:34:16 +01:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function throwsExceptionWhenRequestingVisitsForInvalidShortCode(): void
|
|
|
|
{
|
|
|
|
$shortCode = '123ABC';
|
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
|
|
|
$count = $repo->shortCodeIsInUse($shortCode, null)->willReturn(false);
|
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$this->expectException(ShortUrlNotFoundException::class);
|
|
|
|
$count->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$this->visitsTracker->info(new ShortUrlIdentifier($shortCode), new VisitsParams());
|
|
|
|
}
|
2016-05-01 17:54:56 +02:00
|
|
|
}
|