<?php
/*
    Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301  USA.
*/

namespace TestHelpers;

class TestSetup {
	
	public static function webserviceTestBaseUrl(){
		$file = __DIR__ ."/../testconfig/testconfig.php";
		if(!file_exists($file)){
			error_log("testconfig file ". $file ." not found");
			return false;
		}
		require($file);
		if(is_null($testconfig)){
			error_log("No testconfig found");
			return false;
		}
		require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
		require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
		
		$webserviceConfig = new \SiWeather\Webservice\WebserviceConfig();
		if(is_null($webserviceConfig)){
			error_log("No webservice config created");
			return false;
		}
		return "http://localhost:9999/". $webserviceConfig->getConfig("webserviceApiVersion") ."/";
	}

	/**
	  * @brief Configures the database of the webservice which is used for the tests.
	  *
	  * @note This function checks whether the webservice base url starts with localhost. If not,
	  *       false is returned and the remote webservice is not touched. We do not touch remote
	  *       webservices, not on localhost for testing purposes.
	  */
	public static function databaseSetup(){
		$file = __DIR__ ."/../testconfig/testconfig.php";
		if(!file_exists($file)){
			error_log("testconfig file ". $file ." not found");
			return false;
		}
		require($file);
		if(is_null($testconfig)){
			error_log("No testconfig found");
			return false;
		}
		
		require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
		require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
		
		$webserviceConfig = new \SiWeather\Webservice\WebserviceConfig();
		if(is_null($webserviceConfig)){
			error_log("No webservice config created");
			return false;
		}

		$webserviceBaseUrl = self::webserviceTestBaseUrl();
		if(empty($webserviceBaseUrl) || !preg_match("/^https?:\/\/localhost/", $webserviceBaseUrl)){
			error_log("NOT TESTING AGAINST REMOTE WEBSERVICE. INSTALL WEBSERVICE ON LOCALHOST TO TEST IT!");
			return false;
		}
		
		$client = new \GuzzleHttp\Client
		(
			[
				"base_uri" => $webserviceBaseUrl,
				"timeout" => 5.0,
				"http_errors" => false,
			]
		);
		$response = $client->get("setup.php");
		if($response->getStatusCode() !=  200){
			error_log("Failed to setup webservice database: ". $response->getReasonPhrase());
			return false;
		}
		$body = $response->getBody();
		if(empty($body)){
			error_log("Failed to setup webservice database: Empty response!");
			return false;
		}
		
		if(strpos($body, "error")){
			error_log("Failed to setup webservice database: Found error in reply (". $body .")");
			return false;
		}

		return true;
	}

	public static function wipeDataDirectory(){
		$webserviceConfig = new \SiWeather\Webservice\WebserviceConfig();
		$dataDirectory = $webserviceConfig->dataDirectory();
		if(!file_exists($dataDirectory))
			return true;

		// clear data directory (not recursive, because there should be only a database file in it
		$content = scandir($dataDirectory);
		if(!is_array($content)){
			error_log("result of scandir is not an array");
			return false;
		}
		foreach($content as $file){
			if(!in_array($file, array(".",".."))){
				$file = $dataDirectory ."/". $file;
				if(!unlink($file)){
					error_log("Failed to delete file ". $file);
					return false;
				}
			}
		}
		return rmdir($dataDirectory);
	}
}

?>
