Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
22 | class MaxMindTest extends BaseTestCase |
||
23 | { |
||
24 | protected function getCacheDir() |
||
25 | { |
||
26 | return __DIR__.'/.cached_responses'; |
||
27 | } |
||
28 | |||
29 | public function testGetName() |
||
30 | { |
||
31 | $provider = new MaxMind($this->getMockedHttpClient(), 'api_key'); |
||
32 | $this->assertEquals('maxmind', $provider->getName()); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
37 | * @expectedExceptionMessage The MaxMind provider does not support street addresses, only IP addresses. |
||
38 | */ |
||
39 | public function testGeocodeWithAddress() |
||
40 | { |
||
41 | $provider = new MaxMind($this->getMockedHttpClient(), 'api_key'); |
||
42 | $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
||
43 | } |
||
44 | |||
45 | public function testGeocodeWithLocalhostIPv4() |
||
46 | { |
||
47 | $provider = new MaxMind($this->getMockedHttpClient(), 'api_key'); |
||
48 | $results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
||
49 | |||
50 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
51 | $this->assertCount(1, $results); |
||
52 | |||
53 | /** @var Location $result */ |
||
54 | $result = $results->first(); |
||
55 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
56 | $this->assertEquals('localhost', $result->getLocality()); |
||
57 | $this->assertEquals('localhost', $result->getCountry()->getName()); |
||
58 | } |
||
59 | |||
60 | public function testGeocodeWithLocalhostIPv6() |
||
61 | { |
||
62 | $provider = new MaxMind($this->getMockedHttpClient(), 'api_key'); |
||
63 | $results = $provider->geocodeQuery(GeocodeQuery::create('::1')); |
||
64 | |||
65 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
66 | $this->assertCount(1, $results); |
||
67 | |||
68 | /** @var Location $result */ |
||
69 | $result = $results->first(); |
||
70 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
71 | $this->assertEquals('localhost', $result->getLocality()); |
||
72 | $this->assertEquals('localhost', $result->getCountry()->getName()); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
77 | * @expectedExceptionMessage Unknown MaxMind service foo |
||
78 | */ |
||
79 | public function testGeocodeWithRealIPv4AndNotSupportedService() |
||
80 | { |
||
81 | $provider = new MaxMind($this->getMockedHttpClient(), 'api_key', 'foo'); |
||
82 | $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
87 | * @expectedExceptionMessage Unknown MaxMind service 12345 |
||
88 | */ |
||
89 | public function testGeocodeWithRealIPv6AndNotSupportedService() |
||
90 | { |
||
91 | $provider = new MaxMind($this->getMockedHttpClient(), 'api_key', '12345'); |
||
92 | $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59')); |
||
93 | } |
||
94 | |||
95 | public function testGeocodeWithRealIPv4GetsFakeContentFormattedEmpty() |
||
96 | { |
||
97 | $provider = new MaxMind($this->getMockedHttpClient(',,,,,,,,,'), 'api_key'); |
||
98 | $results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
99 | |||
100 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
101 | $this->assertCount(1, $results); |
||
102 | |||
103 | /** @var Location $result */ |
||
104 | $result = $results->first(); |
||
105 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
106 | $this->assertNull($result->getCoordinates()); |
||
107 | |||
108 | $this->assertNull($result->getStreetNumber()); |
||
109 | $this->assertNull($result->getStreetName()); |
||
110 | $this->assertNull($result->getPostalCode()); |
||
111 | $this->assertNull($result->getLocality()); |
||
112 | $this->assertNull($result->getSubLocality()); |
||
113 | $this->assertEmpty($result->getAdminLevels()); |
||
114 | $this->assertNull($result->getCountry()->getName()); |
||
115 | $this->assertNull($result->getCountry()->getCode()); |
||
116 | $this->assertNull($result->getTimezone()); |
||
117 | } |
||
118 | |||
119 | public function testGeocodeWithRealIPv4GetsFakeContent() |
||
120 | { |
||
121 | $provider = new MaxMind($this->getMockedHttpClient( |
||
122 | 'US,TX,Plano,75093,33.034698486328,-96.813400268555,,,,'), 'api_key'); |
||
123 | $results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
124 | |||
125 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
126 | $this->assertCount(1, $results); |
||
127 | |||
128 | /** @var Location $result */ |
||
129 | $result = $results->first(); |
||
130 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
131 | $this->assertEquals(33.034698486328, $result->getCoordinates()->getLatitude(), '', 0.0001); |
||
132 | $this->assertEquals(-96.813400268555, $result->getCoordinates()->getLongitude(), '', 0.0001); |
||
133 | $this->assertNull($result->getStreetNumber()); |
||
134 | $this->assertNull($result->getStreetName()); |
||
135 | $this->assertEquals(75093, $result->getPostalCode()); |
||
136 | $this->assertEquals('Plano', $result->getLocality()); |
||
137 | $this->assertNull($result->getSubLocality()); |
||
138 | $this->assertCount(1, $result->getAdminLevels()); |
||
139 | $this->assertNull($result->getAdminLevels()->get(1)->getName()); |
||
140 | $this->assertEquals('TX', $result->getAdminLevels()->get(1)->getCode()); |
||
141 | $this->assertEquals('United States', $result->getCountry()->getName()); |
||
142 | $this->assertEquals('US', $result->getCountry()->getCode()); |
||
143 | $this->assertNull($result->getTimezone()); |
||
144 | |||
145 | $provider2 = new MaxMind($this->getMockedHttpClient('FR,,,,,,,,,'), 'api_key'); |
||
146 | $result2 = $provider2->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
147 | $this->assertEquals('France', $result2->first()->getCountry()->getName()); |
||
148 | |||
149 | $provider3 = new MaxMind($this->getMockedHttpClient('GB,,,,,,,,,'), 'api_key'); |
||
150 | $result3 = $provider3->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
151 | $this->assertEquals('United Kingdom', $result3->first()->getCountry()->getName()); |
||
152 | |||
153 | $provider4 = new MaxMind($this->getMockedHttpClient( |
||
154 | 'US,CA,San Francisco,94110,37.748402,-122.415604,807,415,"Layered Technologies","Automattic"'), 'api_key'); |
||
155 | $results = $provider4->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
156 | |||
157 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
158 | $this->assertCount(1, $results); |
||
159 | |||
160 | /** @var Location $result */ |
||
161 | $result = $results->first(); |
||
162 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
163 | $this->assertEquals(37.748402, $result->getCoordinates()->getLatitude(), '', 0.0001); |
||
164 | $this->assertEquals(-122.415604, $result->getCoordinates()->getLongitude(), '', 0.0001); |
||
165 | $this->assertNull($result->getStreetNumber()); |
||
166 | $this->assertNull($result->getStreetName()); |
||
167 | $this->assertEquals(94110, $result->getPostalCode()); |
||
168 | $this->assertEquals('San Francisco', $result->getLocality()); |
||
169 | $this->assertNull($result->getSubLocality()); |
||
170 | $this->assertCount(1, $result->getAdminLevels()); |
||
171 | $this->assertNull($result->getAdminLevels()->get(1)->getName()); |
||
172 | $this->assertEquals('CA', $result->getAdminLevels()->get(1)->getCode()); |
||
173 | $this->assertEquals('United States', $result->getCountry()->getName()); |
||
174 | $this->assertEquals('US', $result->getCountry()->getCode()); |
||
175 | $this->assertNull($result->getTimezone()); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @expectedException \Geocoder\Exception\InvalidCredentials |
||
180 | * @expectedExceptionMessage API Key provided is not valid. |
||
181 | */ |
||
182 | public function testGeocodeWithRealIPv4AndInvalidApiKeyGetsFakeContent() |
||
183 | { |
||
184 | $provider = new MaxMind($this->getMockedHttpClient(',,,,,,,,,,INVALID_LICENSE_KEY'), 'api_key'); |
||
185 | $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @expectedException \Geocoder\Exception\InvalidCredentials |
||
190 | * @expectedExceptionMessage API Key provided is not valid. |
||
191 | */ |
||
192 | public function testGeocodeOmniServiceWithRealIPv6AndInvalidApiKeyGetsFakeContent() |
||
193 | { |
||
194 | $provider = new MaxMind($this->getMockedHttpClient(',,,,,,,,,,,,,,,,,,,,,,,,INVALID_LICENSE_KEY'), |
||
195 | 'api_key', MaxMind::OMNI_SERVICE); |
||
196 | $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59')); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @expectedException \Geocoder\Exception\InvalidCredentials |
||
201 | * @expectedExceptionMessage API Key provided is not valid. |
||
202 | */ |
||
203 | public function testGeocodeWithRealIPv4AndInvalidApiKey() |
||
204 | { |
||
205 | $provider = new MaxMind($this->getHttpClient(), 'api_key'); |
||
206 | $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @expectedException \Geocoder\Exception\InvalidCredentials |
||
211 | * @expectedExceptionMessage API Key provided is not valid. |
||
212 | */ |
||
213 | public function testGeocodeWithRealIPv4AndInvalidApiKeyGetsFakeContent2() |
||
214 | { |
||
215 | $provider = new MaxMind($this->getMockedHttpClient(',,,,,,,,,,LICENSE_REQUIRED'), 'api_key'); |
||
216 | $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @expectedException \Geocoder\Exception\InvalidCredentials |
||
221 | * @expectedExceptionMessage API Key provided is not valid. |
||
222 | */ |
||
223 | public function testGeocodeOmniServiceWithRealIPv6AndInvalidApiKeyGetsFakeContent2() |
||
224 | { |
||
225 | $provider = new MaxMind($this->getMockedHttpClient(',,,,,,,,,,,,,,,,,,,,,,,INVALID_LICENSE_KEY'), |
||
226 | 'api_key', MaxMind::OMNI_SERVICE); |
||
227 | $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59')); |
||
228 | } |
||
229 | |||
230 | public function testGeocodeWithRealIPv4GetsFakeContentWithIpNotFound() |
||
231 | { |
||
232 | $provider = new MaxMind($this->getMockedHttpClient(',,,,,,,,,,IP_NOT_FOUND'), 'api_key'); |
||
233 | $result = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
234 | |||
235 | $this->assertInstanceOf(Collection::class, $result); |
||
236 | $this->assertEquals(0, $result->count()); |
||
237 | } |
||
238 | |||
239 | public function testGeocodeOmniServiceWithRealIPv6GetsFakeContentWithIpNotFound() |
||
240 | { |
||
241 | $provider = new MaxMind($this->getMockedHttpClient(',,,,,,,,,,,,,,,,,,,,,,,IP_NOT_FOUND'), |
||
242 | 'api_key', MaxMind::OMNI_SERVICE); |
||
243 | $result = $provider->geocodeQuery(GeocodeQuery::create('::fff:74.200.247.59')); |
||
244 | |||
245 | $this->assertInstanceOf(Collection::class, $result); |
||
246 | $this->assertEquals(0, $result->count()); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @expectedException \Geocoder\Exception\InvalidServerResponse |
||
251 | */ |
||
252 | public function testGeocodeGetsFakeContentWithInvalidData() |
||
253 | { |
||
254 | $provider = new MaxMind($this->getMockedHttpClient(',,,,,,,,,,'), 'api_key'); |
||
255 | $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
256 | } |
||
257 | |||
258 | public function testGeocodeServiceWithRealIPv4() |
||
259 | { |
||
260 | if (!isset($_SERVER['MAXMIND_API_KEY'])) { |
||
261 | $this->markTestSkipped('You need to configure the MAXMIND_API_KEY value in phpunit.xml'); |
||
262 | } |
||
263 | |||
264 | $provider = new MaxMind($this->getHttpClient($_SERVER['MAXMIND_API_KEY']), $_SERVER['MAXMIND_API_KEY']); |
||
265 | $results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.159')); |
||
266 | |||
267 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
268 | $this->assertCount(1, $results); |
||
269 | |||
270 | /** @var Location $result */ |
||
271 | $result = $results->first(); |
||
272 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
273 | $this->assertEquals(33.034698, $result->getCoordinates()->getLatitude(), '', 0.1); |
||
274 | $this->assertEquals(-96.813400, $result->getCoordinates()->getLongitude(), '', 0.1); |
||
275 | $this->assertNull($result->getBounds()); |
||
276 | $this->assertNull($result->getStreetNumber()); |
||
277 | $this->assertNull($result->getStreetName()); |
||
278 | $this->assertEquals(75093, $result->getPostalCode()); |
||
279 | $this->assertEquals('Plano', $result->getLocality()); |
||
280 | $this->assertNull($result->getSubLocality()); |
||
281 | $this->assertCount(1, $result->getAdminLevels()); |
||
282 | $this->assertNull($result->getAdminLevels()->get(1)->getName()); |
||
283 | $this->assertEquals('TX', $result->getAdminLevels()->get(1)->getCode()); |
||
284 | $this->assertEquals('United States', $result->getCountry()->getName()); |
||
285 | $this->assertEquals('US', $result->getCountry()->getCode()); |
||
286 | $this->assertNull($result->getTimezone()); |
||
287 | } |
||
288 | |||
289 | public function testGeocodeOmniServiceWithRealIPv4() |
||
290 | { |
||
291 | if (!isset($_SERVER['MAXMIND_API_KEY'])) { |
||
292 | $this->markTestSkipped('You need to configure the MAXMIND_API_KEY value in phpunit.xml'); |
||
293 | } |
||
294 | |||
295 | $provider = new MaxMind($this->getHttpClient($_SERVER['MAXMIND_API_KEY']), $_SERVER['MAXMIND_API_KEY'], |
||
296 | MaxMind::OMNI_SERVICE); |
||
297 | $results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.159')); |
||
298 | |||
299 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
300 | $this->assertCount(1, $results); |
||
301 | |||
302 | /** @var Location $result */ |
||
303 | $result = $results->first(); |
||
304 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
305 | $this->assertEquals(33.0347, $result->getCoordinates()->getLatitude(), '', 0.1); |
||
306 | $this->assertEquals(-96.8134, $result->getCoordinates()->getLongitude(), '', 0.1); |
||
307 | $this->assertNull($result->getBounds()); |
||
308 | $this->assertNull($result->getStreetNumber()); |
||
309 | $this->assertNull($result->getStreetName()); |
||
310 | $this->assertEquals(75093, $result->getPostalCode()); |
||
311 | $this->assertEquals('Plano', $result->getLocality()); |
||
312 | $this->assertNull($result->getSubLocality()); |
||
313 | $this->assertCount(1, $result->getAdminLevels()); |
||
314 | $this->assertEquals('Texas', $result->getAdminLevels()->get(1)->getName()); |
||
315 | $this->assertEquals('TX', $result->getAdminLevels()->get(1)->getCode()); |
||
316 | $this->assertEquals('United States', $result->getCountry()->getName()); |
||
317 | $this->assertEquals('US', $result->getCountry()->getCode()); |
||
318 | $this->assertEquals('America/Chicago', $result->getTimezone()); |
||
319 | } |
||
320 | |||
321 | public function testGeocodeOmniServiceWithRealIPv4WithSslAndEncoding() |
||
322 | { |
||
323 | if (!isset($_SERVER['MAXMIND_API_KEY'])) { |
||
324 | $this->markTestSkipped('You need to configure the MAXMIND_API_KEY value in phpunit.xml'); |
||
325 | } |
||
326 | |||
327 | $provider = new MaxMind($this->getHttpClient($_SERVER['MAXMIND_API_KEY']), $_SERVER['MAXMIND_API_KEY'], |
||
328 | MaxMind::OMNI_SERVICE); |
||
329 | $results = $provider->geocodeQuery(GeocodeQuery::create('189.26.128.80')); |
||
330 | |||
331 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
332 | $this->assertCount(1, $results); |
||
333 | |||
334 | /** @var Location $result */ |
||
335 | $result = $results->first(); |
||
336 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
337 | $this->assertEquals(-27.5833, $result->getCoordinates()->getLatitude(), '', 0.1); |
||
338 | $this->assertEquals(-48.5666, $result->getCoordinates()->getLongitude(), '', 0.1); |
||
339 | $this->assertNull($result->getBounds()); |
||
340 | $this->assertNull($result->getStreetNumber()); |
||
341 | $this->assertNull($result->getStreetName()); |
||
342 | $this->assertNull($result->getPostalCode()); |
||
343 | $this->assertEquals('Florianópolis', $result->getLocality()); |
||
344 | $this->assertNull($result->getSubLocality()); |
||
345 | $this->assertCount(1, $result->getAdminLevels()); |
||
346 | $this->assertEquals('Santa Catarina', $result->getAdminLevels()->get(1)->getName()); |
||
347 | $this->assertEquals('26', $result->getAdminLevels()->get(1)->getCode()); |
||
348 | $this->assertEquals('Brazil', $result->getCountry()->getName()); |
||
349 | $this->assertEquals('BR', $result->getCountry()->getCode()); |
||
350 | $this->assertEquals('America/Sao_Paulo', $result->getTimezone()); |
||
351 | } |
||
352 | |||
353 | public function testGeocodeOmniServiceWithRealIPv6WithSsl() |
||
354 | { |
||
355 | if (!isset($_SERVER['MAXMIND_API_KEY'])) { |
||
356 | $this->markTestSkipped('You need to configure the MAXMIND_API_KEY value in phpunit.xml'); |
||
357 | } |
||
358 | |||
359 | $provider = new MaxMind($this->getHttpClient($_SERVER['MAXMIND_API_KEY']), $_SERVER['MAXMIND_API_KEY'], |
||
360 | MaxMind::OMNI_SERVICE); |
||
361 | $results = $provider->geocodeQuery(GeocodeQuery::create('2002:4293:f4d6:0:0:0:0:0')); |
||
362 | |||
363 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
364 | $this->assertCount(1, $results); |
||
365 | |||
366 | /** @var Location $result */ |
||
367 | $result = $results->first(); |
||
368 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
369 | $this->assertEquals(40.2181, $result->getCoordinates()->getLatitude(), '', 0.1); |
||
370 | $this->assertEquals(-111.6133, $result->getCoordinates()->getLongitude(), '', 0.1); |
||
371 | $this->assertNull($result->getBounds()); |
||
372 | $this->assertNull($result->getStreetNumber()); |
||
373 | $this->assertNull($result->getStreetName()); |
||
374 | $this->assertEquals(84606, $result->getPostalCode()); |
||
375 | $this->assertEquals('Provo', $result->getLocality()); |
||
376 | $this->assertNull($result->getSubLocality()); |
||
377 | $this->assertCount(1, $result->getAdminLevels()); |
||
378 | $this->assertEquals('Utah', $result->getAdminLevels()->get(1)->getName()); |
||
379 | $this->assertEquals('UT', $result->getAdminLevels()->get(1)->getCode()); |
||
380 | $this->assertEquals('United States', $result->getCountry()->getName()); |
||
381 | $this->assertEquals('US', $result->getCountry()->getCode()); |
||
382 | $this->assertEquals('America/Denver', $result->getTimezone()); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
387 | * @expectedExceptionMessage The MaxMind provider is not able to do reverse geocoding. |
||
388 | */ |
||
389 | public function testReverse() |
||
390 | { |
||
391 | $provider = new MaxMind($this->getMockedHttpClient(), 'api_key'); |
||
392 | $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2)); |
||
393 | } |
||
394 | } |
||
395 |