mirror of
https://github.com/shlinkio/shlink.git
synced 2024-10-23 04:35:34 +03:00
Created chain IP resolver which wrapps multiple resolver to fallback until one is capable of resolving an address
This commit is contained in:
parent
fd6d180eba
commit
0e3a0a1eec
1 changed files with 86 additions and 0 deletions
|
@ -0,0 +1,86 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ShlinkioTest\Shlink\Common\IpGeolocation;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
|
use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
||||||
|
use Shlinkio\Shlink\Common\IpGeolocation\ChainIpLocationResolver;
|
||||||
|
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
|
||||||
|
|
||||||
|
class ChainIpLocationResolverTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ChainIpLocationResolver
|
||||||
|
*/
|
||||||
|
private $resolver;
|
||||||
|
/**
|
||||||
|
* @var ObjectProphecy
|
||||||
|
*/
|
||||||
|
private $firstInnerResolver;
|
||||||
|
/**
|
||||||
|
* @var ObjectProphecy
|
||||||
|
*/
|
||||||
|
private $secondInnerResolver;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->firstInnerResolver = $this->prophesize(IpLocationResolverInterface::class);
|
||||||
|
$this->secondInnerResolver = $this->prophesize(IpLocationResolverInterface::class);
|
||||||
|
|
||||||
|
$this->resolver = new ChainIpLocationResolver(
|
||||||
|
$this->firstInnerResolver->reveal(),
|
||||||
|
$this->secondInnerResolver->reveal()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function throwsExceptionWhenNoInnerResolverCanHandleTheResolution()
|
||||||
|
{
|
||||||
|
$ipAddress = '1.2.3.4';
|
||||||
|
|
||||||
|
$firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class);
|
||||||
|
$secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class);
|
||||||
|
|
||||||
|
$this->expectException(WrongIpException::class);
|
||||||
|
$firstResolve->shouldBeCalledOnce();
|
||||||
|
$secondResolve->shouldBeCalledOnce();
|
||||||
|
|
||||||
|
$this->resolver->resolveIpLocation($ipAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function returnsResultOfFirstInnerResolver()
|
||||||
|
{
|
||||||
|
$ipAddress = '1.2.3.4';
|
||||||
|
|
||||||
|
$firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willReturn([]);
|
||||||
|
$secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class);
|
||||||
|
|
||||||
|
$this->resolver->resolveIpLocation($ipAddress);
|
||||||
|
|
||||||
|
$firstResolve->shouldHaveBeenCalledOnce();
|
||||||
|
$secondResolve->shouldNotHaveBeenCalled();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function returnsResultOfSecondInnerResolver()
|
||||||
|
{
|
||||||
|
$ipAddress = '1.2.3.4';
|
||||||
|
|
||||||
|
$firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class);
|
||||||
|
$secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willReturn([]);
|
||||||
|
|
||||||
|
$this->resolver->resolveIpLocation($ipAddress);
|
||||||
|
|
||||||
|
$firstResolve->shouldHaveBeenCalledOnce();
|
||||||
|
$secondResolve->shouldHaveBeenCalledOnce();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue