1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\IpGeolocation\Resolver; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
8
|
|
|
use Shlinkio\Shlink\Common\Exception\WrongIpException; |
9
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location; |
10
|
|
|
use Shlinkio\Shlink\IpGeolocation\Resolver\ChainIpLocationResolver; |
11
|
|
|
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface; |
12
|
|
|
|
13
|
|
|
class ChainIpLocationResolverTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var ChainIpLocationResolver */ |
16
|
|
|
private $resolver; |
17
|
|
|
/** @var ObjectProphecy */ |
18
|
|
|
private $firstInnerResolver; |
19
|
|
|
/** @var ObjectProphecy */ |
20
|
|
|
private $secondInnerResolver; |
21
|
|
|
|
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
$this->firstInnerResolver = $this->prophesize(IpLocationResolverInterface::class); |
25
|
|
|
$this->secondInnerResolver = $this->prophesize(IpLocationResolverInterface::class); |
26
|
|
|
|
27
|
|
|
$this->resolver = new ChainIpLocationResolver( |
28
|
|
|
$this->firstInnerResolver->reveal(), |
29
|
|
|
$this->secondInnerResolver->reveal() |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @test */ |
34
|
|
|
public function throwsExceptionWhenNoInnerResolverCanHandleTheResolution() |
35
|
|
|
{ |
36
|
|
|
$ipAddress = '1.2.3.4'; |
37
|
|
|
|
38
|
|
|
$firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class); |
39
|
|
|
$secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class); |
40
|
|
|
|
41
|
|
|
$this->expectException(WrongIpException::class); |
42
|
|
|
$firstResolve->shouldBeCalledOnce(); |
43
|
|
|
$secondResolve->shouldBeCalledOnce(); |
44
|
|
|
|
45
|
|
|
$this->resolver->resolveIpLocation($ipAddress); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** @test */ |
49
|
|
|
public function returnsResultOfFirstInnerResolver(): void |
50
|
|
|
{ |
51
|
|
|
$ipAddress = '1.2.3.4'; |
52
|
|
|
|
53
|
|
|
$firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willReturn(Location::emptyInstance()); |
54
|
|
|
$secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class); |
55
|
|
|
|
56
|
|
|
$this->resolver->resolveIpLocation($ipAddress); |
57
|
|
|
|
58
|
|
|
$firstResolve->shouldHaveBeenCalledOnce(); |
59
|
|
|
$secondResolve->shouldNotHaveBeenCalled(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @test */ |
63
|
|
|
public function returnsResultOfSecondInnerResolver(): void |
64
|
|
|
{ |
65
|
|
|
$ipAddress = '1.2.3.4'; |
66
|
|
|
|
67
|
|
|
$firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class); |
68
|
|
|
$secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willReturn( |
69
|
|
|
Location::emptyInstance() |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$this->resolver->resolveIpLocation($ipAddress); |
73
|
|
|
|
74
|
|
|
$firstResolve->shouldHaveBeenCalledOnce(); |
75
|
|
|
$secondResolve->shouldHaveBeenCalledOnce(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|