Index: src/webservice/tests/TestSpatiallite.php
===================================================================
--- src/webservice/tests/TestSpatiallite.php	(revision 8f38d1eb0d2e82eb85c37617864c60c37bfebf83)
+++ src/webservice/tests/TestSpatiallite.php	(revision 8f38d1eb0d2e82eb85c37617864c60c37bfebf83)
@@ -0,0 +1,187 @@
+<?php
+/*
+    Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+    MA 02110-1301  USA.
+*/
+
+class TestSpatialiteExtension extends PHPUnit_Framework_TestCase
+{
+	public static function setUpBeforeClass()
+	{
+		self::$_databaseFile = tempnam(sys_get_temp_dir(), "siw");
+		# PDO SQlite is not able to load extensions (caused by the sqlite pdo driver)
+		# therefore we use PHP SQlite module
+		self::$_database = new SQLite3(self::$_databaseFile);
+	}
+	
+	public static function tearDownAfterClass()
+	{
+		self::$_database->close();
+		unlink(self::$_databaseFile);
+		self::$_database = null;
+		self::$_databaseFile = null;
+	}
+
+	public function testLoadSpatialite()
+	{
+		$this->assertInstanceOf("SQLite3", self::$_database);
+
+		# loading SpatiaLite as an extension
+		if(!self::$_database->loadExtension("mod_spatialite.so")){
+			echo "Could not load SpatialLite extension: ". self::$_database->lastErrorMsg();
+			return;
+		}
+		
+		$this->assertTrue(self::$_database->exec("BEGIN"));
+		$this->assertTrue(self::$_database->exec("SELECT InitSpatialMetadata()"));
+		$this->assertTrue(self::$_database->exec("COMMIT"));
+		
+		# reporting some version info
+		$rs = self::$_database->query("SELECT spatialite_version()");
+		$this->assertInstanceOf('SQLite3Result', $rs);
+		$row = $rs->fetchArray();
+		$this->assertInternalType("array", $row);
+		$this->assertGreaterThan(1, count($row));
+		$this->assertTrue(!empty($row[0]));
+		$row = $rs->fetchArray();
+		$this->assertTrue(!$row);
+	}
+	
+	public function testDistance()
+	{
+		$this->assertInstanceOf("SQLite3", self::$_database);
+
+		$table = "cities";
+		
+		# creating a POINT table
+		$sql  = "CREATE TABLE ". $table ." (";
+		$sql .= "id INTEGER NOT NULL PRIMARY KEY,";
+		$sql .= "name TEXT NOT NULL)";
+		$this->assertTrue(self::$_database->exec($sql));
+
+		# creating a POINT Geometry column named center,
+		# taking two coordinates in SRID 4326 (WGS84) format
+		$sql  = "SELECT AddGeometryColumn(\"". $table ."\", ";
+		$sql .= "\"center\", 32632, \"POINT\", \"XY\")";
+		$this->assertTrue(self::$_database->exec($sql));
+		
+		# insert some cities
+		# the center must be described in WKT (s. http://giswiki.org/wiki/WKT) format for a point.
+		# since we use WGS84 as a coordinate system, 
+		# CITY, LATITUDE, LONGITUDE
+		$values = array();
+		$values[] = array("Berlin", 52.518611, 13.408333);
+		$values[] = array("Potsdam", 52.395833, 13.061389);
+		$values[] = array("Hamburg", 53.550556, 9.993333);
+		$values[] = array("Dresden", 51.049259, 13.73836);
+		$values[] = array("Cape Town", -33.922667, 18.416689);
+		$values[] = array("New York City", 40.712778, -74.005833);
+		$values[] = array("Sydney", -33.85, 151.2);
+		
+		# distances in meter
+		$distances = array();
+		$distances["Berlin"] = array();
+		$distances["Berlin"]["Potsdam"] = 27185;
+		$distances["Berlin"]["Hamburg"] = 255518;
+		$distances["Berlin"]["Dresden"] = 164954;
+		$distances["Berlin"]["Cape Town"] = 9624137;
+		$distances["Berlin"]["New York City"] = 6385266;
+		$distances["Berlin"]["Sydney"] = 16092238;
+		
+		$distances["Potsdam"] = array();
+		$distances["Potsdam"]["Hamburg"] = 242231;
+		$distances["Potsdam"]["Dresden"] = 156823;
+		$distances["Potsdam"]["Cape Town"] =9612288;
+		$distances["Potsdam"]["New York City"] = 6370104;
+		$distances["Potsdam"]["Sydney"] = 16118486;
+		
+		$distances["Hamburg"] = array();
+		$distances["Hamburg"]["Dresden"] = 377003;
+		$distances["Hamburg"]["Cape Town"] = 9760489;
+		$distances["Hamburg"]["New York City"] = 6129909;
+		$distances["Hamburg"]["Sydney"] = 16275543;
+		
+		$distances["Dresden"] = array();
+		$distances["Dresden"]["Cape Town"] = 9459562;
+		$distances["Dresden"]["New York City"] = 6478589;
+		$distances["Dresden"]["Sydney"] = 16109156;
+		
+		$distances["Cape Town"] = array();
+		$distances["Cape Town"]["New York City"] = 12564111;
+		$distances["Cape Town"]["Sydney"] = 11013398;
+		
+		$distances["New York City"] = array();
+		$distances["New York City"]["Sydney"] = 15988687;
+		
+		$this->assertTrue(self::$_database->exec("BEGIN"));
+		$sql  = "INSERT INTO ". $table ." (name, center) VALUES(:name, GeomFromText(:center, 32632))";
+		$sqlStatement = self::$_database->prepare($sql);
+		foreach ($values as $value){
+			$sqlStatement->clear();
+			$this->assertEquals(3, count($value));
+			$this->assertTrue($sqlStatement->bindValue(":name", $value[0]));
+			$this->assertTrue($sqlStatement->bindValue(":center", "POINT(". $value[1] ." ". $value[2] .")"));
+			$this->assertInstanceOf("SQLite3Result", $sqlStatement->execute());
+		}
+		$this->assertTrue(self::$_database->exec("COMMIT"));
+		
+		# check the inserts
+		$sql  = "SELECT id, name, X(center), Y(center) FROM ". $table ." WHERE name=:name";
+		$sqlStatement = self::$_database->prepare($sql);
+		foreach ($values as $value){
+			$sqlStatement->clear();
+			$this->assertEquals(3, count($value));
+			$this->assertTrue($sqlStatement->bindValue(":name", $value[0]));
+			$result = $sqlStatement->execute();
+			$this->assertInstanceOf("SQLite3Result", $result);
+			while($row = $result->fetchArray()){
+				$this->assertGreaterThan(4, count($row));
+				$this->assertGreaterThan(-1, $row[0]);
+				$this->assertEquals($value[0], $row[1]);
+				$this->assertEquals($value[1], $row[2]);
+				$this->assertEquals($value[2], $row[3]);
+			}
+		}
+
+		# calculate the distances
+		# Calling Distance() with three parameters, which returns distance in Meter
+		$sql  = "SELECT t1.name, t2.name, Distance(t1.center, t2.center) As distance";
+		$sql .= "  FROM ". $table ." AS t1, ". $table ." AS t2";
+		$sql .= "  WHERE t1.name=:from AND t2.name=:to";
+		$sqlStatement = self::$_database->prepare($sql);
+		foreach(array_keys($distances) as $start){
+			foreach(array_keys($distances[$start]) as $end){
+				$sqlStatement->clear();
+				# Calculate distance from start --> end
+				$this->assertTrue($sqlStatement->bindValue(":from", $start));
+				$this->assertTrue($sqlStatement->bindValue(":to", $end));
+				$result = $sqlStatement->execute();
+				$this->assertInstanceOf("SQLite3Result", $result);
+				while($row = $result->fetchArray()){
+					$this->assertGreaterThan(3, count($row));
+					$this->assertEquals($start, $row[0]);
+					$this->assertEquals($end, $row[1]);
+					$this->assertEquals($distances[$start][$end], $row[2], $start ." --> ". $end);
+				}
+			}
+		}
+	}
+
+	protected static $_databaseFile;
+	protected static $_database;
+}
+?>
