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

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

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

//  enable in php 7
// declare(Strict_types=1);

namespace GnuBoat\Nmea
{

	/**
	* @brief Covers Parser.php
	*/
	final class NmeaTest extends \PHPUnit_Framework_TestCase
	{
		/**
		* @dataProvider formatsProvider
		*/
		public function testFormats($format)
		{
			$parser = new Parser(0);
			$formats = $parser->formatsSupported();
			$this->assertInternalType('array', $formats);
			$this->assertContains($format, $formats);
		}
		
		public function formatsProvider()
		{
			return [["0183"]];
		}

		/**
		* @dataProvider nmeaSentenceProvider
		*/
		public function testNmeaSentence($format, $sentence, $classType, $valid)
		{
			$parser = new Parser(0);
			$sentence = $parser->parseSentence($sentence, $format);
			if($classType == null)
				$this->assertNull($sentence);
			else{
				$this->assertInstanceOf($classType, $sentence);
				$message = "";
				$result = $sentence->valid($message);
				$this->assertEquals($valid, $result, $message);
			}
		}
		
		public function nmeaSentenceProvider()
		{
			return [
				"empty" => ['0183', '', null, false],
				"unsupported_format" => ['abc', '', null, false],
				"rmc0183" => ['0183', '$GNRMC,122548,A,4735.4480,N,01042.6240,E,00.0,115.6,110917,,,A*50', Nmea0183\Nmea0183Rmc::class, true],
				"dpt0183" => ['0183', '$SDDPT,00.71,00.00*51', Nmea0183\Nmea0183Dpt::class, true],
				"mtw0183" => ['0183', '$YXMTW,26.3,C*15', Nmea0183\Nmea0183Mtw::class, true],
				"rmc0183timestamp" => ['0183', '2017-09-11 13:20:42: $GNRMC,122548,A,4735.4480,N,01042.6240,E,00.0,115.6,110917,,,A*50', Nmea0183\Nmea0183Rmc::class, true],
			];
		}
		
		/**
		* @dataProvider nmeaFileProvider
		*/
		public function testNmeaFile($file, $format, $count)
		{
			$parser = new Parser(0);
			$sentences = $parser->parseFile($file, $format);
			$this->assertInternalType('array', $sentences);
			$this->assertCount($count, $sentences);
			foreach($sentences as $sentence){
				$this->assertNotNull($sentence);
				$this->assertInstanceOf(NmeaSentence::class, $sentence);
				$this->assertEquals(true, $sentence->valid());
			}
		}
		
		public function nmeaFileProvider()
		{
			return [["webservice/tests/logs/nmea0183.log", '0183', 2000]];
		}
	}

	function error_log($msg)
	{
		echo "ERR Nmea: ". $msg ."\n";
	}
}

namespace GnuBoat\Nmea\Nmea0183
{
	function error_log($msg)
	{
		echo "ERR Nmea0183: ". $msg ."\n";
	}
}

?>
