Completed
Push — master ( 3e7e88...cdbc34 )
by Tobias
04:55
created

GeoIP2AdapterTest::setUpBeforeClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Provider\GeoIP2\Tests;
14
15
use Geocoder\Provider\GeoIP2\GeoIP2Adapter;
16
use RuntimeException;
17
18
/**
19
 * @author Jens Wiese <[email protected]>
20
 */
21
class GeoIP2AdapterTest extends \PHPUnit\Framework\TestCase
22
{
23
    /**
24
     * @var GeoIP2Adapter
25
     */
26
    protected $adapter;
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @throws RuntimeException
32
     */
33
    public static function setUpBeforeClass()
34
    {
35
        if (false === class_exists('\GeoIp2\Database\Reader')) {
36
            throw new RuntimeException("The maxmind's lib 'geoip2/geoip2' is required to run this test.");
37
        }
38
    }
39
40
    public function setUp()
41
    {
42
        $this->adapter = new GeoIP2Adapter($this->getGeoIP2ProviderMock());
43
    }
44
45
    public function testGetName()
46
    {
47
        $expectedName = 'maxmind_geoip2';
48
        $this->assertEquals($expectedName, $this->adapter->getName());
49
    }
50
51
    /**
52
     * @expectedException \Geocoder\Exception\InvalidArgument
53
     * @expectedExceptionMessage must be called with a valid url. Got "127.0.0.1" instead.
54
     */
55
    public function testGetContentMustBeCalledWithUrl()
56
    {
57
        $url = '127.0.0.1';
58
        $this->adapter->getContent($url);
59
    }
60
61
    /**
62
     * @expectedException \Geocoder\Exception\InvalidArgument
63
     * @expectedExceptionMessage URL must contain a valid query-string (an IP address, 127.0.0.1 for instance)
64
     */
65
    public function testAddressPassedToReaderMustBeIpAddress()
66
    {
67
        $url = 'file://database?not-valid=1';
68
        $this->adapter->getContent($url);
69
    }
70
71
    public static function provideDataForSwitchingRequestMethods()
72
    {
73
        return [
74
            [GeoIP2Adapter::GEOIP2_MODEL_CITY],
75
            [GeoIP2Adapter::GEOIP2_MODEL_COUNTRY],
76
        ];
77
    }
78
79
    /**
80
     * @dataProvider provideDataForSwitchingRequestMethods
81
     */
82
    public function testIpAddressIsPassedCorrectToReader($geoIp2Model)
83
    {
84
        $geoIp2Provider = $this->getGeoIP2ProviderMock();
85
        $geoIp2Provider
86
            ->expects($this->any())
87
            ->method($geoIp2Model)
88
            ->with('127.0.0.1')
89
            ->will($this->returnValue(
90
                $this->getGeoIP2ModelMock($geoIp2Model)
91
            ));
92
93
        $adapter = new GeoIP2Adapter($geoIp2Provider, $geoIp2Model);
94
        $adapter->getContent('file://geoip?127.0.0.1');
95
    }
96
97
    /**
98
     * @expectedException \Geocoder\Exception\UnsupportedOperation
99
     * @expectedExceptionMessage Model "unsupported_model" is not available.
100
     */
101
    public function testNotSupportedGeoIP2ModelLeadsToException()
102
    {
103
        new GeoIP2Adapter($this->getGeoIP2ProviderMock(), 'unsupported_model');
104
    }
105
106
    public function testReaderResponseIsJsonEncoded()
107
    {
108
        $cityModel = $this->getGeoIP2ModelMock(GeoIP2Adapter::GEOIP2_MODEL_CITY);
109
110
        $geoIp2Provider = $this->getGeoIP2ProviderMock();
111
        $geoIp2Provider
112
            ->expects($this->any())
113
            ->method('city')
114
            ->will($this->returnValue($cityModel));
115
116
        $adapter = new GeoIP2Adapter($geoIp2Provider);
117
118
        $result = $adapter->getContent('file://database?127.0.0.1');
119
        $this->assertJson($result);
120
121
        $decodedResult = json_decode($result);
122
        $this->assertObjectHasAttribute('city', $decodedResult);
123
    }
124
125
    /**
126
     * @return \PHPUnit_Framework_MockObject_MockObject
127
     */
128
    protected function getGeoIP2ProviderMock()
129
    {
130
        $mock = $this->getMockBuilder('\GeoIp2\ProviderInterface')->getMock();
131
132
        return $mock;
133
    }
134
135
    /**
136
     * @param int $geoIP2Model (e.g. GeoIP2Adapter::GEOIP2_MODEL_CITY, ...)
137
     *
138
     * @return \PHPUnit_Framework_MockObject_MockObject
139
     */
140
    protected function getGeoIP2ModelMock($geoIP2Model)
141
    {
142
        $mockClass = '\\GeoIp2\\Model\\'.ucfirst($geoIP2Model);
143
144
        $mock = $this->getMockBuilder($mockClass)->disableOriginalConstructor()->getMock();
145
        $mock
146
            ->expects($this->any())
147
            ->method('jsonSerialize')
148
            ->will($this->returnValue(
149
                [
150
                    'city' => [
151
                        'geoname_id' => 2911298,
152
                        'names' => [
153
                              'de' => 'Hamburg',
154
                              'en' => 'Hamburg',
155
                              'es' => 'Hamburgo',
156
                              'fr' => 'Hambourg',
157
                              'ja' => 'ハンブルク',
158
                              'pt-BR' => 'Hamburgo',
159
                              'ru' => 'Гамбург',
160
                              'zh-CN' => '汉堡市',
161
                        ],
162
                    ],
163
                ]
164
            ));
165
166
        return $mock;
167
    }
168
}
169