Completed
Push — master ( 82e43e...e1ae07 )
by Jeroen De
02:37
created

testParseWithValidInputs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Tests\DataValues\Geo\Parsers;
4
5
use DataValues\Geo\Parsers\DmsCoordinateParser;
6
use DataValues\Geo\Values\LatLongValue;
7
use PHPUnit\Framework\TestCase;
8
use ValueParsers\ParseException;
9
10
/**
11
 * @covers \DataValues\Geo\Parsers\DmsCoordinateParser
12
 *
13
 * @group ValueParsers
14
 * @group DataValueExtensions
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class DmsCoordinateParserTest extends TestCase {
20
21
	/**
22
	 * @dataProvider validInputProvider
23
	 */
24
	public function testParseWithValidInputs( $value, LatLongValue $expected ) {
25
		$this->assertEquals(
26
			$expected,
27
			( new DmsCoordinateParser() )->parse( $value )
28
		);
29
	}
30
31
	/**
32
	 * @see ValueParserTestBase::validInputProvider
33
	 */
34
	public function validInputProvider() {
35
		$valid = [
36
			// Whitespace
37
			"1°0'0\"N 1°0'0\"E\n" => new LatLongValue( 1, 1 ),
38
			' 1°0\'0"N 1°0\'0"E ' => new LatLongValue( 1, 1 ),
39
40
			'55° 45\' 20.8296", 37° 37\' 3.4788"' => new LatLongValue( 55.755786, 37.617633 ),
41
			'55° 45\' 20.8296", -37° 37\' 3.4788"' => new LatLongValue( 55.755786, -37.617633 ),
42
			'-55° 45\' 20.8296", -37° 37\' 3.4788"' => new LatLongValue( -55.755786, -37.617633 ),
43
			'-55° 45\' 20.8296", 37° 37\' 3.4788"' => new LatLongValue( -55.755786, 37.617633 ),
44
			'55° 0\' 0", 37° 0\' 0"' => new LatLongValue( 55, 37 ),
45
			'55° 30\' 0", 37° 30\' 0"' => new LatLongValue( 55.5, 37.5 ),
46
			'55° 0\' 18", 37° 0\' 18"' => new LatLongValue( 55.005, 37.005 ),
47
			'0° 0\' 0", 0° 0\' 0"' => new LatLongValue( 0, 0 ),
48
			'0° 0\' 18" N, 0° 0\' 18" E' => new LatLongValue( 0.005, 0.005 ),
49
			' 0° 0\' 18" S  , 0°  0\' 18"  W ' => new LatLongValue( -0.005, -0.005 ),
50
			'55° 0′ 18″, 37° 0′ 18″' => new LatLongValue( 55.005, 37.005 ),
51
52
			// Coordinate strings without separator:
53
			'55° 45\' 20.8296" 37° 37\' 3.4788"' => new LatLongValue( 55.755786, 37.617633 ),
54
			'55 ° 45 \' 20.8296 " -37 ° 37 \' 3.4788 "' => new LatLongValue( 55.755786, -37.617633 ),
55
			'-55 ° 45 \' 20.8296 " -37° 37\' 3.4788"' => new LatLongValue( -55.755786, -37.617633 ),
56
			'55° 0′ 18″ 37° 0′ 18″' => new LatLongValue( 55.005, 37.005 ),
57
58
			// Coordinate string starting with direction character:
59
			'N 0° 0\' 18", E 0° 0\' 18"' => new LatLongValue( 0.005, 0.005 ),
60
			'S 0° 0\' 18" E 0° 0\' 18"' => new LatLongValue( -0.005, 0.005 ),
61
		];
62
63
		foreach ( $valid as $input => $expected ) {
64
			yield [ $input, $expected ];
65
		}
66
	}
67
68
	/**
69
	 * @dataProvider invalidInputProvider
70
	 */
71
	public function testParseWithInvalidInputs( $value ) {
72
		$this->expectException( ParseException::class );
73
		( new DmsCoordinateParser() )->parse( $value );
74
	}
75
76
	public function invalidInputProvider() {
77
		yield [ null ];
78
		yield [ 1 ];
79
		yield [ 0.1 ];
80
		yield [ '~=[,,_,,]:3' ];
81
		yield [ 'ohi there' ];
82
	}
83
84
	public function testWhenSingleMinutePositionIsMissing_itGetsDefaultedToZero() {
85
		$this->assertEquals(
86
			new LatLongValue( 1.0005555555555556, 4.085 ),
87
			$this->parse( '1°2"N, 4°5\'6"E' )
88
		);
89
	}
90
91
	private function parse( string $input ): LatLongValue {
92
		return ( new DmsCoordinateParser() )->parse( $input );
93
	}
94
95
}
96