1 |
|
<?php |
2 |
|
/** |
3 |
|
* Unit testing for Services_OpenStreetMap_Nominatim class. |
4 |
|
* |
5 |
|
* PHP Version 5 |
6 |
|
* |
7 |
|
* @category Services |
8 |
|
* @package Services_OpenStreetMap |
9 |
|
* @subpackage UnitTesting |
10 |
|
* @author Ken Guest <kguest@php.net> |
11 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
12 |
|
* @version Release: @package_version@ |
13 |
|
* @link NominatimTest.php |
14 |
|
*/ |
15 |
|
|
16 |
|
$version = '@package_version@'; |
17 |
|
if (strstr($version, 'package_version')) { |
18 |
|
set_include_path(dirname(dirname(__FILE__)) . ':' . get_include_path()); |
19 |
|
} |
20 |
|
|
21 |
|
require_once 'Services/OpenStreetMap.php'; |
22 |
|
|
23 |
|
require_once 'HTTP/Request2.php'; |
24 |
|
require_once 'HTTP/Request2/Adapter/Mock.php'; |
25 |
|
require_once 'PHPUnit/Framework/TestCase.php'; |
26 |
|
|
27 |
|
|
28 |
|
/** |
29 |
|
* Test Services_OpenStreetMap_Config functionality and how it's used |
30 |
|
* throughout the Services_OpenStreetMap package. |
31 |
|
* |
32 |
|
* @category Services |
33 |
|
* @package Services_OpenStreetMap |
34 |
|
* @subpackage UnitTesting |
35 |
|
* @author Ken Guest <kguest@php.net> |
36 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
37 |
|
* @link ConfigTest.php |
38 |
|
*/ |
39 |
|
class NominatimTest extends PHPUnit_Framework_TestCase |
40 |
|
{ |
41 |
|
|
42 |
|
/** |
43 |
|
* test the getCoordsOfPlace method. |
44 |
|
* |
45 |
|
* @return void |
46 |
|
*/ |
47 |
|
public function testGetCoordsOfPlace() |
48 |
|
{ |
49 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
50 |
1 |
$mock->addResponse( |
51 |
1 |
fopen(__DIR__ . '/responses/nominatim_search_limerick.xml', 'rb') |
52 |
1 |
); |
53 |
|
|
54 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
55 |
1 |
$this->AssertEquals( |
56 |
1 |
$osm->getCoordsOfPlace('Limerick, Ireland'), |
57 |
1 |
array('lat'=> '52.6612577', 'lon'=> '-8.6302084') |
58 |
1 |
); |
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
|
* An exception should be thrown if the place of interest can not be |
63 |
|
* found. |
64 |
|
* |
65 |
|
* @expectedException Services_OpenStreetMap_Exception |
66 |
|
* @expectedExceptionMessage Could not get coords for Neeenaaa, Ireland |
67 |
|
* |
68 |
|
* @return void |
69 |
|
*/ |
70 |
|
public function testGetCoordsOfNonExistentPlace() |
71 |
|
{ |
72 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
73 |
1 |
$mock->addResponse( |
74 |
1 |
fopen( |
75 |
1 |
__DIR__ . '/responses/nominatim_search_neeenaaa.xml', |
76 |
|
'rb' |
77 |
1 |
) |
78 |
1 |
); |
79 |
|
|
80 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
81 |
1 |
$osm->getCoordsOfPlace('Neeenaaa, Ireland'); |
82 |
|
} |
83 |
|
|
84 |
|
public function testSetFormatHtml() |
85 |
|
{ |
86 |
1 |
$osm = new Services_OpenStreetMap(); |
87 |
1 |
$transport = $osm->getTransport(); |
88 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($transport); |
89 |
1 |
$nominatim->setFormat('html'); |
90 |
1 |
$this->assertEquals($nominatim->getFormat(), 'html'); |
91 |
|
} |
92 |
|
|
93 |
|
public function testSetFormatJson() |
94 |
|
{ |
95 |
1 |
$osm = new Services_OpenStreetMap(); |
96 |
1 |
$transport = $osm->getTransport(); |
97 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($transport); |
98 |
1 |
$nominatim->setFormat('json'); |
99 |
1 |
$this->assertEquals($nominatim->getFormat(), 'json'); |
100 |
|
} |
101 |
|
|
102 |
|
public function testSetFormatXml() |
103 |
|
{ |
104 |
1 |
$osm = new Services_OpenStreetMap(); |
105 |
1 |
$transport = $osm->getTransport(); |
106 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($transport); |
107 |
1 |
$nominatim->setFormat('xml'); |
108 |
1 |
$this->assertEquals($nominatim->getFormat(), 'xml'); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
|
* Check that an exception is thrown when attempting to set format to an |
113 |
|
* unrecognised value. |
114 |
|
* |
115 |
|
* @expectedException Services_OpenStreetMap_RuntimeException |
116 |
|
* @expectedExceptionMessage Unrecognised format (xhtml) |
117 |
|
* |
118 |
|
* @return void |
119 |
|
*/ |
120 |
|
public function testInvalidFormat() |
121 |
|
{ |
122 |
1 |
$osm = new Services_OpenStreetMap(); |
123 |
1 |
$transport = $osm->getTransport(); |
124 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($transport); |
125 |
1 |
$nominatim->setFormat('xhtml'); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
public function testSetLimit() |
130 |
|
{ |
131 |
1 |
$osm = new Services_OpenStreetMap(); |
132 |
1 |
$transport = $osm->getTransport(); |
133 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($transport); |
134 |
1 |
$nominatim->setLimit(1); |
135 |
1 |
$this->assertEquals($nominatim->getLimit(), 1); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
|
* Check that an exception is thrown when attempting to set limit to an |
140 |
|
* unrecognised value. |
141 |
|
* |
142 |
|
* @expectedException Services_OpenStreetMap_RuntimeException |
143 |
|
* @expectedExceptionMessage Limit must be a numeric value |
144 |
|
* |
145 |
|
* @return void |
146 |
|
*/ |
147 |
|
public function testSetInvalidLimit() |
148 |
|
{ |
149 |
1 |
$osm = new Services_OpenStreetMap(); |
150 |
1 |
$transport = $osm->getTransport(); |
151 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($transport); |
152 |
1 |
$nominatim->setLimit('one'); |
153 |
|
} |
154 |
|
|
155 |
|
public function testJsonSearch() |
156 |
|
{ |
157 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
158 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/search.json', 'rb')); |
159 |
|
|
160 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
161 |
|
|
162 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($osm->getTransport()); |
163 |
1 |
$nominatim->setFormat('json'); |
164 |
1 |
$place = $nominatim->search('Limerick, Ireland', 1); |
165 |
1 |
$this->assertEquals($place[0]->class, 'place'); |
166 |
1 |
$this->assertEquals($place[0]->type, 'city'); |
167 |
1 |
$this->assertEquals($place[0]->osm_type, 'node'); |
168 |
|
} |
169 |
|
|
170 |
|
public function testHtmlSearch() |
171 |
|
{ |
172 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
173 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/search.html', 'rb')); |
174 |
|
|
175 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
176 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($osm->getTransport()); |
177 |
1 |
$nominatim->setFormat('html'); |
178 |
1 |
$place = $nominatim->search('Limerick, Ireland', 1); |
179 |
1 |
$this->assertNotNull($place); |
180 |
|
} |
181 |
|
|
182 |
|
public function testSetServer() |
183 |
|
{ |
184 |
1 |
$osm = new Services_OpenStreetMap(); |
185 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($osm->getTransport()); |
186 |
|
|
187 |
1 |
$this->assertEquals( |
188 |
1 |
$nominatim->getServer(), |
189 |
|
'http://nominatim.openstreetmap.org/' |
190 |
1 |
); |
191 |
1 |
$this->assertEquals( |
192 |
1 |
$nominatim->setServer('mapquest')->getServer(), |
193 |
|
'http://open.mapquestapi.com/nominatim/v1/' |
194 |
1 |
); |
195 |
1 |
$this->assertEquals( |
196 |
1 |
$nominatim->setServer('nominatim')->getServer(), |
197 |
|
'http://nominatim.openstreetmap.org/' |
198 |
1 |
); |
199 |
1 |
$this->assertEquals( |
200 |
1 |
$nominatim->setServer('http://nominatim.example.com/')->getServer(), |
201 |
|
'http://nominatim.example.com/' |
202 |
1 |
); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
|
* Check that an exception is thrown when attempting to set limit to an |
207 |
|
* unrecognised value. |
208 |
|
* |
209 |
|
* @expectedException Services_OpenStreetMap_RuntimeException |
210 |
|
* @expectedExceptionMessage Server endpoint invalid |
211 |
|
* |
212 |
|
* @return void |
213 |
|
*/ |
214 |
|
public function testSetInvalidServerURL() |
215 |
|
{ |
216 |
1 |
$osm = new Services_OpenStreetMap(); |
217 |
1 |
$nominatim = new Services_OpenStreetMap_Nominatim($osm->getTransport()); |
218 |
1 |
$nominatim->setServer('invalid'); |
219 |
|
} |
220 |
|
} |
221 |
|
?> |