Updated visits to support pagination

This commit is contained in:
Alejandro Celaya
2018-11-28 20:39:08 +01:00
parent b0f250ed8a
commit d0e0aea0f1
11 changed files with 167 additions and 60 deletions

View File

@@ -5,6 +5,7 @@ namespace ShlinkioTest\Shlink\Core\Service;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
@@ -15,6 +16,7 @@ use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Model\VisitsParams;
use Shlinkio\Shlink\Core\Repository\VisitRepository;
use Shlinkio\Shlink\Core\Service\VisitsTracker;
use Zend\Stdlib\ArrayUtils;
class VisitsTrackerTest extends TestCase
{
@@ -51,15 +53,14 @@ class VisitsTrackerTest extends TestCase
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())->shouldBeCalledOnce();
$this->em->persist(Argument::any())->will(function ($args) use ($test) {
$this->em->persist(Argument::any())->will(function ($args) {
/** @var Visit $visit */
$visit = $args[0];
$test->assertEquals('4.3.2.0', $visit->getRemoteAddr());
Assert::assertEquals('4.3.2.0', $visit->getRemoteAddr());
})->shouldBeCalledOnce();
$this->em->flush(Argument::type(Visit::class))->shouldBeCalledOnce();
@@ -72,9 +73,8 @@ class VisitsTrackerTest extends TestCase
public function infoReturnsVisistForCertainShortCode()
{
$shortCode = '123ABC';
$shortUrl = new ShortUrl('http://domain.com/foo/bar');
$repo = $this->prophesize(EntityRepository::class);
$repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl);
$count = $repo->count(['shortCode' => $shortCode])->willReturn(1);
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledOnce();
$list = [
@@ -82,9 +82,13 @@ class VisitsTrackerTest extends TestCase
new Visit(new ShortUrl(''), Visitor::emptyInstance()),
];
$repo2 = $this->prophesize(VisitRepository::class);
$repo2->findVisitsByShortUrl($shortUrl, Argument::type(DateRange::class))->willReturn($list);
$repo2->findVisitsByShortCode($shortCode, Argument::type(DateRange::class), 1, 0)->willReturn($list);
$repo2->countVisitsByShortCode($shortCode, Argument::type(DateRange::class))->willReturn(1);
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
$this->assertEquals($list, $this->visitsTracker->info($shortCode, new VisitsParams()));
$paginator = $this->visitsTracker->info($shortCode, new VisitsParams());
$this->assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentItems()));
$count->shouldHaveBeenCalledOnce();
}
}