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

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_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
		
		$this->_webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
		$this->assertNotNull($this->_webserviceConfig);
    }

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

		$this->assertInternalType("array", $stations);
		$this->assertEquals(count($stations), 0);
	}
	
	public function testStationRewrite()
	{
		$apiVersion = $this->_webserviceConfig->getConfig("webserviceApiVersion");
		$this->assertInternalType('string', $apiVersion);
		$this->assertTrue(!empty($apiVersion));

		$stations = $this->query($apiVersion ."/stations");

		$this->assertInternalType("array", $stations);
		$this->assertEquals(count($stations), 0);
	}
	
	protected function query($url){
		require(__DIR__ ."/../vendor/autoload.php");

		$baseUrl = $this->_webserviceConfig->getConfig("webserviceBaseUrl");
		$this->assertInternalType('string', $baseUrl);
		$this->assertTrue(!empty($baseUrl));
		$client = new GuzzleHttp\Client
		(
			[
				"base_uri" => $baseUrl,
				"timeout" => 2.0,
				"http_errors" => false,
			]
		);
		$response = $client->get($url);
		$this->assertNotNull($response);
		$this->assertEquals($response->getStatusCode(), 200);
		$this->assertEquals($response->getReasonPhrase(), "OK");

		$body = $response->getBody();
		$this->assertTrue(!empty($body));
		$json = json_decode($body);
		$this->assertNotNull($json);
		return $json;
	}
	
	protected $_testconfig;
	protected $_webserviceConfig;
}
?>
