|
13 | 13 | namespace Geocoder\Provider\Mapbox\Tests; |
14 | 14 |
|
15 | 15 | use Geocoder\IntegrationTest\BaseTestCase; |
16 | | -use Geocoder\Model\AddressCollection; |
17 | | -use Geocoder\Model\Bounds; |
| 16 | +use Geocoder\Exception\InvalidServerResponse; |
18 | 17 | use Geocoder\Provider\Mapbox\Mapbox; |
19 | | -use Geocoder\Provider\Mapbox\Model\MapboxAddress; |
20 | 18 | use Geocoder\Query\GeocodeQuery; |
21 | 19 | use Geocoder\Query\ReverseQuery; |
22 | 20 |
|
@@ -72,197 +70,21 @@ public function testGeocodeWithQuotaExceeded(): void |
72 | 70 | $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
73 | 71 | } |
74 | 72 |
|
75 | | - public function testGeocodePlaceWithNoCountryShortCode(): void |
76 | | - { |
77 | | - $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']); |
78 | | - |
79 | | - $query = GeocodeQuery::create('princ'); // Principato di Monaco |
80 | | - $query = $query->withLocale('it'); |
81 | | - $query = $query->withBounds(new Bounds( |
82 | | - 35.82809688193029, |
83 | | - -11.36323261153737, |
84 | | - 59.05992036364424, |
85 | | - 34.33947713277206 |
86 | | - )); |
87 | | - $query = $query->withLimit(1); |
88 | | - $query = $query->withData('location_type', [ |
89 | | - Mapbox::TYPE_PLACE, |
90 | | - Mapbox::TYPE_LOCALITY, |
91 | | - Mapbox::TYPE_NEIGHBORHOOD, |
92 | | - Mapbox::TYPE_POI, |
93 | | - Mapbox::TYPE_POI_LANDMARK, |
94 | | - ]); |
95 | | - |
96 | | - $results = $provider->geocodeQuery($query); |
97 | | - |
98 | | - $this->assertInstanceOf(AddressCollection::class, $results); |
99 | | - $this->assertCount(1, $results); |
100 | | - |
101 | | - /** @var MapboxAddress $result */ |
102 | | - $result = $results->first(); |
103 | | - $this->assertInstanceOf(MapboxAddress::class, $result); |
104 | | - $this->assertEqualsWithDelta(43.73125, $result->getCoordinates()->getLatitude(), 0.001); |
105 | | - $this->assertEqualsWithDelta(7.41974, $result->getCoordinates()->getLongitude(), 0.001); |
106 | | - $this->assertEquals('Principato di Monaco', $result->getStreetName()); |
107 | | - $this->assertEquals('Principato di Monaco', $result->getCountry()->getName()); |
108 | | - $this->assertEquals('place.4899176537126140', $result->getId()); |
109 | | - |
110 | | - // not provided |
111 | | - $this->assertNull($result->getCountry()->getCode()); |
112 | | - $this->assertNull($result->getPostalCode()); |
113 | | - $this->assertNull($result->getLocality()); |
114 | | - $this->assertNull($result->getTimezone()); |
115 | | - $this->assertNull($result->getStreetNumber()); |
116 | | - } |
117 | | - |
118 | | - public function testGeocodeWithRealAddress(): void |
119 | | - { |
120 | | - if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) { |
121 | | - $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml'); |
122 | | - } |
123 | | - |
124 | | - $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']); |
125 | | - $results = $provider->geocodeQuery(GeocodeQuery::create('149 9th St, San Francisco, CA 94103')); |
126 | | - |
127 | | - $this->assertInstanceOf(AddressCollection::class, $results); |
128 | | - $this->assertCount(5, $results); |
129 | | - |
130 | | - /** @var MapboxAddress $result */ |
131 | | - $result = $results->first(); |
132 | | - $this->assertInstanceOf(MapboxAddress::class, $result); |
133 | | - $this->assertEqualsWithDelta(37.77572, $result->getCoordinates()->getLatitude(), 0.001); |
134 | | - $this->assertEqualsWithDelta(-122.41362, $result->getCoordinates()->getLongitude(), 0.001); |
135 | | - $this->assertNull($result->getBounds()); |
136 | | - $this->assertEquals(149, $result->getStreetNumber()); |
137 | | - $this->assertEquals('9th Street', $result->getStreetName()); |
138 | | - $this->assertEquals(94103, $result->getPostalCode()); |
139 | | - $this->assertEquals('San Francisco', $result->getLocality()); |
140 | | - $this->assertEquals('California', $result->getAdminLevels()->get(2)->getName()); |
141 | | - $this->assertEquals('CA', $result->getAdminLevels()->get(2)->getCode()); |
142 | | - $this->assertEquals('San Francisco', $result->getAdminLevels()->get(1)->getName()); |
143 | | - $this->assertEquals('United States', $result->getCountry()->getName()); |
144 | | - $this->assertEquals('US', $result->getCountry()->getCode()); |
145 | | - $this->assertEquals('address.3071152063251042', $result->getId()); |
146 | | - |
147 | | - // not provided |
148 | | - $this->assertNull($result->getTimezone()); |
149 | | - } |
150 | | - |
151 | | - public function testReverse(): void |
152 | | - { |
153 | | - $this->expectException(\Geocoder\Exception\InvalidServerResponse::class); |
154 | | - |
155 | | - $provider = new Mapbox($this->getMockedHttpClient(), 'access_token'); |
156 | | - $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2)); |
157 | | - } |
158 | | - |
159 | | - public function testReverseWithRealCoordinates(): void |
160 | | - { |
161 | | - if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) { |
162 | | - $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml'); |
163 | | - } |
164 | | - |
165 | | - $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']); |
166 | | - $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.8631507, 2.388911)); |
167 | | - |
168 | | - $this->assertInstanceOf(AddressCollection::class, $results); |
169 | | - $this->assertCount(4, $results); |
170 | | - |
171 | | - /** @var MapboxAddress $result */ |
172 | | - $result = $results->first(); |
173 | | - $this->assertInstanceOf(MapboxAddress::class, $result); |
174 | | - $this->assertEquals(8, $result->getStreetNumber()); |
175 | | - $this->assertEquals('Avenue Gambetta', $result->getStreetName()); |
176 | | - $this->assertEquals(75020, $result->getPostalCode()); |
177 | | - $this->assertEquals('Paris', $result->getLocality()); |
178 | | - $this->assertCount(1, $result->getAdminLevels()); |
179 | | - $this->assertEquals('Paris', $result->getAdminLevels()->get(1)->getName()); |
180 | | - $this->assertEquals('France', $result->getCountry()->getName()); |
181 | | - $this->assertEquals('FR', $result->getCountry()->getCode()); |
182 | | - $this->assertEquals('address.1085979616', $result->getId()); |
183 | | - } |
184 | | - |
185 | 73 | public function testGeocodeWithInvalidApiKey(): void |
186 | 74 | { |
| 75 | + // The v6 API answers 401 for an invalid token (v5 answered 403). |
| 76 | + // Both status codes map to InvalidCredentials in AbstractHttpProvider. |
187 | 77 | $this->expectException(\Geocoder\Exception\InvalidCredentials::class); |
188 | 78 |
|
189 | | - $provider = new Mapbox($this->getMockedHttpClient('', 403), 'api_key'); |
| 79 | + $provider = new Mapbox($this->getMockedHttpClient('', 401), 'api_key'); |
190 | 80 | $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
191 | 81 | } |
192 | 82 |
|
193 | | - public function testGeocodeWithRealValidApiKey(): void |
194 | | - { |
195 | | - if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) { |
196 | | - $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml'); |
197 | | - } |
198 | | - |
199 | | - $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']); |
200 | | - |
201 | | - $results = $provider->geocodeQuery(GeocodeQuery::create('116th St & Broadway, New York, NY 10027, United States')); |
202 | | - |
203 | | - $this->assertInstanceOf(AddressCollection::class, $results); |
204 | | - $this->assertCount(5, $results); |
205 | | - |
206 | | - /** @var MapboxAddress $result */ |
207 | | - $result = $results->first(); |
208 | | - $this->assertInstanceOf(MapboxAddress::class, $result); |
209 | | - $this->assertEquals('116th Street', $result->getStreetName()); |
210 | | - $this->assertEquals(11356, $result->getPostalCode()); |
211 | | - $this->assertCount(2, $result->getAdminLevels()); |
212 | | - $this->assertEquals('United States', $result->getCountry()->getName()); |
213 | | - $this->assertEquals('US', $result->getCountry()->getCode()); |
214 | | - $this->assertEquals('address.2431617896783536', $result->getId()); |
215 | | - $this->assertNotNull($result->getCoordinates()->getLatitude()); |
216 | | - $this->assertNotNull($result->getCoordinates()->getLongitude()); |
217 | | - $this->assertEqualsWithDelta(40.786596, $result->getCoordinates()->getLatitude(), 0.001); |
218 | | - $this->assertEqualsWithDelta(-73.851157, $result->getCoordinates()->getLongitude(), 0.001); |
219 | | - $this->assertEquals('New York', $result->getLocality()); |
220 | | - $this->assertCount(2, $result->getAdminLevels()); |
221 | | - $this->assertEquals('New York', $result->getAdminLevels()->get(1)->getName()); |
222 | | - $this->assertEquals('NY', $result->getAdminLevels()->get(2)->getCode()); |
223 | | - } |
224 | | - |
225 | | - public function testGeocodeWithFuzzyMatch(): void |
| 83 | + public function testReverseWithInvalidResponse(): void |
226 | 84 | { |
227 | | - if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) { |
228 | | - $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml'); |
229 | | - } |
230 | | - |
231 | | - $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']); |
232 | | - |
233 | | - $query = GeocodeQuery::create('wahsington'); // Washington |
234 | | - $query = $query->withData('fuzzy_match', true); |
235 | | - $query = $query->withBounds(new Bounds( |
236 | | - 45.54372254, |
237 | | - -124.83609163, |
238 | | - 49.00243912, |
239 | | - -116.91742984 |
240 | | - )); |
241 | | - |
242 | | - $results = $provider->geocodeQuery($query); |
243 | | - $this->assertInstanceOf(AddressCollection::class, $results); |
244 | | - $this->assertCount(5, $results); |
245 | | - } |
246 | | - |
247 | | - public function testGeocodeWithoutFuzzyMatch(): void |
248 | | - { |
249 | | - if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) { |
250 | | - $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml'); |
251 | | - } |
252 | | - |
253 | | - $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']); |
| 85 | + $this->expectException(InvalidServerResponse::class); |
254 | 86 |
|
255 | | - $query = GeocodeQuery::create('wahsington'); // Washington |
256 | | - $query = $query->withData('fuzzy_match', false); |
257 | | - $query = $query->withBounds(new Bounds( |
258 | | - 45.54372254, |
259 | | - -124.83609163, |
260 | | - 49.00243912, |
261 | | - -116.91742984 |
262 | | - )); |
263 | | - |
264 | | - $results = $provider->geocodeQuery($query); |
265 | | - $this->assertInstanceOf(AddressCollection::class, $results); |
266 | | - $this->assertEmpty($results); |
| 87 | + $provider = new Mapbox($this->getMockedHttpClient(), 'access_token'); |
| 88 | + $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2)); |
267 | 89 | } |
268 | 90 | } |
0 commit comments