2020-05-01 12:57:46 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Visit;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2021-02-08 21:46:51 +03:00
|
|
|
use Laminas\Stdlib\ArrayUtils;
|
2020-05-01 12:57:46 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-02-08 21:46:51 +03:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2020-05-01 12:57:46 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
2022-04-23 12:02:51 +03:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepository;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
2021-02-08 21:46:51 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
2022-09-23 19:24:14 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\Tag\Entity\Tag;
|
2022-09-23 19:24:14 +03:00
|
|
|
use Shlinkio\Shlink\Core\Tag\Repository\TagRepository;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
|
2020-05-01 12:57:46 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitsStats;
|
2021-05-22 21:16:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering;
|
2021-05-22 21:32:30 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Persistence\VisitsListFiltering;
|
2022-09-23 19:24:14 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Repository\VisitRepository;
|
2020-05-01 12:57:46 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelper;
|
2021-02-08 21:46:51 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|
|
|
use ShlinkioTest\Shlink\Core\Util\ApiKeyHelpersTrait;
|
2020-05-01 12:57:46 +03:00
|
|
|
|
2021-02-10 00:40:40 +03:00
|
|
|
use function count;
|
2020-05-01 12:57:46 +03:00
|
|
|
use function Functional\map;
|
|
|
|
use function range;
|
|
|
|
|
|
|
|
class VisitsStatsHelperTest extends TestCase
|
|
|
|
{
|
2021-02-08 21:46:51 +03:00
|
|
|
use ApiKeyHelpersTrait;
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2020-05-01 12:57:46 +03:00
|
|
|
private VisitsStatsHelper $helper;
|
|
|
|
private ObjectProphecy $em;
|
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2020-05-01 12:57:46 +03:00
|
|
|
{
|
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
$this->helper = new VisitsStatsHelper($this->em->reveal());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideCounts
|
|
|
|
*/
|
|
|
|
public function returnsExpectedVisitsStats(int $expectedCount): void
|
|
|
|
{
|
|
|
|
$repo = $this->prophesize(VisitRepository::class);
|
2022-01-16 13:15:39 +03:00
|
|
|
$count = $repo->countNonOrphanVisits(new VisitsCountFiltering())->willReturn($expectedCount * 3);
|
2021-05-22 21:16:32 +03:00
|
|
|
$countOrphan = $repo->countOrphanVisits(Argument::type(VisitsCountFiltering::class))->willReturn(
|
|
|
|
$expectedCount,
|
|
|
|
);
|
2020-05-01 12:57:46 +03:00
|
|
|
$getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$stats = $this->helper->getVisitsStats();
|
|
|
|
|
2021-02-09 00:44:58 +03:00
|
|
|
self::assertEquals(new VisitsStats($expectedCount * 3, $expectedCount), $stats);
|
2020-05-01 12:57:46 +03:00
|
|
|
$count->shouldHaveBeenCalledOnce();
|
2021-02-09 00:44:58 +03:00
|
|
|
$countOrphan->shouldHaveBeenCalledOnce();
|
2020-05-01 12:57:46 +03:00
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideCounts(): iterable
|
|
|
|
{
|
|
|
|
return map(range(0, 50, 5), fn (int $value) => [$value]);
|
|
|
|
}
|
2021-02-08 21:46:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideAdminApiKeys
|
|
|
|
*/
|
|
|
|
public function infoReturnsVisitsForCertainShortCode(?ApiKey $apiKey): void
|
|
|
|
{
|
|
|
|
$shortCode = '123ABC';
|
2021-05-23 09:41:42 +03:00
|
|
|
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
|
2021-05-23 13:31:10 +03:00
|
|
|
$spec = $apiKey?->spec();
|
2021-05-23 09:41:42 +03:00
|
|
|
|
2021-02-08 21:46:51 +03:00
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
2021-05-23 09:41:42 +03:00
|
|
|
$count = $repo->shortCodeIsInUse($identifier, $spec)->willReturn(
|
2021-05-02 11:33:27 +03:00
|
|
|
true,
|
|
|
|
);
|
2021-02-08 21:46:51 +03:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$list = map(range(0, 1), fn () => Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance()));
|
|
|
|
$repo2 = $this->prophesize(VisitRepository::class);
|
2021-05-23 09:41:42 +03:00
|
|
|
$repo2->findVisitsByShortCode($identifier, Argument::type(VisitsListFiltering::class))->willReturn($list);
|
|
|
|
$repo2->countVisitsByShortCode($identifier, Argument::type(VisitsCountFiltering::class))->willReturn(1);
|
2021-02-08 21:46:51 +03:00
|
|
|
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
|
|
|
|
|
2021-05-23 09:41:42 +03:00
|
|
|
$paginator = $this->helper->visitsForShortUrl($identifier, new VisitsParams(), $apiKey);
|
2021-02-08 21:46:51 +03:00
|
|
|
|
|
|
|
self::assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentPageResults()));
|
|
|
|
$count->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function throwsExceptionWhenRequestingVisitsForInvalidShortCode(): void
|
|
|
|
{
|
|
|
|
$shortCode = '123ABC';
|
2021-05-23 09:41:42 +03:00
|
|
|
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
|
|
|
|
|
2021-02-08 21:46:51 +03:00
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
2021-05-23 09:41:42 +03:00
|
|
|
$count = $repo->shortCodeIsInUse($identifier, null)->willReturn(
|
2021-05-02 11:33:27 +03:00
|
|
|
false,
|
|
|
|
);
|
2021-02-08 21:46:51 +03:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$this->expectException(ShortUrlNotFoundException::class);
|
|
|
|
$count->shouldBeCalledOnce();
|
|
|
|
|
2021-05-23 09:41:42 +03:00
|
|
|
$this->helper->visitsForShortUrl($identifier, new VisitsParams());
|
2021-02-08 21:46:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function throwsExceptionWhenRequestingVisitsForInvalidTag(): void
|
|
|
|
{
|
|
|
|
$tag = 'foo';
|
2021-03-14 11:59:35 +03:00
|
|
|
$apiKey = ApiKey::create();
|
2021-02-08 21:46:51 +03:00
|
|
|
$repo = $this->prophesize(TagRepository::class);
|
|
|
|
$tagExists = $repo->tagExists($tag, $apiKey)->willReturn(false);
|
|
|
|
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$this->expectException(TagNotFoundException::class);
|
|
|
|
$tagExists->shouldBeCalledOnce();
|
|
|
|
$getRepo->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$this->helper->visitsForTag($tag, new VisitsParams(), $apiKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideAdminApiKeys
|
|
|
|
*/
|
|
|
|
public function visitsForTagAreReturnedAsExpected(?ApiKey $apiKey): void
|
|
|
|
{
|
|
|
|
$tag = 'foo';
|
|
|
|
$repo = $this->prophesize(TagRepository::class);
|
|
|
|
$tagExists = $repo->tagExists($tag, $apiKey)->willReturn(true);
|
|
|
|
$getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$list = map(range(0, 1), fn () => Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance()));
|
|
|
|
$repo2 = $this->prophesize(VisitRepository::class);
|
2021-05-22 21:32:30 +03:00
|
|
|
$repo2->findVisitsByTag($tag, Argument::type(VisitsListFiltering::class))->willReturn($list);
|
2021-05-22 21:16:32 +03:00
|
|
|
$repo2->countVisitsByTag($tag, Argument::type(VisitsCountFiltering::class))->willReturn(1);
|
2021-02-08 21:46:51 +03:00
|
|
|
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$paginator = $this->helper->visitsForTag($tag, new VisitsParams(), $apiKey);
|
|
|
|
|
|
|
|
self::assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentPageResults()));
|
|
|
|
$tagExists->shouldHaveBeenCalledOnce();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2021-02-10 00:40:40 +03:00
|
|
|
|
2022-04-23 12:02:51 +03:00
|
|
|
/** @test */
|
|
|
|
public function throwsExceptionWhenRequestingVisitsForInvalidDomain(): void
|
|
|
|
{
|
|
|
|
$domain = 'foo.com';
|
|
|
|
$apiKey = ApiKey::create();
|
|
|
|
$repo = $this->prophesize(DomainRepository::class);
|
|
|
|
$domainExists = $repo->domainExists($domain, $apiKey)->willReturn(false);
|
|
|
|
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$this->expectException(DomainNotFoundException::class);
|
|
|
|
$domainExists->shouldBeCalledOnce();
|
|
|
|
$getRepo->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$this->helper->visitsForDomain($domain, new VisitsParams(), $apiKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideAdminApiKeys
|
|
|
|
*/
|
|
|
|
public function visitsForNonDefaultDomainAreReturnedAsExpected(?ApiKey $apiKey): void
|
|
|
|
{
|
|
|
|
$domain = 'foo.com';
|
|
|
|
$repo = $this->prophesize(DomainRepository::class);
|
|
|
|
$domainExists = $repo->domainExists($domain, $apiKey)->willReturn(true);
|
|
|
|
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$list = map(range(0, 1), fn () => Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance()));
|
|
|
|
$repo2 = $this->prophesize(VisitRepository::class);
|
|
|
|
$repo2->findVisitsByDomain($domain, Argument::type(VisitsListFiltering::class))->willReturn($list);
|
|
|
|
$repo2->countVisitsByDomain($domain, Argument::type(VisitsCountFiltering::class))->willReturn(1);
|
|
|
|
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$paginator = $this->helper->visitsForDomain($domain, new VisitsParams(), $apiKey);
|
|
|
|
|
|
|
|
self::assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentPageResults()));
|
|
|
|
$domainExists->shouldHaveBeenCalledOnce();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideAdminApiKeys
|
|
|
|
*/
|
|
|
|
public function visitsForDefaultDomainAreReturnedAsExpected(?ApiKey $apiKey): void
|
|
|
|
{
|
|
|
|
$repo = $this->prophesize(DomainRepository::class);
|
|
|
|
$domainExists = $repo->domainExists(Argument::cetera());
|
|
|
|
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$list = map(range(0, 1), fn () => Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance()));
|
|
|
|
$repo2 = $this->prophesize(VisitRepository::class);
|
|
|
|
$repo2->findVisitsByDomain('DEFAULT', Argument::type(VisitsListFiltering::class))->willReturn($list);
|
|
|
|
$repo2->countVisitsByDomain('DEFAULT', Argument::type(VisitsCountFiltering::class))->willReturn(1);
|
|
|
|
$this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
|
|
|
|
|
|
|
|
$paginator = $this->helper->visitsForDomain('DEFAULT', new VisitsParams(), $apiKey);
|
|
|
|
|
|
|
|
self::assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentPageResults()));
|
|
|
|
$domainExists->shouldNotHaveBeenCalled();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:40:40 +03:00
|
|
|
/** @test */
|
|
|
|
public function orphanVisitsAreReturnedAsExpected(): void
|
|
|
|
{
|
|
|
|
$list = map(range(0, 3), fn () => Visit::forBasePath(Visitor::emptyInstance()));
|
|
|
|
$repo = $this->prophesize(VisitRepository::class);
|
2021-05-22 21:16:32 +03:00
|
|
|
$countVisits = $repo->countOrphanVisits(Argument::type(VisitsCountFiltering::class))->willReturn(count($list));
|
2021-05-22 21:32:30 +03:00
|
|
|
$listVisits = $repo->findOrphanVisits(Argument::type(VisitsListFiltering::class))->willReturn($list);
|
2021-02-10 00:40:40 +03:00
|
|
|
$getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$paginator = $this->helper->orphanVisits(new VisitsParams());
|
|
|
|
|
|
|
|
self::assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentPageResults()));
|
|
|
|
$listVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$countVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2022-01-16 14:29:36 +03:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function nonOrphanVisitsAreReturnedAsExpected(): void
|
|
|
|
{
|
|
|
|
$list = map(range(0, 3), fn () => Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance()));
|
|
|
|
$repo = $this->prophesize(VisitRepository::class);
|
|
|
|
$countVisits = $repo->countNonOrphanVisits(Argument::type(VisitsCountFiltering::class))->willReturn(
|
|
|
|
count($list),
|
|
|
|
);
|
|
|
|
$listVisits = $repo->findNonOrphanVisits(Argument::type(VisitsListFiltering::class))->willReturn($list);
|
|
|
|
$getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$paginator = $this->helper->nonOrphanVisits(new VisitsParams());
|
|
|
|
|
|
|
|
self::assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentPageResults()));
|
|
|
|
$listVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$countVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2020-05-01 12:57:46 +03:00
|
|
|
}
|