source: src/webservice/tests/TestHelpers/TestSetup.php@ 8a5cdba

Last change on this file since 8a5cdba was 9b1e043, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Fixed testing with php built in webservice

  • Property mode set to 100644
File size: 4.4 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 $webserviceBaseUrl = $webserviceConfig->webserviceBaseUrl();
45 if(empty($webserviceBaseUrl) || preg_match("/^https?:\/\/localhost/", $webserviceBaseUrl) != 1){
46 error_log("NOT TESTING AGAINST REMOTE WEBSERVICE. INSTALL WEBSERVICE ON LOCALHOST TO TEST IT!");
47 return false;
48 }
49 return $webserviceBaseUrl;
50 }
51
52 /**
53 * @brief Configures the database of the webservice which is used for the tests.
54 *
55 * @note This function checks whether the webservice base url starts with localhost. If not,
56 * false is returned and the remote webservice is not touched. We do not touch remote
57 * webservices, not on localhost for testing purposes.
58 */
59 public static function databaseSetup(){
60 $file = __DIR__ ."/../testconfig/testconfig.php";
61 if(!file_exists($file)){
62 error_log("testconfig file ". $file ." not found");
63 return false;
64 }
65 require($file);
66 if(is_null($testconfig)){
67 error_log("No testconfig found");
68 return false;
69 }
70
71 require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
72 require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
73
74 $webserviceConfig = new \SiWeather\Webservice\WebserviceConfig();
75 if(is_null($webserviceConfig)){
76 error_log("No webservice config created");
77 return false;
78 }
79
80 $webserviceBaseUrl = self::webserviceTestBaseUrl();
81 if(!$webserviceBaseUrl){
82 error_log("Got no webservice test base url");
83 return false;
84 }
85
86 $client = new \GuzzleHttp\Client
87 (
88 [
89 "base_uri" => $webserviceBaseUrl,
90 "timeout" => 15.0, // Needs some time to create the database!
91 "http_errors" => false,
92 ]
93 );
94 $response = $client->get("setup.php");
95 if($response->getStatusCode() != 200){
96 error_log("Failed to setup webservice database: ". $response->getReasonPhrase());
97 return false;
98 }
99 $body = $response->getBody();
100 if(empty($body)){
101 error_log("Failed to setup webservice database: Empty response!");
102 return false;
103 }
104
105 // since rewrite api is not working, we get an error here!
106// if(strpos($body, "error")){
107// error_log("Failed to setup webservice database: Found error in reply (". $body .")");
108// return false;
109// }
110
111 return true;
112 }
113
114 public static function wipeDataDirectory(){
115 $webserviceConfig = new \SiWeather\Webservice\WebserviceConfig();
116 $dataDirectory = $webserviceConfig->dataDirectory();
117 if(!file_exists($dataDirectory))
118 return true;
119
120 // clear data directory (not recursive, because there should be only a database file in it
121 $content = scandir($dataDirectory);
122 if(!is_array($content)){
123 error_log("result of scandir is not an array");
124 return false;
125 }
126 foreach($content as $file){
127 if(!in_array($file, array(".",".."))){
128 $file = $dataDirectory ."/". $file;
129 if(!unlink($file)){
130 error_log("Failed to delete file ". $file);
131 return false;
132 }
133 }
134 }
135 return rmdir($dataDirectory);
136 }
137}
138
139?>
Note: See TracBrowser for help on using the repository browser.