2016-05-01 18:54:56 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
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;
|
2020-01-01 23:11:53 +03:00
|
|
|
use Laminas\Stdlib\ArrayUtils;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-05-01 18:54:56 +03:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2016-07-30 23:55:28 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-07-13 13:04:21 +03:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2018-11-27 23:09:27 +03:00
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
2016-07-19 19:01:39 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2020-05-09 11:14:26 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Tag;
|
2016-07-30 23:55:28 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2021-01-17 13:42:35 +03:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlVisited;
|
2020-02-01 19:34:16 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
2020-05-09 11:14:26 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
|
2020-02-01 19:34:16 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
2018-10-18 21:19:22 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2018-11-27 23:09:27 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\VisitsParams;
|
2020-02-01 19:34:16 +03:00
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
|
2020-05-09 11:14:26 +03:00
|
|
|
use Shlinkio\Shlink\Core\Repository\TagRepository;
|
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;
|
2021-01-04 13:14:28 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2021-01-10 11:31:51 +03:00
|
|
|
use ShlinkioTest\Shlink\Core\Util\ApiKeyHelpersTrait;
|
2016-05-01 18:54:56 +03:00
|
|
|
|
2020-02-01 19:34:16 +03:00
|
|
|
use function Functional\map;
|
|
|
|
use function range;
|
|
|
|
|
2016-05-01 18:54:56 +03:00
|
|
|
class VisitsTrackerTest extends TestCase
|
|
|
|
{
|
2021-01-10 11:31:51 +03:00
|
|
|
use ApiKeyHelpersTrait;
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-30 00:48:40 +03:00
|
|
|
private VisitsTracker $visitsTracker;
|
|
|
|
private ObjectProphecy $em;
|
|
|
|
private ObjectProphecy $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);
|
2019-07-13 13:04:21 +03:00
|
|
|
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
|
|
|
|
|
2021-01-09 14:38:06 +03:00
|
|
|
$this->visitsTracker = new VisitsTracker($this->em->reveal(), $this->eventDispatcher->reveal(), true);
|
2016-07-30 23:55:28 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-07-13 13:04:21 +03:00
|
|
|
public function trackPersistsVisit(): void
|
2016-05-01 18:54:56 +03:00
|
|
|
{
|
|
|
|
$shortCode = '123ABC';
|
|
|
|
|
2019-12-30 01:16:55 +03:00
|
|
|
$this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->shouldBeCalledOnce();
|
2019-11-20 22:03:06 +03:00
|
|
|
$this->em->flush()->shouldBeCalledOnce();
|
2016-07-30 23:55:28 +03:00
|
|
|
|
2020-02-01 15:03:48 +03:00
|
|
|
$this->visitsTracker->track(new ShortUrl($shortCode), Visitor::emptyInstance());
|
2019-07-13 13:04:21 +03:00
|
|
|
|
|
|
|
$this->eventDispatcher->dispatch(Argument::type(ShortUrlVisited::class))->shouldHaveBeenCalled();
|
2016-07-30 23:55:28 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 14:38:06 +03:00
|
|
|
/**
|
|
|
|
* @test
|
2021-01-10 11:31:51 +03:00
|
|
|
* @dataProvider provideAdminApiKeys
|
2021-01-09 14:38:06 +03:00
|
|
|
*/
|
|
|
|
public function infoReturnsVisitsForCertainShortCode(?ApiKey $apiKey): void
|
2016-07-30 23:55:28 +03:00
|
|
|
{
|
|
|
|
$shortCode = '123ABC';
|
2021-01-09 14:38:06 +03:00
|
|
|
$spec = $apiKey === null ? null : $apiKey->spec();
|
2020-02-01 19:34:16 +03:00
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
2021-01-09 14:38:06 +03:00
|
|
|
$count = $repo->shortCodeIsInUse($shortCode, null, $spec)->willReturn(true);
|
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
|
|
|
|
2020-02-01 19:34:16 +03:00
|
|
|
$list = map(range(0, 1), fn () => new Visit(new ShortUrl(''), Visitor::emptyInstance()));
|
2016-07-30 23:55:28 +03:00
|
|
|
$repo2 = $this->prophesize(VisitRepository::class);
|
2021-01-09 14:38:06 +03:00
|
|
|
$repo2->findVisitsByShortCode($shortCode, null, Argument::type(DateRange::class), 1, 0, $spec)->willReturn(
|
2021-01-03 19:48:32 +03:00
|
|
|
$list,
|
|
|
|
);
|
2021-01-09 14:38:06 +03:00
|
|
|
$repo2->countVisitsByShortCode($shortCode, null, Argument::type(DateRange::class), $spec)->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
|
|
|
|
2021-01-09 14:38:06 +03:00
|
|
|
$paginator = $this->visitsTracker->info(new ShortUrlIdentifier($shortCode), new VisitsParams(), $apiKey);
|
2018-11-28 22:39:08 +03:00
|
|
|
|
2021-01-23 16:37:34 +03:00
|
|
|
self::assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentPageResults()));
|
2018-11-28 22:39:08 +03:00
|
|
|
$count->shouldHaveBeenCalledOnce();
|
2016-05-01 18:54:56 +03:00
|
|
|
}
|
2020-02-01 19:34:16 +03:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function throwsExceptionWhenRequestingVisitsForInvalidShortCode(): void
|
|
|
|
{
|
|
|
|
$shortCode = '123ABC';
|
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
2021-01-03 19:48:32 +03:00
|
|
|
$count = $repo->shortCodeIsInUse($shortCode, null, null)->willReturn(false);
|
2020-02-01 19:34:16 +03:00
|
|
|
$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 11:14:26 +03:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function throwsExceptionWhenRequestingVisitsForInvalidTag(): void
|
|
|
|
{
|
|
|
|
$tag = 'foo';
|
2021-01-04 13:14:28 +03:00
|
|
|
$apiKey = new ApiKey();
|
2020-05-09 11:14:26 +03:00
|
|
|
$repo = $this->prophesize(TagRepository::class);
|
2021-01-04 13:14:28 +03:00
|
|
|
$tagExists = $repo->tagExists($tag, $apiKey)->willReturn(false);
|
2020-05-09 11:14:26 +03:00
|
|
|
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$this->expectException(TagNotFoundException::class);
|
2021-01-04 13:14:28 +03:00
|
|
|
$tagExists->shouldBeCalledOnce();
|
2020-05-09 11:14:26 +03:00
|
|
|
$getRepo->shouldBeCalledOnce();
|
|
|
|
|
2021-01-04 13:14:28 +03:00
|
|
|
$this->visitsTracker->visitsForTag($tag, new VisitsParams(), $apiKey);
|
2020-05-09 11:14:26 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 14:38:06 +03:00
|
|
|
/**
|
|
|
|
* @test
|
2021-01-10 11:31:51 +03:00
|
|
|
* @dataProvider provideAdminApiKeys
|
2021-01-09 14:38:06 +03:00
|
|
|
*/
|
|
|
|
public function visitsForTagAreReturnedAsExpected(?ApiKey $apiKey): void
|
2020-05-09 11:14:26 +03:00
|
|
|
{
|
|
|
|
$tag = 'foo';
|
|
|
|
$repo = $this->prophesize(TagRepository::class);
|
2021-01-09 14:38:06 +03:00
|
|
|
$tagExists = $repo->tagExists($tag, $apiKey)->willReturn(true);
|
2020-05-09 11:14:26 +03:00
|
|
|
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
|
|
|
|
|
2021-01-09 14:38:06 +03:00
|
|
|
$spec = $apiKey === null ? null : $apiKey->spec();
|
2020-05-09 11:14:26 +03:00
|
|
|
$list = map(range(0, 1), fn () => new Visit(new ShortUrl(''), Visitor::emptyInstance()));
|
|
|
|
$repo2 = $this->prophesize(VisitRepository::class);
|
2021-01-09 14:38:06 +03:00
|
|
|
$repo2->findVisitsByTag($tag, Argument::type(DateRange::class), 1, 0, $spec)->willReturn($list);
|
|
|
|
$repo2->countVisitsByTag($tag, Argument::type(DateRange::class), $spec)->willReturn(1);
|
2020-05-09 11:14:26 +03:00
|
|
|
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
|
|
|
|
|
2021-01-09 14:38:06 +03:00
|
|
|
$paginator = $this->visitsTracker->visitsForTag($tag, new VisitsParams(), $apiKey);
|
2020-05-09 11:14:26 +03:00
|
|
|
|
2021-01-23 16:37:34 +03:00
|
|
|
self::assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentPageResults()));
|
2021-01-04 13:14:28 +03:00
|
|
|
$tagExists->shouldHaveBeenCalledOnce();
|
2020-05-09 11:14:26 +03:00
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2016-05-01 18:54:56 +03:00
|
|
|
}
|