Changeset 8c356c1 for src/webservice/tests
- Timestamp:
- Aug 26, 2015, 8:14:40 PM (11 years ago)
- Branches:
- master
- Children:
- b38cc03
- Parents:
- 8f38d1e
- Location:
- src/webservice/tests
- Files:
-
- 1 added
- 2 moved
-
SetupTest.php (added)
-
SpatialiteTest.php (moved) (moved from src/webservice/tests/TestSpatiallite.php ) (7 diffs)
-
StationsTest.php (moved) (moved from src/webservice/tests/TestStations.php ) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/webservice/tests/SpatialiteTest.php
r8f38d1e r8c356c1 19 19 */ 20 20 21 class TestSpatialiteExtensionextends PHPUnit_Framework_TestCase21 class SpatialiteTest extends PHPUnit_Framework_TestCase 22 22 { 23 23 public static function setUpBeforeClass() 24 24 { 25 global $testconfig; 26 25 27 self::$_databaseFile = tempnam(sys_get_temp_dir(), "siw"); 26 28 # PDO SQlite is not able to load extensions (caused by the sqlite pdo driver) 27 29 # therefore we use PHP SQlite module 28 30 self::$_database = new SQLite3(self::$_databaseFile); 31 32 $file = __DIR__ ."/testconfig/testconfig.php"; 33 self::assertFileExists($file); 34 require($file); 35 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php"); 36 37 self::$_webserviceConfig = new SiWeather\Webservice\WebserviceConfig(); 38 self::assertNotNull(self::$_webserviceConfig); 29 39 } 30 40 31 41 public static function tearDownAfterClass() 32 42 { 33 self:: $_database->close();34 unlink(self::$_databaseFile);43 self::assertTrue(self::$_database->close()); 44 self::assertTrue(unlink(self::$_databaseFile)); 35 45 self::$_database = null; 36 46 self::$_databaseFile = null; … … 42 52 43 53 # loading SpatiaLite as an extension 44 if(!self::$_database->loadExtension( "mod_spatialite.so")){54 if(!self::$_database->loadExtension(self::$_webserviceConfig->getConfig("spatialiteExtension"))){ 45 55 echo "Could not load SpatialLite extension: ". self::$_database->lastErrorMsg(); 46 56 return; … … 77 87 # taking two coordinates in SRID 4326 (WGS84) format 78 88 $sql = "SELECT AddGeometryColumn(\"". $table ."\", "; 79 $sql .= "\"center\", 32632, \"POINT\", \"XY\")";89 $sql .= "\"center\", 4326, \"POINT\", \"XY\")"; 80 90 $this->assertTrue(self::$_database->exec($sql)); 81 91 82 92 # insert some cities 83 93 # the center must be described in WKT (s. http://giswiki.org/wiki/WKT) format for a point. 84 # since we use WGS84 as a coordinate system, 94 # Since we use WGS84 with SRID 4326 as a coordinate system, it expects POINT(LONGITUDE LATITUDE) 95 # as values. 85 96 # CITY, LATITUDE, LONGITUDE 97 # example Berlin: 98 # 52°31′7″N, 13°24′30″E 99 # --> 52.518611° Latitude and 13.408333° Longitude 100 # --> $values[] = array("name" => "Berlin", "latitude" => 52.518611, "longitude" => 13.408333); 101 # --> POINT("13.408333 52.518611") 86 102 $values = array(); 87 $values[] = array(" Berlin", 52.518611,13.408333);88 $values[] = array(" Potsdam", 52.395833,13.061389);89 $values[] = array(" Hamburg", 53.550556,9.993333);90 $values[] = array(" Dresden", 51.049259,13.73836);91 $values[] = array(" Cape Town", -33.922667,18.416689);92 $values[] = array(" New York City", 40.712778,-74.005833);93 $values[] = array(" Sydney", -33.85,151.2);103 $values[] = array("name" => "Berlin", "latitude" => 52.518611, "longitude" => 13.408333); 104 $values[] = array("name" => "Potsdam", "latitude" => 52.395833, "longitude" => 13.061389); 105 $values[] = array("name" => "Hamburg", "latitude" => 53.550556, "longitude" => 9.993333); 106 $values[] = array("name" => "Dresden", "latitude" => 51.049259, "longitude" => 13.73836); 107 $values[] = array("name" => "Cape Town", "latitude" => -33.922667, "longitude" => 18.416689); 108 $values[] = array("name" => "New York City", "latitude" => 40.712778, "longitude" => -74.005833); 109 $values[] = array("name" => "Sydney", "latitude" => -33.85, "longitude" => 151.2); 94 110 95 111 # distances in meter … … 129 145 130 146 $this->assertTrue(self::$_database->exec("BEGIN")); 131 $sql = "INSERT INTO ". $table ." (name, center) VALUES(:name, GeomFromText(:center, 32632))";147 $sql = "INSERT INTO ". $table ." (name, center) VALUES(:name, GeomFromText(:center, 4326))"; 132 148 $sqlStatement = self::$_database->prepare($sql); 133 149 foreach ($values as $value){ 134 150 $sqlStatement->clear(); 135 151 $this->assertEquals(3, count($value)); 136 $this->assertTrue($sqlStatement->bindValue(":name", $value[ 0]));137 $this->assertTrue($sqlStatement->bindValue(":center", "POINT(". $value[ 1] ." ". $value[2] .")"));152 $this->assertTrue($sqlStatement->bindValue(":name", $value["name"])); 153 $this->assertTrue($sqlStatement->bindValue(":center", "POINT(". $value["longitude"] ." ". $value["latitude"] .")")); 138 154 $this->assertInstanceOf("SQLite3Result", $sqlStatement->execute()); 155 $sqlStatement->reset(); 139 156 } 140 157 $this->assertTrue(self::$_database->exec("COMMIT")); 141 158 142 159 # check the inserts 143 $sql = "SELECT id, name, X(center) , Y(center)FROM ". $table ." WHERE name=:name";160 $sql = "SELECT id, name, X(center) as longitude, Y(center) as latitude FROM ". $table ." WHERE name=:name"; 144 161 $sqlStatement = self::$_database->prepare($sql); 145 162 foreach ($values as $value){ 146 163 $sqlStatement->clear(); 147 164 $this->assertEquals(3, count($value)); 148 $this->assertTrue($sqlStatement->bindValue(":name", $value[ 0]));165 $this->assertTrue($sqlStatement->bindValue(":name", $value["name"])); 149 166 $result = $sqlStatement->execute(); 150 167 $this->assertInstanceOf("SQLite3Result", $result); … … 152 169 $this->assertGreaterThan(4, count($row)); 153 170 $this->assertGreaterThan(-1, $row[0]); 154 $this->assertEquals($value[ 0], $row[1]);155 $this->assertEquals($value[ 1], $row[2]);156 $this->assertEquals($value[ 2], $row[3]);171 $this->assertEquals($value["name"], $row[1]); 172 $this->assertEquals($value["longitude"], $row[2]); 173 $this->assertEquals($value["latitude"], $row[3]); 157 174 } 175 $sqlStatement->reset(); 158 176 } 159 177 160 178 # calculate the distances 161 179 # Calling Distance() with three parameters, which returns distance in Meter 162 $sql = "SELECT t1.name, t2.name, Distance(t1.center, t2.center) As distance";180 $sql = "SELECT t1.name, t2.name, Round(Distance(t1.center, t2.center, 1), 0) As distance"; 163 181 $sql .= " FROM ". $table ." AS t1, ". $table ." AS t2"; 164 182 $sql .= " WHERE t1.name=:from AND t2.name=:to"; … … 176 194 $this->assertEquals($start, $row[0]); 177 195 $this->assertEquals($end, $row[1]); 178 $this->assertEquals($distances[$start][$end], $row[2], $start ." --> ". $end); 196 # 0.5% difference of the distance is accepted 197 $delta = abs($distances[$start][$end] - $row[2])/$distances[$start][$end]; 198 $this->assertLessThan(0.005, $delta, $start ." --> ". $end .":\n Expected: ". $distances[$start][$end] ."\n Is: ". $row[2]); 179 199 } 200 $sqlStatement->reset(); 180 201 } 181 202 } … … 184 205 protected static $_databaseFile; 185 206 protected static $_database; 207 protected static $_webserviceConfig; 186 208 } 187 209 ?> -
src/webservice/tests/StationsTest.php
r8f38d1e r8c356c1 19 19 */ 20 20 21 class TestStationsextends PHPUnit_Framework_TestCase21 class StationsTest extends PHPUnit_Framework_TestCase 22 22 { 23 protected $config;24 25 23 protected function setUp() 26 24 { 27 $file = "webtests/webservice/testconfig/testconfig.php";25 $file = __DIR__ ."/testconfig/testconfig.php"; 28 26 $this->assertFileExists($file); 29 27 require($file); 30 $this->config = $testconfig; 31 $this->assertNotNull($this->config); 28 $this->_testconfig = $testconfig; 29 $this->assertNotNull($this->_testconfig); 30 31 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php"); 32 33 $this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig(); 34 $this->assertNotNull($this->_webserviceConfig); 32 35 } 33 36 34 37 public function testEmptyStations() 35 38 { 36 $this->queryEmptyStationList($this->config["webserviceUrl"]); 39 $stations = $this->query("index.php/stations"); 40 41 $this->assertInternalType("array", $stations); 42 $this->assertEquals(count($stations), 0); 37 43 } 38 44 39 45 public function testStationRewrite() 40 46 { 41 $this->queryEmptyStationList($this->config["webserviceUrlRewrite"]); 47 $apiVersion = $this->_webserviceConfig->getConfig("webserviceApiVersion"); 48 $this->assertInternalType('string', $apiVersion); 49 $this->assertTrue(!empty($apiVersion)); 50 51 $stations = $this->query($apiVersion ."/stations"); 52 53 $this->assertInternalType("array", $stations); 54 $this->assertEquals(count($stations), 0); 42 55 } 43 56 44 protected function queryEmptyStationList($url){ 45 $this->assertInternalType('string', $url ."/"); 57 protected function query($url){ 58 require(__DIR__ ."/../vendor/autoload.php"); 59 60 $baseUrl = $this->_webserviceConfig->getConfig("webserviceBaseUrl"); 61 $this->assertInternalType('string', $baseUrl); 62 $this->assertTrue(!empty($baseUrl)); 46 63 $client = new GuzzleHttp\Client 47 64 ( 48 65 [ 49 "base_uri" => $ url,66 "base_uri" => $baseUrl, 50 67 "timeout" => 2.0, 68 "http_errors" => false, 51 69 ] 52 70 ); 53 $response = $client->get( "index.php/stations");71 $response = $client->get($url); 54 72 $this->assertNotNull($response); 55 73 $this->assertEquals($response->getStatusCode(), 200); … … 58 76 $body = $response->getBody(); 59 77 $this->assertTrue(!empty($body)); 60 $stations = json_decode($body); 61 $this->assertNotNull($stations); 62 $this->assertInternalType("array", $stations); 63 $this->assertEquals(count($stations), 0); 78 $json = json_decode($body); 79 $this->assertNotNull($json); 80 return $json; 64 81 } 82 83 protected $_testconfig; 84 protected $_webserviceConfig; 65 85 } 66 86 ?>
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/siweather/chrome/site/your_project_logo.png)