GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#840)
by
unknown
04:36 queued 01:11
created

MapQuestTest::testGeocodeWithSpecificCity()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 79
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 8.8701
c 0
b 0
f 0
cc 2
eloc 61
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\MapQuest\Tests;
14
15
use Geocoder\Collection;
16
use Geocoder\IntegrationTest\BaseTestCase;
17
use Geocoder\Location;
18
use Geocoder\Model\AddressBuilder;
19
use Geocoder\Model\AdminLevel;
20
use Geocoder\Model\Bounds;
21
use Geocoder\Provider\MapQuest\GeocodeQuery as MapQuestGeocodeQuery;
22
use Geocoder\Query\GeocodeQuery;
23
use Geocoder\Query\ReverseQuery;
24
use Geocoder\Provider\MapQuest\MapQuest;
25
26
/**
27
 * @author William Durand <[email protected]>
28
 */
29
class MapQuestTest extends BaseTestCase
30
{
31
    protected function getCacheDir()
32
    {
33
        return __DIR__.'/.cached_responses';
34
    }
35
36
    public function testGetName()
37
    {
38
        $provider = new MapQuest($this->getMockedHttpClient(), 'api_key');
39
        $this->assertEquals('map_quest', $provider->getName());
40
    }
41
42
    public function testGeocode()
43
    {
44
        $provider = new MapQuest($this->getMockedHttpClient('{}'), 'api_key');
45
        $result = $provider->geocodeQuery(GeocodeQuery::create('foobar'));
46
47
        $this->assertInstanceOf(Collection::class, $result);
48
        $this->assertEquals(0, $result->count());
49
    }
50
51
    public function testGetNotRelevantData()
52
    {
53
        $json = '{"results":[{"locations":[{"street":"","postalCode":"","adminArea5":"","adminArea4":"","adminArea3":"","adminArea1":""}]}]}';
54
55
        $provider = new MapQuest($this->getMockedHttpClient($json), 'api_key');
56
        $result = $provider->reverseQuery(ReverseQuery::fromCoordinates(11, 12));
57
58
        $this->assertInstanceOf(Collection::class, $result);
59
        $this->assertEquals(0, $result->count());
60
    }
61
62
    public function testGeocodeWithRealAddress()
0 ignored issues
show
Coding Style introduced by
testGeocodeWithRealAddress uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
63
    {
64
        if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
65
            $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
66
        }
67
68
        $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']);
69
        $results = $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
70
71
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
72
        $this->assertCount(2, $results);
73
74
        /** @var Location $result */
75
        $result = $results->first();
76
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
77
        $this->assertEquals(48.866205, $result->getCoordinates()->getLatitude(), '', 0.01);
78
        $this->assertEquals(2.389089, $result->getCoordinates()->getLongitude(), '', 0.01);
79
        $this->assertEquals('10 Avenue Gambetta', $result->getStreetName());
80
        $this->assertEquals(75020, $result->getPostalCode());
81
        $this->assertEquals('Paris', $result->getLocality());
82
        $this->assertCount(2, $result->getAdminLevels());
83
        $this->assertEquals('Paris', $result->getAdminLevels()->get(2)->getName());
84
        $this->assertEquals('Ile-de-France', $result->getAdminLevels()->get(1)->getName());
85
        $this->assertEquals('FR', $result->getCountry()->getName());
86
        $this->assertEquals('FR', $result->getCountry()->getCode());
87
88
        $this->assertNull($result->getBounds());
89
        $this->assertNull($result->getStreetNumber());
90
        $this->assertNull($result->getAdminLevels()->get(1)->getCode());
91
        $this->assertNull($result->getTimezone());
92
93
        $result = $results->get(1);
94
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
95
        $this->assertEquals(48.810071, $result->getCoordinates()->getLatitude(), '', 0.01);
96
        $this->assertEquals(2.435937, $result->getCoordinates()->getLongitude(), '', 0.01);
97
        $this->assertEquals('10 Avenue Gambetta', $result->getStreetName());
98
        $this->assertEquals(94700, $result->getPostalCode());
99
        $this->assertEquals('Maisons-Alfort', $result->getLocality());
100
        $this->assertCount(2, $result->getAdminLevels());
101
        $this->assertEquals('Créteil', $result->getAdminLevels()->get(2)->getName());
102
        $this->assertEquals('Ile-de-France', $result->getAdminLevels()->get(1)->getName());
103
        $this->assertEquals('FR', $result->getCountry()->getName());
104
        $this->assertEquals('FR', $result->getCountry()->getCode());
105
106
        $this->assertNull($result->getBounds());
107
        $this->assertNull($result->getStreetNumber());
108
        $this->assertNull($result->getAdminLevels()->get(1)->getCode());
109
        $this->assertNull($result->getTimezone());
110
    }
111
112
    public function testGeocodeWithRealSpecificAddress()
0 ignored issues
show
Coding Style introduced by
testGeocodeWithRealSpecificAddress uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
113
    {
114
        if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
115
            $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
116
        }
117
118
        $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']);
119
120
        $addressBuilder = new AddressBuilder('tests');
121
        $addressBuilder
122
            ->setStreetNumber(4868)
123
            ->setStreetName('Payne Rd')
124
            ->setLocality('Nashville')
125
            ->setSubLocality('Antioch')
126
            ->setAdminLevels([
127
                new AdminLevel(1, 'Tennessee', 'TN'),
128
            ])
129
            ->setPostalCode('37013')
130
            ->setCountry('USA')
131
            ->setCountryCode('US');
132
        $address = $addressBuilder->build();
133
134
        $results = $provider->geocodeQuery(MapQuestGeocodeQuery::createFromAddress($address));
135
136
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
137
        $this->assertCount(1, $results);
138
139
        /** @var Location $result */
140
        $result = $results->first();
141
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
142
        $this->assertEquals(36.062933, $result->getCoordinates()->getLatitude(), '', 0.01);
143
        $this->assertEquals(-86.672811, $result->getCoordinates()->getLongitude(), '', 0.01);
144
        $this->assertEquals('Payne Road', $result->getStreetName());
145
        $this->assertEquals('37013', $result->getPostalCode());
146
        $this->assertEquals('Nashville', $result->getLocality());
147
        $this->assertCount(2, $result->getAdminLevels());
148
        $this->assertEquals('Davidson County', $result->getAdminLevels()->get(2)->getName());
149
        $this->assertEquals('TN', $result->getAdminLevels()->get(1)->getName());
150
        $this->assertEquals('TN', $result->getAdminLevels()->get(1)->getCode());
151
        $this->assertEquals('US', $result->getCountry()->getName());
152
        $this->assertEquals('US', $result->getCountry()->getCode());
153
154
        $this->assertNull($result->getBounds());
155
        $this->assertNull($result->getStreetNumber());
156
        $this->assertNull($result->getTimezone());
157
    }
158
159
    public function testReverseWithRealCoordinates()
0 ignored issues
show
Coding Style introduced by
testReverseWithRealCoordinates uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
160
    {
161
        if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
162
            $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
163
        }
164
165
        $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']);
166
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(54.0484068, -2.7990345));
167
168
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
169
        $this->assertCount(1, $results);
170
171
        /** @var Location $result */
172
        $result = $results->first();
173
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
174
        $this->assertEquals(54.0484068, $result->getCoordinates()->getLatitude(), '', 0.001);
175
        $this->assertEquals(-2.7990345, $result->getCoordinates()->getLongitude(), '', 0.001);
176
        $this->assertEquals('Collegian W.M.C.', $result->getStreetName());
177
        $this->assertEquals('LA1 1NP', $result->getPostalCode());
178
        $this->assertEquals('Lancaster', $result->getLocality());
179
        $this->assertCount(1, $result->getAdminLevels());
180
        $this->assertEquals('England', $result->getAdminLevels()->get(1)->getName());
181
        $this->assertEquals('GB', $result->getCountry()->getName());
182
        $this->assertEquals('GB', $result->getCountry()->getCode());
183
184
        $this->assertNull($result->getBounds());
185
        $this->assertNull($result->getStreetNumber());
186
        $this->assertNull($result->getAdminLevels()->get(1)->getCode());
187
        $this->assertNull($result->getTimezone());
188
    }
189
190
    public function testGeocodeWithCity()
0 ignored issues
show
Coding Style introduced by
testGeocodeWithCity uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
191
    {
192
        if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
193
            $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
194
        }
195
196
        $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']);
197
        $results = $provider->geocodeQuery(GeocodeQuery::create('Hanover'));
198
199
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
200
        $this->assertCount(5, $results);
201
202
        $resultsArray = $results->all();
203
204
        /** @var Location $result */
205
        $result = reset($resultsArray);
206
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
207
        $this->assertEquals(52.374478, $result->getCoordinates()->getLatitude(), '', 0.01);
208
        $this->assertEquals(9.738553, $result->getCoordinates()->getLongitude(), '', 0.01);
209
        $this->assertEquals('Hanover', $result->getLocality());
210
        $this->assertCount(2, $result->getAdminLevels());
211
        $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName());
212
        $this->assertEquals('Lower Saxony', $result->getAdminLevels()->get(1)->getName());
213
        $this->assertEquals('DE', $result->getCountry()->getName());
214
        $this->assertEquals('DE', $result->getCountry()->getCode());
215
216
        /** @var Location $result */
217
        $result = next($resultsArray);
218
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
219
        $this->assertEquals(18.384049, $result->getCoordinates()->getLatitude(), '', 0.01);
220
        $this->assertEquals(-78.131485, $result->getCoordinates()->getLongitude(), '', 0.01);
221
        $this->assertEquals('', $result->getLocality());
222
        $this->assertCount(1, $result->getAdminLevels());
223
        $this->assertEquals('Hanover', $result->getAdminLevels()->get(2)->getName());
224
        $this->assertEquals('JM', $result->getCountry()->getName());
225
        $this->assertEquals('JM', $result->getCountry()->getCode());
226
227
        /** @var Location $result */
228
        $result = next($resultsArray);
229
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
230
        $this->assertEquals(43.703622, $result->getCoordinates()->getLatitude(), '', 0.01);
231
        $this->assertEquals(-72.288666, $result->getCoordinates()->getLongitude(), '', 0.01);
232
        $this->assertEquals('Hanover', $result->getLocality());
233
        $this->assertCount(2, $result->getAdminLevels());
234
        $this->assertEquals('Grafton County', $result->getAdminLevels()->get(2)->getName());
235
        $this->assertEquals('NH', $result->getAdminLevels()->get(1)->getName());
236
        $this->assertEquals('US', $result->getCountry()->getName());
237
        $this->assertEquals('US', $result->getCountry()->getCode());
238
239
        /** @var Location $result */
240
        $result = next($resultsArray);
241
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
242
        $this->assertEquals(39.806325, $result->getCoordinates()->getLatitude(), '', 0.01);
243
        $this->assertEquals(-76.984273, $result->getCoordinates()->getLongitude(), '', 0.01);
244
        $this->assertEquals('Hanover', $result->getLocality());
245
        $this->assertCount(2, $result->getAdminLevels());
246
        $this->assertEquals('York County', $result->getAdminLevels()->get(2)->getName());
247
        $this->assertEquals('PA', $result->getAdminLevels()->get(1)->getName());
248
        $this->assertEquals('US', $result->getCountry()->getName());
249
        $this->assertEquals('US', $result->getCountry()->getCode());
250
251
        /** @var Location $result */
252
        $result = next($resultsArray);
253
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
254
        $this->assertEquals(37.744783, $result->getCoordinates()->getLatitude(), '', 0.01);
255
        $this->assertEquals(-77.446416, $result->getCoordinates()->getLongitude(), '', 0.01);
256
        $this->assertEquals('', $result->getLocality());
257
        $this->assertCount(2, $result->getAdminLevels());
258
        $this->assertEquals('Hanover County', $result->getAdminLevels()->get(2)->getName());
259
        $this->assertEquals('VA', $result->getAdminLevels()->get(1)->getName());
260
        $this->assertEquals('US', $result->getCountry()->getName());
261
        $this->assertEquals('US', $result->getCountry()->getCode());
262
    }
263
264
    public function testGeocodeWithSpecificCity()
0 ignored issues
show
Coding Style introduced by
testGeocodeWithSpecificCity uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
265
    {
266
        if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
267
            $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
268
        }
269
270
        $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']);
271
272
        $addressBuilder = new AddressBuilder('tests');
273
        $addressBuilder->setLocality('Hanover');
274
        $address = $addressBuilder->build();
275
276
        $results = $provider->geocodeQuery(MapQuestGeocodeQuery::createFromAddress($address));
277
278
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
279
        $this->assertCount(5, $results);
280
281
        $resultsArray = $results->all();
282
283
        /** @var Location $result */
284
        $result = reset($resultsArray);
285
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
286
        $this->assertEquals(52.374478, $result->getCoordinates()->getLatitude(), '', 0.01);
287
        $this->assertEquals(9.738553, $result->getCoordinates()->getLongitude(), '', 0.01);
288
        $this->assertEquals('Hanover', $result->getLocality());
289
        $this->assertCount(2, $result->getAdminLevels());
290
        $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName());
291
        $this->assertEquals('Lower Saxony', $result->getAdminLevels()->get(1)->getName());
292
        $this->assertEquals('DE', $result->getCountry()->getName());
293
        $this->assertEquals('DE', $result->getCountry()->getCode());
294
295
        /** @var Location $result */
296
        $result = next($resultsArray);
297
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
298
        $this->assertEquals(43.703622, $result->getCoordinates()->getLatitude(), '', 0.01);
299
        $this->assertEquals(-72.288666, $result->getCoordinates()->getLongitude(), '', 0.01);
300
        $this->assertEquals('Hanover', $result->getLocality());
301
        $this->assertCount(2, $result->getAdminLevels());
302
        $this->assertEquals('Grafton County', $result->getAdminLevels()->get(2)->getName());
303
        $this->assertEquals('NH', $result->getAdminLevels()->get(1)->getName());
304
        $this->assertEquals('US', $result->getCountry()->getName());
305
        $this->assertEquals('US', $result->getCountry()->getCode());
306
307
        /** @var Location $result */
308
        $result = next($resultsArray);
309
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
310
        $this->assertEquals(39.806325, $result->getCoordinates()->getLatitude(), '', 0.01);
311
        $this->assertEquals(-76.984273, $result->getCoordinates()->getLongitude(), '', 0.01);
312
        $this->assertEquals('Hanover', $result->getLocality());
313
        $this->assertCount(2, $result->getAdminLevels());
314
        $this->assertEquals('York County', $result->getAdminLevels()->get(2)->getName());
315
        $this->assertEquals('PA', $result->getAdminLevels()->get(1)->getName());
316
        $this->assertEquals('US', $result->getCountry()->getName());
317
        $this->assertEquals('US', $result->getCountry()->getCode());
318
319
        /** @var Location $result */
320
        $result = next($resultsArray);
321
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
322
        $this->assertEquals(40.661764, $result->getCoordinates()->getLatitude(), '', 0.01);
323
        $this->assertEquals(-75.412404, $result->getCoordinates()->getLongitude(), '', 0.01);
324
        $this->assertEquals('Hanover', $result->getLocality());
325
        $this->assertCount(2, $result->getAdminLevels());
326
        $this->assertEquals('Northampton County', $result->getAdminLevels()->get(2)->getName());
327
        $this->assertEquals('PA', $result->getAdminLevels()->get(1)->getName());
328
        $this->assertEquals('US', $result->getCountry()->getName());
329
        $this->assertEquals('US', $result->getCountry()->getCode());
330
331
        /** @var Location $result */
332
        $result = next($resultsArray);
333
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
334
        $this->assertEquals(40.651401, $result->getCoordinates()->getLatitude(), '', 0.01);
335
        $this->assertEquals(-75.440663, $result->getCoordinates()->getLongitude(), '', 0.01);
336
        $this->assertEquals('Hanover', $result->getLocality());
337
        $this->assertCount(2, $result->getAdminLevels());
338
        $this->assertEquals('Lehigh County', $result->getAdminLevels()->get(2)->getName());
339
        $this->assertEquals('PA', $result->getAdminLevels()->get(1)->getName());
340
        $this->assertEquals('US', $result->getCountry()->getName());
341
        $this->assertEquals('US', $result->getCountry()->getCode());
342
    }
343
344
    public function testGeocodeWithSpecificCityAndBounds()
0 ignored issues
show
Coding Style introduced by
testGeocodeWithSpecificCityAndBounds uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
345
    {
346
        if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
347
            $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
348
        }
349
350
        $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']);
351
352
        $addressBuilder = new AddressBuilder('tests');
353
        $addressBuilder->setLocality('Hanover');
354
        $address = $addressBuilder->build();
355
356
        $query = MapQuestGeocodeQuery::createFromAddress($address)
357
            ->withBounds(new Bounds('39', '-77', '41', '-75'));
358
        $results = $provider->geocodeQuery($query);
359
360
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
361
        $this->assertCount(5, $results);
362
363
        $resultsArray = $results->all();
364
365
        /** @var Location $result */
366
        $result = reset($resultsArray);
367
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
368
        $this->assertEquals('17331', $result->getPostalCode());
369
        $this->assertEquals(39.806325, $result->getCoordinates()->getLatitude(), '', 0.01);
370
        $this->assertEquals(-76.984273, $result->getCoordinates()->getLongitude(), '', 0.01);
371
        $this->assertEquals('Hanover', $result->getLocality());
372
        $this->assertCount(2, $result->getAdminLevels());
373
        $this->assertEquals('York County', $result->getAdminLevels()->get(2)->getName());
374
        $this->assertEquals('PA', $result->getAdminLevels()->get(1)->getName());
375
        $this->assertEquals('US', $result->getCountry()->getName());
376
        $this->assertEquals('US', $result->getCountry()->getCode());
377
378
        /** @var Location $result */
379
        $result = next($resultsArray);
380
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
381
        $this->assertEquals(40.661764, $result->getCoordinates()->getLatitude(), '', 0.01);
382
        $this->assertEquals(-75.412404, $result->getCoordinates()->getLongitude(), '', 0.01);
383
        $this->assertEquals('Hanover', $result->getLocality());
384
        $this->assertCount(2, $result->getAdminLevels());
385
        $this->assertEquals('Northampton County', $result->getAdminLevels()->get(2)->getName());
386
        $this->assertEquals('PA', $result->getAdminLevels()->get(1)->getName());
387
        $this->assertEquals('US', $result->getCountry()->getName());
388
        $this->assertEquals('US', $result->getCountry()->getCode());
389
390
        /** @var Location $result */
391
        $result = next($resultsArray);
392
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
393
        $this->assertEquals(40.651401, $result->getCoordinates()->getLatitude(), '', 0.01);
394
        $this->assertEquals(-75.440663, $result->getCoordinates()->getLongitude(), '', 0.01);
395
        $this->assertEquals('Hanover', $result->getLocality());
396
        $this->assertCount(2, $result->getAdminLevels());
397
        $this->assertEquals('Lehigh County', $result->getAdminLevels()->get(2)->getName());
398
        $this->assertEquals('PA', $result->getAdminLevels()->get(1)->getName());
399
        $this->assertEquals('US', $result->getCountry()->getName());
400
        $this->assertEquals('US', $result->getCountry()->getCode());
401
402
        /** @var Location $result */
403
        $result = next($resultsArray);
404
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
405
        $this->assertEquals('20794:21076', $result->getPostalCode());
406
        $this->assertEquals(39.192885, $result->getCoordinates()->getLatitude(), '', 0.01);
407
        $this->assertEquals(-76.724137, $result->getCoordinates()->getLongitude(), '', 0.01);
408
        $this->assertEquals('Hanover', $result->getLocality());
409
        $this->assertCount(2, $result->getAdminLevels());
410
        $this->assertEquals('Howard County', $result->getAdminLevels()->get(2)->getName());
411
        $this->assertEquals('MD', $result->getAdminLevels()->get(1)->getName());
412
        $this->assertEquals('US', $result->getCountry()->getName());
413
        $this->assertEquals('US', $result->getCountry()->getCode());
414
415
        /** @var Location $result */
416
        $result = next($resultsArray);
417
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
418
        $this->assertEquals(52.374478, $result->getCoordinates()->getLatitude(), '', 0.01);
419
        $this->assertEquals(9.738553, $result->getCoordinates()->getLongitude(), '', 0.01);
420
        $this->assertEquals('Hanover', $result->getLocality());
421
        $this->assertCount(2, $result->getAdminLevels());
422
        $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName());
423
        $this->assertEquals('Lower Saxony', $result->getAdminLevels()->get(1)->getName());
424
        $this->assertEquals('DE', $result->getCountry()->getName());
425
        $this->assertEquals('DE', $result->getCountry()->getCode());
426
    }
427
428
    public function testGeocodeWithCityDistrict()
0 ignored issues
show
Coding Style introduced by
testGeocodeWithCityDistrict uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
429
    {
430
        if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
431
            $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
432
        }
433
434
        $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']);
435
        $results = $provider->geocodeQuery(GeocodeQuery::create('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'));
436
437
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
438
        $this->assertCount(1, $results);
439
440
        /** @var Location $result */
441
        $result = $results->first();
442
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
443
        $this->assertEquals(50.189062, $result->getCoordinates()->getLatitude(), '', 0.01);
444
        $this->assertEquals(8.636567, $result->getCoordinates()->getLongitude(), '', 0.01);
445
        $this->assertEquals('Kalbacher Hauptstraße 10', $result->getStreetName());
446
        $this->assertEquals(60437, $result->getPostalCode());
447
        $this->assertEquals('Frankfurt', $result->getLocality());
448
        $this->assertCount(2, $result->getAdminLevels());
449
        $this->assertEquals('Frankfurt', $result->getAdminLevels()->get(2)->getName());
450
        $this->assertEquals('Hesse', $result->getAdminLevels()->get(1)->getName());
451
        $this->assertEquals('DE', $result->getCountry()->getName());
452
        $this->assertEquals('DE', $result->getCountry()->getCode());
453
454
        $this->assertNull($result->getBounds());
455
        $this->assertNull($result->getStreetNumber());
456
        $this->assertNull($result->getAdminLevels()->get(1)->getCode());
457
        $this->assertNull($result->getTimezone());
458
    }
459
460
    /**
461
     * @expectedException \Geocoder\Exception\UnsupportedOperation
462
     * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses.
463
     */
464
    public function testGeocodeWithLocalhostIPv4()
465
    {
466
        $provider = new MapQuest($this->getMockedHttpClient(), 'api_key');
467
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
468
    }
469
470
    /**
471
     * @expectedException \Geocoder\Exception\UnsupportedOperation
472
     * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses.
473
     */
474
    public function testGeocodeWithLocalhostIPv6()
475
    {
476
        $provider = new MapQuest($this->getMockedHttpClient(), 'api_key');
477
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
478
    }
479
480
    /**
481
     * @expectedException \Geocoder\Exception\UnsupportedOperation
482
     * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses.
483
     */
484
    public function testGeocodeWithRealIPv4()
485
    {
486
        $provider = new MapQuest($this->getMockedHttpClient(), 'api_key');
487
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
488
    }
489
490
    /**
491
     * @expectedException \Geocoder\Exception\UnsupportedOperation
492
     * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses.
493
     */
494
    public function testGeocodeWithRealIPv6()
495
    {
496
        $provider = new MapQuest($this->getMockedHttpClient(), 'api_key');
497
        $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59'));
498
    }
499
}
500