2016-07-31 00:01:07 +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-31 00:01:07 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Service;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
2019-12-30 00:27:00 +03:00
|
|
|
use Exception;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-11-17 11:42:15 +03:00
|
|
|
use Prophecy\Argument;
|
2016-07-31 00:01:07 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-10-28 18:20:10 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-07-31 00:01:07 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2018-11-17 09:47:42 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
2018-11-17 11:42:15 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
|
2018-10-28 18:20:10 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2016-07-31 00:01:07 +03:00
|
|
|
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
|
|
|
use Shlinkio\Shlink\Core\Service\VisitService;
|
2019-08-10 14:42:37 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-11-17 11:42:15 +03:00
|
|
|
use function array_shift;
|
|
|
|
use function count;
|
2019-02-23 11:58:02 +03:00
|
|
|
use function floor;
|
2018-11-17 11:42:15 +03:00
|
|
|
use function func_get_args;
|
2019-02-23 11:58:02 +03:00
|
|
|
use function Functional\map;
|
|
|
|
use function range;
|
|
|
|
use function sprintf;
|
2016-07-31 00:01:07 +03:00
|
|
|
|
|
|
|
class VisitServiceTest extends TestCase
|
|
|
|
{
|
2019-12-30 00:48:40 +03:00
|
|
|
private VisitService $visitService;
|
|
|
|
private ObjectProphecy $em;
|
2016-07-31 00:01:07 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-07-31 00:01:07 +03:00
|
|
|
{
|
|
|
|
$this->em = $this->prophesize(EntityManager::class);
|
|
|
|
$this->visitService = new VisitService($this->em->reveal());
|
|
|
|
}
|
|
|
|
|
2019-02-17 14:59:55 +03:00
|
|
|
/** @test */
|
|
|
|
public function locateVisitsIteratesAndLocatesUnlocatedVisits(): void
|
2016-07-31 00:01:07 +03:00
|
|
|
{
|
2019-12-30 01:16:55 +03:00
|
|
|
$unlocatedVisits = map(
|
|
|
|
range(1, 200),
|
2020-01-01 22:48:31 +03:00
|
|
|
fn (int $i) => new Visit(new ShortUrl(sprintf('short_code_%s', $i)), Visitor::emptyInstance()),
|
2019-12-30 01:16:55 +03:00
|
|
|
);
|
2018-11-17 11:42:15 +03:00
|
|
|
|
|
|
|
$repo = $this->prophesize(VisitRepository::class);
|
2019-02-23 11:58:02 +03:00
|
|
|
$findUnlocatedVisits = $repo->findUnlocatedVisits(false)->willReturn($unlocatedVisits);
|
2018-11-17 11:42:15 +03:00
|
|
|
$getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal());
|
|
|
|
|
2020-01-01 22:48:31 +03:00
|
|
|
$persist = $this->em->persist(Argument::type(Visit::class))->will(function (): void {
|
2018-11-17 11:42:15 +03:00
|
|
|
});
|
2020-01-01 22:48:31 +03:00
|
|
|
$flush = $this->em->flush()->will(function (): void {
|
2018-11-17 11:42:15 +03:00
|
|
|
});
|
2020-01-01 22:48:31 +03:00
|
|
|
$clear = $this->em->clear()->will(function (): void {
|
2018-11-17 11:42:15 +03:00
|
|
|
});
|
|
|
|
|
2020-01-01 22:48:31 +03:00
|
|
|
$this->visitService->locateUnlocatedVisits(fn () => Location::emptyInstance(), function (): void {
|
2018-11-17 11:42:15 +03:00
|
|
|
$args = func_get_args();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(VisitLocation::class, array_shift($args));
|
|
|
|
$this->assertInstanceOf(Visit::class, array_shift($args));
|
|
|
|
});
|
|
|
|
|
|
|
|
$findUnlocatedVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
|
|
|
$persist->shouldHaveBeenCalledTimes(count($unlocatedVisits));
|
2019-02-23 11:58:02 +03:00
|
|
|
$flush->shouldHaveBeenCalledTimes(floor(count($unlocatedVisits) / 200) + 1);
|
|
|
|
$clear->shouldHaveBeenCalledTimes(floor(count($unlocatedVisits) / 200) + 1);
|
2016-07-31 00:01:07 +03:00
|
|
|
}
|
|
|
|
|
2019-02-27 00:31:07 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideIsNonLocatableAddress
|
|
|
|
*/
|
|
|
|
public function visitsWhichCannotBeLocatedAreIgnoredOrLocatedAsEmpty(bool $isNonLocatableAddress): void
|
2016-07-31 00:01:07 +03:00
|
|
|
{
|
2018-11-17 11:42:15 +03:00
|
|
|
$unlocatedVisits = [
|
2019-02-22 21:31:03 +03:00
|
|
|
new Visit(new ShortUrl('foo'), Visitor::emptyInstance()),
|
2018-11-17 11:42:15 +03:00
|
|
|
];
|
|
|
|
|
2016-07-31 00:01:07 +03:00
|
|
|
$repo = $this->prophesize(VisitRepository::class);
|
2019-02-23 11:58:02 +03:00
|
|
|
$findUnlocatedVisits = $repo->findUnlocatedVisits(false)->willReturn($unlocatedVisits);
|
2018-11-17 11:42:15 +03:00
|
|
|
$getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal());
|
|
|
|
|
2020-01-01 22:48:31 +03:00
|
|
|
$persist = $this->em->persist(Argument::type(Visit::class))->will(function (): void {
|
2018-11-17 11:42:15 +03:00
|
|
|
});
|
2020-01-01 22:48:31 +03:00
|
|
|
$flush = $this->em->flush()->will(function (): void {
|
2018-11-17 11:42:15 +03:00
|
|
|
});
|
2020-01-01 22:48:31 +03:00
|
|
|
$clear = $this->em->clear()->will(function (): void {
|
2018-11-17 11:42:15 +03:00
|
|
|
});
|
|
|
|
|
2020-01-01 22:48:31 +03:00
|
|
|
$this->visitService->locateUnlocatedVisits(function () use ($isNonLocatableAddress): void {
|
2019-12-30 00:27:00 +03:00
|
|
|
throw $isNonLocatableAddress
|
|
|
|
? new IpCannotBeLocatedException('Cannot be located')
|
|
|
|
: IpCannotBeLocatedException::forError(new Exception(''));
|
2018-11-17 11:42:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
$findUnlocatedVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
2019-02-27 00:31:07 +03:00
|
|
|
$persist->shouldHaveBeenCalledTimes($isNonLocatableAddress ? 1 : 0);
|
2019-02-23 11:58:02 +03:00
|
|
|
$flush->shouldHaveBeenCalledOnce();
|
|
|
|
$clear->shouldHaveBeenCalledOnce();
|
2016-07-31 00:01:07 +03:00
|
|
|
}
|
2019-02-27 00:31:07 +03:00
|
|
|
|
|
|
|
public function provideIsNonLocatableAddress(): iterable
|
|
|
|
{
|
|
|
|
yield 'The address is locatable' => [false];
|
|
|
|
yield 'The address is non-locatable' => [true];
|
|
|
|
}
|
2016-07-31 00:01:07 +03:00
|
|
|
}
|