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