| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Unit test class for Way related functionality. |
| 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 WayTest.php |
| 14 |
|
* @todo |
| 15 |
|
*/ |
| 16 |
|
|
| 17 |
|
$version = '@package_version@'; |
| 18 |
|
if (strstr($version, 'package_version')) { |
| 19 |
|
set_include_path(dirname(dirname(__FILE__)) . ':' . get_include_path()); |
| 20 |
|
} |
| 21 |
|
|
| 22 |
|
require_once 'Services/Openstreetmap.php'; |
| 23 |
|
|
| 24 |
|
require_once 'HTTP/Request2.php'; |
| 25 |
|
require_once 'HTTP/Request2/Adapter/Mock.php'; |
| 26 |
|
|
| 27 |
|
/** |
| 28 |
|
* Unit test class for manipulation of Services_Openstreetmap_Way. |
| 29 |
|
* |
| 30 |
|
* @category Services |
| 31 |
|
* @package Services_Openstreetmap |
| 32 |
|
* @subpackage UnitTesting |
| 33 |
|
* @author Ken Guest <kguest@php.net> |
| 34 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
| 35 |
|
* @link WayTest.php |
| 36 |
|
*/ |
| 37 |
|
class WayTest extends PHPUnit_Framework_TestCase |
| 38 |
|
{ |
| 39 |
|
|
| 40 |
|
public function testGetWay() |
| 41 |
|
{ |
| 42 |
1 |
$id = 25978036; |
| 43 |
|
|
| 44 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 45 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 46 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way.xml', 'rb')); |
| 47 |
|
|
| 48 |
|
$config = array( |
| 49 |
1 |
'adapter' => $mock, |
| 50 |
|
'server' => 'http://api06.dev.openstreetmap.org/' |
| 51 |
1 |
); |
| 52 |
1 |
$osm = new Services_Openstreetmap($config); |
| 53 |
1 |
$way = $osm->getWay($id); |
| 54 |
1 |
$getTags = $way->getTags(); |
| 55 |
1 |
$this->assertEquals($id, (int) $way->getAttributes()->id); |
| 56 |
1 |
$this->assertEquals($getTags['highway'], 'service'); |
| 57 |
1 |
$this->assertEquals($way->getUid(), 1379); |
| 58 |
1 |
$this->assertEquals($way->getVersion(), 1); |
| 59 |
1 |
$this->assertEquals($way->getUser(), "AndrewMcCarthy"); |
| 60 |
1 |
$this->assertEquals($way->getNodes(), array("283393706","283393707")); |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
public function testGetClosedWay() |
| 64 |
|
{ |
| 65 |
1 |
$id = 18197393; |
| 66 |
|
|
| 67 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 68 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 69 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way_closed.xml', 'rb')); |
| 70 |
|
|
| 71 |
|
$config = array( |
| 72 |
1 |
'adapter' => $mock, |
| 73 |
|
'server' => 'http://api06.dev.openstreetmap.org/' |
| 74 |
1 |
); |
| 75 |
1 |
$osm = new Services_Openstreetmap($config); |
| 76 |
1 |
$way = $osm->getWay($id); |
| 77 |
1 |
$getTags = $way->getTags(); |
| 78 |
1 |
$this->assertEquals($id, (int) $way->getAttributes()->id); |
| 79 |
1 |
$this->assertEquals($getTags['building'], 'yes'); |
| 80 |
1 |
$this->assertTrue($way->isClosed()); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
public function testOpenWay() |
| 84 |
|
{ |
| 85 |
1 |
$id = 23010474; |
| 86 |
|
|
| 87 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 88 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 89 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way_open.xml', 'rb')); |
| 90 |
|
|
| 91 |
|
$config = array( |
| 92 |
1 |
'adapter' => $mock, |
| 93 |
|
'server' => 'http://api06.dev.openstreetmap.org' |
| 94 |
1 |
); |
| 95 |
1 |
$osm = new Services_Openstreetmap($config); |
| 96 |
1 |
$way = $osm->getWay($id); |
| 97 |
1 |
$getTags = $way->getTags(); |
| 98 |
1 |
$this->assertEquals($id, (int) $way->getAttributes()->id); |
| 99 |
1 |
$this->assertFalse($way->isClosed()); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
public function testWayWithOneNode() |
| 103 |
|
{ |
| 104 |
1 |
$id = 23010475; |
| 105 |
|
|
| 106 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 107 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 108 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way_one_node.xml', 'rb')); |
| 109 |
|
|
| 110 |
|
$config = array( |
| 111 |
1 |
'adapter' => $mock, |
| 112 |
|
'server' => 'http://api06.dev.openstreetmap.org' |
| 113 |
1 |
); |
| 114 |
1 |
$osm = new Services_Openstreetmap($config); |
| 115 |
1 |
$way = $osm->getWay($id); |
| 116 |
1 |
$getTags = $way->getTags(); |
| 117 |
1 |
$this->assertEquals($id, (int) $way->getAttributes()->id); |
| 118 |
1 |
$this->assertFalse($way->isClosed()); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
public function testAddNodeToWay() |
| 122 |
|
{ |
| 123 |
1 |
$id = 23010474; |
| 124 |
|
|
| 125 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 126 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 127 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way_open.xml', 'rb')); |
| 128 |
|
|
| 129 |
|
$config = array( |
| 130 |
1 |
'adapter' => $mock, |
| 131 |
|
'server' => 'http://api06.dev.openstreetmap.org' |
| 132 |
1 |
); |
| 133 |
1 |
$osm = new Services_Openstreetmap($config); |
| 134 |
1 |
$way = $osm->getWay($id); |
| 135 |
|
|
| 136 |
1 |
$lat = 52.8638729; |
| 137 |
1 |
$lon = -8.1983611; |
| 138 |
1 |
$nodes = $way->getNodes(); |
| 139 |
1 |
$node = $osm->createNode($lat, $lon); |
| 140 |
1 |
$way->addNode($node); |
| 141 |
1 |
$lat = $lat + 0.00002; |
| 142 |
1 |
$node = $osm->createNode($lat, $lon); |
| 143 |
1 |
$way->addNode($node); |
| 144 |
1 |
$this->assertEquals(sizeof($nodes) + 2, sizeof($way->getNodes())); |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
/** |
| 148 |
|
* @expectedException InvalidArgumentException |
| 149 |
|
* @expectedExceptionMessage $node must be either an instance of Services_Openstreetmap_Node or a numeric id |
| 150 |
|
*/ |
| 151 |
|
public function testIncorrectTypeToRemoveNode() |
| 152 |
|
{ |
| 153 |
1 |
$id = 23010474; |
| 154 |
|
|
| 155 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 156 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 157 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way_open.xml', 'rb')); |
| 158 |
|
|
| 159 |
|
$config = array( |
| 160 |
1 |
'adapter' => $mock, |
| 161 |
|
'server' => 'http://api06.dev.openstreetmap.org' |
| 162 |
1 |
); |
| 163 |
1 |
$osm = new Services_Openstreetmap($config); |
| 164 |
1 |
$way = $osm->getWay($id); |
| 165 |
1 |
$way->removeNode("way5432456"); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
public function testRemoveNode() |
| 169 |
|
{ |
| 170 |
1 |
$id = 23010474; |
| 171 |
|
|
| 172 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 173 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 174 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way_open.xml', 'rb')); |
| 175 |
|
|
| 176 |
|
$config = array( |
| 177 |
1 |
'adapter' => $mock, |
| 178 |
|
'server' => 'http://api06.dev.openstreetmap.org' |
| 179 |
1 |
); |
| 180 |
1 |
$osm = new Services_Openstreetmap($config); |
| 181 |
1 |
$way = $osm->getWay($id); |
| 182 |
1 |
$nb = count($way->getNodes()); |
| 183 |
1 |
$way->removeNode(248081798); |
| 184 |
1 |
$way->setTag('note', 'testing...'); |
| 185 |
1 |
$na = count($way->getNodes()); |
| 186 |
1 |
$this->assertEquals($na, $nb - 1); |
| 187 |
|
} |
| 188 |
|
|
| 189 |
|
public function testGetWays() |
| 190 |
|
{ |
| 191 |
1 |
$wayId = 30357328; |
| 192 |
1 |
$way2Id = 30357329; |
| 193 |
|
|
| 194 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 195 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 196 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way_30357328_30357329.xml', 'rb')); |
| 197 |
|
$config = array( |
| 198 |
1 |
'adapter' => $mock, |
| 199 |
1 |
'server' => 'http://api06.dev.openstreetmap.org/', |
| 200 |
1 |
); |
| 201 |
1 |
$osm = new Services_Openstreetmap($config); |
| 202 |
1 |
$ways = $osm->getWays($wayId, $way2Id); |
| 203 |
1 |
foreach ($ways as $key=>$way) { |
| 204 |
1 |
$this->assertEquals($way, $ways[$key]); |
| 205 |
1 |
} |
| 206 |
|
} |
| 207 |
|
|
| 208 |
|
public function testWayWithAddressSet() |
| 209 |
|
{ |
| 210 |
1 |
$id = 75490756; |
| 211 |
|
|
| 212 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 213 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 214 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way_75490756.xml', 'rb')); |
| 215 |
|
|
| 216 |
|
$config = array( |
| 217 |
1 |
'adapter' => $mock, |
| 218 |
|
'server' => 'http://api06.dev.openstreetmap.org' |
| 219 |
1 |
); |
| 220 |
1 |
$osm = new Services_Openstreetmap($config); |
| 221 |
1 |
$way = $osm->getWay($id); |
| 222 |
|
$address = array( |
| 223 |
1 |
'addr_housename' => null, |
| 224 |
1 |
'addr_housenumber' => '20-21', |
| 225 |
1 |
'addr_street' => 'Pearse Street', |
| 226 |
1 |
'addr_city' => 'Nenagh', |
| 227 |
1 |
'addr_country' => 'IE', |
| 228 |
1 |
); |
| 229 |
1 |
$this->assertEquals($address, $way->getAddress()); |
| 230 |
|
} |
| 231 |
|
|
| 232 |
|
public function testWayBackRelations() |
| 233 |
|
{ |
| 234 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 235 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 236 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/way_5850969.xml', 'rb')); |
| 237 |
1 |
$mock->addResponse(fopen( |
| 238 |
1 |
__DIR__ . '/responses/way_5850969_relations.xml', |
| 239 |
|
'rb' |
| 240 |
1 |
)); |
| 241 |
|
|
| 242 |
|
$config = array( |
| 243 |
1 |
'adapter' => $mock, |
| 244 |
|
'server' => 'http://api06.dev.openstreetmap.org' |
| 245 |
1 |
); |
| 246 |
|
|
| 247 |
1 |
$osm = new Services_Openstreetmap($config); |
| 248 |
|
|
| 249 |
1 |
$relations = $osm->getWay(5850969)->getRelations(); |
| 250 |
1 |
$this->assertInstanceOf('Services_Openstreetmap_Relations', $relations); |
| 251 |
1 |
$this->assertEquals(2, sizeof($relations)); |
| 252 |
1 |
$this->assertEquals($relations[0]->getTag('name'), 'Dublin Bus route 14'); |
| 253 |
1 |
$this->assertEquals($relations[1]->getTag('name'), 'Dublin Bus route 75'); |
| 254 |
|
} |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
// vim:set et ts=4 sw=4: |
| 258 |
|
?> |