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

93 lines
3 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;
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;
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\Model\Visitor;
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;
2016-05-01 18:54:56 +03:00
class VisitsTrackerTest extends TestCase
{
2016-07-30 23:55:28 +03:00
/**
* @var VisitsTracker
*/
protected $visitsTracker;
/**
* @var ObjectProphecy
*/
protected $em;
public function setUp()
{
$this->em = $this->prophesize(EntityManager::class);
$this->visitsTracker = new VisitsTracker($this->em->reveal());
}
2016-05-01 18:54:56 +03:00
/**
* @test
*/
public function trackPersistsVisit()
{
$shortCode = '123ABC';
$repo = $this->prophesize(EntityRepository::class);
$repo->findOneBy(['shortCode' => $shortCode])->willReturn(new ShortUrl());
2016-07-30 23:55:28 +03:00
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
$this->em->persist(Argument::any())->shouldBeCalledTimes(1);
2017-10-22 10:00:32 +03:00
$this->em->flush(Argument::type(Visit::class))->shouldBeCalledTimes(1);
2016-07-30 23:55:28 +03:00
$this->visitsTracker->track($shortCode, Visitor::emptyInstance());
2016-07-30 23:55:28 +03:00
}
/**
* @test
*/
public function trackedIpAddressGetsObfuscated()
{
$shortCode = '123ABC';
$test = $this;
$repo = $this->prophesize(EntityRepository::class);
$repo->findOneBy(['shortCode' => $shortCode])->willReturn(new ShortUrl());
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
$this->em->persist(Argument::any())->will(function ($args) use ($test) {
/** @var Visit $visit */
$visit = $args[0];
$test->assertEquals('4.3.2.0', $visit->getRemoteAddr());
})->shouldBeCalledTimes(1);
2017-10-22 10:00:32 +03:00
$this->em->flush(Argument::type(Visit::class))->shouldBeCalledTimes(1);
$this->visitsTracker->track($shortCode, new Visitor('', '', '4.3.2.1'));
}
2016-07-30 23:55:28 +03:00
/**
* @test
*/
public function infoReturnsVisistForCertainShortCode()
{
$shortCode = '123ABC';
$shortUrl = (new ShortUrl())->setOriginalUrl('http://domain.com/foo/bar');
$repo = $this->prophesize(EntityRepository::class);
$repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl);
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
$list = [
new Visit(),
new Visit(),
];
$repo2 = $this->prophesize(VisitRepository::class);
$repo2->findVisitsByShortUrl($shortUrl, null)->willReturn($list);
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledTimes(1);
2016-05-01 18:54:56 +03:00
2016-07-30 23:55:28 +03:00
$this->assertEquals($list, $this->visitsTracker->info($shortCode));
2016-05-01 18:54:56 +03:00
}
}