Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added new test for iterating through history/versions of a number of …

…nodes and fixed some bugs highlighted by same.
  • Loading branch information...
commit 07d9995dcef963e91bdc887ec18ec456b997937d 1 parent 9015335
@kenguest authored
View
6 Services/Openstreetmap.php
@@ -79,7 +79,7 @@ function __construct($config = array())
$config = $this->config;
}
$this->getConfig()->setTransport($this->getTransport());
- if ($config !== null) {
+ if (!is_null($config)) {
$this->getConfig()->setValue($config);
}
$version = $this->getConfig()->getValue('api_version');
@@ -438,7 +438,7 @@ public function setConfig(Services_Openstreetmap_Config $config)
*/
public function getConfig()
{
- if ($this->config === null) {
+ if (is_null($this->config)) {
$config = new Services_Openstreetmap_Config();
$this->config = $config;
}
@@ -454,7 +454,7 @@ public function getConfig()
*/
public function getTransport()
{
- if ($this->transport == null) {
+ if (is_null($this->transport)) {
$transport = new Services_Openstreetmap_Transport();
$transport->setConfig($this->getConfig());
$this->transport = $transport;
View
6 Services/Openstreetmap/Changeset.php
@@ -64,10 +64,10 @@ public function begin($message)
. "/changeset/create";
$user = $config->getValue('user');
$password = $config->getValue('password');
- if ($user == null) {
+ if (is_null($user)) {
throw new Services_Openstreetmap_Exception('User must be set');
}
- if ($password == null) {
+ if (is_null($password)) {
throw new Services_Openstreetmap_Exception('Password must be set');
}
$response = $this->getTransport()->getResponse(
@@ -228,7 +228,7 @@ public function getClosedAt()
public function isOpen()
{
$attribs = $this->getAttributes();
- if ($attribs !== null) {
+ if (!is_null($attribs)) {
return $attribs->open == 'true';
} else {
return $this->open;
View
2  Services/Openstreetmap/Config.php
@@ -123,7 +123,7 @@ class Services_Openstreetmap_Config
*/
public function getValue($name = null)
{
- if ($name === null) {
+ if (is_null($name)) {
return $this->config;
} elseif (!array_key_exists($name, $this->config)) {
throw new Services_Openstreetmap_Exception(
View
2  Services/Openstreetmap/Criterion.php
@@ -86,7 +86,7 @@ public function __construct()
}
$before = gmstrftime("%Y-%m-%dT%H:%M:%SZ", $t);
}
- if ($before !== null) {
+ if (!is_null($before)) {
$this->value = "$after,$before";
} else {
$this->value = $after;
View
20 Services/Openstreetmap/Object.php
@@ -146,7 +146,7 @@ public function getOsmChangeXML()
$nodelist->item(0)->setAttribute("action", $this->action);
$nodelist->item(0)->setAttribute("id", $this->getId());
- if ($this->changeset_id !== null) {
+ if (!is_null($this->changeset_id)) {
$nodelist->item(0)->setAttribute("changeset", $this->changeset_id);
}
$tags = $xpath->query("//{$type}/tag");
@@ -187,7 +187,7 @@ public function getOsmChangeXML()
$n = $xpath->query("//{$type}");
$version = $this->getVersion();
$version++;
- if ($this->changeset_id !== null) {
+ if (!is_null($this->changeset_id)) {
$n->item(0)->setAttribute("changeset", $this->changeset_id);
}
$n->item(0)->setAttribute('action', 'delete');
@@ -221,7 +221,7 @@ public function getId()
}
$attribs = $this->getAttributes();
- if ($attribs !== null) {
+ if (!is_null($attribs)) {
return (integer) $attribs->id;
}
}
@@ -251,7 +251,7 @@ public function setId($value)
public function getUid()
{
$attribs = $this->getAttributes();
- if ($attribs !== null) {
+ if (!is_null($attribs)) {
return (integer) $attribs->uid;
}
}
@@ -264,7 +264,7 @@ public function getUid()
public function getUser()
{
$attribs = $this->getAttributes();
- if ($attribs !== null) {
+ if (!is_null($attribs)) {
return (string) $attribs->user;
}
}
@@ -277,7 +277,7 @@ public function getUser()
public function getVersion()
{
$attribs = $this->getAttributes();
- if ($attribs !== null) {
+ if (!is_null($attribs)) {
return (integer) $attribs->version;
}
}
@@ -290,7 +290,7 @@ public function getVersion()
public function getAttributes()
{
- if ($this->obj[0] === null) {
+ if (is_null($this->obj[0])) {
return null;
}
return $this->obj[0]->attributes();
@@ -341,6 +341,7 @@ public function getType()
*/
public function history()
{
+ $transport = null;
$type = $this->getType();
$id = $this->getId();
$config = $this->getConfig();
@@ -349,7 +350,8 @@ public function history()
. $config->getValue('api_version')
. "/$type/$id/history";
$class = 'Services_Openstreetmap_' . ucfirst($type) . 's';
- $response = $this->getTransport()->getResponse($url);
+ $transport = $this->getTransport();
+ $response = $transport->getResponse($url);
$obj = new $class();
$sxe = @simplexml_load_string($response->getBody());
if ($sxe === false) {
@@ -399,7 +401,7 @@ public function getRelations()
*/
public function setTag($key, $value)
{
- if ($this->action == null) {
+ if (is_null($this->action)) {
if ($this->getId() < 0) {
$this->action = 'create';
} else {
View
60 Services/Openstreetmap/Objects.php
@@ -30,6 +30,10 @@ class Services_Openstreetmap_Objects implements Iterator, ArrayAccess, Countable
protected $position = 0;
+ protected $transport = null;
+
+ protected $config = null;
+
/**
* getXml
*
@@ -100,6 +104,11 @@ public function current()
{
$class = 'Services_Openstreetmap_' . ucfirst(strtolower($this->getType()));
$way = new $class();
+ $config = $this->getConfig();
+ if (!is_null($config)) {
+ $way->setConfig($config);
+ }
+ $way->setTransport($this->getTransport());
$way->setXml(simplexml_load_string($this->objects[$this->position]));
return $way;
}
@@ -158,6 +167,11 @@ public function offsetGet($offset)
{
$class = 'Services_Openstreetmap_' . ucfirst(strtolower($this->getType()));
$way = new $class();
+ $config = $this->getConfig();
+ if (!is_null($config)) {
+ $way->setConfig($config);
+ }
+ $way->setTransport($this->getTransport());
if (isset($this->objects[$offset])) {
$way->setXml(simplexml_load_string($this->objects[$offset]));
return $way;
@@ -186,6 +200,52 @@ public function offsetSet($offset, $value)
public function offsetUnset($offset)
{
}
+
+ /**
+ * Set Config object
+ *
+ * @param Services_Openstreetmap_Config $config Config object
+ *
+ * @return Services_Openstreetmap_Changeset
+ */
+ public function setConfig(Services_Openstreetmap_Config $config)
+ {
+ $this->config = $config;
+ return $this;
+ }
+
+ /**
+ * Get current Config object
+ *
+ * @return Services_Openstreetmap_Config
+ */
+ public function getConfig()
+ {
+ return $this->config;
+ }
+
+ /**
+ * Set the Transport instance.
+ *
+ * @param Services_Openstreetmap_Transport $transport Transport instance.
+ *
+ * @return Services_Openstreetmap_Config
+ */
+ public function setTransport($transport)
+ {
+ $this->transport = $transport;
+ return $this;
+ }
+
+ /**
+ * Retrieve the current Transport instance.
+ *
+ * @return Services_Openstreetmap_Transport.
+ */
+ public function getTransport()
+ {
+ return $this->transport;
+ }
}
?>
View
14 Services/Openstreetmap/Transport.php
@@ -46,7 +46,6 @@ class Services_Openstreetmap_Transport
const GONE = 410;
/**#@-*/
-
/**
* The HTTP_Request2 instance.
*
@@ -111,7 +110,7 @@ function getResponse(
'User-Agent',
$this->getConfig()->getValue('User-Agent')
);
- if ($user !== null && $password !== null) {
+ if (!is_null($user) && !is_null($password)) {
$request->setAuth($user, $password);
}
if ($post_data != array()) {
@@ -125,7 +124,7 @@ function getResponse(
$request->setHeader($header[0], $header[1], $header[2]);
}
}
- if ($body !== null) {
+ if (!is_null($body)) {
$request->setBody($body);
}
$code = 0;
@@ -170,7 +169,7 @@ function getResponse(
*/
function getRequest()
{
- if ($this->request === null) {
+ if (is_null($this->request)) {
$this->request = new HTTP_Request2();
}
return $this->request;
@@ -219,7 +218,7 @@ public function getObject($type, $id, $version = null)
. $config['api_version']
. '/' . $type . '/'
. $id;
- if ($version !== null) {
+ if (!is_null($version)) {
$url .= "/$version";
}
try {
@@ -274,8 +273,13 @@ public function getObjects($type, array $ids)
throw $ex;
}
}
+
$class = 'Services_Openstreetmap_' . ucfirst(strtolower($type)) . 's';
$obj = new $class();
+ if (!is_null($config)) {
+ $obj->setConfig($config);
+ }
+ $obj->setTransport($this);
$sxe = @simplexml_load_string($response->getBody());
if ($sxe === false) {
$obj->setVal(trim($response->getBody()));
View
30 tests/NodeTest.php
@@ -398,6 +398,36 @@ public function testGetNodes500()
$nodes = $osm->getNodes(array(621953926,621953928,621953939));
}
+ public function testGetNodesHistory()
+ {
+ $mock = new HTTP_Request2_Adapter_Mock();
+ $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
+ $mock->addResponse(fopen(__DIR__ . '/responses/nodes_621953926_621953928_621953939.xml', 'rb'));
+ $mock->addResponse(fopen(__DIR__ . '/responses/node_621953926.xml', 'rb'));
+ $mock->addResponse(fopen(__DIR__ . '/responses/node_621953928.xml', 'rb'));
+ $mock->addResponse(fopen(__DIR__ . '/responses/node_621953939_history.xml', 'rb'));
+
+ $config = array(
+ 'adapter' => $mock,
+ 'server' => 'http://api06.dev.openstreetmap.org/',
+ );
+ $osm = new Services_Openstreetmap($config);
+ $nodes = $osm->getNodes(array(621953926, 621953928, 621953939));
+ $versions = array(
+ 621953926 => array(1),
+ 621953928 => array(1),
+ 621953939 => array(1, 2)
+ );
+ foreach ($nodes as $node) {
+ $history = $node->history();
+ $id = $node->getId();
+ foreach ($history as $item) {
+ $version = $item->getVersion();
+ $this->assertEquals(true, in_array($version, $versions[$id]));
+ }
+ }
+ }
+
public function testGetWayBackRef()
{
$mock = new HTTP_Request2_Adapter_Mock();
View
10 tests/responses/node_621953926.xml
@@ -0,0 +1,10 @@
+HTTP/1.1 200 OK
+
+<?xml version="1.0" encoding="UTF-8"?>
+<osm version="0.6" generator="OpenStreetMap server">
+ <node id="621953926" lat="52.3744688" lon="-7.9258388" changeset="3736898" user="brianboru" uid="9065" visible="true" timestamp="2010-01-28T16:22:40Z" version="1">
+ <tag k="tourism" v="information"/>
+ <tag k="information" v="office"/>
+ <tag k="source" v="survey"/>
+ </node>
+</osm>
View
10 tests/responses/node_621953928.xml
@@ -0,0 +1,10 @@
+HTTP/1.1 200 OK
+
+<?xml version="1.0" encoding="UTF-8"?>
+<osm version="0.6" generator="OpenStreetMap server">
+ <node id="621953928" lat="52.374505" lon="-7.9256212" changeset="3736898" user="brianboru" uid="9065" visible="true" timestamp="2010-01-28T16:22:40Z" version="1">
+ <tag k="highway" v="bus_stop"/>
+ <tag k="source" v="survey"/>
+ <tag k="shelter" v="no"/>
+ </node>
+</osm>
View
19 tests/responses/node_621953939_history.xml
@@ -0,0 +1,19 @@
+HTTP/1.1 200 OK
+
+<?xml version="1.0" encoding="UTF-8"?>
+<osm version="0.6" generator="OpenStreetMap server">
+ <node id="621953939" lat="52.3747828" lon="-7.9257069" changeset="3736898" user="brianboru" uid="9065" visible="true" timestamp="2010-01-28T16:22:40Z" version="1">
+ <tag k="name" v="Morrissey's"/>
+ <tag k="amenity" v="pub"/>
+ <tag k="source" v="survey"/>
+ </node>
+ <node id="621953939" lat="52.3747828" lon="-7.9257069" changeset="4933315" user="kenguest" uid="11324" visible="true" timestamp="2010-06-07T22:47:39Z" version="2">
+ <tag k="name" v="Morrissey's"/>
+ <tag k="addr:housenumber" v="5"/>
+ <tag k="addr:city" v="Cahir"/>
+ <tag k="addr:street" v="Castle Street"/>
+ <tag k="amenity" v="pub"/>
+ <tag k="addr:country" v="IE"/>
+ <tag k="source" v="survey"/>
+ </node>
+</osm>
Please sign in to comment.
Something went wrong with that request. Please try again.