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;
|
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
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var VisitService */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $visitService;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $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-02-23 11:58:02 +03:00
|
|
|
$unlocatedVisits = map(range(1, 200), function (int $i) {
|
|
|
|
return new Visit(new ShortUrl(sprintf('short_code_%s', $i)), Visitor::emptyInstance());
|
|
|
|
});
|
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());
|
|
|
|
|
|
|
|
$persist = $this->em->persist(Argument::type(Visit::class))->will(function () {
|
|
|
|
});
|
|
|
|
$flush = $this->em->flush()->will(function () {
|
|
|
|
});
|
|
|
|
$clear = $this->em->clear()->will(function () {
|
|
|
|
});
|
|
|
|
|
2019-02-17 15:21:07 +03:00
|
|
|
$this->visitService->locateUnlocatedVisits(function () {
|
2019-02-17 14:59:55 +03:00
|
|
|
return Location::emptyInstance();
|
2018-11-17 11:42:15 +03:00
|
|
|
}, function () {
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$persist = $this->em->persist(Argument::type(Visit::class))->will(function () {
|
|
|
|
});
|
|
|
|
$flush = $this->em->flush()->will(function () {
|
|
|
|
});
|
|
|
|
$clear = $this->em->clear()->will(function () {
|
|
|
|
});
|
|
|
|
|
2019-02-27 00:31:07 +03:00
|
|
|
$this->visitService->locateUnlocatedVisits(function () use ($isNonLocatableAddress) {
|
|
|
|
throw new IpCannotBeLocatedException($isNonLocatableAddress, 'Cannot be located');
|
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
|
|
|
}
|