2019-07-13 13:04:21 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-07-13 13:04:21 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2021-04-07 13:53:53 +03:00
|
|
|
use OutOfRangeException;
|
2022-10-22 08:54:57 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-07-13 13:04:21 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-12-28 15:07:11 +03:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2019-07-13 13:04:21 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
2021-02-09 22:25:28 +03:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
|
2021-01-17 13:42:35 +03:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
2021-02-09 22:25:28 +03:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\LocateVisit;
|
2022-09-23 20:03:32 +03: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 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2019-08-11 00:26:39 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
2021-04-07 12:52:50 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
2019-08-10 14:42:37 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
2019-08-10 14:56:06 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
2019-07-13 13:04:21 +03:00
|
|
|
|
2021-02-09 22:25:28 +03:00
|
|
|
class LocateVisitTest extends TestCase
|
2019-07-13 13:04:21 +03:00
|
|
|
{
|
2021-02-09 22:25:28 +03:00
|
|
|
private LocateVisit $locateVisit;
|
2022-10-24 20:53:13 +03:00
|
|
|
private MockObject & IpLocationResolverInterface $ipLocationResolver;
|
|
|
|
private MockObject & EntityManagerInterface $em;
|
|
|
|
private MockObject & LoggerInterface $logger;
|
|
|
|
private MockObject & EventDispatcherInterface $eventDispatcher;
|
|
|
|
private MockObject & DbUpdaterInterface $dbUpdater;
|
2019-07-13 13:04:21 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2019-07-13 13:04:21 +03:00
|
|
|
{
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->ipLocationResolver = $this->createMock(IpLocationResolverInterface::class);
|
|
|
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
|
|
|
$this->dbUpdater = $this->createMock(DbUpdaterInterface::class);
|
2021-04-07 12:52:50 +03:00
|
|
|
|
2021-02-09 22:25:28 +03:00
|
|
|
$this->locateVisit = new LocateVisit(
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->ipLocationResolver,
|
|
|
|
$this->em,
|
|
|
|
$this->logger,
|
|
|
|
$this->dbUpdater,
|
|
|
|
$this->eventDispatcher,
|
2019-07-13 13:04:21 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function invalidVisitLogsWarning(): void
|
|
|
|
{
|
2021-02-09 22:25:28 +03:00
|
|
|
$event = new UrlVisited('123');
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, '123')->willReturn(null);
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->em->expects($this->never())->method('flush');
|
|
|
|
$this->logger->expects($this->once())->method('warning')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
'Tried to locate visit with id "{visitId}", but it does not exist.',
|
|
|
|
['visitId' => 123],
|
2022-10-22 08:54:57 +03:00
|
|
|
);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->eventDispatcher->expects($this->never())->method('dispatch')->with(new VisitLocated('123'));
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->ipLocationResolver->expects($this->never())->method('resolveIpLocation');
|
2019-07-13 13:04:21 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:52:50 +03:00
|
|
|
/** @test */
|
|
|
|
public function nonExistingGeoLiteDbLogsWarning(): void
|
|
|
|
{
|
|
|
|
$event = new UrlVisited('123');
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, '123')->willReturn(
|
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
|
|
|
);
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->em->expects($this->never())->method('flush');
|
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->withAnyParameters()->willReturn(false);
|
|
|
|
$this->logger->expects($this->once())->method('warning')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
'Tried to locate visit with id "{visitId}", but a GeoLite2 db was not found.',
|
|
|
|
['visitId' => 123],
|
2021-04-07 12:52:50 +03:00
|
|
|
);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())->method('dispatch')->with(new VisitLocated('123'));
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->ipLocationResolver->expects($this->never())->method('resolveIpLocation');
|
2021-04-07 12:52:50 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
}
|
|
|
|
|
2019-07-13 16:43:54 +03:00
|
|
|
/** @test */
|
|
|
|
public function invalidAddressLogsWarning(): void
|
|
|
|
{
|
2021-02-09 22:25:28 +03:00
|
|
|
$event = new UrlVisited('123');
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, '123')->willReturn(
|
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
|
|
|
);
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->em->expects($this->never())->method('flush');
|
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->withAnyParameters()->willReturn(true);
|
|
|
|
$this->ipLocationResolver->expects(
|
|
|
|
$this->once(),
|
|
|
|
)->method('resolveIpLocation')->withAnyParameters()->willThrowException(WrongIpException::fromIpAddress(''));
|
|
|
|
$this->logger->expects($this->once())->method('warning')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
'Tried to locate visit with id "{visitId}", but its address seems to be wrong. {e}',
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->isType('array'),
|
2019-07-13 16:43:54 +03:00
|
|
|
);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())->method('dispatch')->with(new VisitLocated('123'));
|
2019-07-13 16:43:54 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
}
|
|
|
|
|
2021-04-07 13:53:53 +03:00
|
|
|
/** @test */
|
|
|
|
public function unhandledExceptionLogsError(): void
|
|
|
|
{
|
|
|
|
$event = new UrlVisited('123');
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, '123')->willReturn(
|
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
|
|
|
|
);
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->em->expects($this->never())->method('flush');
|
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->withAnyParameters()->willReturn(true);
|
|
|
|
$this->ipLocationResolver->expects(
|
|
|
|
$this->once(),
|
|
|
|
)->method('resolveIpLocation')->withAnyParameters()->willThrowException(new OutOfRangeException());
|
|
|
|
$this->logger->expects($this->once())->method('error')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
'An unexpected error occurred while trying to locate visit with id "{visitId}". {e}',
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->isType('array'),
|
2021-04-07 13:53:53 +03:00
|
|
|
);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())->method('dispatch')->with(new VisitLocated('123'));
|
2021-04-07 13:53:53 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
}
|
|
|
|
|
2019-07-13 13:04:21 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideNonLocatableVisits
|
|
|
|
*/
|
|
|
|
public function nonLocatableVisitsResolveToEmptyLocations(Visit $visit): void
|
|
|
|
{
|
2021-02-09 22:25:28 +03:00
|
|
|
$event = new UrlVisited('123');
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, '123')->willReturn($visit);
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->em->expects($this->once())->method('flush');
|
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->withAnyParameters()->willReturn(true);
|
|
|
|
$this->ipLocationResolver->expects($this->never())->method('resolveIpLocation');
|
|
|
|
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())->method('dispatch')->with(new VisitLocated('123'));
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->logger->expects($this->never())->method('warning');
|
2019-07-13 13:04:21 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
2021-04-11 00:24:01 +03:00
|
|
|
self::assertEquals($visit->getVisitLocation(), VisitLocation::fromGeolocation(Location::emptyInstance()));
|
2019-07-13 13:04:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideNonLocatableVisits(): iterable
|
|
|
|
{
|
2021-01-30 16:18:44 +03:00
|
|
|
$shortUrl = ShortUrl::createEmpty();
|
2019-07-13 13:04:21 +03:00
|
|
|
|
2021-02-07 23:31:12 +03: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 13:04:21 +03:00
|
|
|
}
|
|
|
|
|
2020-03-22 13:12:30 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideIpAddresses
|
|
|
|
*/
|
2021-02-12 00:12:38 +03:00
|
|
|
public function locatableVisitsResolveToLocation(Visit $visit, ?string $originalIpAddress): void
|
|
|
|
{
|
2021-02-09 22:25:28 +03:00
|
|
|
$ipAddr = $originalIpAddress ?? $visit->getRemoteAddr();
|
2019-07-13 13:04:21 +03:00
|
|
|
$location = new Location('', '', '', '', 0.0, 0.0, '');
|
2022-08-26 18:58:25 +03:00
|
|
|
$event = UrlVisited::withOriginalIpAddress('123', $originalIpAddress);
|
2019-07-13 13:04:21 +03:00
|
|
|
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, '123')->willReturn($visit);
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->em->expects($this->once())->method('flush');
|
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->withAnyParameters()->willReturn(true);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->ipLocationResolver->expects($this->once())->method('resolveIpLocation')->with($ipAddr)->willReturn(
|
|
|
|
$location,
|
2022-10-22 08:54:57 +03:00
|
|
|
);
|
2022-10-23 19:15:57 +03:00
|
|
|
|
|
|
|
$this->eventDispatcher->expects($this->once())->method('dispatch')->with(new VisitLocated('123'));
|
2022-10-22 08:54:57 +03:00
|
|
|
$this->logger->expects($this->never())->method('warning');
|
2019-07-13 13:04:21 +03:00
|
|
|
|
|
|
|
($this->locateVisit)($event);
|
|
|
|
|
2021-04-11 00:24:01 +03:00
|
|
|
self::assertEquals($visit->getVisitLocation(), VisitLocation::fromGeolocation($location));
|
2019-07-13 13:04:21 +03:00
|
|
|
}
|
2019-07-20 13:11:07 +03:00
|
|
|
|
2020-03-22 13:12:30 +03:00
|
|
|
public function provideIpAddresses(): iterable
|
|
|
|
{
|
2021-02-09 22:25:28 +03: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-12 00:12:38 +03: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 13:12:30 +03:00
|
|
|
}
|
2019-07-13 13:04:21 +03:00
|
|
|
}
|