MapTilerTest::testGeocodeWithLocalhostIPv6()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
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\MapTiler\Tests;
14
15
use Geocoder\IntegrationTest\BaseTestCase;
16
use Geocoder\Model\Bounds;
17
use Geocoder\Provider\MapTiler\MapTiler;
18
use Geocoder\Query\GeocodeQuery;
19
use Geocoder\Query\ReverseQuery;
20
21
/**
22
 * @author Jonathan Beliën
23
 */
24
class MapTilerTest extends BaseTestCase
25
{
26
    protected function getCacheDir(): string
27
    {
28
        return __DIR__.'/.cached_responses';
29
    }
30
31
    public function testGeocodeWithLocalhostIPv4(): void
32
    {
33
        if (!isset($_SERVER['MAPTILER_KEY'])) {
34
            $this->markTestSkipped('You need to configure the MAPTILER_KEY value in phpunit.xml');
35
        }
36
37
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
38
        $this->expectExceptionMessage('The MapTiler provider does not support IP addresses.');
39
40
        $provider = new MapTiler($this->getMockedHttpClient(), $_SERVER['MAPTILER_KEY']);
41
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
42
    }
43
44
    public function testGeocodeWithLocalhostIPv6(): void
45
    {
46
        if (!isset($_SERVER['MAPTILER_KEY'])) {
47
            $this->markTestSkipped('You need to configure the MAPTILER_KEY value in phpunit.xml');
48
        }
49
50
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
51
        $this->expectExceptionMessage('The MapTiler provider does not support IP addresses.');
52
53
        $provider = new MapTiler($this->getMockedHttpClient(), $_SERVER['MAPTILER_KEY']);
54
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
55
    }
56
57
    public function testGeocodeWithRealIPv6(): void
58
    {
59
        if (!isset($_SERVER['MAPTILER_KEY'])) {
60
            $this->markTestSkipped('You need to configure the MAPTILER_KEY value in phpunit.xml');
61
        }
62
63
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
64
        $this->expectExceptionMessage('The MapTiler provider does not support IP addresses.');
65
66
        $provider = new MapTiler($this->getMockedHttpClient(), $_SERVER['MAPTILER_KEY']);
67
        $provider->geocodeQuery(GeocodeQuery::create('::ffff:88.188.221.14'));
68
    }
69
70
    public function testGeocodeQueryStreet(): void
71
    {
72
        if (!isset($_SERVER['MAPTILER_KEY'])) {
73
            $this->markTestSkipped('You need to configure the MAPTILER_KEY value in phpunit.xml');
74
        }
75
76
        $query = GeocodeQuery::create('Avenue Gambetta, Paris, France');
77
        $query = $query->withBounds(new Bounds(2.293039, 48.821036, 2.406336, 48.894899));
78
79
        $provider = new MapTiler($this->getHttpClient($_SERVER['MAPTILER_KEY']), $_SERVER['MAPTILER_KEY']);
80
        $results = $provider->geocodeQuery($query);
81
82
        $this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results);
83
        // $this->assertCount(1, $results);
84
85
        /** @var \Geocoder\Model\Address $result */
86
        $result = $results->first();
87
        $this->assertInstanceOf(\Geocoder\Model\Address::class, $result);
88
        $this->assertEqualsWithDelta(48.8658863, $result->getCoordinates()->getLatitude(), 0.00001);
89
        $this->assertEqualsWithDelta(2.3993232, $result->getCoordinates()->getLongitude(), 0.00001);
90
        $this->assertEquals('Avenue Gambetta', $result->getStreetName());
91
        $this->assertEquals('Paris', $result->getLocality());
92
        $this->assertEquals('France', $result->getCountry());
93
    }
94
95
    public function testGeocodeQueryCity(): void
96
    {
97
        if (!isset($_SERVER['MAPTILER_KEY'])) {
98
            $this->markTestSkipped('You need to configure the MAPTILER_KEY value in phpunit.xml');
99
        }
100
101
        $query = GeocodeQuery::create('Paris, France');
102
103
        $provider = new MapTiler($this->getHttpClient($_SERVER['MAPTILER_KEY']), $_SERVER['MAPTILER_KEY']);
104
        $results = $provider->geocodeQuery($query);
105
106
        $this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results);
107
        // $this->assertCount(1, $results);
108
109
        /** @var \Geocoder\Model\Address $result */
110
        $result = $results->get(1);
111
        $this->assertInstanceOf(\Geocoder\Model\Address::class, $result);
112
        $this->assertEqualsWithDelta(48.85881, $result->getCoordinates()->getLatitude(), 0.00001);
113
        $this->assertEqualsWithDelta(2.320031, $result->getCoordinates()->getLongitude(), 0.00001);
114
        $this->assertEquals('Paris', $result->getLocality());
115
        $this->assertEquals('France', $result->getCountry());
116
    }
117
118
    public function testReverseQuery(): void
119
    {
120
        if (!isset($_SERVER['MAPTILER_KEY'])) {
121
            $this->markTestSkipped('You need to configure the MAPTILER_KEY value in phpunit.xml');
122
        }
123
124
        $query = ReverseQuery::fromCoordinates(47.3774434, 8.528509);
125
126
        $provider = new MapTiler($this->getHttpClient($_SERVER['MAPTILER_KEY']), $_SERVER['MAPTILER_KEY']);
127
        $results = $provider->reverseQuery($query);
128
129
        $this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results);
130
        // $this->assertCount(1, $results);
131
132
        /** @var \Geocoder\Model\Address $result */
133
        $result = $results->first();
134
        $this->assertInstanceOf(\Geocoder\Model\Address::class, $result);
135
        $this->assertEqualsWithDelta(47.3774434, $result->getCoordinates()->getLatitude(), 0.00001);
136
        $this->assertEqualsWithDelta(8.528509, $result->getCoordinates()->getLongitude(), 0.00001);
137
        $this->assertEquals('Zurich', $result->getLocality());
138
        $this->assertEquals('Switzerland', $result->getCountry());
139
    }
140
}
141