Completed
Branch master (8e77a6)
by Tobias
45:12 queued 20:15
created

MapboxTest::testGetName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Mapbox\Tests;
14
15
use Geocoder\IntegrationTest\BaseTestCase;
16
use Geocoder\Location;
17
use Geocoder\Model\Address;
18
use Geocoder\Model\AddressCollection;
19
use Geocoder\Query\GeocodeQuery;
20
use Geocoder\Query\ReverseQuery;
21
use Geocoder\Provider\Mapbox\Mapbox;
22
23
class MapboxTest extends BaseTestCase
24
{
25
    protected function getCacheDir()
26
    {
27
        if (isset($_SERVER['USE_CACHED_RESPONSES']) && true === $_SERVER['USE_CACHED_RESPONSES']) {
28
            return __DIR__.'/.cached_responses';
29
        }
30
31
        return null;
32
    }
33
34
    public function testGetName()
35
    {
36
        $provider = new Mapbox($this->getMockedHttpClient(), 'access_token');
37
        $this->assertEquals('mapbox', $provider->getName());
38
    }
39
40
    /**
41
     * @expectedException \Geocoder\Exception\UnsupportedOperation
42
     */
43
    public function testGeocodeWithLocalhostIPv4()
44
    {
45
        $provider = new Mapbox($this->getMockedHttpClient(), 'access_token');
46
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
47
    }
48
49
    /**
50
     * @expectedException \Geocoder\Exception\UnsupportedOperation
51
     * @expectedExceptionMessage The Mapbox provider does not support IP addresses, only street addresses.
52
     */
53
    public function testGeocodeWithLocalhostIPv6()
54
    {
55
        $provider = new Mapbox($this->getMockedHttpClient(), 'access_token');
56
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
57
    }
58
59
    /**
60
     * @expectedException \Geocoder\Exception\UnsupportedOperation
61
     * @expectedExceptionMessage The Mapbox provider does not support IP addresses, only street addresses.
62
     */
63
    public function testGeocodeWithRealIp()
64
    {
65
        $provider = new Mapbox($this->getHttpClient(), 'access_token');
66
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
67
    }
68
69
    /**
70
     * @expectedException \Geocoder\Exception\QuotaExceeded
71
     */
72
    public function testGeocodeWithQuotaExceeded()
73
    {
74
        $provider = new Mapbox($this->getMockedHttpClient('', 429), 'access_token');
75
        $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
76
    }
77
78
    public function testGeocodeWithRealAddress()
79
    {
80
        if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
81
            $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
82
        }
83
84
        $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
85
        $results = $provider->geocodeQuery(GeocodeQuery::create('149 9th St, San Francisco, CA 94103'));
86
87
        $this->assertInstanceOf(AddressCollection::class, $results);
88
        $this->assertCount(5, $results);
89
90
        /** @var Location $result */
91
        $result = $results->first();
92
        $this->assertInstanceOf(Address::class, $result);
93
        $this->assertEquals(37.77572, $result->getCoordinates()->getLatitude(), '', 0.001);
94
        $this->assertEquals(-122.41362, $result->getCoordinates()->getLongitude(), '', 0.001);
95
        $this->assertNull($result->getBounds());
96
        $this->assertEquals(149, $result->getStreetNumber());
97
        $this->assertEquals('9th Street', $result->getStreetName());
98
        $this->assertEquals(94103, $result->getPostalCode());
99
        $this->assertEquals('San Francisco', $result->getLocality());
100
        $this->assertEquals('California', $result->getAdminLevels()->get(2)->getName());
101
        $this->assertEquals('CA', $result->getAdminLevels()->get(2)->getCode());
102
        $this->assertEquals('San Francisco', $result->getAdminLevels()->get(1)->getName());
103
        $this->assertEquals('United States', $result->getCountry()->getName());
104
        $this->assertEquals('US', $result->getCountry()->getCode());
105
        $this->assertEquals('address.3071152063251042', $result->getId());
106
107
        // not provided
108
        $this->assertNull($result->getTimezone());
109
    }
110
111
    /**
112
     * @expectedException \Geocoder\Exception\InvalidServerResponse
113
     */
114
    public function testReverse()
115
    {
116
        $provider = new Mapbox($this->getMockedHttpClient(), 'access_token');
117
        $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
118
    }
119
120
    public function testReverseWithRealCoordinates()
121
    {
122
        if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
123
            $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
124
        }
125
126
        $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
127
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.8631507, 2.388911));
128
129
        $this->assertInstanceOf(AddressCollection::class, $results);
130
        $this->assertCount(4, $results);
131
132
        /** @var Location $result */
133
        $result = $results->first();
134
        $this->assertInstanceOf(Address::class, $result);
135
        $this->assertEquals(8, $result->getStreetNumber());
136
        $this->assertEquals('Avenue Gambetta', $result->getStreetName());
137
        $this->assertEquals(75020, $result->getPostalCode());
138
        $this->assertEquals('Paris', $result->getLocality());
139
        $this->assertCount(1, $result->getAdminLevels());
140
        $this->assertEquals('Paris', $result->getAdminLevels()->get(1)->getName());
141
        $this->assertEquals('France', $result->getCountry()->getName());
142
        $this->assertEquals('FR', $result->getCountry()->getCode());
143
        $this->assertEquals('address.1085979616', $result->getId());
144
    }
145
146
    /**
147
     * @expectedException \Geocoder\Exception\InvalidCredentials
148
     */
149
    public function testGeocodeWithInvalidApiKey()
150
    {
151
        $provider = new Mapbox($this->getMockedHttpClient('', 403), 'api_key');
152
        $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
153
    }
154
155
    public function testGeocodeWithRealValidApiKey()
156
    {
157
        if (!isset($_SERVER['MAPBOX_GEOCODING_KEY'])) {
158
            $this->markTestSkipped('You need to configure the MAPBOX_GEOCODING_KEY value in phpunit.xml');
159
        }
160
161
        $provider = new Mapbox($this->getHttpClient($_SERVER['MAPBOX_GEOCODING_KEY']), $_SERVER['MAPBOX_GEOCODING_KEY']);
162
163
        $results = $provider->geocodeQuery(GeocodeQuery::create('116th St & Broadway, New York, NY 10027, United States'));
164
165
        $this->assertInstanceOf(AddressCollection::class, $results);
166
        $this->assertCount(5, $results);
167
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
}
187