|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Geocoder package. |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license MIT License |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Geocoder\Provider\GoogleMapsPlaces\Tests; |
|
14
|
|
|
|
|
15
|
|
|
use Geocoder\Exception\InvalidArgument; |
|
16
|
|
|
use Geocoder\Exception\QuotaExceeded; |
|
17
|
|
|
use Geocoder\Exception\UnsupportedOperation; |
|
18
|
|
|
use Geocoder\IntegrationTest\BaseTestCase; |
|
19
|
|
|
use Geocoder\Provider\GoogleMapsPlaces\GoogleMapsPlaces; |
|
20
|
|
|
use Geocoder\Provider\GoogleMapsPlaces\Model\GooglePlace; |
|
21
|
|
|
use Geocoder\Provider\GoogleMapsPlaces\Model\OpeningHours; |
|
22
|
|
|
use Geocoder\Provider\GoogleMapsPlaces\Model\Photo; |
|
23
|
|
|
use Geocoder\Provider\GoogleMapsPlaces\Model\PlusCode; |
|
24
|
|
|
use Geocoder\Query\GeocodeQuery; |
|
25
|
|
|
use Geocoder\Query\ReverseQuery; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author atymic <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class GoogleMapsPlacesTest extends BaseTestCase |
|
31
|
|
|
{ |
|
32
|
|
|
protected function getCacheDir() |
|
33
|
|
|
{ |
|
34
|
|
|
if (isset($_SERVER['USE_CACHED_RESPONSES']) && true === $_SERVER['USE_CACHED_RESPONSES']) { |
|
35
|
|
|
return __DIR__.'/.cached_responses'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testGetName() |
|
42
|
|
|
{ |
|
43
|
|
|
$provider = new GoogleMapsPlaces($this->getMockedHttpClient(), 'key'); |
|
44
|
|
|
$this->assertEquals('google_maps_places', $provider->getName()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testGeocodeWithLocalhostIPv4() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->expectException(UnsupportedOperation::class); |
|
50
|
|
|
$this->expectExceptionMessage('The GoogleMapsPlaces provider does not support IP addresses'); |
|
51
|
|
|
|
|
52
|
|
|
$provider = new GoogleMapsPlaces($this->getMockedHttpClient(), 'key'); |
|
53
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testGeocodeWithLocalhostIPv6() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->expectException(UnsupportedOperation::class); |
|
59
|
|
|
$this->expectExceptionMessage('The GoogleMapsPlaces provider does not support IP addresses'); |
|
60
|
|
|
|
|
61
|
|
|
$provider = new GoogleMapsPlaces($this->getMockedHttpClient(), 'key'); |
|
62
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('::1')); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testGeocodeWithRealIp() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->expectException(UnsupportedOperation::class); |
|
68
|
|
|
$this->expectExceptionMessage('The GoogleMapsPlaces provider does not support IP addresses'); |
|
69
|
|
|
|
|
70
|
|
|
$provider = $this->getGoogleMapsProvider(); |
|
71
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testGeocodeWithQuotaExceeded() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->expectException(QuotaExceeded::class); |
|
77
|
|
|
$this->expectExceptionMessage('Daily quota exceeded https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=10+avenue+Gambetta%2C+Paris%2C+France&inputtype=textquery&fields=formatted_address%2Cgeometry%2Cicon%2Cname%2Cpermanently_closed%2Cphotos%2Cplace_id%2Cplus_code%2Ctypes&key=key'); |
|
78
|
|
|
|
|
79
|
|
|
$provider = new GoogleMapsPlaces($this->getMockedHttpClient('{"status":"OVER_QUERY_LIMIT"}'), 'key'); |
|
80
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testGeocodePlaceFindMode() |
|
84
|
|
|
{ |
|
85
|
|
|
$provider = $this->getGoogleMapsProvider(); |
|
86
|
|
|
|
|
87
|
|
|
$query = GeocodeQuery::create('Museum of Contemporary Art Australia'); |
|
88
|
|
|
|
|
89
|
|
|
$results = $provider->geocodeQuery($query); |
|
90
|
|
|
$this->assertCount(1, $results); |
|
91
|
|
|
|
|
92
|
|
|
$result = $results->first(); |
|
93
|
|
|
$this->assertInstanceOf(GooglePlace::class, $result); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertSame('ChIJ68aBlEKuEmsRHUA9oME5Zh0', $result->getId()); |
|
96
|
|
|
$this->assertSame('https://maps.gstatic.com/mapfiles/place_api/icons/museum-71.png', $result->getIcon()); |
|
97
|
|
|
$this->assertInstanceOf(PlusCode::class, $result->getPlusCode()); |
|
98
|
|
|
$this->assertContainsOnlyInstancesOf(Photo::class, $result->getPhotos()); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertSame([ |
|
101
|
|
|
'art_gallery', |
|
102
|
|
|
'museum', |
|
103
|
|
|
'cafe', |
|
104
|
|
|
'food', |
|
105
|
|
|
'store', |
|
106
|
|
|
'point_of_interest', |
|
107
|
|
|
'establishment', |
|
108
|
|
|
], $result->getType()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testGeocodePlaceSearchMode() |
|
112
|
|
|
{ |
|
113
|
|
|
$provider = $this->getGoogleMapsProvider(); |
|
114
|
|
|
|
|
115
|
|
|
$query = GeocodeQuery::create('bar in sydney') |
|
116
|
|
|
->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_SEARCH); |
|
117
|
|
|
|
|
118
|
|
|
$results = $provider->geocodeQuery($query); |
|
119
|
|
|
$this->assertCount(20, $results); |
|
120
|
|
|
|
|
121
|
|
|
/** @var GooglePlace $resultOne */ |
|
122
|
|
|
$resultOne = $results->first(); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertInstanceOf(GooglePlace::class, $resultOne); |
|
125
|
|
|
$this->assertSame('ChIJ3SS9Lj-uEmsRrVS7u1OEV_0', $resultOne->getId()); |
|
126
|
|
|
$this->assertSame('Papa Gede\'s Bar', $resultOne->getName()); |
|
127
|
|
|
$this->assertSame('348 Kent St, Sydney NSW 2000, Australia', $resultOne->getFormattedAddress()); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertSame([ |
|
130
|
|
|
'bar', |
|
131
|
|
|
'restaurant', |
|
132
|
|
|
'food', |
|
133
|
|
|
'point_of_interest', |
|
134
|
|
|
'establishment', |
|
135
|
|
|
], $resultOne->getType()); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertSame('https://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png', $resultOne->getIcon()); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertInstanceOf(PlusCode::class, $resultOne->getPlusCode()); |
|
140
|
|
|
$this->assertSame('4RRH46J3+3X', $resultOne->getPlusCode()->getGlobalCode()); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertContainsOnlyInstancesOf(Photo::class, $resultOne->getPhotos()); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertSame(2, $resultOne->getPriceLevel()); |
|
145
|
|
|
$this->assertSame(4.7, $resultOne->getRating()); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertNull($resultOne->getFormattedPhoneNumber()); |
|
148
|
|
|
$this->assertNull($resultOne->getInternationalPhoneNumber()); |
|
149
|
|
|
$this->assertNull($resultOne->getWebsite()); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertInstanceOf(OpeningHours::class, $resultOne->getOpeningHours()); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertFalse($resultOne->isPermanentlyClosed()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testGeocodePlaceSearchAroundLocation() |
|
157
|
|
|
{ |
|
158
|
|
|
$provider = $this->getGoogleMapsProvider(); |
|
159
|
|
|
|
|
160
|
|
|
$query = GeocodeQuery::create('bar') |
|
161
|
|
|
->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_SEARCH) |
|
162
|
|
|
->withData('location', '-32.926642, 151.783026')// Newcastle, NSW |
|
163
|
|
|
->withData('radius', 100); |
|
164
|
|
|
|
|
165
|
|
|
$results = $provider->geocodeQuery($query); |
|
166
|
|
|
$this->assertCount(20, $results); |
|
167
|
|
|
|
|
168
|
|
|
/** @var GooglePlace $resultOne */ |
|
169
|
|
|
$resultOne = $results->first(); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertInstanceOf(GooglePlace::class, $resultOne); |
|
172
|
|
|
$this->assertSame('ChIJ5_ZqMHsUc2sRHgfnw5D1FlY', $resultOne->getId()); |
|
173
|
|
|
$this->assertSame('Reserve', $resultOne->getName()); |
|
174
|
|
|
$this->assertSame('102 Hunter St, Newcastle NSW 2300, Australia', $resultOne->getFormattedAddress()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testReverseGeocodePlaceWithoutType() |
|
178
|
|
|
{ |
|
179
|
|
|
$this->expectException(InvalidArgument::class); |
|
180
|
|
|
$this->expectExceptionMessage('One of `type`, `keyword`, `name` is required to be set in the Query data for Reverse Geocoding'); |
|
181
|
|
|
|
|
182
|
|
|
$provider = $this->getGoogleMapsProvider(); |
|
183
|
|
|
$query = ReverseQuery::fromCoordinates(-33.8865019, 151.2080413); |
|
184
|
|
|
|
|
185
|
|
|
$provider->reverseQuery($query); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function testReverseGeocodePlace() |
|
189
|
|
|
{ |
|
190
|
|
|
$provider = $this->getGoogleMapsProvider(); |
|
191
|
|
|
|
|
192
|
|
|
$query = ReverseQuery::fromCoordinates(-33.892674, 151.200727)->withData('type', 'bar'); |
|
193
|
|
|
|
|
194
|
|
|
$results = $provider->reverseQuery($query); |
|
195
|
|
|
$this->assertCount(20, $results); |
|
196
|
|
|
|
|
197
|
|
|
/** @var GooglePlace $resultOne */ |
|
198
|
|
|
$resultOne = $results->first(); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertInstanceOf(GooglePlace::class, $resultOne); |
|
201
|
|
|
$this->assertSame('ChIJ3Y3vQdqxEmsRTvCcbZnsYJ8', $resultOne->getId()); |
|
202
|
|
|
$this->assertSame('Arcadia', $resultOne->getName()); |
|
203
|
|
|
$this->assertSame('7 Cope St, Redfern NSW 2016', $resultOne->getFormattedAddress()); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function testReverseGeocodePlaceWithEmptyOpeningHours() |
|
207
|
|
|
{ |
|
208
|
|
|
$provider = $this->getGoogleMapsProvider(); |
|
209
|
|
|
|
|
210
|
|
|
$query = ReverseQuery::fromCoordinates(51.0572773, 13.7763207)->withData('type', 'transit_station'); |
|
211
|
|
|
|
|
212
|
|
|
$results = $provider->reverseQuery($query); |
|
213
|
|
|
$this->assertCount(20, $results); |
|
214
|
|
|
|
|
215
|
|
|
/** @var GooglePlace $resultOne */ |
|
216
|
|
|
$resultOne = $results->get(13); |
|
217
|
|
|
$this->assertNull($resultOne->getOpeningHours()->isOpenNow()); |
|
218
|
|
|
|
|
219
|
|
|
/** @var GooglePlace $resultTwo */ |
|
220
|
|
|
$resultTwo = $results->first(); |
|
221
|
|
|
$this->assertNull($resultTwo->getOpeningHours()); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
private function getGoogleMapsProvider(): GoogleMapsPlaces |
|
225
|
|
|
{ |
|
226
|
|
|
if (!isset($_SERVER['GOOGLE_GEOCODING_KEY'])) { |
|
227
|
|
|
$this->markTestSkipped('You need to configure the GOOGLE_GEOCODING_KEY value in phpunit.xml'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$provider = new GoogleMapsPlaces( |
|
231
|
|
|
$this->getHttpClient($_SERVER['GOOGLE_GEOCODING_KEY']), |
|
232
|
|
|
$_SERVER['GOOGLE_GEOCODING_KEY'] |
|
233
|
|
|
); |
|
234
|
|
|
|
|
235
|
|
|
return $provider; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|