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
RevLine 
[1c92d58]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
[d55ec21]21require_once(__DIR__ ."/TestHelpers/TestSetup.php");
22
[8c356c1]23class StationsTest extends PHPUnit_Framework_TestCase
[1c92d58]24{
25 protected function setUp()
26 {
[8c356c1]27 $file = __DIR__ ."/testconfig/testconfig.php";
[1c92d58]28 $this->assertFileExists($file);
29 require($file);
[8c356c1]30 $this->_testconfig = $testconfig;
31 $this->assertNotNull($this->_testconfig);
32
[1e37648]33 require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
[8c356c1]34 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
35
36 $this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
37 $this->assertNotNull($this->_webserviceConfig);
[d55ec21]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());
[1c92d58]50 }
51
52 public function testEmptyStations()
53 {
[8c356c1]54 $stations = $this->query("index.php/stations");
55
56 $this->assertInternalType("array", $stations);
[d55ec21]57 $message = "";
58 if(array_key_exists("message", $stations))
59 $message = $stations["message"];
60 $this->assertTrue(!array_key_exists("result", $stations), $message);
[8c356c1]61 $this->assertEquals(count($stations), 0);
[1c92d58]62 }
[8c356c1]63
[02fbeb8]64 public function testRegisterStationUnprovenLocation()
[b858e6f]65 {
[d55ec21]66 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
[b858e6f]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,
[02fbeb8]73 "timeout" => 5.0,
[b858e6f]74 "http_errors" => false,
75 ]
76 );
[519df6e]77
78 // sending must failing register request, caused by unproven location
[b858e6f]79 $response = $client->post("index.php/stations/register",
[02fbeb8]80 [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "email" => "test@example.org"]]
[b858e6f]81 );
[519df6e]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));
[02fbeb8]86 $this->assertEquals("unprovenlocation", $json["result"], $json["message"]);
87 }
88
89 public function testRegisterStation()
90 {
[d55ec21]91 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
[02fbeb8]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
[519df6e]103 $response = $client->post("index.php/stations/register",
[02fbeb8]104 [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "email" => "test@example.org"]]
[519df6e]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"]);
[02fbeb8]110 $this->assertTrue(array_key_exists("uuid", $json));
[519df6e]111 $this->assertTrue(!empty($json["uuid"]));
112
[02fbeb8]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"]);
[b858e6f]118 }
119
[8c356c1]120 protected function query($url){
[d55ec21]121 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
[1e37648]122 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
123 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
[1c92d58]124 $client = new GuzzleHttp\Client
125 (
126 [
[8c356c1]127 "base_uri" => $baseUrl,
[1c92d58]128 "timeout" => 2.0,
[8c356c1]129 "http_errors" => false,
[1c92d58]130 ]
131 );
[8c356c1]132 $response = $client->get($url);
[1e37648]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);
[1c92d58]136
137 $body = $response->getBody();
[1e37648]138 $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
[519df6e]139 $json = json_decode($body, true);
[1e37648]140 $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
[8c356c1]141 return $json;
[1c92d58]142 }
[8c356c1]143
144 protected $_testconfig;
145 protected $_webserviceConfig;
[d55ec21]146 protected $_dataDirectory;
[1c92d58]147}
148?>
Note: See TracBrowser for help on using the repository browser.