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