source: src/webservice/tests/TestHelpers/TestSetup.php@ 098c36e

Last change on this file since 098c36e was d55ec21, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Improved testing execution of webservice related tests (they use the built in php webserver to create a test webservice instance)
  • Property mode set to 100644
File size: 4.2 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
21namespace TestHelpers;
22
23class TestSetup {
24
25 public static function webserviceTestBaseUrl(){
26 $file = __DIR__ ."/../testconfig/testconfig.php";
27 if(!file_exists($file)){
28 error_log("testconfig file ". $file ." not found");
29 return false;
30 }
31 require($file);
32 if(is_null($testconfig)){
33 error_log("No testconfig found");
34 return false;
35 }
36 require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
37 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
38
39 $webserviceConfig = new \SiWeather\Webservice\WebserviceConfig();
40 if(is_null($webserviceConfig)){
41 error_log("No webservice config created");
42 return false;
43 }
44 return "http://localhost:9999/". $webserviceConfig->getConfig("webserviceApiVersion") ."/";
45 }
46
47 /**
48 * @brief Configures the database of the webservice which is used for the tests.
49 *
50 * @note This function checks whether the webservice base url starts with localhost. If not,
51 * false is returned and the remote webservice is not touched. We do not touch remote
52 * webservices, not on localhost for testing purposes.
53 */
54 public static function databaseSetup(){
55 $file = __DIR__ ."/../testconfig/testconfig.php";
56 if(!file_exists($file)){
57 error_log("testconfig file ". $file ." not found");
58 return false;
59 }
60 require($file);
61 if(is_null($testconfig)){
62 error_log("No testconfig found");
63 return false;
64 }
65
66 require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
67 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
68
69 $webserviceConfig = new \SiWeather\Webservice\WebserviceConfig();
70 if(is_null($webserviceConfig)){
71 error_log("No webservice config created");
72 return false;
73 }
74
75 $webserviceBaseUrl = self::webserviceTestBaseUrl();
76 if(empty($webserviceBaseUrl) || !preg_match("/^https?:\/\/localhost/", $webserviceBaseUrl)){
77 error_log("NOT TESTING AGAINST REMOTE WEBSERVICE. INSTALL WEBSERVICE ON LOCALHOST TO TEST IT!");
78 return false;
79 }
80
81 $client = new \GuzzleHttp\Client
82 (
83 [
84 "base_uri" => $webserviceBaseUrl,
85 "timeout" => 5.0,
86 "http_errors" => false,
87 ]
88 );
89 $response = $client->get("setup.php");
90 if($response->getStatusCode() != 200){
91 error_log("Failed to setup webservice database: ". $response->getReasonPhrase());
92 return false;
93 }
94 $body = $response->getBody();
95 if(empty($body)){
96 error_log("Failed to setup webservice database: Empty response!");
97 return false;
98 }
99
100 if(strpos($body, "error")){
101 error_log("Failed to setup webservice database: Found error in reply (". $body .")");
102 return false;
103 }
104
105 return true;
106 }
107
108 public static function wipeDataDirectory(){
109 $webserviceConfig = new \SiWeather\Webservice\WebserviceConfig();
110 $dataDirectory = $webserviceConfig->dataDirectory();
111 if(!file_exists($dataDirectory))
112 return true;
113
114 // clear data directory (not recursive, because there should be only a database file in it
115 $content = scandir($dataDirectory);
116 if(!is_array($content)){
117 error_log("result of scandir is not an array");
118 return false;
119 }
120 foreach($content as $file){
121 if(!in_array($file, array(".",".."))){
122 $file = $dataDirectory ."/". $file;
123 if(!unlink($file)){
124 error_log("Failed to delete file ". $file);
125 return false;
126 }
127 }
128 }
129 return rmdir($dataDirectory);
130 }
131}
132
133?>
Note: See TracBrowser for help on using the repository browser.