close Warning: AdminModule failed with TracError: Unable to instantiate component <class 'trac.ticket.admin.PriorityAdminPanel'> (super(type, obj): obj (type PriorityAdminPanel) is not an instance or subtype of type (Markup).)

source: src/webservice/tests/StationsTest.php@ 098c36e

Last change on this file since 098c36e was 256102b, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Restructered cmake webservice_* variables

  • 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{
[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",
[02fbeb8]79 [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "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",
[02fbeb8]103 [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "email" => "test@example.org"]]
[519df6e]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"]);
[02fbeb8]109 $this->assertTrue(array_key_exists("uuid", $json));
[519df6e]110 $this->assertTrue(!empty($json["uuid"]));
111
[02fbeb8]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"]);
[b858e6f]117 }
118
[8c356c1]119 protected function query($url){
[d55ec21]120 $baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
[1e37648]121 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
122 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
[1c92d58]123 $client = new GuzzleHttp\Client
124 (
125 [
[8c356c1]126 "base_uri" => $baseUrl,
[1c92d58]127 "timeout" => 2.0,
[8c356c1]128 "http_errors" => false,
[1c92d58]129 ]
130 );
[8c356c1]131 $response = $client->get($url);
[1e37648]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);
[1c92d58]135
136 $body = $response->getBody();
[1e37648]137 $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
[519df6e]138 $json = json_decode($body, true);
[1e37648]139 $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
[8c356c1]140 return $json;
[1c92d58]141 }
[8c356c1]142
143 protected $_testconfig;
144 protected $_webserviceConfig;
[d55ec21]145 protected $_dataDirectory;
[1c92d58]146}
147?>
Note: See TracBrowser for help on using the repository browser.