mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 17:23:12 +08:00
Extracted logic to geolocate a visit, handling possible domain errors
This commit is contained in:
66
module/Core/test/Visit/VisitToLocationHelperTest.php
Normal file
66
module/Core/test/Visit/VisitToLocationHelperTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\Visit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Common\Util\IpAddress;
|
||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
|
||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Visit\VisitToLocationHelper;
|
||||
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
||||
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
||||
|
||||
class VisitToLocationHelperTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private VisitToLocationHelper $helper;
|
||||
private ObjectProphecy $ipLocationResolver;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->ipLocationResolver = $this->prophesize(IpLocationResolverInterface::class);
|
||||
$this->helper = new VisitToLocationHelper($this->ipLocationResolver->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideNonLocatableVisits
|
||||
*/
|
||||
public function throwsExpectedErrorForNonLocatableVisit(
|
||||
Visit $visit,
|
||||
IpCannotBeLocatedException $expectedException,
|
||||
): void {
|
||||
$this->expectExceptionObject($expectedException);
|
||||
$this->ipLocationResolver->resolveIpLocation(Argument::cetera())->shouldNotBeCalled();
|
||||
|
||||
$this->helper->resolveVisitLocation($visit);
|
||||
}
|
||||
|
||||
public function provideNonLocatableVisits(): iterable
|
||||
{
|
||||
yield [Visit::forBasePath(Visitor::emptyInstance()), IpCannotBeLocatedException::forEmptyAddress()];
|
||||
yield [
|
||||
Visit::forBasePath(new Visitor('foo', 'bar', IpAddress::LOCALHOST, '')),
|
||||
IpCannotBeLocatedException::forLocalhost(),
|
||||
];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function throwsGenericErrorWhenResolvingIpFails(): void
|
||||
{
|
||||
$e = new WrongIpException('');
|
||||
|
||||
$this->expectExceptionObject(IpCannotBeLocatedException::forError($e));
|
||||
$this->ipLocationResolver->resolveIpLocation(Argument::cetera())->willThrow($e)
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->helper->resolveVisitLocation(Visit::forBasePath(new Visitor('foo', 'bar', '1.2.3.4', '')));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user