source: src/webservice/tests/StationsTest.php@ fb602c6

Last change on this file since fb602c6 was e498070, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Property mode set to 100644
File size: 5.7 KB
Line 
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
21require_once(__DIR__ ."/TestHelpers/TestSetup.php");
22
23class 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", "type" => "testing", "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, "type" => "testing", "email" => "test@example.org"]]
104 );
105 $this->assertEquals(200, $response->getStatusCode(), $response->getReasonPhrase());
106 $json = json_decode($response->getBody(), true);
107 $this->assertTrue(array_key_exists("result", $json));
108 $this->assertEquals("success", $json["result"], $json["message"]);
109 $this->assertTrue(array_key_exists("uuid", $json));
110 $this->assertTrue(!empty($json["uuid"]));
111 $this->assertTrue(array_key_exists("message", $json));
112 $this->assertTrue(!empty($json["message"]));
113
114 $stations = $this->query("index.php/stations");
115 $this->assertInternalType("array", $stations);
116 $this->assertEquals(1, count($stations));
117 $this->assertTrue(array_key_exists("uuid", $stations[0]));
118 $this->assertEquals($json["uuid"], $stations[0]["uuid"]);
119 }
120
121 protected function query($url){
122 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
123 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
124 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
125 $client = new GuzzleHttp\Client
126 (
127 [
128 "base_uri" => $baseUrl,
129 "timeout" => 2.0,
130 "http_errors" => false,
131 ]
132 );
133 $response = $client->get($url);
134 $this->assertNotNull($response, "Response is empty. Url: ". $baseUrl . $url);
135 $this->assertEquals($response->getStatusCode(), 200, "Status code != 200. Url: ". $baseUrl . $url);
136 $this->assertEquals($response->getReasonPhrase(), "OK", "Reason phrase not empty. Url: ". $baseUrl . $url);
137
138 $body = $response->getBody();
139 $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
140 $json = json_decode($body, true);
141 $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
142 return $json;
143 }
144
145 protected $_testconfig;
146 protected $_webserviceConfig;
147 protected $_dataDirectory;
148}
149?>
Note: See TracBrowser for help on using the repository browser.