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 | /** |
||||
14 | * @author Sébastien Barré <[email protected]> |
||||
15 | */ |
||||
16 | |||||
17 | namespace Geocoder\Provider\Here\Tests; |
||||
18 | |||||
19 | use Geocoder\IntegrationTest\BaseTestCase; |
||||
20 | use Geocoder\Location; |
||||
21 | use Geocoder\Query\GeocodeQuery; |
||||
22 | use Geocoder\Query\ReverseQuery; |
||||
23 | use Geocoder\Provider\Here\Here; |
||||
24 | |||||
25 | class HereTest extends BaseTestCase |
||||
26 | { |
||||
27 | protected function getCacheDir() |
||||
28 | { |
||||
29 | if (isset($_SERVER['USE_CACHED_RESPONSES']) && true === $_SERVER['USE_CACHED_RESPONSES']) { |
||||
30 | return __DIR__.'/.cached_responses'; |
||||
31 | } |
||||
32 | |||||
33 | return null; |
||||
34 | } |
||||
35 | |||||
36 | public function testGeocodeWithRealAddress() |
||||
37 | { |
||||
38 | if (!isset($_SERVER['HERE_API_KEY'])) { |
||||
39 | $this->markTestSkipped('You need to configure the HERE_API_KEY value in phpunit.xml'); |
||||
40 | } |
||||
41 | |||||
42 | $provider = Here::createUsingApiKey($this->getHttpClient($_SERVER['HERE_API_KEY']), $_SERVER['HERE_API_KEY']); |
||||
43 | |||||
44 | $results = $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')->withLocale('fr-FR')); |
||||
45 | |||||
46 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||||
47 | $this->assertCount(1, $results); |
||||
48 | |||||
49 | /** @var Location $result */ |
||||
50 | $result = $results->first(); |
||||
51 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||||
52 | $this->assertEqualsWithDelta(48.8653, $result->getCoordinates()->getLatitude(), 0.01); |
||||
53 | $this->assertEqualsWithDelta(2.39844, $result->getCoordinates()->getLongitude(), 0.01); |
||||
54 | $this->assertNotNull($result->getBounds()); |
||||
55 | $this->assertEqualsWithDelta(48.8664242, $result->getBounds()->getSouth(), 0.01); |
||||
56 | $this->assertEqualsWithDelta(2.3967311, $result->getBounds()->getWest(), 0.01); |
||||
57 | $this->assertEqualsWithDelta(48.8641758, $result->getBounds()->getNorth(), 0.01); |
||||
58 | $this->assertEqualsWithDelta(2.4001489, $result->getBounds()->getEast(), 0.01); |
||||
59 | $this->assertEquals(10, $result->getStreetNumber()); |
||||
60 | |||||
61 | $this->assertEquals('Avenue Gambetta', $result->getStreetName()); |
||||
62 | $this->assertEquals(75020, $result->getPostalCode()); |
||||
63 | $this->assertEquals('Paris', $result->getLocality()); |
||||
64 | $this->assertEquals('France', $result->getCountry()->getName()); |
||||
65 | $this->assertEquals('FRA', $result->getCountry()->getCode()); |
||||
66 | } |
||||
67 | |||||
68 | /** |
||||
69 | * @throws \Geocoder\Exception\Exception |
||||
70 | */ |
||||
71 | public function testGeocodeWithDefaultAdditionalData() |
||||
72 | { |
||||
73 | if (!isset($_SERVER['HERE_API_KEY'])) { |
||||
74 | $this->markTestSkipped('You need to configure the HERE_API_KEY value in phpunit.xml'); |
||||
75 | } |
||||
76 | |||||
77 | $provider = Here::createUsingApiKey($this->getHttpClient($_SERVER['HERE_API_KEY']), $_SERVER['HERE_API_KEY']); |
||||
78 | |||||
79 | $results = $provider->geocodeQuery(GeocodeQuery::create('Sant Roc, Santa Coloma de Cervelló, Espanya')->withLocale('ca')); |
||||
80 | |||||
81 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||||
82 | $this->assertCount(1, $results); |
||||
83 | |||||
84 | /** @var Location $result */ |
||||
85 | $result = $results->first(); |
||||
86 | |||||
87 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||||
88 | $this->assertEqualsWithDelta(41.37854, $result->getCoordinates()->getLatitude(), 0.01); |
||||
89 | $this->assertEqualsWithDelta(2.01196, $result->getCoordinates()->getLongitude(), 0.01); |
||||
90 | $this->assertNotNull($result->getBounds()); |
||||
91 | $this->assertEqualsWithDelta(41.36505, $result->getBounds()->getSouth(), 0.01); |
||||
92 | $this->assertEqualsWithDelta(1.99398, $result->getBounds()->getWest(), 0.01); |
||||
93 | $this->assertEqualsWithDelta(41.39203, $result->getBounds()->getNorth(), 0.01); |
||||
94 | $this->assertEqualsWithDelta(2.02994, $result->getBounds()->getEast(), 0.01); |
||||
95 | |||||
96 | $this->assertEquals('08690', $result->getPostalCode()); |
||||
97 | $this->assertEquals('Sant Roc', $result->getSubLocality()); |
||||
98 | $this->assertEquals('Santa Coloma de Cervelló', $result->getLocality()); |
||||
99 | $this->assertEquals('Espanya', $result->getCountry()->getName()); |
||||
100 | $this->assertEquals('ESP', $result->getCountry()->getCode()); |
||||
101 | |||||
102 | $this->assertEquals('Espanya', $result->getAdditionalDataValue('CountryName')); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
103 | $this->assertEquals('Catalunya', $result->getAdditionalDataValue('StateName')); |
||||
104 | $this->assertEquals('Barcelona', $result->getAdditionalDataValue('CountyName')); |
||||
105 | } |
||||
106 | |||||
107 | /** |
||||
108 | * Validation of some AdditionalData filters. |
||||
109 | * https://developer.here.com/documentation/geocoder/topics/resource-params-additional.html. |
||||
110 | * |
||||
111 | * @throws \Geocoder\Exception\Exception |
||||
112 | */ |
||||
113 | public function testGeocodeWithAdditionalData() |
||||
114 | { |
||||
115 | if (!isset($_SERVER['HERE_API_KEY'])) { |
||||
116 | $this->markTestSkipped('You need to configure the HERE_API_KEY value in phpunit.xml'); |
||||
117 | } |
||||
118 | |||||
119 | $provider = Here::createUsingApiKey($this->getHttpClient($_SERVER['HERE_API_KEY']), $_SERVER['HERE_API_KEY']); |
||||
120 | |||||
121 | $results = $provider->geocodeQuery(GeocodeQuery::create('Sant Roc, Santa Coloma de Cervelló, Espanya') |
||||
122 | ->withData('Country2', 'true') |
||||
123 | ->withData('IncludeShapeLevel', 'country') |
||||
124 | ->withData('IncludeRoutingInformation', 'true') |
||||
125 | ->withLocale('ca')); |
||||
126 | |||||
127 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||||
128 | $this->assertCount(1, $results); |
||||
129 | |||||
130 | /** @var Location $result */ |
||||
131 | $result = $results->first(); |
||||
132 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||||
133 | $this->assertEqualsWithDelta(41.37854, $result->getCoordinates()->getLatitude(), 0.01); |
||||
134 | $this->assertEqualsWithDelta(2.01196, $result->getCoordinates()->getLongitude(), 0.01); |
||||
135 | $this->assertNotNull($result->getBounds()); |
||||
136 | $this->assertEqualsWithDelta(41.36505, $result->getBounds()->getSouth(), 0.01); |
||||
137 | $this->assertEqualsWithDelta(1.99398, $result->getBounds()->getWest(), 0.01); |
||||
138 | $this->assertEqualsWithDelta(41.39203, $result->getBounds()->getNorth(), 0.01); |
||||
139 | $this->assertEqualsWithDelta(2.02994, $result->getBounds()->getEast(), 0.01); |
||||
140 | |||||
141 | $this->assertEquals('08690', $result->getPostalCode()); |
||||
142 | $this->assertEquals('Sant Roc', $result->getSubLocality()); |
||||
143 | $this->assertEquals('Santa Coloma de Cervelló', $result->getLocality()); |
||||
144 | $this->assertEquals('Espanya', $result->getCountry()->getName()); |
||||
145 | $this->assertEquals('ESP', $result->getCountry()->getCode()); |
||||
146 | |||||
147 | $this->assertEquals('ES', $result->getAdditionalDataValue('Country2')); |
||||
148 | $this->assertEquals('Espanya', $result->getAdditionalDataValue('CountryName')); |
||||
149 | $this->assertEquals('Catalunya', $result->getAdditionalDataValue('StateName')); |
||||
150 | $this->assertEquals('Barcelona', $result->getAdditionalDataValue('CountyName')); |
||||
151 | $this->assertEquals('district', $result->getAdditionalDataValue('routing_address_matchLevel')); |
||||
152 | $this->assertEquals('NT_TzyupfxmTFN0Rh1TXEMqSA', $result->getAdditionalDataValue('routing_locationId')); |
||||
153 | $this->assertEquals('address', $result->getAdditionalDataValue('routing_result_type')); |
||||
154 | $this->assertEquals('WKTShapeType', $result->getShapeValue('_type')); |
||||
0 ignored issues
–
show
The method
getShapeValue() does not exist on Geocoder\Location . It seems like you code against a sub-type of Geocoder\Location such as Geocoder\Provider\Here\Model\HereAddress .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
155 | $this->assertMatchesRegularExpression('/^MULTIPOLYGON/', $result->getShapeValue('Value')); |
||||
156 | } |
||||
157 | |||||
158 | /** |
||||
159 | * Search for a specific city in a different country. |
||||
160 | * |
||||
161 | * @throws \Geocoder\Exception\Exception |
||||
162 | */ |
||||
163 | public function testGeocodeWithExtraFilterCountry() |
||||
164 | { |
||||
165 | if (!isset($_SERVER['HERE_API_KEY'])) { |
||||
166 | $this->markTestSkipped('You need to configure the HERE_API_KEY value in phpunit.xml'); |
||||
167 | } |
||||
168 | |||||
169 | $provider = Here::createUsingApiKey($this->getHttpClient($_SERVER['HERE_API_KEY']), $_SERVER['HERE_API_KEY']); |
||||
170 | |||||
171 | $queryBarcelonaFromSpain = GeocodeQuery::create('Barcelona')->withData('country', 'ES')->withLocale('ca'); |
||||
172 | $queryBarcelonaFromVenezuela = GeocodeQuery::create('Barcelona')->withData('country', 'VE')->withLocale('ca'); |
||||
173 | |||||
174 | $resultsSpain = $provider->geocodeQuery($queryBarcelonaFromSpain); |
||||
175 | $resultsVenezuela = $provider->geocodeQuery($queryBarcelonaFromVenezuela); |
||||
176 | |||||
177 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $resultsSpain); |
||||
178 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $resultsVenezuela); |
||||
179 | $this->assertCount(1, $resultsSpain); |
||||
180 | $this->assertCount(1, $resultsVenezuela); |
||||
181 | |||||
182 | $resultSpain = $resultsSpain->first(); |
||||
183 | $resultVenezuela = $resultsVenezuela->first(); |
||||
184 | |||||
185 | $this->assertEquals('Barcelona', $resultSpain->getLocality()); |
||||
186 | $this->assertEquals('Barcelona', $resultVenezuela->getLocality()); |
||||
187 | $this->assertEquals('Espanya', $resultSpain->getCountry()->getName()); |
||||
188 | $this->assertEquals('República Bolivariana De Venezuela', $resultVenezuela->getCountry()->getName()); |
||||
189 | $this->assertEquals('ESP', $resultSpain->getCountry()->getCode()); |
||||
190 | $this->assertEquals('VEN', $resultVenezuela->getCountry()->getCode()); |
||||
191 | } |
||||
192 | |||||
193 | /** |
||||
194 | * Search for a specific street in different towns in the same country. |
||||
195 | * |
||||
196 | * @throws \Geocoder\Exception\Exception |
||||
197 | */ |
||||
198 | public function testGeocodeWithExtraFilterCity() |
||||
199 | { |
||||
200 | if (!isset($_SERVER['HERE_API_KEY'])) { |
||||
201 | $this->markTestSkipped('You need to configure the HERE_API_KEY value in phpunit.xml'); |
||||
202 | } |
||||
203 | |||||
204 | $provider = Here::createUsingApiKey($this->getHttpClient($_SERVER['HERE_API_KEY']), $_SERVER['HERE_API_KEY']); |
||||
205 | |||||
206 | $queryStreetCity1 = GeocodeQuery::create('Carrer de Barcelona')->withData('city', 'Sant Vicenç dels Horts')->withLocale('ca')->withLimit(1); |
||||
207 | $queryStreetCity2 = GeocodeQuery::create('Carrer de Barcelona')->withData('city', 'Girona')->withLocale('ca')->withLimit(1); |
||||
208 | $queryStreetCity3 = GeocodeQuery::create('Carrer de Barcelona')->withData('city', 'Pallejà')->withLocale('ca')->withLimit(1); |
||||
209 | |||||
210 | $resultsCity1 = $provider->geocodeQuery($queryStreetCity1); |
||||
211 | $resultsCity2 = $provider->geocodeQuery($queryStreetCity2); |
||||
212 | $resultsCity3 = $provider->geocodeQuery($queryStreetCity3); |
||||
213 | |||||
214 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $resultsCity1); |
||||
215 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $resultsCity2); |
||||
216 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $resultsCity3); |
||||
217 | |||||
218 | $resultCity1 = $resultsCity1->first(); |
||||
219 | $resultCity2 = $resultsCity2->first(); |
||||
220 | $resultCity3 = $resultsCity3->first(); |
||||
221 | |||||
222 | $this->assertEquals('Carrer de Barcelona', $resultCity1->getStreetName()); |
||||
223 | $this->assertEquals('Carrer de Barcelona', $resultCity2->getStreetName()); |
||||
224 | $this->assertEquals('Carrer de Barcelona', $resultCity3->getStreetName()); |
||||
225 | $this->assertEquals('Sant Vicenç dels Horts', $resultCity1->getLocality()); |
||||
226 | $this->assertEquals('Girona', $resultCity2->getLocality()); |
||||
227 | $this->assertEquals('Pallejà', $resultCity3->getLocality()); |
||||
228 | $this->assertEquals('Espanya', $resultCity1->getCountry()->getName()); |
||||
229 | $this->assertEquals('Espanya', $resultCity2->getCountry()->getName()); |
||||
230 | $this->assertEquals('Espanya', $resultCity3->getCountry()->getName()); |
||||
231 | $this->assertEquals('ESP', $resultCity1->getCountry()->getCode()); |
||||
232 | $this->assertEquals('ESP', $resultCity2->getCountry()->getCode()); |
||||
233 | $this->assertEquals('ESP', $resultCity3->getCountry()->getCode()); |
||||
234 | } |
||||
235 | |||||
236 | public function testGeocodeWithExtraFilterCounty() |
||||
237 | { |
||||
238 | if (!isset($_SERVER['HERE_API_KEY'])) { |
||||
239 | $this->markTestSkipped('You need to configure the HERE_API_KEY value in phpunit.xml'); |
||||
240 | } |
||||
241 | |||||
242 | $provider = Here::createUsingApiKey($this->getHttpClient($_SERVER['HERE_API_KEY']), $_SERVER['HERE_API_KEY']); |
||||
243 | |||||
244 | $queryCityRegion1 = GeocodeQuery::create('Cabanes')->withData('county', 'Girona')->withLocale('ca')->withLimit(1); |
||||
245 | $queryCityRegion2 = GeocodeQuery::create('Cabanes')->withData('county', 'Castelló')->withLocale('ca')->withLimit(1); |
||||
246 | |||||
247 | $resultsRegion1 = $provider->geocodeQuery($queryCityRegion1); |
||||
248 | $resultsRegion2 = $provider->geocodeQuery($queryCityRegion2); |
||||
249 | |||||
250 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $resultsRegion1); |
||||
251 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $resultsRegion2); |
||||
252 | |||||
253 | $resultRegion1 = $resultsRegion1->first(); |
||||
254 | $resultRegion2 = $resultsRegion2->first(); |
||||
255 | |||||
256 | $this->assertEquals('Cabanes', $resultRegion1->getLocality()); |
||||
257 | $this->assertEquals('Cabanes', $resultRegion2->getLocality()); |
||||
258 | $this->assertEquals('Girona', $resultRegion1->getAdditionalDataValue('CountyName')); |
||||
259 | $this->assertEquals('Castelló', $resultRegion2->getAdditionalDataValue('CountyName')); |
||||
260 | $this->assertEquals('Catalunya', $resultRegion1->getAdditionalDataValue('StateName')); |
||||
261 | $this->assertEquals('Comunitat Valenciana', $resultRegion2->getAdditionalDataValue('StateName')); |
||||
262 | $this->assertEquals('Espanya', $resultRegion1->getCountry()->getName()); |
||||
263 | $this->assertEquals('Espanya', $resultRegion2->getCountry()->getName()); |
||||
264 | $this->assertEquals('ESP', $resultRegion1->getCountry()->getCode()); |
||||
265 | $this->assertEquals('ESP', $resultRegion2->getCountry()->getCode()); |
||||
266 | } |
||||
267 | |||||
268 | public function testReverseWithRealCoordinates() |
||||
269 | { |
||||
270 | if (!isset($_SERVER['HERE_API_KEY'])) { |
||||
271 | $this->markTestSkipped('You need to configure the HERE_API_KEY value in phpunit.xml'); |
||||
272 | } |
||||
273 | |||||
274 | $provider = Here::createUsingApiKey($this->getHttpClient($_SERVER['HERE_API_KEY']), $_SERVER['HERE_API_KEY']); |
||||
275 | |||||
276 | $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.8632156, 2.3887722)); |
||||
277 | |||||
278 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||||
279 | $this->assertCount(1, $results); |
||||
280 | |||||
281 | /** @var Location $result */ |
||||
282 | $result = $results->first(); |
||||
283 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||||
284 | $this->assertEqualsWithDelta(48.8632147, $result->getCoordinates()->getLatitude(), 0.001); |
||||
285 | $this->assertEqualsWithDelta(2.3887722, $result->getCoordinates()->getLongitude(), 0.001); |
||||
286 | $this->assertNotNull($result->getBounds()); |
||||
287 | $this->assertEqualsWithDelta(48.86315, $result->getBounds()->getSouth(), 0.001); |
||||
288 | $this->assertEqualsWithDelta(2.38853, $result->getBounds()->getWest(), 0.001); |
||||
289 | $this->assertEqualsWithDelta(48.8632147, $result->getBounds()->getNorth(), 0.001); |
||||
290 | $this->assertEqualsWithDelta(2.38883, $result->getBounds()->getEast(), 0.001); |
||||
291 | $this->assertEquals('Avenue Gambetta', $result->getStreetName()); |
||||
292 | $this->assertEquals(75020, $result->getPostalCode()); |
||||
293 | $this->assertEquals('Paris', $result->getLocality()); |
||||
294 | $this->assertEquals('France', $result->getCountry()->getName()); |
||||
295 | $this->assertEquals('FRA', $result->getCountry()->getCode()); |
||||
296 | } |
||||
297 | |||||
298 | public function testGetName() |
||||
299 | { |
||||
300 | $provider = new Here($this->getMockedHttpClient(), 'appId', 'appCode'); |
||||
301 | $this->assertEquals('Here', $provider->getName()); |
||||
302 | } |
||||
303 | |||||
304 | public function testGeocodeWithInvalidData() |
||||
305 | { |
||||
306 | $this->expectException(\Geocoder\Exception\InvalidServerResponse::class); |
||||
307 | |||||
308 | $provider = new Here($this->getMockedHttpClient(), 'appId', 'appCode'); |
||||
309 | $provider->geocodeQuery(GeocodeQuery::create('foobar')); |
||||
310 | } |
||||
311 | |||||
312 | public function testGeocodeIpv4() |
||||
313 | { |
||||
314 | $this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
||||
315 | $this->expectExceptionMessage('The Here provider does not support IP addresses, only street addresses.'); |
||||
316 | |||||
317 | $provider = $this->getProvider(); |
||||
318 | $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
||||
319 | } |
||||
320 | |||||
321 | public function testGeocodeWithLocalhostIPv6() |
||||
322 | { |
||||
323 | $this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
||||
324 | $this->expectExceptionMessage('The Here provider does not support IP addresses, only street addresses.'); |
||||
325 | |||||
326 | $provider = $this->getProvider(); |
||||
327 | $provider->geocodeQuery(GeocodeQuery::create('::1')); |
||||
328 | } |
||||
329 | |||||
330 | public function testGeocodeInvalidApiKey() |
||||
331 | { |
||||
332 | $this->expectException(\Geocoder\Exception\InvalidCredentials::class); |
||||
333 | $this->expectExceptionMessage('Invalid or missing api key.'); |
||||
334 | |||||
335 | $provider = new Here( |
||||
336 | $this->getMockedHttpClient( |
||||
337 | '{ |
||||
338 | "type": { |
||||
339 | "subtype": "InvalidCredentials" |
||||
340 | } |
||||
341 | }' |
||||
342 | ), |
||||
343 | 'appId', |
||||
344 | 'appCode' |
||||
345 | ); |
||||
346 | $provider->geocodeQuery(GeocodeQuery::create('New York')); |
||||
347 | } |
||||
348 | |||||
349 | public function testGeocodeWithRealIPv6() |
||||
350 | { |
||||
351 | $this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
||||
352 | $this->expectExceptionMessage('The Here provider does not support IP addresses, only street addresses.'); |
||||
353 | |||||
354 | $provider = $this->getProvider(); |
||||
355 | $provider->geocodeQuery(GeocodeQuery::create('::ffff:88.188.221.14')); |
||||
356 | } |
||||
357 | |||||
358 | public function getProvider() |
||||
359 | { |
||||
360 | if (!isset($_SERVER['HERE_API_KEY'])) { |
||||
361 | $this->markTestSkipped('You need to configure the HERE_API_KEY value in phpunit.xml'); |
||||
362 | } |
||||
363 | |||||
364 | return Here::createUsingApiKey($this->getHttpClient(), $_SERVER['HERE_API_KEY']); |
||||
365 | } |
||||
366 | } |
||||
367 |