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

105 lines
4 KiB
PHP
Raw Normal View History

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