<?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.
*/

require_once(__DIR__ ."/TestHelpers/TestSetup.php");

class StationsTest extends PHPUnit_Framework_TestCase
{
	protected function setUp(){
		$file = __DIR__ ."/testconfig/testconfig.php";
		$this->assertFileExists($file);
		require($file);
		$this->_testconfig = $testconfig; 
		$this->assertNotNull($this->_testconfig);
		
		require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
		require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
		
		$this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
		$this->assertNotNull($this->_webserviceConfig);

		$this->_dataDirectory = $this->_webserviceConfig->getConfig("dataDirectory");
		$this->assertInternaltype("string", $this->_dataDirectory);
		$this->assertTrue(!empty($this->_dataDirectory));
		if(file_exists($this->_dataDirectory))
			$this->assertTrue(TestHelpers\TestSetup::wipeDataDirectory());
		$this->assertTrue(TestHelpers\TestSetup::databaseSetup());
	}
    
	protected function tearDown(){
		if(file_exists($this->_dataDirectory))
			$this->assertTrue(TestHelpers\TestSetup::wipeDataDirectory());
	}

	public function testEmptyStations()
	{
		$stations = $this->query("index.php/stations");

		$this->assertInternalType("array", $stations);
		$message = "";
		if(array_key_exists("message", $stations))
			$message = $stations["message"];
		$this->assertTrue(!array_key_exists("result", $stations), $message);
		$this->assertEquals(count($stations), 0);
	}

	public function testRegisterStationUnprovenLocation()
	{
		$baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
		$this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
		$this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
		$client = new GuzzleHttp\Client
		(
			[
				"base_uri" => $baseUrl,
				"timeout" => 5.0,
				"http_errors" => false,
			]
		);

		// sending must failing register request, caused by unproven location
		$response = $client->post("index.php/stations/register",
			[ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "email" => "test@example.org"]]
		);
		$this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
		$json = json_decode($response->getBody(), true);
		$this->assertTrue(array_key_exists("result", $json));
		$this->assertTrue(array_key_exists("message", $json));
		$this->assertEquals("unprovenlocation", $json["result"], $json["message"]);
	}

	public function testRegisterStation()
	{
		$baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
		$this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
		$this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
		$client = new GuzzleHttp\Client
		(
			[
				"base_uri" => $baseUrl,
				"timeout" => 5.0,
				"http_errors" => false,
			]
		);

		$response = $client->post("index.php/stations/register",
			[ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "email" => "test@example.org"]]
		);
		$this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
		$json = json_decode($response->getBody(), true);
		$this->assertTrue(array_key_exists("result", $json));
		$this->assertEquals("success", $json["result"]);
		$this->assertTrue(array_key_exists("uuid", $json));
		$this->assertTrue(!empty($json["uuid"]));

		$stations = $this->query("stations");
		$this->assertInternalType("array", $stations);
		$this->assertEquals(1, count($stations));
		$this->assertTrue(array_key_exists("uuid", $stations[0]));
		$this->assertEquals($json["uuid"], $stations[0]["uuid"]);
	}
	
	protected function query($url){
		$baseUrl = TestHelpers\TestSetup::webserviceTestBaseUrl();
		$this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
		$this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
		$client = new GuzzleHttp\Client
		(
			[
				"base_uri" => $baseUrl,
				"timeout" => 2.0,
				"http_errors" => false,
			]
		);
		$response = $client->get($url);
		$this->assertNotNull($response, "Response is empty. Url: ". $baseUrl . $url);
		$this->assertEquals($response->getStatusCode(), 200, "Status code != 200. Url: ". $baseUrl . $url);
		$this->assertEquals($response->getReasonPhrase(), "OK", "Reason phrase not empty. Url: ". $baseUrl . $url);

		$body = $response->getBody();
		$this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
		$json = json_decode($body, true);
		$this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
		return $json;
	}
	
	protected $_testconfig;
	protected $_webserviceConfig;
	protected $_dataDirectory;
}
?>
