Completed
Push — master ( a4f6ba...3014ff )
by Tobias
02:45
created

LocationIQTest::testGetNodeStreetName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
rs 9.4285
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\LocationIQ\Tests;
14
15
use Geocoder\Collection;
16
use Geocoder\IntegrationTest\BaseTestCase;
17
use Geocoder\Location;
18
use Geocoder\Provider\LocationIQ\LocationIQ;
19
use Geocoder\Query\GeocodeQuery;
20
use Geocoder\Query\ReverseQuery;
21
22
class LocationIQTest extends BaseTestCase
23
{
24
    protected function getCacheDir()
25
    {
26
        return __DIR__.'/.cached_responses';
27
    }
28
29
    /**
30
     * @expectedException \Geocoder\Exception\InvalidServerResponse
31
     */
32
    public function testGeocodeWithAddressGetsEmptyContent()
33
    {
34
        $provider = new LocationIQ($this->getMockedHttpClient('<foo></foo>'), $_SERVER['LOCATIONIQ_API_KEY']);
35
        $provider->geocodeQuery(GeocodeQuery::create('Läntinen Pitkäkatu 35, Turku'));
36
    }
37
38
    /**
39
     * @expectedException \Geocoder\Exception\InvalidServerResponse
40
     */
41
    public function testGeocodeWithAddressGetsEmptyXML()
42
    {
43
        $emptyXML = <<<'XML'
44
<?xml version="1.0" encoding="utf-8"?><searchresults_empty></searchresults_empty>
45
XML;
46
47
        $provider = new LocationIQ($this->getMockedHttpClient($emptyXML), $_SERVER['LOCATIONIQ_API_KEY']);
48
        $provider->geocodeQuery(GeocodeQuery::create('Läntinen Pitkäkatu 35, Turku'));
49
    }
50
51
    public function testReverseWithCoordinatesGetsError()
52
    {
53
        $errorXml = <<<'XML'
54
<?xml version="1.0" encoding="UTF-8" ?>
55
<reversegeocode querystring='format=xml&amp;lat=-80.000000&amp;lon=-170.000000&amp;addressdetails=1'>
56
    <error>Unable to geocode</error>
57
</reversegeocode>
58
XML;
59
60
        $provider = new LocationIQ($this->getMockedHttpClient($errorXml), $_SERVER['LOCATIONIQ_API_KEY']);
61
62
        $result = $provider->reverseQuery(ReverseQuery::fromCoordinates(-80.000000, -170.000000));
63
64
        $this->assertInstanceOf(Collection::class, $result);
65
        $this->assertEquals(0, $result->count());
66
    }
67
68
    public function testGetNodeStreetName()
69
    {
70
        $provider = new LocationIQ($this->getHttpClient($_SERVER['LOCATIONIQ_API_KEY']), $_SERVER['LOCATIONIQ_API_KEY']);
71
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.86, 2.35));
72
73
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
74
        $this->assertCount(1, $results);
75
76
        /** @var Location $result */
77
        $result = $results->first();
78
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
79
        $this->assertEquals('Rue Quincampoix', $result->getStreetName());
80
    }
81
}
82