<?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);

/**
  * @brief Covers Parser.php
  */
final class NmeaTest extends PHPUnit_Framework_TestCase
{
	/**
	  * @dataProvider formatsProvider
	  */
	public function testFormats($format)
	{
		$parser = new GnuBoat\Nmea\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 GnuBoat\Nmea\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,173658,A,0047.589,N,00010.713,E,01.1,147.4,260817,,,A*5F', GnuBoat\Nmea\Nmea0183\Nmea0183Rmc::class, true],
			"dpt0183" => ['0183', '$SDDPT,00.71,00.00*51', GnuBoat\Nmea\Nmea0183\Nmea0183Dpt::class, true],
			"mtw0183" => ['0183', '$YXMTW,26.3,C*15', GnuBoat\Nmea\Nmea0183\Nmea0183Mtw::class, true],
		];
	}
	
	/**
	 * @dataProvider nmeaFileProvider
	 */
	public function testNmeaFile($file, $format, $count)
	{
		$parser = new GnuBoat\Nmea\Parser(0);
		$sentences = $parser->parseFile($file, $format);
		$this->assertInternalType('array', $sentences);
		$this->assertCount($count, $sentences);
		foreach($sentences as $sentence){
			$this->assertNotNull($sentence);
			$this->assertInstanceOf(GnuBoat\Nmea\NmeaSentence::class, $sentence);
			$this->assertEquals(true, $sentence->valid());
		}
	}
	
	public function nmeaFileProvider()
	{
		return [["webservice/tests/data/nmea0183.log", '0183', 2000]];
	}
}

?>
