Completed
Push — master ( 502ebf...ce3a1d )
by Jeroen De
192:28 queued 171:53
created

tests/unit/Parsers/GlobeCoordinateParserTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Tests\DataValues\Geo\Parsers;
6
7
use DataValues\Geo\Parsers\GlobeCoordinateParser;
8
use DataValues\Geo\Values\GlobeCoordinateValue;
9
use DataValues\Geo\Values\LatLongValue;
10
use ValueParsers\ParserOptions;
11
12
/**
13
 * @covers \DataValues\Geo\Parsers\GlobeCoordinateParser
14
 *
15
 * @group ValueParsers
16
 * @group DataValueExtensions
17
 *
18
 * @license GPL-2.0-or-later
19
 * @author Jeroen De Dauw < [email protected] >
20
 * @author Thiemo Kreuz
21
 */
22
class GlobeCoordinateParserTest extends ParserTestBase {
0 ignored issues
show
Deprecated Code introduced by
The class Tests\DataValues\Geo\Parsers\ParserTestBase has been deprecated with message: This is a copy of ValueParserTestBase from DataValues Common, temporarily created to
be able to refactor it away in easy to follow steps.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
23
24
	/**
25
	 * @see ValueParserTestBase::getInstance
26
	 *
27
	 * @return GlobeCoordinateParser
28
	 */
29
	protected function getInstance() {
30
		return new GlobeCoordinateParser();
31
	}
32
33
	/**
34
	 * @see ValueParserTestBase::validInputProvider
35
	 */
36
	public function validInputProvider() {
37
		$argLists = [];
38
39
		$valid = [
40
			// Whitespace
41
			"1N 1E\n" => [ 1, 1, 1 ],
42
			' 1N 1E ' => [ 1, 1, 1 ],
43
44
			// Float
45
			'55.7557860 N, 37.6176330 W' => [ 55.7557860, -37.6176330, 0.000001 ],
46
			'55.7557860N,37.6176330W' => [ 55.7557860, -37.6176330, 0.000001 ],
47
			'55.7557860, -37.6176330' => [ 55.7557860, -37.6176330, 0.000001 ],
48
			'55.7557860, -37.6176330    ' => [ 55.7557860, -37.6176330, 0.000001 ],
49
			'55 S, 37.6176330 W' => [ -55, -37.6176330, 0.000001 ],
50
			'55 S,37.6176330W' => [ -55, -37.6176330, 0.000001 ],
51
			'-55, -37.6176330' => [ -55, -37.6176330, 0.000001 ],
52
			'5.5S,37W ' => [ -5.5, -37, 0.1 ],
53
			'-5.5,-37 ' => [ -5.5, -37, 0.1 ],
54
			'-5.5 -37 ' => [ -5.5, -37, 0.1 ],
55
			'4,2' => [ 4, 2, 1 ],
56
			'5.5S 37W ' => [ -5.5, -37, 0.1 ],
57
			'5.5 S 37 W ' => [ -5.5, -37, 0.1 ],
58
			'4 2' => [ 4, 2, 1 ],
59
			'S5.5 W37 ' => [ -5.5, -37, 0.1 ],
60
61
			// DD
62
			'55.7557860° N, 37.6176330° W' => [ 55.7557860, -37.6176330, 0.000001 ],
63
			'55.7557860°, -37.6176330°' => [ 55.7557860, -37.6176330, 0.000001 ],
64
			'55.7557860°,-37.6176330°' => [ 55.7557860, -37.6176330, 0.000001 ],
65
			'55.7557860°,-37.6176330°  ' => [ 55.7557860, -37.6176330, 0.000001 ],
66
			'55° S, 37.6176330 ° W' => [ -55, -37.6176330, 0.000001 ],
67
			'-55°, -37.6176330 °' => [ -55, -37.6176330, 0.000001 ],
68
			'5.5°S,37°W ' => [ -5.5, -37, 0.1 ],
69
			'5.5° S,37° W ' => [ -5.5, -37, 0.1 ],
70
			'-5.5°,-37° ' => [ -5.5, -37, 0.1 ],
71
			'-55° -37.6176330 °' => [ -55, -37.6176330, 0.000001 ],
72
			'5.5°S 37°W ' => [ -5.5, -37, 0.1 ],
73
			'-5.5 ° -37 ° ' => [ -5.5, -37, 0.1 ],
74
			'S5.5° W37°' => [ -5.5, -37, 0.1 ],
75
			' S 5.5° W 37°' => [ -5.5, -37, 0.1 ],
76
77
			// DMS
78
			'55° 45\' 20.8296", 37° 37\' 3.4788"' => [ 55.755786, 37.617633, 0.0001 / 3600 ],
79
			'55° 45\' 20.8296", -37° 37\' 3.4788"' => [ 55.755786, -37.617633, 0.0001 / 3600 ],
80
			'-55° 45\' 20.8296", -37° 37\' 3.4788"' => [ -55.755786, -37.617633, 0.0001 / 3600 ],
81
			'-55° 45\' 20.8296", 37° 37\' 3.4788"' => [ -55.755786, 37.617633, 0.0001 / 3600 ],
82
			'-55° 45\' 20.8296", 37° 37\' 3.4788"  ' => [ -55.755786, 37.617633, 0.0001 / 3600 ],
83
			'55° 0\' 0", 37° 0\' 0"' => [ 55, 37, 1 / 3600 ],
84
			'55° 30\' 0", 37° 30\' 0"' => [ 55.5, 37.5, 1 / 3600 ],
85
			'55° 0\' 18", 37° 0\' 18"' => [ 55.005, 37.005, 1 / 3600 ],
86
			'  55° 0\' 18", 37° 0\' 18"' => [ 55.005, 37.005, 1 / 3600 ],
87
			'0° 0\' 0", 0° 0\' 0"' => [ 0, 0, 1 / 3600 ],
88
			'0° 0\' 18" N, 0° 0\' 18" E' => [ 0.005, 0.005, 1 / 3600 ],
89
			' 0° 0\' 18" S  , 0°  0\' 18"  W ' => [ -0.005, -0.005, 1 / 3600 ],
90
			'0° 0′ 18″ N, 0° 0′ 18″ E' => [ 0.005, 0.005, 1 / 3600 ],
91
			'0° 0\' 18" N  0° 0\' 18" E' => [ 0.005, 0.005, 1 / 3600 ],
92
			' 0 ° 0 \' 18 " S   0 °  0 \' 18 "  W ' => [ -0.005, -0.005, 1 / 3600 ],
93
			'0° 0′ 18″ N 0° 0′ 18″ E' => [ 0.005, 0.005, 1 / 3600 ],
94
			'N 0° 0\' 18" E 0° 0\' 18"' => [ 0.005, 0.005, 1 / 3600 ],
95
			'N0°0\'18"E0°0\'18"' => [ 0.005, 0.005, 1 / 3600 ],
96
			'N0°0\'18" E0°0\'18"' => [ 0.005, 0.005, 1 / 3600 ],
97
98
			// DM
99
			'55° 0\', 37° 0\'' => [ 55, 37, 1 / 60 ],
100
			'55° 30\', 37° 30\'' => [ 55.5, 37.5, 1 / 60 ],
101
			'0° 0\', 0° 0\'' => [ 0, 0, 1 / 60 ],
102
			'   0° 0\', 0° 0\'' => [ 0, 0, 1 / 60 ],
103
			'   0° 0\', 0° 0\'  ' => [ 0, 0, 1 / 60 ],
104
			'-55° 30\', -37° 30\'' => [ -55.5, -37.5, 1 / 60 ],
105
			'0° 0.3\' S, 0° 0.3\' W' => [ -0.005, -0.005, 1 / 3600 ],
106
			'-55° 30′, -37° 30′' => [ -55.5, -37.5, 1 / 60 ],
107
			'-55 ° 30 \' -37 ° 30 \'' => [ -55.5, -37.5, 1 / 60 ],
108
			'0° 0.3\' S 0° 0.3\' W' => [ -0.005, -0.005, 1 / 3600 ],
109
			'-55° 30′ -37° 30′' => [ -55.5, -37.5, 1 / 60 ],
110
			'S 0° 0.3\' W 0° 0.3\'' => [ -0.005, -0.005, 1 / 3600 ],
111
			'S0°0.3\'W0°0.3\'' => [ -0.005, -0.005, 1 / 3600 ],
112
			'S0°0.3\' W0°0.3\'' => [ -0.005, -0.005, 1 / 3600 ],
113
		];
114
115
		foreach ( $valid as $value => $expected ) {
116
			$expected = new GlobeCoordinateValue( new LatLongValue( $expected[0], $expected[1] ), $expected[2] );
117
			$argLists[] = [ (string)$value, $expected ];
118
		}
119
120
		return $argLists;
121
	}
122
123
	/**
124
	 * @see StringValueParserTest::invalidInputProvider
125
	 */
126
	public function invalidInputProvider() {
127
		return [
128
			[ null ],
129
			[ 1 ],
130
			[ 0.1 ],
131
			[ '~=[,,_,,]:3' ],
132
			[ 'ohi there' ],
133
		];
134
	}
135
136
	public function testWithGlobeOptionMatchingTheDefault() {
137
		$parser = new GlobeCoordinateParser( new ParserOptions( [
138
			'globe' => 'http://www.wikidata.org/entity/Q2'
139
		] ) );
140
141
		$this->assertEquals(
142
			new GlobeCoordinateValue(
143
				new LatLongValue( 55.7557860, -37.6176330 ),
144
				0.000001,
145
				'http://www.wikidata.org/entity/Q2'
146
			),
147
			$parser->parse( '55.7557860° N, 37.6176330° W' )
148
		);
149
	}
150
151
	public function testWithGlobeOptionDifferingFromTheDefault() {
152
		$parser = new GlobeCoordinateParser( new ParserOptions( [
153
			'globe' => 'http://www.wikidata.org/entity/Q111'
154
		] ) );
155
156
		$this->assertEquals(
157
			new GlobeCoordinateValue(
158
				new LatLongValue( 60.5, 260 ),
159
				0.1,
160
				'http://www.wikidata.org/entity/Q111'
161
			),
162
			$parser->parse( '60.5, 260' )
163
		);
164
	}
165
166
	public function testWithoutGlobeOption() {
167
		$parser = new GlobeCoordinateParser();
168
169
		$this->assertEquals(
170
			new GlobeCoordinateValue(
171
				new LatLongValue( 40.2, 22.5 ),
172
				0.1,
173
				'http://www.wikidata.org/entity/Q2'
174
			),
175
			$parser->parse( '40.2, 22.5' )
176
		);
177
	}
178
179
	/**
180
	 * @dataProvider precisionDetectionProvider
181
	 */
182
	public function testPrecisionDetection( $value, $expected ) {
183
		$parser = new GlobeCoordinateParser();
184
		$globeCoordinateValue = $parser->parse( $value );
185
186
		$this->assertSame( (float)$expected, $globeCoordinateValue->getPrecision() );
187
	}
188
189
	public function precisionDetectionProvider() {
190
		return [
191
			// Float
192
			[ '10 20', 1 ],
193
			[ '1 2', 1 ],
194
			[ '1.3 2.4', 0.1 ],
195
			[ '1.3 20', 0.1 ],
196
			[ '10 2.4', 0.1 ],
197
			[ '1.35 2.46', 0.01 ],
198
			[ '1.357 2.468', 0.001 ],
199
			[ '1.3579 2.468', 0.0001 ],
200
			[ '1.00001 2.00001', 0.00001 ],
201
			[ '1.000001 2.000001', 0.000001 ],
202
			[ '1.0000001 2.0000001', 0.0000001 ],
203
			[ '1.00000001 2.00000001', 0.00000001 ],
204
			[ '1.000000001 2.000000001', 1 ],
205
			[ '1.555555555 2.555555555', 0.00000001 ],
206
207
			// Dd
208
			[ '10° 20°', 1 ],
209
			[ '1° 2°', 1 ],
210
			[ '1.3° 2.4°', 0.1 ],
211
			[ '1.3° 20°', 0.1 ],
212
			[ '10° 2.4°', 0.1 ],
213
			[ '1.35° 2.46°', 0.01 ],
214
			[ '1.357° 2.468°', 0.001 ],
215
			[ '1.3579° 2.468°', 0.0001 ],
216
			[ '1.00001° 2.00001°', 0.00001 ],
217
			[ '1.000001° 2.000001°', 0.000001 ],
218
			[ '1.0000001° 2.0000001°', 0.0000001 ],
219
			[ '1.00000001° 2.00000001°', 0.00000001 ],
220
			[ '1.000000001° 2.000000001°', 1 ],
221
			[ '1.555555555° 2.555555555°', 0.00000001 ],
222
223
			// Dm
224
			[ '1°3\' 2°4\'', 1 / 60 ],
225
			[ '1°3\' 2°0\'', 1 / 60 ],
226
			[ '1°0\' 2°4\'', 1 / 60 ],
227
			[ '1°3.5\' 2°4.6\'', 1 / 3600 ],
228
			[ '1°3.57\' 2°4.68\'', 1 / 36000 ],
229
			[ '1°3.579\' 2°4.68\'', 1 / 360000 ],
230
			[ '1°3.0001\' 2°4.0001\'', 1 / 3600000 ],
231
			[ '1°3.00001\' 2°4.00001\'', 1 / 36000000 ],
232
			[ '1°3.000001\' 2°4.000001\'', 1 / 36000000 ],
233
			[ '1°3.0000001\' 2°4.0000001\'', 1 / 60 ],
234
			[ '1°3.5555555\' 2°4.5555555\'', 1 / 36000000 ],
235
236
			// Dms
237
			[ '1°3\'5" 2°4\'6"', 1 / 3600 ],
238
			[ '1°3\'5" 2°0\'0"', 1 / 3600 ],
239
			[ '1°0\'0" 2°4\'6"', 1 / 3600 ],
240
			[ '1°3\'0" 2°4\'0"', 1 / 3600 ],
241
			[ '1°3\'5.7" 2°4\'6.8"', 1 / 36000 ],
242
			[ '1°3\'5.79" 2°4\'6.8"', 1 / 360000 ],
243
			[ '1°3\'5.001" 2°4\'6.001"', 1 / 3600000 ],
244
			[ '1°3\'5.0001" 2°4\'6.0001"', 1 / 36000000 ],
245
			[ '1°3\'5.00001" 2°4\'6.00001"', 1 / 3600 ],
246
			[ '1°3\'5.55555" 2°4\'6.55555"', 1 / 36000000 ],
247
248
			/**
249
			 * @fixme What do the users expect in this case, 1/3600 or 1/360000?
250
			 * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=64820
251
			 */
252
			[ '47°42\'0.00"N, 15°27\'0.00"E', 1 / 3600 ],
253
		];
254
	}
255
256
}
257