Completed
Push — master ( 1cd704...73930c )
by
unknown
02:29 queued 10s
created

LatLongParserTest::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\LatLongParser;
6
use DataValues\Geo\Values\LatLongValue;
7
use PHPUnit\Framework\TestCase;
8
use ValueParsers\ParseException;
9
10
/**
11
 * @covers \DataValues\Geo\Parsers\LatLongParser
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 LatLongParserTest extends TestCase {
20
21
	/**
22
	 * @dataProvider validInputProvider
23
	 */
24
	public function testParseWithValidInputs( $value, LatLongValue $expected ) {
25
		$actual = ( new LatLongParser() )->parse( $value );
26
		$msg = json_encode( $actual->toArray() ) . " should equal\n"
27
			. json_encode( $expected->toArray() );
28
		$this->assertTrue( $expected->equals( $actual ), $msg );
29
	}
30
31
	public function validInputProvider() {
32
		$valid = [
33
			// Whitespace
34
			"1N 1E\n" => new LatLongValue( 1, 1 ),
35
			' 1N 1E ' => new LatLongValue( 1, 1 ),
36
37
			// Float
38
			'55.7557860 N, 37.6176330 W' => new LatLongValue( 55.7557860, -37.6176330 ),
39
			'55.7557860, -37.6176330' => new LatLongValue( 55.7557860, -37.6176330 ),
40
			'55 S, 37.6176330 W' => new LatLongValue( -55, -37.6176330 ),
41
			'-55, -37.6176330' => new LatLongValue( -55, -37.6176330 ),
42
			'5.5S,37W ' => new LatLongValue( -5.5, -37 ),
43
			'-5.5,-37 ' => new LatLongValue( -5.5, -37 ),
44
			'4,2' => new LatLongValue( 4, 2 ),
45
			'5.5S 37W ' => new LatLongValue( -5.5, -37 ),
46
			'-5.5 -37 ' => new LatLongValue( -5.5, -37 ),
47
			'4 2' => new LatLongValue( 4, 2 ),
48
			'S5.5 W37 ' => new LatLongValue( -5.5, -37 ),
49
50
			// DD
51
			'55.7557860° N, 37.6176330° W' => new LatLongValue( 55.7557860, -37.6176330 ),
52
			'55.7557860°, -37.6176330°' => new LatLongValue( 55.7557860, -37.6176330 ),
53
			'55° S, 37.6176330 ° W' => new LatLongValue( -55, -37.6176330 ),
54
			'-55°, -37.6176330 °' => new LatLongValue( -55, -37.6176330 ),
55
			'5.5°S,37°W ' => new LatLongValue( -5.5, -37 ),
56
			'-5.5°,-37° ' => new LatLongValue( -5.5, -37 ),
57
			'-55° -37.6176330 °' => new LatLongValue( -55, -37.6176330 ),
58
			'5.5°S 37°W ' => new LatLongValue( -5.5, -37 ),
59
			'-5.5 ° -37 ° ' => new LatLongValue( -5.5, -37 ),
60
			'S5.5° W37°' => new LatLongValue( -5.5, -37 ),
61
62
			// DMS
63
			'55° 45\' 20.8296", 37° 37\' 3.4788"' => new LatLongValue( 55.755786, 37.6176330000 ),
64
			'55° 45\' 20.8296", -37° 37\' 3.4788"' => new LatLongValue( 55.755786, -37.6176330000 ),
65
			'-55° 45\' 20.8296", -37° 37\' 3.4788"' => new LatLongValue( -55.755786, -37.6176330000 ),
66
			'-55° 45\' 20.8296", 37° 37\' 3.4788"' => new LatLongValue( -55.755786, 37.6176330000 ),
67
			'55° 0\' 0", 37° 0\' 0"' => new LatLongValue( 55, 37 ),
68
			'55° 30\' 0", 37° 30\' 0"' => new LatLongValue( 55.5, 37.5 ),
69
			'55° 0\' 18", 37° 0\' 18"' => new LatLongValue( 55.005, 37.005 ),
70
			'0° 0\' 0", 0° 0\' 0"' => new LatLongValue( 0, 0 ),
71
			'0° 0\' 18" N, 0° 0\' 18" E' => new LatLongValue( 0.005, 0.005 ),
72
			' 0° 0\' 18" S  , 0°  0\' 18"  W ' => new LatLongValue( -0.005, -0.005 ),
73
			'0° 0′ 18″ N, 0° 0′ 18″ E' => new LatLongValue( 0.005, 0.005 ),
74
			'0° 0\' 18" N  0° 0\' 18" E' => new LatLongValue( 0.005, 0.005 ),
75
			' 0 ° 0 \' 18 " S   0 °  0 \' 18 "  W ' => new LatLongValue( -0.005, -0.005 ),
76
			'0° 0′ 18″ N 0° 0′ 18″ E' => new LatLongValue( 0.005, 0.005 ),
77
			'N 0° 0\' 18" E 0° 0\' 18"' => new LatLongValue( 0.005, 0.005 ),
78
79
			// DM
80
			'55° 0\', 37° 0\'' => new LatLongValue( 55, 37 ),
81
			'55° 30\', 37° 30\'' => new LatLongValue( 55.5, 37.5 ),
82
			'0° 0\', 0° 0\'' => new LatLongValue( 0, 0 ),
83
			'-55° 30\', -37° 30\'' => new LatLongValue( -55.5, -37.5 ),
84
			'0° 0.3\' S, 0° 0.3\' W' => new LatLongValue( -0.005, -0.005 ),
85
			'-55° 30′, -37° 30′' => new LatLongValue( -55.5, -37.5 ),
86
			'-55 ° 30 \' -37 ° 30 \'' => new LatLongValue( -55.5, -37.5 ),
87
			'0° 0.3\' S 0° 0.3\' W' => new LatLongValue( -0.005, -0.005 ),
88
			'-55° 30′ -37° 30′' => new LatLongValue( -55.5, -37.5 ),
89
			'S 0° 0.3\' W 0° 0.3\'' => new LatLongValue( -0.005, -0.005 ),
90
91
			// Case insensitivity
92
			'55 s, 37.6176330 w' => new LatLongValue( -55, -37.6176330 ),
93
			'5.5s,37w ' => new LatLongValue( -5.5, -37 ),
94
			'5.5s 37w ' => new LatLongValue( -5.5, -37 ),
95
			's5.5 w37 ' => new LatLongValue( -5.5, -37 ),
96
			'5.5S 37w ' => new LatLongValue( -5.5, -37 ),
97
			'5.5s 37W ' => new LatLongValue( -5.5, -37 ),
98
		];
99
100
		foreach ( $valid as $input => $expected ) {
101
			yield [ $input, $expected ];
102
		}
103
	}
104
105
	/**
106
	 * @dataProvider invalidInputProvider
107
	 */
108
	public function testWhenInvalidInputIsProvided_exceptionIsThrown( $value ) {
109
		$parser = new LatLongParser();
110
111
		$this->expectException( ParseException::class );
112
		$parser->parse( $value );
113
	}
114
115
	public function invalidInputProvider() {
116
		yield [ null ];
117
		yield [ 1 ];
118
		yield [ 0.1 ];
119
		yield [ '~=[,,_,,]:3' ];
120
		yield [ 'ohi there' ];
121
122
		yield 'Latitude and longitude mixup' => [ '1E 1N' ];
123
		yield 'Doubled directional indicators' => [ '1NN, 1EE' ];
124
		yield 'Invalid directional indicators' => [ '1X, 1Y' ];
125
		yield 'Directional indicators with junk' => [ '1NX, 1EY' ];
126
		yield 'Directional indicators on both sides' => [ 'N1N, E1E' ];
127
128
		yield 'No coordinates' => [ 'N, E' ];
129
		yield 'Missing latitude' => [ 'N, 1E' ];
130
		yield 'Missing longitude' => [ '1N, E' ];
131
	}
132
133
}
134