PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Provider/Mapbox/Tests/MapboxTest.php

http://github.com/willdurand/Geocoder
PHP | 269 lines | 209 code | 47 blank | 13 comment | 7 complexity | 32251c90c58708b7e25ace4de9c3405c MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Geocoder package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. namespace Geocoder\Provider\Mapbox\Tests;
  11. use Geocoder\IntegrationTest\BaseTestCase;
  12. use Geocoder\Location;
  13. use Geocoder\Model\Address;
  14. use Geocoder\Model\AddressCollection;
  15. use Geocoder\Model\Bounds;
  16. use Geocoder\Query\GeocodeQuery;
  17. use Geocoder\Query\ReverseQuery;
  18. use Geocoder\Provider\Mapbox\Mapbox;
  19. class MapboxTest extends BaseTestCase
  20. {
  21. protected function getCacheDir()
  22. {
  23. if (isset($_SERVER['USE_CACHED_RESPONSES']) && true === $_SERVER['USE_CACHED_RESPONSES']) {
  24. return __DIR__.'/.cached_responses';
  25. }
  26. return null;
  27. }
  28. public function testGetName()
  29. {
  30. $provider = new Mapbox($this->getMockedHttpClient(), 'access_token');
  31. $this->assertEquals('mapbox', $provider->getName());
  32. }
  33. public function testGeocodeWithLocalhostIPv4()
  34. {
  35. $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
  36. $this->expectExceptionMessage('The Mapbox provider does not support IP addresses, only street addresses.');
  37. $provider = new Mapbox($this->getMockedHttpClient(), 'access_token');
  38. $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
  39. }
  40. public function testGeocodeWithLocalhostIPv6()
  41. {
  42. $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
  43. $this->expectExceptionMessage('The Mapbox provider does not support IP addresses, only street addresses.');
  44. $provider = new Mapbox($this->getMockedHttpClient(), 'access_token');
  45. $provider->geocodeQuery(GeocodeQuery::create('::1'));
  46. }
  47. public function testGeocodeWithRealIp()
  48. {
  49. $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
  50. $this->expectExceptionMessage('The Mapbox provider does not support IP addresses, only street addresses.');
  51. $provider = new Mapbox($this->getHttpClient(), 'access_token');
  52. $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
  53. }
  54. public function testGeocodeWithQuotaExceeded()
  55. {
  56. $this->expectException(\Geocoder\Exception\QuotaExceeded::class);
  57. $provider = new Mapbox($this->getMockedHttpClient('', 429), 'access_token');
  58. $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
  59. }
  60. public function testGeocodePlaceWithNoCountryShortCode()
  61. {
  62. $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
  63. $query = GeocodeQuery::create('princ'); // Principato di Monaco
  64. $query = $query->withLocale('it');
  65. $query = $query->withBounds(new Bounds(
  66. 35.82809688193029,
  67. -11.36323261153737,
  68. 59.05992036364424,
  69. 34.33947713277206
  70. ));
  71. $query = $query->withLimit(1);
  72. $query = $query->withData('location_type', [
  73. Mapbox::TYPE_PLACE,
  74. Mapbox::TYPE_LOCALITY,
  75. Mapbox::TYPE_NEIGHBORHOOD,
  76. Mapbox::TYPE_POI,
  77. Mapbox::TYPE_POI_LANDMARK,
  78. ]);
  79. $results = $provider->geocodeQuery($query);
  80. $this->assertInstanceOf(AddressCollection::class, $results);
  81. $this->assertCount(1, $results);
  82. /** @var Location $result */
  83. $result = $results->first();
  84. $this->assertInstanceOf(Address::class, $result);
  85. $this->assertEquals(43.73125, $result->getCoordinates()->getLatitude(), '', 0.001);
  86. $this->assertEquals(7.41974, $result->getCoordinates()->getLongitude(), '', 0.001);
  87. $this->assertEquals('Principato di Monaco', $result->getStreetName());
  88. $this->assertEquals('Principato di Monaco', $result->getCountry()->getName());
  89. $this->assertEquals('place.4899176537126140', $result->getId());
  90. // not provided
  91. $this->assertNull($result->getCountry()->getCode());
  92. $this->assertNull($result->getPostalCode());
  93. $this->assertNull($result->getLocality());
  94. $this->assertNull($result->getTimezone());
  95. $this->assertNull($result->getStreetNumber());
  96. }
  97. public function testGeocodeWithRealAddress()
  98. {
  99. if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
  100. $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
  101. }
  102. $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
  103. $results = $provider->geocodeQuery(GeocodeQuery::create('149 9th St, San Francisco, CA 94103'));
  104. $this->assertInstanceOf(AddressCollection::class, $results);
  105. $this->assertCount(5, $results);
  106. /** @var Location $result */
  107. $result = $results->first();
  108. $this->assertInstanceOf(Address::class, $result);
  109. $this->assertEquals(37.77572, $result->getCoordinates()->getLatitude(), '', 0.001);
  110. $this->assertEquals(-122.41362, $result->getCoordinates()->getLongitude(), '', 0.001);
  111. $this->assertNull($result->getBounds());
  112. $this->assertEquals(149, $result->getStreetNumber());
  113. $this->assertEquals('9th Street', $result->getStreetName());
  114. $this->assertEquals(94103, $result->getPostalCode());
  115. $this->assertEquals('San Francisco', $result->getLocality());
  116. $this->assertEquals('California', $result->getAdminLevels()->get(2)->getName());
  117. $this->assertEquals('CA', $result->getAdminLevels()->get(2)->getCode());
  118. $this->assertEquals('San Francisco', $result->getAdminLevels()->get(1)->getName());
  119. $this->assertEquals('United States', $result->getCountry()->getName());
  120. $this->assertEquals('US', $result->getCountry()->getCode());
  121. $this->assertEquals('address.3071152063251042', $result->getId());
  122. // not provided
  123. $this->assertNull($result->getTimezone());
  124. }
  125. public function testReverse()
  126. {
  127. $this->expectException(\Geocoder\Exception\InvalidServerResponse::class);
  128. $provider = new Mapbox($this->getMockedHttpClient(), 'access_token');
  129. $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
  130. }
  131. public function testReverseWithRealCoordinates()
  132. {
  133. if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
  134. $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
  135. }
  136. $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
  137. $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.8631507, 2.388911));
  138. $this->assertInstanceOf(AddressCollection::class, $results);
  139. $this->assertCount(4, $results);
  140. /** @var Location $result */
  141. $result = $results->first();
  142. $this->assertInstanceOf(Address::class, $result);
  143. $this->assertEquals(8, $result->getStreetNumber());
  144. $this->assertEquals('Avenue Gambetta', $result->getStreetName());
  145. $this->assertEquals(75020, $result->getPostalCode());
  146. $this->assertEquals('Paris', $result->getLocality());
  147. $this->assertCount(1, $result->getAdminLevels());
  148. $this->assertEquals('Paris', $result->getAdminLevels()->get(1)->getName());
  149. $this->assertEquals('France', $result->getCountry()->getName());
  150. $this->assertEquals('FR', $result->getCountry()->getCode());
  151. $this->assertEquals('address.1085979616', $result->getId());
  152. }
  153. public function testGeocodeWithInvalidApiKey()
  154. {
  155. $this->expectException(\Geocoder\Exception\InvalidCredentials::class);
  156. $provider = new Mapbox($this->getMockedHttpClient('', 403), 'api_key');
  157. $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
  158. }
  159. public function testGeocodeWithRealValidApiKey()
  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. $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
  165. $results = $provider->geocodeQuery(GeocodeQuery::create('116th St & Broadway, New York, NY 10027, United States'));
  166. $this->assertInstanceOf(AddressCollection::class, $results);
  167. $this->assertCount(5, $results);
  168. /** @var Location $result */
  169. $result = $results->first();
  170. $this->assertInstanceOf(Address::class, $result);
  171. $this->assertEquals('116th Street', $result->getStreetName());
  172. $this->assertEquals(11356, $result->getPostalCode());
  173. $this->assertCount(2, $result->getAdminLevels());
  174. $this->assertEquals('United States', $result->getCountry()->getName());
  175. $this->assertEquals('US', $result->getCountry()->getCode());
  176. $this->assertEquals('address.2431617896783536', $result->getId());
  177. $this->assertNotNull($result->getCoordinates()->getLatitude());
  178. $this->assertNotNull($result->getCoordinates()->getLongitude());
  179. $this->assertEquals(40.786596, $result->getCoordinates()->getLatitude(), '', 0.001);
  180. $this->assertEquals(-73.851157, $result->getCoordinates()->getLongitude(), '', 0.001);
  181. $this->assertEquals('New York', $result->getLocality());
  182. $this->assertCount(2, $result->getAdminLevels());
  183. $this->assertEquals('New York', $result->getAdminLevels()->get(1)->getName());
  184. $this->assertEquals('NY', $result->getAdminLevels()->get(2)->getCode());
  185. }
  186. public function testGeocodeWithFuzzyMatch()
  187. {
  188. if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
  189. $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
  190. }
  191. $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
  192. $query = GeocodeQuery::create('wahsington'); // Washington
  193. $query = $query->withData('fuzzy_match', true);
  194. $query = $query->withBounds(new Bounds(
  195. 45.54372254,
  196. -124.83609163,
  197. 49.00243912,
  198. -116.91742984
  199. ));
  200. $results = $provider->geocodeQuery($query);
  201. $this->assertInstanceOf(AddressCollection::class, $results);
  202. $this->assertCount(5, $results);
  203. }
  204. public function testGeocodeWithoutFuzzyMatch()
  205. {
  206. if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
  207. $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
  208. }
  209. $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
  210. $query = GeocodeQuery::create('wahsington'); // Washington
  211. $query = $query->withData('fuzzy_match', false);
  212. $query = $query->withBounds(new Bounds(
  213. 45.54372254,
  214. -124.83609163,
  215. 49.00243912,
  216. -116.91742984
  217. ));
  218. $results = $provider->geocodeQuery($query);
  219. $this->assertInstanceOf(AddressCollection::class, $results);
  220. $this->assertEmpty($results);
  221. }
  222. }