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

128 lines
5.1 KiB
PHP
Raw Normal View History

2016-05-01 10:54:56 -05:00
<?php
2019-10-05 10:26:10 -05:00
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;
2020-01-01 14:11:53 -06:00
use Laminas\Stdlib\ArrayUtils;
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;
use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\Common\Util\DateRange;
2016-07-19 11:01:39 -05:00
use Shlinkio\Shlink\Core\Entity\ShortUrl;
2020-05-09 03:14:26 -05:00
use Shlinkio\Shlink\Core\Entity\Tag;
2016-07-30 15:55:28 -05:00
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
2020-05-09 03:14:26 -05:00
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Model\VisitsParams;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
2020-05-09 03:14:26 -05:00
use Shlinkio\Shlink\Core\Repository\TagRepository;
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;
2016-05-01 10:54:56 -05:00
use function Functional\map;
use function range;
2016-05-01 10:54:56 -05:00
class VisitsTrackerTest extends TestCase
{
private VisitsTracker $visitsTracker;
private ObjectProphecy $em;
private ObjectProphecy $eventDispatcher;
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->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$this->visitsTracker = new VisitsTracker($this->em->reveal(), $this->eventDispatcher->reveal(), true);
2016-07-30 15:55:28 -05:00
}
2019-02-17 13:28:34 -06:00
/** @test */
public function trackPersistsVisit(): void
2016-05-01 10:54:56 -05:00
{
$shortCode = '123ABC';
$this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->shouldBeCalledOnce();
$this->em->flush()->shouldBeCalledOnce();
2016-07-30 15:55:28 -05:00
$this->visitsTracker->track(new ShortUrl($shortCode), Visitor::emptyInstance());
$this->eventDispatcher->dispatch(Argument::type(ShortUrlVisited::class))->shouldHaveBeenCalled();
2016-07-30 15:55:28 -05:00
}
2019-02-17 13:28:34 -06:00
/** @test */
public function infoReturnsVisitsForCertainShortCode(): void
2016-07-30 15:55:28 -05:00
{
$shortCode = '123ABC';
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
$count = $repo->shortCodeIsInUse($shortCode, null)->willReturn(true);
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 = map(range(0, 1), fn () => new Visit(new ShortUrl(''), Visitor::emptyInstance()));
2016-07-30 15:55:28 -05:00
$repo2 = $this->prophesize(VisitRepository::class);
$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 06:18:21 -06:00
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
2016-05-01 10:54:56 -05:00
$paginator = $this->visitsTracker->info(new ShortUrlIdentifier($shortCode), new VisitsParams());
2018-11-28 13:39:08 -06:00
$this->assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentItems()));
$count->shouldHaveBeenCalledOnce();
2016-05-01 10:54:56 -05: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());
}
2020-05-09 03:14:26 -05:00
/** @test */
public function throwsExceptionWhenRequestingVisitsForInvalidTag(): void
{
$tag = 'foo';
$repo = $this->prophesize(TagRepository::class);
$count = $repo->count(['name' => $tag])->willReturn(0);
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
$this->expectException(TagNotFoundException::class);
$count->shouldBeCalledOnce();
$getRepo->shouldBeCalledOnce();
$this->visitsTracker->visitsForTag($tag, new VisitsParams());
}
/** @test */
public function visitsForTagAreReturnedAsExpected(): void
{
$tag = 'foo';
$repo = $this->prophesize(TagRepository::class);
$count = $repo->count(['name' => $tag])->willReturn(1);
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
$list = map(range(0, 1), fn () => new Visit(new ShortUrl(''), Visitor::emptyInstance()));
$repo2 = $this->prophesize(VisitRepository::class);
$repo2->findVisitsByTag($tag, Argument::type(DateRange::class), 1, 0)->willReturn($list);
$repo2->countVisitsByTag($tag, Argument::type(DateRange::class))->willReturn(1);
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
$paginator = $this->visitsTracker->visitsForTag($tag, new VisitsParams());
$this->assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentItems()));
$count->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
}
2016-05-01 10:54:56 -05:00
}