| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This library is free software; you can redistribute it and/or
|
|---|
| 6 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 7 | License as published by the Free Software Foundation; either
|
|---|
| 8 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This library is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | Lesser General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 16 | License along with this library; if not, write to the Free Software
|
|---|
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|---|
| 18 | MA 02110-1301 USA.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | require_once(__DIR__ ."/TestHelpers/TestSetup.php");
|
|---|
| 22 |
|
|---|
| 23 | class StationsTest extends PHPUnit_Framework_TestCase
|
|---|
| 24 | {
|
|---|
| 25 | protected function setUp(){
|
|---|
| 26 | $file = __DIR__ ."/testconfig/testconfig.php";
|
|---|
| 27 | $this->assertFileExists($file);
|
|---|
| 28 | require($file);
|
|---|
| 29 | $this->_testconfig = $testconfig;
|
|---|
| 30 | $this->assertNotNull($this->_testconfig);
|
|---|
| 31 |
|
|---|
| 32 | require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
|
|---|
| 33 | require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
|
|---|
| 34 |
|
|---|
| 35 | $this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
|
|---|
| 36 | $this->assertNotNull($this->_webserviceConfig);
|
|---|
| 37 |
|
|---|
| 38 | $this->_dataDirectory = $this->_webserviceConfig->getConfig("dataDirectory");
|
|---|
| 39 | $this->assertInternaltype("string", $this->_dataDirectory);
|
|---|
| 40 | $this->assertTrue(!empty($this->_dataDirectory));
|
|---|
| 41 | if(file_exists($this->_dataDirectory))
|
|---|
| 42 | $this->assertTrue(TestHelpers\TestSetup::wipeDataDirectory());
|
|---|
| 43 | $this->assertTrue(TestHelpers\TestSetup::databaseSetup());
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | protected function tearDown(){
|
|---|
| 47 | if(file_exists($this->_dataDirectory))
|
|---|
| 48 | $this->assertTrue(TestHelpers\TestSetup::wipeDataDirectory());
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | public function testEmptyStations()
|
|---|
| 52 | {
|
|---|
| 53 | $stations = $this->query("index.php/stations");
|
|---|
| 54 |
|
|---|
| 55 | $this->assertInternalType("array", $stations);
|
|---|
| 56 | $message = "";
|
|---|
| 57 | if(array_key_exists("message", $stations))
|
|---|
| 58 | $message = $stations["message"];
|
|---|
| 59 | $this->assertTrue(!array_key_exists("result", $stations), $message);
|
|---|
| 60 | $this->assertEquals(count($stations), 0);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | public function testRegisterStationUnprovenLocation()
|
|---|
| 64 | {
|
|---|
| 65 | $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
|
|---|
| 66 | $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
|
|---|
| 67 | $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
|
|---|
| 68 | $client = new GuzzleHttp\Client
|
|---|
| 69 | (
|
|---|
| 70 | [
|
|---|
| 71 | "base_uri" => $baseUrl,
|
|---|
| 72 | "timeout" => 5.0,
|
|---|
| 73 | "http_errors" => false,
|
|---|
| 74 | ]
|
|---|
| 75 | );
|
|---|
| 76 |
|
|---|
| 77 | // sending must failing register request, caused by unproven location
|
|---|
| 78 | $response = $client->post("index.php/stations/register",
|
|---|
| 79 | [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "email" => "test@example.org"]]
|
|---|
| 80 | );
|
|---|
| 81 | $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
|
|---|
| 82 | $json = json_decode($response->getBody(), true);
|
|---|
| 83 | $this->assertTrue(array_key_exists("result", $json));
|
|---|
| 84 | $this->assertTrue(array_key_exists("message", $json));
|
|---|
| 85 | $this->assertEquals("unprovenlocation", $json["result"], $json["message"]);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | public function testRegisterStation()
|
|---|
| 89 | {
|
|---|
| 90 | $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
|
|---|
| 91 | $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
|
|---|
| 92 | $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
|
|---|
| 93 | $client = new GuzzleHttp\Client
|
|---|
| 94 | (
|
|---|
| 95 | [
|
|---|
| 96 | "base_uri" => $baseUrl,
|
|---|
| 97 | "timeout" => 5.0,
|
|---|
| 98 | "http_errors" => false,
|
|---|
| 99 | ]
|
|---|
| 100 | );
|
|---|
| 101 |
|
|---|
| 102 | $response = $client->post("index.php/stations/register",
|
|---|
| 103 | [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "email" => "test@example.org"]]
|
|---|
| 104 | );
|
|---|
| 105 | $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
|
|---|
| 106 | $json = json_decode($response->getBody(), true);
|
|---|
| 107 | $this->assertTrue(array_key_exists("result", $json));
|
|---|
| 108 | $this->assertEquals("success", $json["result"]);
|
|---|
| 109 | $this->assertTrue(array_key_exists("uuid", $json));
|
|---|
| 110 | $this->assertTrue(!empty($json["uuid"]));
|
|---|
| 111 |
|
|---|
| 112 | $stations = $this->query("stations");
|
|---|
| 113 | $this->assertInternalType("array", $stations);
|
|---|
| 114 | $this->assertEquals(1, count($stations));
|
|---|
| 115 | $this->assertTrue(array_key_exists("uuid", $stations[0]));
|
|---|
| 116 | $this->assertEquals($json["uuid"], $stations[0]["uuid"]);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | protected function query($url){
|
|---|
| 120 | $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
|
|---|
| 121 | $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
|
|---|
| 122 | $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
|
|---|
| 123 | $client = new GuzzleHttp\Client
|
|---|
| 124 | (
|
|---|
| 125 | [
|
|---|
| 126 | "base_uri" => $baseUrl,
|
|---|
| 127 | "timeout" => 2.0,
|
|---|
| 128 | "http_errors" => false,
|
|---|
| 129 | ]
|
|---|
| 130 | );
|
|---|
| 131 | $response = $client->get($url);
|
|---|
| 132 | $this->assertNotNull($response, "Response is empty. Url: ". $baseUrl . $url);
|
|---|
| 133 | $this->assertEquals($response->getStatusCode(), 200, "Status code != 200. Url: ". $baseUrl . $url);
|
|---|
| 134 | $this->assertEquals($response->getReasonPhrase(), "OK", "Reason phrase not empty. Url: ". $baseUrl . $url);
|
|---|
| 135 |
|
|---|
| 136 | $body = $response->getBody();
|
|---|
| 137 | $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
|
|---|
| 138 | $json = json_decode($body, true);
|
|---|
| 139 | $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
|
|---|
| 140 | return $json;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | protected $_testconfig;
|
|---|
| 144 | protected $_webserviceConfig;
|
|---|
| 145 | protected $_dataDirectory;
|
|---|
| 146 | }
|
|---|
| 147 | ?>
|
|---|