assertFileExists($file); require($file); $this->_testconfig = $testconfig; $this->assertNotNull($this->_testconfig); require($testconfig["webserviceDirectory"] ."/inc/envsetup.php"); require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php"); $this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig(); $this->assertNotNull($this->_webserviceConfig); $this->_dataDirectory = $this->_webserviceConfig->getConfig("dataDirectory"); $this->assertInternaltype("string", $this->_dataDirectory); $this->assertTrue(!empty($this->_dataDirectory)); if(file_exists($this->_dataDirectory)) $this->assertTrue(TestHelpers\TestSetup::wipeDataDirectory()); $this->assertTrue(TestHelpers\TestSetup::databaseSetup()); } protected function tearDown(){ if(file_exists($this->_dataDirectory)) $this->assertTrue(TestHelpers\TestSetup::wipeDataDirectory()); } public function testEmptyStations() { $stations = $this->query("index.php/stations"); $this->assertInternalType("array", $stations); $message = ""; if(array_key_exists("message", $stations)) $message = $stations["message"]; $this->assertTrue(!array_key_exists("result", $stations), $message); $this->assertEquals(count($stations), 0); } public function testRegisterStationUnprovenLocation() { $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl(); $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl); $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl); $client = new GuzzleHttp\Client ( [ "base_uri" => $baseUrl, "timeout" => 5.0, "http_errors" => false, ] ); // sending must failing register request, caused by unproven location $response = $client->post("index.php/stations/register", [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "type" => "testing", "email" => "test@example.org"]] ); $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase()); $json = json_decode($response->getBody(), true); $this->assertTrue(array_key_exists("result", $json)); $this->assertTrue(array_key_exists("message", $json)); $this->assertEquals("unprovenlocation", $json["result"], $json["message"]); } public function testRegisterStation() { $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl(); $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl); $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl); $client = new GuzzleHttp\Client ( [ "base_uri" => $baseUrl, "timeout" => 5.0, "http_errors" => false, ] ); $response = $client->post("index.php/stations/register", [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "type" => "testing", "email" => "test@example.org"]] ); $this->assertEquals(200, $response->getStatusCode(), $response->getReasonPhrase()); $json = json_decode($response->getBody(), true); $this->assertTrue(array_key_exists("result", $json)); $this->assertEquals("success", $json["result"], $json["message"]); $this->assertTrue(array_key_exists("uuid", $json)); $this->assertTrue(!empty($json["uuid"])); $this->assertTrue(array_key_exists("message", $json)); $this->assertTrue(!empty($json["message"])); $stations = $this->query("index.php/stations"); $this->assertInternalType("array", $stations); $this->assertEquals(1, count($stations)); $this->assertTrue(array_key_exists("uuid", $stations[0])); $this->assertEquals($json["uuid"], $stations[0]["uuid"]); } protected function query($url){ $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl(); $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl); $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl); $client = new GuzzleHttp\Client ( [ "base_uri" => $baseUrl, "timeout" => 2.0, "http_errors" => false, ] ); $response = $client->get($url); $this->assertNotNull($response, "Response is empty. Url: ". $baseUrl . $url); $this->assertEquals($response->getStatusCode(), 200, "Status code != 200. Url: ". $baseUrl . $url); $this->assertEquals($response->getReasonPhrase(), "OK", "Reason phrase not empty. Url: ". $baseUrl . $url); $body = $response->getBody(); $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url); $json = json_decode($body, true); $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body); return $json; } protected $_testconfig; protected $_webserviceConfig; protected $_dataDirectory; } ?>