Completed
Branch master (1f9acc)
by Tobias
45:08 queued 20:09
created

AlgoliaPlacesTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 15
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\AlgoliaPlaces\Tests;
14
15
use Geocoder\IntegrationTest\BaseTestCase;
16
use Geocoder\IntegrationTest\CachedResponseClient;
17
use Geocoder\Location;
18
use Geocoder\Query\GeocodeQuery;
19
use Geocoder\Provider\AlgoliaPlaces\AlgoliaPlaces;
20
use Http\Client\Curl\Client as HttplugClient;
21
22
/**
23
 * @author Sébastien Barré <[email protected]>
24
 */
25
class AlgoliaPlacesTest extends BaseTestCase
26
{
27
    protected function getCacheDir()
28
    {
29
        return __DIR__.'/.cached_responses';
30
    }
31
32
    /**
33
     * Get a real HTTP client. If a cache dir is set to a path it will use cached responses.
34
     *
35
     * @return HttpClient
36
     */
37
    protected function getHttpClient($apiKey = null, $appCode = null)
38
    {
39
        if (null !== $cacheDir = $this->getCacheDir()) {
40
            return new CachedResponseClient(new HttplugClient(), $cacheDir, $apiKey, $appCode);
41
        } else {
42
            return new HttplugClient();
43
        }
44
    }
45
46
    public function testGeocodeQuery()
47
    {
48
        if (!isset($_SERVER['ALGOLIA_APP_ID']) || !isset($_SERVER['ALGOLIA_API_KEY'])) {
49
            $this->markTestSkipped('You need to configure the ALGOLIA_APP_ID and ALGOLIA_API_KEY value in phpunit.xml');
50
        }
51
52
        $provider = new AlgoliaPlaces($this->getHttpClient($_SERVER['ALGOLIA_API_KEY'], $_SERVER['ALGOLIA_APP_ID']), $_SERVER['ALGOLIA_API_KEY'], $_SERVER['ALGOLIA_APP_ID']);
53
        $results = $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')->withLocale('fr-FR'));
54
55
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
56
        $this->assertCount(1, $results);
57
58
        /** @var Location $result */
59
        $result = $results->first();
60
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
61
        $this->assertEquals(48.8653, $result->getCoordinates()->getLatitude(), '', 0.01);
62
        $this->assertEquals(2.39844, $result->getCoordinates()->getLongitude(), '', 0.01);
63
64
        $this->assertEquals('10 Avenue Gambetta', $result->getStreetName());
65
        $this->assertEquals(75020, $result->getPostalCode());
66
        $this->assertEquals('Paris 20e Arrondissement', $result->getLocality());
67
        $this->assertEquals('France', $result->getCountry()->getName());
68
        $this->assertEquals('fr', $result->getCountry()->getCode());
69
    }
70
71
    public function testGetName()
72
    {
73
        $provider = new AlgoliaPlaces($this->getMockedHttpClient(), 'appId', 'appCode');
74
        $this->assertEquals('algolia_places', $provider->getName());
75
    }
76
77
    /**
78
     * @expectedException \Geocoder\Exception\InvalidServerResponse
79
     */
80
    public function testGeocodeWithInvalidData()
81
    {
82
        $provider = new AlgoliaPlaces($this->getMockedHttpClient(), 'appId', 'appCode');
83
        $provider->geocodeQuery(GeocodeQuery::create('foobar'));
84
    }
85
86
    /**
87
     * @expectedException \Geocoder\Exception\UnsupportedOperation
88
     * @expectedExceptionMessage The AlgoliaPlaces provider does not support IP addresses, only street addresses.
89
     */
90
    public function testGeocodeIpv4()
91
    {
92
        if (!isset($_SERVER['ALGOLIA_APP_ID']) || !isset($_SERVER['ALGOLIA_API_KEY'])) {
93
            $this->markTestSkipped('You need to configure the ALGOLIA_APP_ID and ALGOLIA_API_KEY value in phpunit.xml');
94
        }
95
96
        $provider = new AlgoliaPlaces($this->getHttpClient(), $_SERVER['ALGOLIA_API_KEY'], $_SERVER['ALGOLIA_APP_ID']);
97
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
98
    }
99
100
    /**
101
     * @expectedException \Geocoder\Exception\UnsupportedOperation
102
     * @expectedExceptionMessage The AlgoliaPlaces provider does not support IP addresses, only street addresses.
103
     */
104
    public function testGeocodeWithLocalhostIPv6()
105
    {
106
        $provider = new AlgoliaPlaces($this->getMockedHttpClient(), 'appId', 'appCode');
107
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
108
    }
109
110
    /**
111
     * @expectedException \Geocoder\Exception\UnsupportedOperation
112
     * @expectedExceptionMessage The AlgoliaPlaces provider does not support IP addresses, only street addresses.
113
     */
114
    public function testGeocodeWithRealIPv6()
115
    {
116
        if (!isset($_SERVER['ALGOLIA_APP_ID']) || !isset($_SERVER['ALGOLIA_API_KEY'])) {
117
            $this->markTestSkipped('You need to configure the ALGOLIA_APP_ID and ALGOLIA_API_KEY value in phpunit.xml');
118
        }
119
120
        $provider = new AlgoliaPlaces($this->getHttpClient(), $_SERVER['ALGOLIA_API_KEY'], $_SERVER['ALGOLIA_APP_ID']);
121
        $provider->geocodeQuery(GeocodeQuery::create('::ffff:88.188.221.14'));
122
    }
123
}
124