|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maps\Test; |
|
4
|
|
|
|
|
5
|
|
|
use FileFetcher\FileFetcher; |
|
6
|
|
|
use FileFetcher\InMemoryFileFetcher; |
|
7
|
|
|
use Maps\Geocoders\GeoNamesGeocoder; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers \Maps\Geocoders\GeoNamesGeocoder |
|
11
|
|
|
* |
|
12
|
|
|
* @licence GNU GPL v2+ |
|
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
14
|
|
|
*/ |
|
15
|
|
|
class GeoNamesGeocoderTest extends \PHPUnit_Framework_TestCase { |
|
16
|
|
|
|
|
17
|
|
|
const USER_NAME = 'TestUserName'; |
|
18
|
|
|
const NEW_YORK_FETCH_URL = 'http://api.geonames.org/search?q=New+York&maxRows=1&username=TestUserName'; |
|
19
|
|
|
|
|
20
|
|
|
public function testHappyPath() { |
|
21
|
|
|
$fileFetcher = new InMemoryFileFetcher( [ |
|
22
|
|
|
self::NEW_YORK_FETCH_URL |
|
23
|
|
|
=> '<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
|
24
|
|
|
<geonames style="MEDIUM"> |
|
25
|
|
|
<totalResultsCount>82194</totalResultsCount> |
|
26
|
|
|
<geoname> |
|
27
|
|
|
<toponymName>New York City</toponymName> |
|
28
|
|
|
<name>New York</name> |
|
29
|
|
|
<lat>40.71427</lat> |
|
30
|
|
|
<lng>-74.00597</lng> |
|
31
|
|
|
<geonameId>5128581</geonameId> |
|
32
|
|
|
<countryCode>US</countryCode> |
|
33
|
|
|
<countryName>United States</countryName> |
|
34
|
|
|
<fcl>P</fcl> |
|
35
|
|
|
<fcode>PPL</fcode> |
|
36
|
|
|
</geoname> |
|
37
|
|
|
</geonames>' |
|
38
|
|
|
] ); |
|
39
|
|
|
|
|
40
|
|
|
$geocoder = $this->newGeocoder( $fileFetcher ); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertSame( 40.71427, $geocoder->geocode( 'New York' )->getLatitude() ); |
|
43
|
|
|
$this->assertSame( -74.00597, $geocoder->geocode( 'New York' )->getLongitude() ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function newGeocoder( FileFetcher $fileFetcher ) { |
|
47
|
|
|
return new GeoNamesGeocoder( $fileFetcher, self::USER_NAME ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testWhenFetcherThrowsException_nullIsReturned() { |
|
51
|
|
|
$geocoder = $this->newGeocoder( new InMemoryFileFetcher( [] ) ); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertNull( $geocoder->geocode( 'New York' ) ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @dataProvider invalidResponseProvider |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testWhenFetcherReturnsInvalidResponse_nullIsReturned( $invalidResponse ) { |
|
60
|
|
|
$geocoder = $this->newGeocoder( new InMemoryFileFetcher( [ |
|
61
|
|
|
self::NEW_YORK_FETCH_URL => $invalidResponse |
|
62
|
|
|
] ) ); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertNull( $geocoder->geocode( 'New York' ) ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function invalidResponseProvider() { |
|
68
|
|
|
yield 'Not XML' => [ '~=[,,_,,]:3' ]; |
|
69
|
|
|
|
|
70
|
|
|
yield 'Missing latitude key' => [ '<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
|
71
|
|
|
<geonames style="MEDIUM"> |
|
72
|
|
|
<totalResultsCount>82194</totalResultsCount> |
|
73
|
|
|
<geoname> |
|
74
|
|
|
<toponymName>New York City</toponymName> |
|
75
|
|
|
<name>New York</name> |
|
76
|
|
|
<lng>-74.00597</lng> |
|
77
|
|
|
<geonameId>5128581</geonameId> |
|
78
|
|
|
<countryCode>US</countryCode> |
|
79
|
|
|
<countryName>United States</countryName> |
|
80
|
|
|
<fcl>P</fcl> |
|
81
|
|
|
<fcode>PPL</fcode> |
|
82
|
|
|
</geoname> |
|
83
|
|
|
</geonames>' ]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|