|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Geocoder\Tests\Provider; |
| 4 | + |
| 5 | +use Geocoder\Tests\TestCase; |
| 6 | +use Geocoder\Provider\NaverProvider; |
| 7 | + |
| 8 | +/** |
| 9 | + * @author Antoine Corcy <[email protected]> |
| 10 | + */ |
| 11 | +class NaverProviderTest extends TestCase |
| 12 | +{ |
| 13 | + public function testGetName() |
| 14 | + { |
| 15 | + $provider = new NaverProvider($this->getMockAdapter($this->never()), 'api_key'); |
| 16 | + $this->assertEquals('naver', $provider->getName()); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * @expectedException \RuntimeException |
| 21 | + */ |
| 22 | + public function testGetGeocodedDataWithNullApiKey() |
| 23 | + { |
| 24 | + $provider = new NaverProvider($this->getMockAdapter($this->never()), null); |
| 25 | + $provider->getGeocodedData('foo'); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @expectedException \Geocoder\Exception\NoResultException |
| 30 | + * @expectedExceptionMessage Could not execute query http://openapi.map.naver.com/api/geocode.php?key=api_key&encoding=utf-8&coord=latlng&query=%EC%84%9C%EC%9A%B8 |
| 31 | + */ |
| 32 | + public function testGetGeocodedDataWithAddress() |
| 33 | + { |
| 34 | + $provider = new NaverProvider($this->getMockAdapter(), 'api_key'); |
| 35 | + $provider->getGeocodedData('서울'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @expectedException \Geocoder\Exception\NoResultException |
| 40 | + * @expectedExceptionMessage Could not execute query http://openapi.map.naver.com/api/geocode.php?key=api_key&encoding=utf-8&coord=latlng&query=foobar |
| 41 | + */ |
| 42 | + public function testGetGeocodedDataWithAddressGetsZeroResult() |
| 43 | + { |
| 44 | + $xml = <<<XML |
| 45 | +<geocode><userquery>foobar</userquery><total>0</total></geocode> |
| 46 | +XML; |
| 47 | + |
| 48 | + $provider = new NaverProvider($this->getMockAdapterReturns($xml), 'api_key'); |
| 49 | + $provider->getGeocodedData('foobar'); |
| 50 | + } |
| 51 | + |
| 52 | + public function testGetGeocodedDataWithAddressGetsOneResult() |
| 53 | + { |
| 54 | + $xml = <<<XML |
| 55 | +<geocode> |
| 56 | + <userquery>경북 영천시 임고면 매호리 143-9번지</userquery> |
| 57 | + <total>1</total> |
| 58 | + <item> |
| 59 | + <point> |
| 60 | + <x>128.9675615</x> |
| 61 | + <y>36.0062826</y> |
| 62 | + </point> |
| 63 | + <address>경상북도 영천시 임고면 매호리 143-9</address> |
| 64 | + <addrdetail> |
| 65 | + <sido> |
| 66 | + 경상북도 |
| 67 | + <sigugun> |
| 68 | + 영천시 임고면 |
| 69 | + <dongmyun> |
| 70 | + 매호리 |
| 71 | + <rest>143-9</rest> |
| 72 | + </dongmyun> |
| 73 | + </sigugun> |
| 74 | + </sido> |
| 75 | + </addrdetail> |
| 76 | + </item> |
| 77 | +</geocode> |
| 78 | +XML; |
| 79 | + |
| 80 | + $provider = new NaverProvider($this->getMockAdapterReturns($xml), 'api_key'); |
| 81 | + $results = $provider->getGeocodedData('경북 영천시 임고면 매호리 143-9번지'); |
| 82 | + |
| 83 | + $this->assertInternalType('array', $results); |
| 84 | + $this->assertCount(1, $results); |
| 85 | + |
| 86 | + $result = $results[0]; |
| 87 | + $this->assertInternalType('array', $result); |
| 88 | + $this->assertEquals(128.9675615, $result['latitude'], '', 0.01); |
| 89 | + $this->assertEquals(36.0062826, $result['longitude'], '', 0.01); |
| 90 | + $this->assertNull($result['bounds']); |
| 91 | + |
| 92 | + $this->assertEquals('143-9', $result['streetNumber']); |
| 93 | + $this->assertEquals('매호리', $result['streetName']); |
| 94 | + $this->assertEquals('영천시 임고면', $result['city']); |
| 95 | + $this->assertNull($result['zipcode']); |
| 96 | + $this->assertNull($result['cityDistrict']); |
| 97 | + $this->assertNull($result['county']); |
| 98 | + $this->assertNull($result['countyCode']); |
| 99 | + $this->assertEquals('경상북도', $result['region']); |
| 100 | + $this->assertNull($result['regionCode']); |
| 101 | + $this->assertNull($result['country']); |
| 102 | + $this->assertNull($result['countryCode']); |
| 103 | + $this->assertNull($result['timezone']); |
| 104 | + } |
| 105 | + |
| 106 | + public function testGetGeocodedDataWithRealAddress() |
| 107 | + { |
| 108 | + if (!isset($_SERVER['NAVER_API_KEY'])) { |
| 109 | + $this->markTestSkipped('You need to configure the NAVER_API_KEY value in phpunit.xml'); |
| 110 | + } |
| 111 | + |
| 112 | + $provider = new NaverProvider($this->getAdapter(), $_SERVER['NAVER_API_KEY']); |
| 113 | + $results = $provider->getGeocodedData('경북 영천시 임고면 매호리 143-9번지'); |
| 114 | + |
| 115 | + $this->assertInternalType('array', $results); |
| 116 | + $this->assertCount(1, $results); |
| 117 | + |
| 118 | + $result = $results[0]; |
| 119 | + $this->assertInternalType('array', $result); |
| 120 | + $this->assertEquals(128.9675615, $result['latitude'], '', 0.01); |
| 121 | + $this->assertEquals(36.0062826, $result['longitude'], '', 0.01); |
| 122 | + $this->assertNull($result['bounds']); |
| 123 | + |
| 124 | + $this->assertEquals('143-9', $result['streetNumber']); |
| 125 | + $this->assertEquals('매호리', $result['streetName']); |
| 126 | + $this->assertEquals('영천시 임고면', $result['city']); |
| 127 | + $this->assertNull($result['zipcode']); |
| 128 | + $this->assertNull($result['cityDistrict']); |
| 129 | + $this->assertNull($result['county']); |
| 130 | + $this->assertNull($result['countyCode']); |
| 131 | + $this->assertEquals('경상북도', $result['region']); |
| 132 | + $this->assertNull($result['regionCode']); |
| 133 | + $this->assertNull($result['country']); |
| 134 | + $this->assertNull($result['countryCode']); |
| 135 | + $this->assertNull($result['timezone']); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @expectedException \Geocoder\Exception\UnsupportedException |
| 140 | + * @expectedExceptionMessage The NaverProvider does not support IP addresses. |
| 141 | + */ |
| 142 | + public function testGetGeocodedDataWithIPv4() |
| 143 | + { |
| 144 | + $provider = new NaverProvider($this->getAdapter(), 'api_key'); |
| 145 | + $provider->getGeocodedData('74.200.247.59'); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @expectedException \Geocoder\Exception\UnsupportedException |
| 150 | + * @expectedExceptionMessage The NaverProvider does not support IP addresses. |
| 151 | + */ |
| 152 | + public function testGetGeocodedDataWithIPv6() |
| 153 | + { |
| 154 | + $provider = new NaverProvider($this->getAdapter(), 'api_key'); |
| 155 | + $provider->getGeocodedData('::ffff:74.200.247.59'); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @expectedException \Geocoder\Exception\UnsupportedException |
| 160 | + * @expectedExceptionMessage The NaverProvider is not able to do reverse geocoding. |
| 161 | + */ |
| 162 | + public function testGetReverseData() |
| 163 | + { |
| 164 | + $provider = new NaverProvider($this->getAdapter(), 'api_key'); |
| 165 | + $provider->getReversedData(array(1, 2)); |
| 166 | + } |
| 167 | +} |
0 commit comments