| [d55ec21] | 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 | namespace TestHelpers;
|
|---|
| 22 |
|
|---|
| 23 | class 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 | }
|
|---|
| [cf345ee] | 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;
|
|---|
| [d55ec21] | 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();
|
|---|
| [cf345ee] | 81 | if(!$webserviceBaseUrl){
|
|---|
| 82 | error_log("Got no webservice test base url");
|
|---|
| [d55ec21] | 83 | return false;
|
|---|
| 84 | }
|
|---|
| [cf345ee] | 85 |
|
|---|
| 86 | error_log($webserviceBaseUrl);
|
|---|
| [d55ec21] | 87 | $client = new \GuzzleHttp\Client
|
|---|
| 88 | (
|
|---|
| 89 | [
|
|---|
| 90 | "base_uri" => $webserviceBaseUrl,
|
|---|
| [cf345ee] | 91 | "timeout" => 2.0,
|
|---|
| [d55ec21] | 92 | "http_errors" => false,
|
|---|
| 93 | ]
|
|---|
| 94 | );
|
|---|
| 95 | $response = $client->get("setup.php");
|
|---|
| 96 | if($response->getStatusCode() != 200){
|
|---|
| 97 | error_log("Failed to setup webservice database: ". $response->getReasonPhrase());
|
|---|
| 98 | return false;
|
|---|
| 99 | }
|
|---|
| 100 | $body = $response->getBody();
|
|---|
| 101 | if(empty($body)){
|
|---|
| 102 | error_log("Failed to setup webservice database: Empty response!");
|
|---|
| 103 | return false;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 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 | ?>
|
|---|