|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Service; |
|
5
|
|
|
|
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\Exception\TransferException; |
|
8
|
|
|
use GuzzleHttp\Psr7\Response; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
11
|
|
|
use Shlinkio\Shlink\Common\Service\IpApiLocationResolver; |
|
12
|
|
|
|
|
13
|
|
|
class IpApiLocationResolverTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var IpApiLocationResolver |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $ipResolver; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ObjectProphecy |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $client; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->client = $this->prophesize(Client::class); |
|
27
|
|
|
$this->ipResolver = new IpApiLocationResolver($this->client->reveal()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @test |
|
32
|
|
|
*/ |
|
33
|
|
|
public function correctIpReturnsDecodedInfo() |
|
34
|
|
|
{ |
|
35
|
|
|
$actual = [ |
|
36
|
|
|
'countryCode' => 'bar', |
|
37
|
|
|
'lat' => 5, |
|
38
|
|
|
'lon' => 10, |
|
39
|
|
|
]; |
|
40
|
|
|
$expected = [ |
|
41
|
|
|
'country_code' => 'bar', |
|
42
|
|
|
'country_name' => '', |
|
43
|
|
|
'region_name' => '', |
|
44
|
|
|
'city' => '', |
|
45
|
|
|
'latitude' => 5, |
|
46
|
|
|
'longitude' => 10, |
|
47
|
|
|
'time_zone' => '', |
|
48
|
|
|
]; |
|
49
|
|
|
$response = new Response(); |
|
50
|
|
|
$response->getBody()->write(\json_encode($actual)); |
|
51
|
|
|
$response->getBody()->rewind(); |
|
52
|
|
|
|
|
53
|
|
|
$this->client->get('http://ip-api.com/json/1.2.3.4')->willReturn($response) |
|
54
|
|
|
->shouldBeCalledTimes(1); |
|
55
|
|
|
$this->assertEquals($expected, $this->ipResolver->resolveIpLocation('1.2.3.4')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @test |
|
60
|
|
|
* @expectedException \Shlinkio\Shlink\Common\Exception\WrongIpException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function guzzleExceptionThrowsShlinkException() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->client->get('http://ip-api.com/json/1.2.3.4')->willThrow(new TransferException()) |
|
65
|
|
|
->shouldBeCalledTimes(1); |
|
66
|
|
|
$this->ipResolver->resolveIpLocation('1.2.3.4'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @test |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getApiIntervalReturnsExpectedValue() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->assertEquals(65, $this->ipResolver->getApiInterval()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @test |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getApiLimitReturnsExpectedValue() |
|
81
|
|
|
{ |
|
82
|
|
|
$this->assertEquals(145, $this->ipResolver->getApiLimit()); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|