source: src/webservice/tests/StationsTest.php@ 17ef75d

Last change on this file since 17ef75d was e498070, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Property mode set to 100644
File size: 5.7 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{
[256102b]25 protected function setUp(){
[8c356c1]26 $file = __DIR__ ."/testconfig/testconfig.php";
[1c92d58]27 $this->assertFileExists($file);
28 require($file);
[8c356c1]29 $this->_testconfig = $testconfig;
30 $this->assertNotNull($this->_testconfig);
31
[1e37648]32 require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
[8c356c1]33 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
34
35 $this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
36 $this->assertNotNull($this->_webserviceConfig);
[d55ec21]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());
[256102b]44 }
[d55ec21]45
[256102b]46 protected function tearDown(){
[d55ec21]47 if(file_exists($this->_dataDirectory))
48 $this->assertTrue(TestHelpers\TestSetup::wipeDataDirectory());
[256102b]49 }
[1c92d58]50
51 public function testEmptyStations()
52 {
[8c356c1]53 $stations = $this->query("index.php/stations");
54
55 $this->assertInternalType("array", $stations);
[d55ec21]56 $message = "";
57 if(array_key_exists("message", $stations))
58 $message = $stations["message"];
59 $this->assertTrue(!array_key_exists("result", $stations), $message);
[8c356c1]60 $this->assertEquals(count($stations), 0);
[1c92d58]61 }
[8c356c1]62
[02fbeb8]63 public function testRegisterStationUnprovenLocation()
[b858e6f]64 {
[d55ec21]65 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
[b858e6f]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,
[02fbeb8]72 "timeout" => 5.0,
[b858e6f]73 "http_errors" => false,
74 ]
75 );
[519df6e]76
77 // sending must failing register request, caused by unproven location
[b858e6f]78 $response = $client->post("index.php/stations/register",
[e498070]79 [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "type" => "testing", "email" => "test@example.org"]]
[b858e6f]80 );
[519df6e]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));
[02fbeb8]85 $this->assertEquals("unprovenlocation", $json["result"], $json["message"]);
86 }
87
88 public function testRegisterStation()
89 {
[d55ec21]90 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
[02fbeb8]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
[519df6e]102 $response = $client->post("index.php/stations/register",
[e498070]103 [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "type" => "testing", "email" => "test@example.org"]]
[519df6e]104 );
[9b1e043]105 $this->assertEquals(200, $response->getStatusCode(), $response->getReasonPhrase());
[519df6e]106 $json = json_decode($response->getBody(), true);
107 $this->assertTrue(array_key_exists("result", $json));
[e498070]108 $this->assertEquals("success", $json["result"], $json["message"]);
[02fbeb8]109 $this->assertTrue(array_key_exists("uuid", $json));
[519df6e]110 $this->assertTrue(!empty($json["uuid"]));
[e498070]111 $this->assertTrue(array_key_exists("message", $json));
112 $this->assertTrue(!empty($json["message"]));
[519df6e]113
[9b1e043]114 $stations = $this->query("index.php/stations");
[02fbeb8]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"]);
[b858e6f]119 }
120
[8c356c1]121 protected function query($url){
[d55ec21]122 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
[1e37648]123 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
124 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
[1c92d58]125 $client = new GuzzleHttp\Client
126 (
127 [
[8c356c1]128 "base_uri" => $baseUrl,
[1c92d58]129 "timeout" => 2.0,
[8c356c1]130 "http_errors" => false,
[1c92d58]131 ]
132 );
[8c356c1]133 $response = $client->get($url);
[1e37648]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);
[1c92d58]137
138 $body = $response->getBody();
[1e37648]139 $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
[519df6e]140 $json = json_decode($body, true);
[1e37648]141 $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
[8c356c1]142 return $json;
[1c92d58]143 }
[8c356c1]144
145 protected $_testconfig;
146 protected $_webserviceConfig;
[d55ec21]147 protected $_dataDirectory;
[1c92d58]148}
149?>
Note: See TracBrowser for help on using the repository browser.