2019-07-13 05:04:21 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2019-07-13 05:04:21 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2021-04-07 05:53:53 -05:00
|
|
|
use OutOfRangeException;
|
2019-07-13 05:04:21 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2019-07-13 05:04:21 -05:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-12-28 06:07:11 -06:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2019-07-13 05:04:21 -05:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
2021-02-09 13:25:28 -06:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
|
2021-01-17 04:42:35 -06:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
2021-02-09 13:25:28 -06:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\LocateVisit;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2019-08-10 16:26:39 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
2021-04-07 04:52:50 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
2019-08-10 06:42:37 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
2019-08-10 06:56:06 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
2019-07-13 05:04:21 -05:00
|
|
|
|
2021-02-09 13:25:28 -06:00
|
|
|
class LocateVisitTest extends TestCase
|
2019-07-13 05:04:21 -05:00
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2021-02-09 13:25:28 -06:00
|
|
|
private LocateVisit $locateVisit;
|
2019-12-29 15:48:40 -06:00
|
|
|
private ObjectProphecy $ipLocationResolver;
|
|
|
|
private ObjectProphecy $em;
|
|
|
|
private ObjectProphecy $logger;
|
|
|
|
private ObjectProphecy $dbUpdater;
|
|
|
|
private ObjectProphecy $eventDispatcher;
|
2019-07-13 05:04:21 -05:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2019-07-13 05:04:21 -05:00
|
|
|
{
|
|
|
|
$this->ipLocationResolver = $this->prophesize(IpLocationResolverInterface::class);
|
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
$this->logger = $this->prophesize(LoggerInterface::class);
|
2019-12-28 06:07:11 -06:00
|
|
|
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
|
2019-07-13 05:04:21 -05:00
|
|
|
|
2021-04-07 04:52:50 -05:00
|
|
|
$this->dbUpdater = $this->prophesize(DbUpdaterInterface::class);
|
|
|
|
$this->dbUpdater->databaseFileExists()->willReturn(true);
|
|
|
|
|
2021-02-09 13:25:28 -06:00
|
|
|
$this->locateVisit = new LocateVisit(
|
2019-07-13 05:04:21 -05:00
|
|
|
$this->ipLocationResolver->reveal(),
|
|
|
|
$this->em->reveal(),
|
2019-07-20 05:11:07 -05:00
|
|
|
$this->logger->reveal(),
|
2019-12-28 06:07:11 -06:00
|
|
|
$this->dbUpdater->reveal(),
|
2020-01-01 13:48:31 -06:00
|
|
|
$this->eventDispatcher->reveal(),
|
2019-07-13 05:04:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function invalidVisitLogsWarning(): void
|
|
|
|
{
|
2021-02-09 13:25:28 -06:00
|
|
|
$event = new UrlVisited('123');
|
2019-07-13 05:04:21 -05:00
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn(null);
|
2019-12-27 09:15:14 -06:00
|
|
|
$logWarning = $this->logger->warning('Tried to locate visit with id "{visitId}", but it does not exist.', [
|
|
|
|
'visitId' => 123,
|
|
|
|
]);
|
2020-01-01 13:48:31 -06:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 06:07:11 -06:00
|
|
|
});
|
2019-07-13 05:04:21 -05:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
2019-11-20 13:03:06 -06:00
|
|
|
$this->em->flush()->shouldNotHaveBeenCalled();
|
2019-07-13 05:04:21 -05:00
|
|
|
$this->ipLocationResolver->resolveIpLocation(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
|
|
$logWarning->shouldHaveBeenCalled();
|
2019-12-28 06:07:11 -06:00
|
|
|
$dispatch->shouldNotHaveBeenCalled();
|
2019-07-13 05:04:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-07 04:52:50 -05:00
|
|
|
/** @test */
|
|
|
|
public function nonExistingGeoLiteDbLogsWarning(): void
|
|
|
|
{
|
|
|
|
$event = new UrlVisited('123');
|
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn(
|
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
|
|
|
);
|
|
|
|
$dbExists = $this->dbUpdater->databaseFileExists()->willReturn(false);
|
|
|
|
$logWarning = $this->logger->warning(
|
|
|
|
'Tried to locate visit with id "{visitId}", but a GeoLite2 db was not found.',
|
|
|
|
['visitId' => 123],
|
|
|
|
);
|
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
|
|
|
});
|
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$dbExists->shouldHaveBeenCalledOnce();
|
|
|
|
$this->em->flush()->shouldNotHaveBeenCalled();
|
|
|
|
$this->ipLocationResolver->resolveIpLocation(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
|
|
$logWarning->shouldHaveBeenCalled();
|
2021-04-08 07:09:26 -05:00
|
|
|
$dispatch->shouldHaveBeenCalledOnce();
|
2021-04-07 04:52:50 -05:00
|
|
|
}
|
|
|
|
|
2019-07-13 08:43:54 -05:00
|
|
|
/** @test */
|
|
|
|
public function invalidAddressLogsWarning(): void
|
|
|
|
{
|
2021-02-09 13:25:28 -06:00
|
|
|
$event = new UrlVisited('123');
|
2019-07-13 08:43:54 -05:00
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn(
|
2021-02-07 14:31:12 -06:00
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
2019-07-13 08:43:54 -05:00
|
|
|
);
|
|
|
|
$resolveLocation = $this->ipLocationResolver->resolveIpLocation(Argument::cetera())->willThrow(
|
2020-01-01 13:48:31 -06:00
|
|
|
WrongIpException::class,
|
2019-07-13 08:43:54 -05:00
|
|
|
);
|
|
|
|
$logWarning = $this->logger->warning(
|
2021-04-07 05:53:53 -05:00
|
|
|
'Tried to locate visit with id "{visitId}", but its address seems to be wrong. {e}',
|
2020-01-01 13:48:31 -06:00
|
|
|
Argument::type('array'),
|
2019-07-13 08:43:54 -05:00
|
|
|
);
|
2020-01-01 13:48:31 -06:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 06:07:11 -06:00
|
|
|
});
|
2019-07-13 08:43:54 -05:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveLocation->shouldHaveBeenCalledOnce();
|
|
|
|
$logWarning->shouldHaveBeenCalled();
|
2019-11-20 13:03:06 -06:00
|
|
|
$this->em->flush()->shouldNotHaveBeenCalled();
|
2019-12-28 06:07:11 -06:00
|
|
|
$dispatch->shouldHaveBeenCalledOnce();
|
2019-07-13 08:43:54 -05:00
|
|
|
}
|
|
|
|
|
2021-04-07 05:53:53 -05:00
|
|
|
/** @test */
|
|
|
|
public function unhandledExceptionLogsError(): void
|
|
|
|
{
|
|
|
|
$event = new UrlVisited('123');
|
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn(
|
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
|
|
|
);
|
|
|
|
$resolveLocation = $this->ipLocationResolver->resolveIpLocation(Argument::cetera())->willThrow(
|
|
|
|
OutOfRangeException::class,
|
|
|
|
);
|
|
|
|
$logError = $this->logger->error(
|
|
|
|
'An unexpected error occurred while trying to locate visit with id "{visitId}". {e}',
|
|
|
|
Argument::type('array'),
|
|
|
|
);
|
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
|
|
|
});
|
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveLocation->shouldHaveBeenCalledOnce();
|
|
|
|
$logError->shouldHaveBeenCalled();
|
|
|
|
$this->em->flush()->shouldNotHaveBeenCalled();
|
|
|
|
$dispatch->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
2019-07-13 05:04:21 -05:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideNonLocatableVisits
|
|
|
|
*/
|
|
|
|
public function nonLocatableVisitsResolveToEmptyLocations(Visit $visit): void
|
|
|
|
{
|
2021-02-09 13:25:28 -06:00
|
|
|
$event = new UrlVisited('123');
|
2019-07-13 05:04:21 -05:00
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
2020-01-01 13:48:31 -06:00
|
|
|
$flush = $this->em->flush()->will(function (): void {
|
2019-07-13 05:04:21 -05:00
|
|
|
});
|
|
|
|
$resolveIp = $this->ipLocationResolver->resolveIpLocation(Argument::any());
|
2020-01-01 13:48:31 -06:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 06:07:11 -06:00
|
|
|
});
|
2019-07-13 05:04:21 -05:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
2021-04-10 16:24:01 -05:00
|
|
|
self::assertEquals($visit->getVisitLocation(), VisitLocation::fromGeolocation(Location::emptyInstance()));
|
2019-07-13 05:04:21 -05:00
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$flush->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIp->shouldNotHaveBeenCalled();
|
|
|
|
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
|
2019-12-28 06:07:11 -06:00
|
|
|
$dispatch->shouldHaveBeenCalledOnce();
|
2019-07-13 05:04:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideNonLocatableVisits(): iterable
|
|
|
|
{
|
2021-01-30 07:18:44 -06:00
|
|
|
$shortUrl = ShortUrl::createEmpty();
|
2019-07-13 05:04:21 -05:00
|
|
|
|
2021-02-07 14:31:12 -06:00
|
|
|
yield 'null IP' => [Visit::forValidShortUrl($shortUrl, new Visitor('', '', null, ''))];
|
|
|
|
yield 'empty IP' => [Visit::forValidShortUrl($shortUrl, new Visitor('', '', '', ''))];
|
|
|
|
yield 'localhost' => [Visit::forValidShortUrl($shortUrl, new Visitor('', '', IpAddress::LOCALHOST, ''))];
|
2019-07-13 05:04:21 -05:00
|
|
|
}
|
|
|
|
|
2020-03-22 05:12:30 -05:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideIpAddresses
|
|
|
|
*/
|
2021-02-11 15:12:38 -06:00
|
|
|
public function locatableVisitsResolveToLocation(Visit $visit, ?string $originalIpAddress): void
|
|
|
|
{
|
2021-02-09 13:25:28 -06:00
|
|
|
$ipAddr = $originalIpAddress ?? $visit->getRemoteAddr();
|
2019-07-13 05:04:21 -05:00
|
|
|
$location = new Location('', '', '', '', 0.0, 0.0, '');
|
2022-08-26 10:58:25 -05:00
|
|
|
$event = UrlVisited::withOriginalIpAddress('123', $originalIpAddress);
|
2019-07-13 05:04:21 -05:00
|
|
|
|
|
|
|
$findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
2020-01-01 13:48:31 -06:00
|
|
|
$flush = $this->em->flush()->will(function (): void {
|
2019-07-13 05:04:21 -05:00
|
|
|
});
|
|
|
|
$resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
|
2020-01-01 13:48:31 -06:00
|
|
|
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
2019-12-28 06:07:11 -06:00
|
|
|
});
|
2019-07-13 05:04:21 -05:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
2021-04-10 16:24:01 -05:00
|
|
|
self::assertEquals($visit->getVisitLocation(), VisitLocation::fromGeolocation($location));
|
2019-07-13 05:04:21 -05:00
|
|
|
$findVisit->shouldHaveBeenCalledOnce();
|
|
|
|
$flush->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIp->shouldHaveBeenCalledOnce();
|
|
|
|
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
|
2021-02-11 15:12:38 -06:00
|
|
|
$dispatch->shouldHaveBeenCalledOnce();
|
2019-07-13 05:04:21 -05:00
|
|
|
}
|
2019-07-20 05:11:07 -05:00
|
|
|
|
2020-03-22 05:12:30 -05:00
|
|
|
public function provideIpAddresses(): iterable
|
|
|
|
{
|
2021-02-09 13:25:28 -06:00
|
|
|
yield 'no original IP address' => [
|
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
|
|
|
null,
|
|
|
|
];
|
|
|
|
yield 'original IP address' => [
|
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
|
|
|
'1.2.3.4',
|
|
|
|
];
|
2021-02-11 15:12:38 -06:00
|
|
|
yield 'base url' => [Visit::forBasePath(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4'];
|
|
|
|
yield 'invalid short url' => [Visit::forInvalidShortUrl(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4'];
|
|
|
|
yield 'regular not found' => [Visit::forRegularNotFound(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4'];
|
2020-03-22 05:12:30 -05:00
|
|
|
}
|
2019-07-13 05:04:21 -05:00
|
|
|
}
|