|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maps\Test; |
|
4
|
|
|
|
|
5
|
|
|
use FileFetcher\FileFetcher; |
|
6
|
|
|
use FileFetcher\InMemoryFileFetcher; |
|
7
|
|
|
use Maps\Geocoders\GoogleGeocoder; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers \Maps\Geocoders\GoogleGeocoder |
|
11
|
|
|
* |
|
12
|
|
|
* @licence GNU GPL v2+ |
|
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
14
|
|
|
*/ |
|
15
|
|
|
class GoogleGeocoderTest extends \PHPUnit_Framework_TestCase { |
|
16
|
|
|
|
|
17
|
|
|
const API_KEY = 'TestApiKey'; |
|
18
|
|
|
const NEW_YORK_FETCH_URL = 'https://maps.googleapis.com/maps/api/geocode/json?address=New+York&key=TestApiKey'; |
|
19
|
|
|
|
|
20
|
|
|
public function testHappyPath() { |
|
21
|
|
|
$fileFetcher = new InMemoryFileFetcher( [ |
|
22
|
|
|
self::NEW_YORK_FETCH_URL |
|
23
|
|
|
=> '{ |
|
24
|
|
|
"results" : [ |
|
25
|
|
|
{ |
|
26
|
|
|
"address_components" : [ |
|
27
|
|
|
{ |
|
28
|
|
|
"long_name" : "New York", |
|
29
|
|
|
"short_name" : "New York", |
|
30
|
|
|
"types" : [ "locality", "political" ] |
|
31
|
|
|
}, |
|
32
|
|
|
{ |
|
33
|
|
|
"long_name" : "New York", |
|
34
|
|
|
"short_name" : "NY", |
|
35
|
|
|
"types" : [ "administrative_area_level_1", "political" ] |
|
36
|
|
|
}, |
|
37
|
|
|
{ |
|
38
|
|
|
"long_name" : "United States", |
|
39
|
|
|
"short_name" : "US", |
|
40
|
|
|
"types" : [ "country", "political" ] |
|
41
|
|
|
} |
|
42
|
|
|
], |
|
43
|
|
|
"formatted_address" : "New York, NY, USA", |
|
44
|
|
|
"geometry" : { |
|
45
|
|
|
"bounds" : { |
|
46
|
|
|
"northeast" : { |
|
47
|
|
|
"lat" : 40.9175771, |
|
48
|
|
|
"lng" : -73.70027209999999 |
|
49
|
|
|
}, |
|
50
|
|
|
"southwest" : { |
|
51
|
|
|
"lat" : 40.4773991, |
|
52
|
|
|
"lng" : -74.25908989999999 |
|
53
|
|
|
} |
|
54
|
|
|
}, |
|
55
|
|
|
"location" : { |
|
56
|
|
|
"lat" : 40.7127837, |
|
57
|
|
|
"lng" : -74.0059413 |
|
58
|
|
|
}, |
|
59
|
|
|
"location_type" : "APPROXIMATE", |
|
60
|
|
|
"viewport" : { |
|
61
|
|
|
"northeast" : { |
|
62
|
|
|
"lat" : 40.9175771, |
|
63
|
|
|
"lng" : -73.70027209999999 |
|
64
|
|
|
}, |
|
65
|
|
|
"southwest" : { |
|
66
|
|
|
"lat" : 40.4773991, |
|
67
|
|
|
"lng" : -74.25908989999999 |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
}, |
|
71
|
|
|
"place_id" : "ChIJOwg_06VPwokRYv534QaPC8g", |
|
72
|
|
|
"types" : [ "locality", "political" ] |
|
73
|
|
|
} |
|
74
|
|
|
], |
|
75
|
|
|
"status" : "OK" |
|
76
|
|
|
}' |
|
77
|
|
|
] ); |
|
78
|
|
|
|
|
79
|
|
|
$geocoder = $this->newGeocoder( $fileFetcher ); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertSame( 40.7127837, $geocoder->geocode( 'New York' )->getLatitude() ); |
|
82
|
|
|
$this->assertSame( -74.0059413, $geocoder->geocode( 'New York' )->getLongitude() ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function newGeocoder( FileFetcher $fileFetcher ) { |
|
86
|
|
|
return new GoogleGeocoder( $fileFetcher, self::API_KEY, '' ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testWhenFetcherThrowsException_nullIsReturned() { |
|
90
|
|
|
$geocoder = $this->newGeocoder( new InMemoryFileFetcher( [] ) ); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertNull( $geocoder->geocode( 'New York' ) ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @dataProvider invalidResponseProvider |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testWhenFetcherReturnsInvalidResponse_nullIsReturned( $invalidResponse ) { |
|
99
|
|
|
$geocoder = $this->newGeocoder( new InMemoryFileFetcher( [ |
|
100
|
|
|
self::NEW_YORK_FETCH_URL => $invalidResponse |
|
101
|
|
|
] ) ); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertNull( $geocoder->geocode( 'New York' ) ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function invalidResponseProvider() { |
|
107
|
|
|
yield 'Not JSON' => [ '~=[,,_,,]:3' ]; |
|
108
|
|
|
yield 'Empty JSON' => [ '{}' ]; |
|
109
|
|
|
yield 'Empty results section' => [ '{"results":[]}' ]; |
|
110
|
|
|
yield 'Result without expected keys' => [ '{"results":[ {} ]}' ]; |
|
111
|
|
|
yield 'Location without expected keys' => [ '{"results":[ {"geometry": {"location": {}}} ]}' ]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|