source: src/webservice/tests/StationsTest.php@ 519df6e

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

Proceeded in implementing /stations/register function

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