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