Files

135 lines
4.7 KiB
PHP
Raw Permalink Normal View History

2024-11-15 10:17:09 +01:00
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Geolocation\Middleware;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Shlinkio\Shlink\Core\Config\Options\TrackingOptions;
use Shlinkio\Shlink\Core\Geolocation\Middleware\GeolocationMiddleware;
2026-11-02 14:00:46 +01:00
use Shlinkio\Shlink\Geolocation\Exception\LocationResolutionException;
use Shlinkio\Shlink\Geolocation\Model\Location;
use Shlinkio\Shlink\Geolocation\Resolver\LocationResolverInterface;
2024-11-15 10:17:09 +01:00
use Throwable;
2026-11-02 14:00:46 +01:00
use const Shlinkio\Shlink\Geolocation\IP_ADDRESS_REQUEST_ATTRIBUTE;
class GeolocationMiddlewareTest extends TestCase
2024-11-15 10:17:09 +01:00
{
2026-11-02 14:00:46 +01:00
private MockObject&LocationResolverInterface $ipLocationResolver;
private MockObject&LoggerInterface $logger;
private MockObject&RequestHandlerInterface $handler;
2024-11-15 10:17:09 +01:00
protected function setUp(): void
{
2026-11-02 14:00:46 +01:00
$this->ipLocationResolver = $this->createMock(LocationResolverInterface::class);
2024-11-15 10:17:09 +01:00
$this->logger = $this->createMock(LoggerInterface::class);
$this->handler = $this->createMock(RequestHandlerInterface::class);
}
#[Test]
public function geolocationIsSkippedIfTrackingIsDisabled(): void
{
2026-11-02 14:00:46 +01:00
$this->ipLocationResolver->expects($this->never())->method('resolveRequestLocation');
2024-11-15 10:17:09 +01:00
$this->logger->expects($this->never())->method('warning');
$request = ServerRequestFactory::fromGlobals();
$this->handler->expects($this->once())->method('handle')->with($request);
$this->middleware(disableTracking: true)->process($request, $this->handler);
}
#[Test]
public function locationIsResolvedFromIpAddress(): void
{
2026-11-02 14:00:46 +01:00
$request = ServerRequestFactory::fromGlobals()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '1.2.3.4');
$this->ipLocationResolver
->expects($this->once())
2026-11-02 14:00:46 +01:00
->method('resolveRequestLocation')
->with($request)
->willReturn(new Location(countryCode: 'ES'));
2024-11-15 10:17:09 +01:00
$this->logger->expects($this->never())->method('warning');
$this->handler
->expects($this->once())
->method('handle')
->with($this->callback(
static function (ServerRequestInterface $req): bool {
$location = $req->getAttribute(Location::class);
if (!$location instanceof Location) {
return false;
}
Assert::assertEquals('ES', $location->countryCode);
return true;
},
));
2024-11-15 10:17:09 +01:00
$this->middleware()->process($request, $this->handler);
}
/**
* @param non-empty-string $loggerMethod
*/
2024-11-15 10:17:09 +01:00
#[Test]
#[TestWith([
2026-11-02 14:00:46 +01:00
new LocationResolutionException(),
2024-11-15 10:17:09 +01:00
'warning',
'Tried to geolocate visitor, but it was not possible. {e}',
2024-11-15 10:17:09 +01:00
])]
#[TestWith([
new RuntimeException('Unknown'),
'error',
'An unexpected error occurred while trying to locate IP address. {e}',
])]
public function warningIsPrintedIfAnErrorOccurs(
Throwable $exception,
string $loggerMethod,
string $expectedLoggedMessage,
): void {
2026-11-02 14:00:46 +01:00
$request = ServerRequestFactory::fromGlobals()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '1.2.3.4');
2024-11-15 10:17:09 +01:00
$this->ipLocationResolver
->expects($this->once())
2026-11-02 14:00:46 +01:00
->method('resolveRequestLocation')
->with($request)
2024-11-15 10:17:09 +01:00
->willThrowException($exception);
$this->logger->expects($this->once())->method($loggerMethod)->with($expectedLoggedMessage, ['e' => $exception]);
$this->handler
->expects($this->once())
->method('handle')
->with($this->callback(
static function (ServerRequestInterface $req): bool {
$location = $req->getAttribute(Location::class);
if (!$location instanceof Location) {
return false;
}
Assert::assertEmpty($location->countryCode);
return true;
},
));
2024-11-15 10:17:09 +01:00
$this->middleware()->process($request, $this->handler);
}
private function middleware(bool $disableTracking = false): GeolocationMiddleware
2024-11-15 10:17:09 +01:00
{
return new GeolocationMiddleware(
2024-11-15 10:17:09 +01:00
$this->ipLocationResolver,
$this->logger,
new TrackingOptions(disableTracking: $disableTracking),
);
}
}