2018-11-11 20:36:56 +03:00
|
|
|
<?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;
|
2019-02-17 14:59:55 +03:00
|
|
|
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
|
2018-11-11 20:36:56 +03:00
|
|
|
|
|
|
|
class ChainIpLocationResolverTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ChainIpLocationResolver */
|
2018-11-11 20:36:56 +03:00
|
|
|
private $resolver;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-11 20:36:56 +03:00
|
|
|
private $firstInnerResolver;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-11 20:36:56 +03:00
|
|
|
private $secondInnerResolver;
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-11-11 20:36:56 +03:00
|
|
|
{
|
|
|
|
$this->firstInnerResolver = $this->prophesize(IpLocationResolverInterface::class);
|
|
|
|
$this->secondInnerResolver = $this->prophesize(IpLocationResolverInterface::class);
|
|
|
|
|
|
|
|
$this->resolver = new ChainIpLocationResolver(
|
|
|
|
$this->firstInnerResolver->reveal(),
|
|
|
|
$this->secondInnerResolver->reveal()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-11-11 20:36:56 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-17 14:59:55 +03:00
|
|
|
/** @test */
|
|
|
|
public function returnsResultOfFirstInnerResolver(): void
|
2018-11-11 20:36:56 +03:00
|
|
|
{
|
|
|
|
$ipAddress = '1.2.3.4';
|
|
|
|
|
2019-02-17 14:59:55 +03:00
|
|
|
$firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willReturn(Location::emptyInstance());
|
2018-11-11 20:36:56 +03:00
|
|
|
$secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class);
|
|
|
|
|
|
|
|
$this->resolver->resolveIpLocation($ipAddress);
|
|
|
|
|
|
|
|
$firstResolve->shouldHaveBeenCalledOnce();
|
|
|
|
$secondResolve->shouldNotHaveBeenCalled();
|
|
|
|
}
|
|
|
|
|
2019-02-17 14:59:55 +03:00
|
|
|
/** @test */
|
|
|
|
public function returnsResultOfSecondInnerResolver(): void
|
2018-11-11 20:36:56 +03:00
|
|
|
{
|
|
|
|
$ipAddress = '1.2.3.4';
|
|
|
|
|
|
|
|
$firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class);
|
2019-02-17 14:59:55 +03:00
|
|
|
$secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willReturn(
|
|
|
|
Location::emptyInstance()
|
|
|
|
);
|
2018-11-11 20:36:56 +03:00
|
|
|
|
|
|
|
$this->resolver->resolveIpLocation($ipAddress);
|
|
|
|
|
|
|
|
$firstResolve->shouldHaveBeenCalledOnce();
|
|
|
|
$secondResolve->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
}
|