forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoIP2ProviderTest.php
More file actions
184 lines (160 loc) · 6.86 KB
/
Copy pathGeoIP2ProviderTest.php
File metadata and controls
184 lines (160 loc) · 6.86 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* 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\Tests\Provider;
use Geocoder\Exception\NoResultException;
use Geocoder\HttpAdapter\CurlHttpAdapter;
use Geocoder\Provider\GeoIP2Provider;
use Geocoder\Tests\TestCase;
/**
* @author Jens Wiese <jens@howtrueisfalse.de>
*/
class GeoIP2ProviderTest extends TestCase
{
/**
* @var GeoIP2Provider
*/
protected $provider;
public function setUp()
{
$this->provider = new GeoIP2Provider($this->getGeoIP2AdapterMock());
}
/**
* @expectedException \Geocoder\Exception\InvalidArgumentException
* @expectedExceptionMessage GeoIP2Adapter is needed in order to access the GeoIP2 service.
*/
public function testWrongAdapterLeadsToException()
{
new GeoIP2Provider(new CurlHttpAdapter());
}
public function testGetName()
{
$expectedName = 'maxmind_geoip2';
$this->assertEquals($expectedName, $this->provider->getName());
}
/**
* @expectedException \Geocoder\Exception\UnsupportedException
* @expectedExceptionMessage The Geocoder\Provider\GeoIP2Provider is not able to do reverse geocoding.
*/
public function testQueryingReversedDataLeadToException()
{
$this->provider->getReversedData(array(50, 9));
}
public function testLocalhostDefaults()
{
$expectedResult = array(
'city' => 'localhost',
'region' => 'localhost',
'county' => 'localhost',
'country' => 'localhost',
);
$actualResult = $this->provider->getGeocodedData('127.0.0.1');
$this->assertSame($expectedResult, $actualResult);
}
/**
* @expectedException \Geocoder\Exception\UnsupportedException
* @expectedExceptionMessage The Geocoder\Provider\GeoIP2Provider does not support street addresses.
*/
public function testOnlyIpAddressesCouldBeResolved()
{
$this->provider->getGeocodedData('Street 123, Somewhere');
}
/**
* Provides data for getGeocodedData test
*
* @return array
*/
public static function provideDataForRetrievingGeodata()
{
$testdata = array(
'Response with all possible data' => array(
'74.200.247.59',
'{"city":{"geoname_id":2911298,"names":{"de":"Hamburg","en":"Hamburg","es":"Hamburgo","fr":"Hambourg","ja":"\u30cf\u30f3\u30d6\u30eb\u30af","pt-BR":"Hamburgo","ru":"\u0413\u0430\u043c\u0431\u0443\u0440\u0433","zh-CN":"\u6c49\u5821\u5e02"}},"continent":{"code":"EU","geoname_id":6255148,"names":{"de":"Europa","en":"Europe","es":"Europa","fr":"Europe","ja":"\u30e8\u30fc\u30ed\u30c3\u30d1","pt-BR":"Europa","ru":"\u0415\u0432\u0440\u043e\u043f\u0430","zh-CN":"\u6b27\u6d32"}},"country":{"geoname_id":2921044,"iso_code":"DE","names":{"de":"Deutschland","en":"Germany","es":"Alemania","fr":"Allemagne","ja":"\u30c9\u30a4\u30c4\u9023\u90a6\u5171\u548c\u56fd","pt-BR":"Alemanha","ru":"\u0413\u0435\u0440\u043c\u0430\u043d\u0438\u044f","zh-CN":"\u5fb7\u56fd"}},"location":{"latitude":53.55,"longitude":10,"time_zone":"Europe\/Berlin"},"registered_country":{"geoname_id":2921044,"iso_code":"DE","names":{"de":"Deutschland","en":"Germany","es":"Alemania","fr":"Allemagne","ja":"\u30c9\u30a4\u30c4\u9023\u90a6\u5171\u548c\u56fd","pt-BR":"Alemanha","ru":"\u0413\u0435\u0440\u043c\u0430\u043d\u0438\u044f","zh-CN":"\u5fb7\u56fd"}},"subdivisions":[{"geoname_id":2911297,"iso_code":"HH","names":{"de":"Hamburg","en":"Hamburg","es":"Hamburgo","fr":"Hambourg"}}],"traits":{"ip_address":"74.200.247.59"}}',
array(
'latitude' => 53.55,
'longitude' => 10,
'bounds' => null,
'streetNumber' => null,
'streetName' => null,
'city' => 'Hamburg',
'zipcode' => null,
'cityDistrict' => null,
'county' => null,
'countyCode' => null,
'region' => 'Hamburg',
'regionCode' => 'HH',
'country' => 'Germany',
'countryCode' => 'DE',
'timezone' => null,
)
),
'Response with all data null' => array(
'74.200.247.59',
'{}',
array(
'latitude' => null,
'longitude' => null,
'bounds' => null,
'streetNumber' => null,
'streetName' => null,
'city' => null,
'zipcode' => null,
'cityDistrict' => null,
'county' => null,
'countyCode' => null,
'region' => null,
'regionCode' => null,
'country' => null,
'countryCode' => null,
'timezone' => null,
)
)
);
return $testdata;
}
/**
* @dataProvider provideDataForRetrievingGeodata
* @param string $address
* @param $adapterResponse
* @param $expectedGeodata
*/
public function testRetrievingGeodata($address, $adapterResponse, $expectedGeodata)
{
$adapter = $this->getGeoIP2AdapterMock($adapterResponse);
$provider = new GeoIP2Provider($adapter);
$actualGeodata = $provider->getGeocodedData($address);
$this->assertSame($expectedGeodata, $actualGeodata[0]);
}
/**
* @expectedException \Geocoder\Exception\NoResultException
* @expectedExceptionMessage No results found for IP address 74.200.247.59
*/
public function testRetrievingGeodataNotExistingLocation()
{
$adapterReturn = new NoResultException('No results found for IP address 74.200.247.59');
$adapter = $this->getGeoIP2AdapterMock($adapterReturn);
$provider = new GeoIP2Provider($adapter);
$provider->getGeocodedData('74.200.247.59');
}
/**
* @param mixed $returnValue
* @return \PHPUnit_Framework_MockObject_MockObject | GeoIP2DatabaseAdapter
*/
public function getGeoIP2AdapterMock($returnValue = '')
{
$mock = $this->getMockBuilder('\Geocoder\HttpAdapter\GeoIP2Adapter')->disableOriginalConstructor()->getMock();
if ($returnValue instanceof \Exception) {
$returnValue = $this->throwException($returnValue);
} else {
$returnValue = $this->returnValue($returnValue);
}
$mock->expects($this->any())->method('setLocale')->will($this->returnSelf());
$mock->expects($this->any())->method('getContent')->will($returnValue);
return $mock;
}
}