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

provideReaderExceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
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 GeoIp2\Database\Reader;
7
use GeoIp2\Exception\AddressNotFoundException;
8
use GeoIp2\Model\City;
9
use MaxMind\Db\Reader\InvalidDatabaseException;
10
use PHPUnit\Framework\TestCase;
11
use Prophecy\Prophecy\ObjectProphecy;
12
use Shlinkio\Shlink\Common\Exception\WrongIpException;
13
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2LocationResolver;
14
15
class GeoLite2LocationResolverTest extends TestCase
16
{
17
    /**
18
     * @var GeoLite2LocationResolver
19
     */
20
    private $resolver;
21
    /**
22
     * @var ObjectProphecy
23
     */
24
    private $reader;
25
26
    public function setUp()
27
    {
28
        $this->reader = $this->prophesize(Reader::class);
29
        $this->resolver = new GeoLite2LocationResolver($this->reader->reveal());
30
    }
31
32
    /**
33
     * @test
34
     * @dataProvider provideReaderExceptions
35
     */
36
    public function exceptionIsThrownIfReaderThrowsException(string $e, string $message)
37
    {
38
        $ipAddress = '1.2.3.4';
39
40
        $cityMethod = $this->reader->city($ipAddress)->willThrow($e);
41
42
        $this->expectException(WrongIpException::class);
43
        $this->expectExceptionMessage($message);
44
        $cityMethod->shouldBeCalledOnce();
45
46
        $this->resolver->resolveIpLocation($ipAddress);
47
    }
48
49
    public function provideReaderExceptions(): array
50
    {
51
        return [
52
            [AddressNotFoundException::class, 'Provided IP "1.2.3.4" is invalid'],
53
            [InvalidDatabaseException::class, 'Provided GeoLite2 db file is invalid'],
54
        ];
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function resolvedCityIsProperlyMapped()
61
    {
62
        $ipAddress = '1.2.3.4';
63
        $city = new City([]);
64
65
        $cityMethod = $this->reader->city($ipAddress)->willReturn($city);
66
67
        $result = $this->resolver->resolveIpLocation($ipAddress);
68
69
        $this->assertEquals([
70
            'country_code' => '',
71
            'country_name' => '',
72
            'region_name' => '',
73
            'city' => '',
74
            'latitude' => '',
75
            'longitude' => '',
76
            'time_zone' => '',
77
        ], $result);
78
        $cityMethod->shouldHaveBeenCalledOnce();
79
    }
80
}
81