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

Last change on this file since d55ec21 was d55ec21, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Improved testing execution of webservice related tests (they use the built in php webserver to create a test webservice instance)
  • Property mode set to 100644
File size: 5.6 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 {
27 $file = __DIR__ ."/testconfig/testconfig.php";
28 $this->assertFileExists($file);
29 require($file);
30 $this->_testconfig = $testconfig;
31 $this->assertNotNull($this->_testconfig);
32
33 require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
34 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
35
36 $this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
37 $this->assertNotNull($this->_webserviceConfig);
38
39 $this->_dataDirectory = $this->_webserviceConfig->getConfig("dataDirectory");
40 $this->assertInternaltype("string", $this->_dataDirectory);
41 $this->assertTrue(!empty($this->_dataDirectory));
42 if(file_exists($this->_dataDirectory))
43 $this->assertTrue(TestHelpers\TestSetup::wipeDataDirectory());
44 $this->assertTrue(TestHelpers\TestSetup::databaseSetup());
45 }
46
47 protected function tearDown(){
48 if(file_exists($this->_dataDirectory))
49 $this->assertTrue(TestHelpers\TestSetup::wipeDataDirectory());
50 }
51
52 public function testEmptyStations()
53 {
54 $stations = $this->query("index.php/stations");
55
56 $this->assertInternalType("array", $stations);
57 $message = "";
58 if(array_key_exists("message", $stations))
59 $message = $stations["message"];
60 $this->assertTrue(!array_key_exists("result", $stations), $message);
61 $this->assertEquals(count($stations), 0);
62 }
63
64 public function testRegisterStationUnprovenLocation()
65 {
66 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
67 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
68 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
69 $client = new GuzzleHttp\Client
70 (
71 [
72 "base_uri" => $baseUrl,
73 "timeout" => 5.0,
74 "http_errors" => false,
75 ]
76 );
77
78 // sending must failing register request, caused by unproven location
79 $response = $client->post("index.php/stations/register",
80 [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "email" => "test@example.org"]]
81 );
82 $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
83 $json = json_decode($response->getBody(), true);
84 $this->assertTrue(array_key_exists("result", $json));
85 $this->assertTrue(array_key_exists("message", $json));
86 $this->assertEquals("unprovenlocation", $json["result"], $json["message"]);
87 }
88
89 public function testRegisterStation()
90 {
91 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
92 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
93 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
94 $client = new GuzzleHttp\Client
95 (
96 [
97 "base_uri" => $baseUrl,
98 "timeout" => 5.0,
99 "http_errors" => false,
100 ]
101 );
102
103 $response = $client->post("index.php/stations/register",
104 [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "email" => "test@example.org"]]
105 );
106 $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
107 $json = json_decode($response->getBody(), true);
108 $this->assertTrue(array_key_exists("result", $json));
109 $this->assertEquals("success", $json["result"]);
110 $this->assertTrue(array_key_exists("uuid", $json));
111 $this->assertTrue(!empty($json["uuid"]));
112
113 $stations = $this->query("stations");
114 $this->assertInternalType("array", $stations);
115 $this->assertEquals(1, count($stations));
116 $this->assertTrue(array_key_exists("uuid", $stations[0]));
117 $this->assertEquals($json["uuid"], $stations[0]["uuid"]);
118 }
119
120 protected function query($url){
121 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
122 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
123 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
124 $client = new GuzzleHttp\Client
125 (
126 [
127 "base_uri" => $baseUrl,
128 "timeout" => 2.0,
129 "http_errors" => false,
130 ]
131 );
132 $response = $client->get($url);
133 $this->assertNotNull($response, "Response is empty. Url: ". $baseUrl . $url);
134 $this->assertEquals($response->getStatusCode(), 200, "Status code != 200. Url: ". $baseUrl . $url);
135 $this->assertEquals($response->getReasonPhrase(), "OK", "Reason phrase not empty. Url: ". $baseUrl . $url);
136
137 $body = $response->getBody();
138 $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
139 $json = json_decode($body, true);
140 $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
141 return $json;
142 }
143
144 protected $_testconfig;
145 protected $_webserviceConfig;
146 protected $_dataDirectory;
147}
148?>
Note: See TracBrowser for help on using the repository browser.