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