2016-07-31 00:01:07 +03:00
|
|
|
<?php
|
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;
|
2018-11-17 11:42:15 +03:00
|
|
|
use function array_shift;
|
|
|
|
use function count;
|
|
|
|
use function func_get_args;
|
2016-07-31 00:01:07 +03:00
|
|
|
|
|
|
|
class VisitServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var VisitService
|
|
|
|
*/
|
|
|
|
protected $visitService;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
protected $em;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->em = $this->prophesize(EntityManager::class);
|
|
|
|
$this->visitService = new VisitService($this->em->reveal());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2018-11-17 11:42:15 +03:00
|
|
|
public function locateVisitsIteratesAndLocatesUnlocatedVisits()
|
2016-07-31 00:01:07 +03:00
|
|
|
{
|
2018-11-17 11:42:15 +03:00
|
|
|
$unlocatedVisits = [
|
|
|
|
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())],
|
|
|
|
[new Visit(new ShortUrl('bar'), Visitor::emptyInstance())],
|
|
|
|
];
|
|
|
|
|
|
|
|
$repo = $this->prophesize(VisitRepository::class);
|
|
|
|
$findUnlocatedVisits = $repo->findUnlocatedVisits()->willReturn($unlocatedVisits);
|
|
|
|
$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 () {
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->visitService->locateVisits(function () {
|
|
|
|
return [];
|
|
|
|
}, 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));
|
|
|
|
$flush->shouldHaveBeenCalledTimes(count($unlocatedVisits));
|
|
|
|
$clear->shouldHaveBeenCalledTimes(count($unlocatedVisits));
|
2016-07-31 00:01:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2018-11-17 11:42:15 +03:00
|
|
|
public function visitsWhichCannotBeLocatedAreIgnored()
|
2016-07-31 00:01:07 +03:00
|
|
|
{
|
2018-11-17 11:42:15 +03:00
|
|
|
$unlocatedVisits = [
|
|
|
|
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())],
|
|
|
|
];
|
|
|
|
|
2016-07-31 00:01:07 +03:00
|
|
|
$repo = $this->prophesize(VisitRepository::class);
|
2018-11-17 11:42:15 +03:00
|
|
|
$findUnlocatedVisits = $repo->findUnlocatedVisits()->willReturn($unlocatedVisits);
|
|
|
|
$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 () {
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->visitService->locateVisits(function () {
|
|
|
|
throw new IpCannotBeLocatedException('Cannot be located');
|
|
|
|
});
|
|
|
|
|
|
|
|
$findUnlocatedVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
|
|
|
$persist->shouldNotHaveBeenCalled();
|
|
|
|
$flush->shouldNotHaveBeenCalled();
|
|
|
|
$clear->shouldNotHaveBeenCalled();
|
2016-07-31 00:01:07 +03:00
|
|
|
}
|
|
|
|
}
|