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
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
[02fbeb8]21/**
22 * @todo Reset database before this test is run
23 */
[8c356c1]24class StationsTest extends PHPUnit_Framework_TestCase
[1c92d58]25{
26 protected function setUp()
27 {
[8c356c1]28 $file = __DIR__ ."/testconfig/testconfig.php";
[1c92d58]29 $this->assertFileExists($file);
30 require($file);
[8c356c1]31 $this->_testconfig = $testconfig;
32 $this->assertNotNull($this->_testconfig);
33
[1e37648]34 require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
[8c356c1]35 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
36
37 $this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
38 $this->assertNotNull($this->_webserviceConfig);
[1c92d58]39 }
40
41 public function testEmptyStations()
42 {
[8c356c1]43 $stations = $this->query("index.php/stations");
44
45 $this->assertInternalType("array", $stations);
46 $this->assertEquals(count($stations), 0);
[1c92d58]47 }
48
49 public function testStationRewrite()
50 {
[1e37648]51 $stations = $this->query("stations");
[8c356c1]52
53 $this->assertInternalType("array", $stations);
54 $this->assertEquals(count($stations), 0);
[1c92d58]55 }
56
[02fbeb8]57 public function testRegisterStationUnprovenLocation()
[b858e6f]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,
[02fbeb8]66 "timeout" => 5.0,
[b858e6f]67 "http_errors" => false,
68 ]
69 );
[519df6e]70
71 // sending must failing register request, caused by unproven location
[b858e6f]72 $response = $client->post("index.php/stations/register",
[02fbeb8]73 [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "email" => "test@example.org"]]
[b858e6f]74 );
[519df6e]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));
[02fbeb8]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
[519df6e]97 $response = $client->post("index.php/stations/register",
[02fbeb8]98 [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "email" => "test@example.org"]]
[519df6e]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"]);
[02fbeb8]104 $this->assertTrue(array_key_exists("uuid", $json));
[519df6e]105 $this->assertTrue(!empty($json["uuid"]));
106
[02fbeb8]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"]);
[b858e6f]112 }
113
[8c356c1]114 protected function query($url){
[1e37648]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);
[1c92d58]118 $client = new GuzzleHttp\Client
119 (
120 [
[8c356c1]121 "base_uri" => $baseUrl,
[1c92d58]122 "timeout" => 2.0,
[8c356c1]123 "http_errors" => false,
[1c92d58]124 ]
125 );
[8c356c1]126 $response = $client->get($url);
[1e37648]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);
[1c92d58]130
131 $body = $response->getBody();
[1e37648]132 $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
[519df6e]133 $json = json_decode($body, true);
[1e37648]134 $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
[8c356c1]135 return $json;
[1c92d58]136 }
[8c356c1]137
138 protected $_testconfig;
139 protected $_webserviceConfig;
[1c92d58]140}
141?>
Note: See TracBrowser for help on using the repository browser.