source: src/webservice/tests/StationsTest.php@ 02fbeb8

Last change on this file since 02fbeb8 was 02fbeb8, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Implmented and started testing of /stations/register

  • Property mode set to 100644
File size: 5.1 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
21/**
22 * @todo Reset database before this test is run
23 */
24class StationsTest extends PHPUnit_Framework_TestCase
25{
26 protected function setUp()
27 {
28 $file = __DIR__ ."/testconfig/testconfig.php";
29 $this->assertFileExists($file);
30 require($file);
31 $this->_testconfig = $testconfig;
32 $this->assertNotNull($this->_testconfig);
33
34 require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
35 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
36
37 $this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
38 $this->assertNotNull($this->_webserviceConfig);
39 }
40
41 public function testEmptyStations()
42 {
43 $stations = $this->query("index.php/stations");
44
45 $this->assertInternalType("array", $stations);
46 $this->assertEquals(count($stations), 0);
47 }
48
49 public function testStationRewrite()
50 {
51 $stations = $this->query("stations");
52
53 $this->assertInternalType("array", $stations);
54 $this->assertEquals(count($stations), 0);
55 }
56
57 public function testRegisterStationUnprovenLocation()
58 {
59 $baseUrl = $this->_webserviceConfig->webserviceBaseUrl();
60 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
61 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
62 $client = new GuzzleHttp\Client
63 (
64 [
65 "base_uri" => $baseUrl,
66 "timeout" => 5.0,
67 "http_errors" => false,
68 ]
69 );
70
71 // sending must failing register request, caused by unproven location
72 $response = $client->post("index.php/stations/register",
73 [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "email" => "test@example.org"]]
74 );
75 $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
76 $json = json_decode($response->getBody(), true);
77 $this->assertTrue(array_key_exists("result", $json));
78 $this->assertTrue(array_key_exists("message", $json));
79 $this->assertEquals("unprovenlocation", $json["result"], $json["message"]);
80 }
81
82 public function testRegisterStation()
83 {
84 $baseUrl = $this->_webserviceConfig->webserviceBaseUrl();
85 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
86 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
87 $client = new GuzzleHttp\Client
88 (
89 [
90 "base_uri" => $baseUrl,
91 "timeout" => 5.0,
92 "http_errors" => false,
93 ]
94 );
95
96 // sending must failing register request, caused by unproven location
97 $response = $client->post("index.php/stations/register",
98 [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "email" => "test@example.org"]]
99 );
100 $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
101 $json = json_decode($response->getBody(), true);
102 $this->assertTrue(array_key_exists("result", $json));
103 $this->assertEquals("success", $json["result"]);
104 $this->assertTrue(array_key_exists("uuid", $json));
105 $this->assertTrue(!empty($json["uuid"]));
106
107 $stations = $this->query("stations");
108 $this->assertInternalType("array", $stations);
109 $this->assertEquals(1, count($stations));
110 $this->assertTrue(array_key_exists("uuid", $stations[0]));
111 $this->assertEquals($json["uuid"], $stations[0]["uuid"]);
112 }
113
114 protected function query($url){
115 $baseUrl = $this->_webserviceConfig->webserviceBaseUrl();
116 $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
117 $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
118 $client = new GuzzleHttp\Client
119 (
120 [
121 "base_uri" => $baseUrl,
122 "timeout" => 2.0,
123 "http_errors" => false,
124 ]
125 );
126 $response = $client->get($url);
127 $this->assertNotNull($response, "Response is empty. Url: ". $baseUrl . $url);
128 $this->assertEquals($response->getStatusCode(), 200, "Status code != 200. Url: ". $baseUrl . $url);
129 $this->assertEquals($response->getReasonPhrase(), "OK", "Reason phrase not empty. Url: ". $baseUrl . $url);
130
131 $body = $response->getBody();
132 $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
133 $json = json_decode($body, true);
134 $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
135 return $json;
136 }
137
138 protected $_testconfig;
139 protected $_webserviceConfig;
140}
141?>
Note: See TracBrowser for help on using the repository browser.