2022-09-18 18:44:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-09-23 14:50:26 +02:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Visit\Geolocation;
|
2022-09-18 18:44:01 +02:00
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 21:24:30 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2022-09-18 18:44:01 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
|
|
|
|
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
|
2022-09-23 19:03:32 +02:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
2022-09-23 14:50:26 +02:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Geolocation\VisitToLocationHelper;
|
2022-09-23 18:05:17 +02:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2022-09-18 18:44:01 +02:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
|
|
|
|
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
|
|
|
|
|
|
|
|
|
class VisitToLocationHelperTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
private VisitToLocationHelper $helper;
|
2022-10-24 19:59:03 +02:00
|
|
|
private MockObject & IpLocationResolverInterface $ipLocationResolver;
|
2022-09-18 18:44:01 +02:00
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
2022-10-23 21:24:30 +02:00
|
|
|
$this->ipLocationResolver = $this->createMock(IpLocationResolverInterface::class);
|
|
|
|
|
$this->helper = new VisitToLocationHelper($this->ipLocationResolver);
|
2022-09-18 18:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test, DataProvider('provideNonLocatableVisits')]
|
2022-09-18 18:44:01 +02:00
|
|
|
public function throwsExpectedErrorForNonLocatableVisit(
|
|
|
|
|
Visit $visit,
|
|
|
|
|
IpCannotBeLocatedException $expectedException,
|
|
|
|
|
): void {
|
|
|
|
|
$this->expectExceptionObject($expectedException);
|
2022-10-23 21:24:30 +02:00
|
|
|
$this->ipLocationResolver->expects($this->never())->method('resolveIpLocation');
|
2022-09-18 18:44:01 +02:00
|
|
|
|
|
|
|
|
$this->helper->resolveVisitLocation($visit);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 09:32:38 +01:00
|
|
|
public static function provideNonLocatableVisits(): iterable
|
2022-09-18 18:44:01 +02:00
|
|
|
{
|
2024-11-11 08:51:55 +01:00
|
|
|
yield [Visit::forBasePath(Visitor::empty()), IpCannotBeLocatedException::forEmptyAddress()];
|
2022-09-18 18:44:01 +02:00
|
|
|
yield [
|
2024-11-14 14:48:18 +01:00
|
|
|
Visit::forBasePath(Visitor::fromParams('foo', 'bar', IpAddress::LOCALHOST)),
|
2022-09-18 18:44:01 +02:00
|
|
|
IpCannotBeLocatedException::forLocalhost(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2022-09-18 18:44:01 +02:00
|
|
|
public function throwsGenericErrorWhenResolvingIpFails(): void
|
|
|
|
|
{
|
|
|
|
|
$e = new WrongIpException('');
|
|
|
|
|
|
|
|
|
|
$this->expectExceptionObject(IpCannotBeLocatedException::forError($e));
|
2022-10-23 21:24:30 +02:00
|
|
|
$this->ipLocationResolver->expects($this->once())->method('resolveIpLocation')->willThrowException($e);
|
2022-09-18 18:44:01 +02:00
|
|
|
|
2024-11-14 14:48:18 +01:00
|
|
|
$this->helper->resolveVisitLocation(Visit::forBasePath(Visitor::fromParams('foo', 'bar', '1.2.3.4')));
|
2022-09-18 18:44:01 +02:00
|
|
|
}
|
|
|
|
|
}
|