Passed
Pull Request — master (#258)
by Alejandro
04:00
created

IpApiLocationResolverTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Common\IpGeolocation;
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\IpGeolocation\IpApiLocationResolver;
12
use function json_encode;
13
14
class IpApiLocationResolverTest extends TestCase
15
{
16
    /**
17
     * @var IpApiLocationResolver
18
     */
19
    protected $ipResolver;
20
    /**
21
     * @var ObjectProphecy
22
     */
23
    protected $client;
24
25
    public function setUp()
26
    {
27
        $this->client = $this->prophesize(Client::class);
28
        $this->ipResolver = new IpApiLocationResolver($this->client->reveal());
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function correctIpReturnsDecodedInfo()
35
    {
36
        $actual = [
37
            'countryCode' => 'bar',
38
            'lat' => 5,
39
            'lon' => 10,
40
        ];
41
        $expected = [
42
            'country_code' => 'bar',
43
            'country_name' => '',
44
            'region_name' => '',
45
            'city' => '',
46
            'latitude' => 5,
47
            'longitude' => 10,
48
            'time_zone' => '',
49
        ];
50
        $response = new Response();
51
        $response->getBody()->write(json_encode($actual));
52
        $response->getBody()->rewind();
53
54
        $this->client->get('http://ip-api.com/json/1.2.3.4')->willReturn($response)
55
                                                            ->shouldBeCalledOnce();
56
        $this->assertEquals($expected, $this->ipResolver->resolveIpLocation('1.2.3.4'));
57
    }
58
59
    /**
60
     * @test
61
     * @expectedException \Shlinkio\Shlink\Common\Exception\WrongIpException
62
     */
63
    public function guzzleExceptionThrowsShlinkException()
64
    {
65
        $this->client->get('http://ip-api.com/json/1.2.3.4')->willThrow(new TransferException())
66
                                                            ->shouldBeCalledOnce();
67
        $this->ipResolver->resolveIpLocation('1.2.3.4');
68
    }
69
}
70