Test Failed
Push — php71 ( 62a1a4...353585 )
by no
04:30 queued 02:17
created

testParseWithValidInputs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
namespace Tests\DataValues\Geo\Parsers;
4
5
use DataValues\Geo\Parsers\GlobeCoordinateParser;
6
use DataValues\Geo\Values\GlobeCoordinateValue;
7
use DataValues\Geo\Values\LatLongValue;
8
use ValueParsers\ParserOptions;
9
use ValueParsers\Test\StringValueParserTest;
10
use ValueParsers\ValueParser;
11
12
/**
13
 * @covers DataValues\Geo\Parsers\GlobeCoordinateParser
14
 *
15
 * @group ValueParsers
16
 * @group DataValueExtensions
17
 * @group GeoCoordinateParserTest
18
 *
19
 * @license GPL-2.0+
20
 * @author Jeroen De Dauw < [email protected] >
21
 * @author Thiemo Mättig
22
 */
23
class GlobeCoordinateParserTest extends StringValueParserTest {
24
25
	/**
26
	 * @deprecated since DataValues Common 0.3, just use getInstance.
27
	 */
28
	protected function getParserClass() {
29
		throw new \LogicException( 'Should not be called, use getInstance' );
30
	}
31
32
	/**
33
	 * @see ValueParserTestBase::getInstance
34
	 *
35
	 * @return GlobeCoordinateParser
36
	 */
37
	protected function getInstance() {
38
		return new GlobeCoordinateParser();
39
	}
40
41
	/**
42
	 * @see ValueParserTestBase::testParseWithValidInputs
43
	 *
44
	 * @dataProvider validInputProvider
45
	 */
46
	public function testParseWithValidInputs( $value, $expected, ValueParser $parser = null ) {
47
		$parser = new GlobeCoordinateParser();
48
		$this->assertSame( serialize( $expected ), serialize( $parser->parse( $value ) ) );
49
	}
50
51
	/**
52
	 * @see ValueParserTestBase::validInputProvider
53
	 */
54
	public function validInputProvider() {
55
		$argLists = array();
56
57
		$valid = array(
58
			// Whitespace
59
			"1N 1E\n" => array( 1, 1, 1 ),
60
			' 1N 1E ' => array( 1, 1, 1 ),
61
62
			// Float
63
			'55.7557860 N, 37.6176330 W' => array( 55.7557860, -37.6176330, 0.000001 ),
64
			'55.7557860N,37.6176330W' => array( 55.7557860, -37.6176330, 0.000001 ),
65
			'55.7557860, -37.6176330' => array( 55.7557860, -37.6176330, 0.000001 ),
66
			'55.7557860, -37.6176330    ' => array( 55.7557860, -37.6176330, 0.000001 ),
67
			'55 S, 37.6176330 W' => array( -55, -37.6176330, 0.000001 ),
68
			'55 S,37.6176330W' => array( -55, -37.6176330, 0.000001 ),
69
			'-55, -37.6176330' => array( -55, -37.6176330, 0.000001 ),
70
			'5.5S,37W ' => array( -5.5, -37, 0.1 ),
71
			'-5.5,-37 ' => array( -5.5, -37, 0.1 ),
72
			'-5.5 -37 ' => array( -5.5, -37, 0.1 ),
73
			'4,2' => array( 4, 2, 1 ),
74
			'5.5S 37W ' => array( -5.5, -37, 0.1 ),
75
			'5.5 S 37 W ' => array( -5.5, -37, 0.1 ),
76
			'4 2' => array( 4, 2, 1 ),
77
			'S5.5 W37 ' => array( -5.5, -37, 0.1 ),
78
79
			// DD
80
			'55.7557860° N, 37.6176330° W' => array( 55.7557860, -37.6176330, 0.000001 ),
81
			'55.7557860°, -37.6176330°' => array( 55.7557860, -37.6176330, 0.000001 ),
82
			'55.7557860°,-37.6176330°' => array( 55.7557860, -37.6176330, 0.000001 ),
83
			'55.7557860°,-37.6176330°  ' => array( 55.7557860, -37.6176330, 0.000001 ),
84
			'55° S, 37.6176330 ° W' => array( -55, -37.6176330, 0.000001 ),
85
			'-55°, -37.6176330 °' => array( -55, -37.6176330, 0.000001 ),
86
			'5.5°S,37°W ' => array( -5.5, -37, 0.1 ),
87
			'5.5° S,37° W ' => array( -5.5, -37, 0.1 ),
88
			'-5.5°,-37° ' => array( -5.5, -37, 0.1 ),
89
			'-55° -37.6176330 °' => array( -55, -37.6176330, 0.000001 ),
90
			'5.5°S 37°W ' => array( -5.5, -37, 0.1 ),
91
			'-5.5 ° -37 ° ' => array( -5.5, -37, 0.1 ),
92
			'S5.5° W37°' => array( -5.5, -37, 0.1 ),
93
			' S 5.5° W 37°' => array( -5.5, -37, 0.1 ),
94
95
			// DMS
96
			'55° 45\' 20.8296", 37° 37\' 3.4788"' => array(
97
				55 + 45 / 60 + 20.8296 / 3600,
98
				37 + 37 / 60 + 3.4788 / 3600,
99
				1 / 36000000
100
			),
101
			'55° 45\' 20.8296", -37° 37\' 3.4788"' => array(
102
				55 + 45 / 60 + 20.8296 / 3600,
103
				-37 - 37 / 60 - 3.4788 / 3600,
104
				1 / 36000000
105
			),
106
			'-55° 45\' 20.8296", -37° 37\' 3.4788"' => array(
107
				-55 - 45 / 60 - 20.8296 / 3600,
108
				-37 - 37 / 60 - 3.4788 / 3600,
109
				1 / 36000000
110
			),
111
			'-55° 45\' 20.8296", 37° 37\' 3.4788"' => array(
112
				-55 - 45 / 60 - 20.8296 / 3600,
113
				37 + 37 / 60 + 3.4788 / 3600,
114
				1 / 36000000
115
			),
116
			'-55° 45\' 20.8296", 37° 37\' 3.4788"  ' => array(
117
				-55 - 45 / 60 - 20.8296 / 3600,
118
				37 + 37 / 60 + 3.4788 / 3600,
119
				1 / 36000000
120
			),
121
			'55° 0\' 0", 37° 0\' 0"' => array( 55, 37, 1 / 3600 ),
122
			'55° 30\' 0", 37° 30\' 0"' => array( 55.5, 37.5, 1 / 3600 ),
123
			'55° 0\' 18", 37° 0\' 18"' => array( 55.005, 37.005, 1 / 3600 ),
124
			'  55° 0\' 18", 37° 0\' 18"' => array( 55.005, 37.005, 1 / 3600 ),
125
			'0° 0\' 0", 0° 0\' 0"' => array( 0, 0, 1 / 3600 ),
126
			'0° 0\' 18" N, 0° 0\' 18" E' => array( 0.005, 0.005, 1 / 3600 ),
127
			' 0° 0\' 18" S  , 0°  0\' 18"  W ' => array( -0.005, -0.005, 1 / 3600 ),
128
			'0° 0′ 18″ N, 0° 0′ 18″ E' => array( 0.005, 0.005, 1 / 3600 ),
129
			'0° 0\' 18" N  0° 0\' 18" E' => array( 0.005, 0.005, 1 / 3600 ),
130
			' 0 ° 0 \' 18 " S   0 °  0 \' 18 "  W ' => array( -0.005, -0.005, 1 / 3600 ),
131
			'0° 0′ 18″ N 0° 0′ 18″ E' => array( 0.005, 0.005, 1 / 3600 ),
132
			'N 0° 0\' 18" E 0° 0\' 18"' => array( 0.005, 0.005, 1 / 3600 ),
133
			'N0°0\'18"E0°0\'18"' => array( 0.005, 0.005, 1 / 3600 ),
134
			'N0°0\'18" E0°0\'18"' => array( 0.005, 0.005, 1 / 3600 ),
135
136
			// DM
137
			'55° 0\', 37° 0\'' => array( 55, 37, 1 / 60 ),
138
			'55° 30\', 37° 30\'' => array( 55.5, 37.5, 1 / 60 ),
139
			'0° 0\', 0° 0\'' => array( 0, 0, 1 / 60 ),
140
			'   0° 0\', 0° 0\'' => array( 0, 0, 1 / 60 ),
141
			'   0° 0\', 0° 0\'  ' => array( 0, 0, 1 / 60 ),
142
			'-55° 30\', -37° 30\'' => array( -55.5, -37.5, 1 / 60 ),
143
			'0° 0.3\' S, 0° 0.3\' W' => array( -0.005, -0.005, 1 / 3600 ),
144
			'-55° 30′, -37° 30′' => array( -55.5, -37.5, 1 / 60 ),
145
			'-55 ° 30 \' -37 ° 30 \'' => array( -55.5, -37.5, 1 / 60 ),
146
			'0° 0.3\' S 0° 0.3\' W' => array( -0.005, -0.005, 1 / 3600 ),
147
			'-55° 30′ -37° 30′' => array( -55.5, -37.5, 1 / 60 ),
148
			'S 0° 0.3\' W 0° 0.3\'' => array( -0.005, -0.005, 1 / 3600 ),
149
			'S0°0.3\'W0°0.3\'' => array( -0.005, -0.005, 1 / 3600 ),
150
			'S0°0.3\' W0°0.3\'' => array( -0.005, -0.005, 1 / 3600 ),
151
		);
152
153
		foreach ( $valid as $value => $expected ) {
154
			$expected = new GlobeCoordinateValue( new LatLongValue( $expected[0], $expected[1] ), $expected[2] );
155
			$argLists[] = array( (string)$value, $expected );
156
		}
157
158
		return $argLists;
159
	}
160
161
	/**
162
	 * @see StringValueParserTest::invalidInputProvider
163
	 */
164
	public function invalidInputProvider() {
165
		$argLists = parent::invalidInputProvider();
166
167
		$invalid = array(
168
			'~=[,,_,,]:3',
169
			'ohi there',
170
		);
171
172
		foreach ( $invalid as $value ) {
173
			$argLists[] = array( $value );
174
		}
175
176
		return $argLists;
177
	}
178
179
	/**
180
	 * @dataProvider withGlobeOptionProvider
181
	 */
182
	public function testWithGlobeOption( $expected, $value, $options = null ) {
183
		$parser = new GlobeCoordinateParser();
184
185
		if ( $options ) {
186
			$parserOptions = new ParserOptions( json_decode( $options, true ) );
187
			$parser->setOptions( $parserOptions );
188
		}
189
190
		$value = $parser->parse( $value );
191
192
		$this->assertEquals( $expected, $value );
193
	}
194
195
	public function withGlobeOptionProvider() {
196
		$data = array();
197
198
		$data[] = array(
199
			new GlobeCoordinateValue(
200
				new LatLongValue( 55.7557860, -37.6176330 ),
201
				0.000001,
202
				'http://www.wikidata.org/entity/Q2'
203
			),
204
			'55.7557860° N, 37.6176330° W',
205
			'{"globe":"http://www.wikidata.org/entity/Q2"}'
206
		);
207
208
		$data[] = array(
209
			new GlobeCoordinateValue(
210
				new LatLongValue( 60.5, 260 ),
211
				0.1,
212
				'http://www.wikidata.org/entity/Q111'
213
			),
214
			'60.5, 260',
215
			'{"globe":"http://www.wikidata.org/entity/Q111"}'
216
		);
217
218
		$data[] = array(
219
			new GlobeCoordinateValue(
220
				new LatLongValue( 40.2, 22.5 ),
221
				0.1,
222
				'http://www.wikidata.org/entity/Q2'
223
			),
224
			'40.2, 22.5',
225
		);
226
227
		return $data;
228
	}
229
230
	/**
231
	 * @dataProvider precisionDetectionProvider
232
	 */
233
	public function testPrecisionDetection( $value, $expected ) {
234
		$parser = new GlobeCoordinateParser();
235
		$globeCoordinateValue = $parser->parse( $value );
236
237
		$this->assertSame( $expected, $globeCoordinateValue->getPrecision() );
238
	}
239
240
	public function precisionDetectionProvider() {
241
		return array(
242
			// Float
243
			array( '10 20', 1 ),
244
			array( '1 2', 1 ),
245
			array( '1.3 2.4', 0.1 ),
246
			array( '1.3 20', 0.1 ),
247
			array( '10 2.4', 0.1 ),
248
			array( '1.35 2.46', 0.01 ),
249
			array( '1.357 2.468', 0.001 ),
250
			array( '1.3579 2.468', 0.0001 ),
251
			array( '1.00001 2.00001', 0.00001 ),
252
			array( '1.000001 2.000001', 0.000001 ),
253
			array( '1.0000001 2.0000001', 0.0000001 ),
254
			array( '1.00000001 2.00000001', 0.00000001 ),
255
			array( '1.000000001 2.000000001', 1 ),
256
			array( '1.555555555 2.555555555', 0.00000001 ),
257
258
			// Dd
259
			array( '10° 20°', 1 ),
260
			array( '1° 2°', 1 ),
261
			array( '1.3° 2.4°', 0.1 ),
262
			array( '1.3° 20°', 0.1 ),
263
			array( '10° 2.4°', 0.1 ),
264
			array( '1.35° 2.46°', 0.01 ),
265
			array( '1.357° 2.468°', 0.001 ),
266
			array( '1.3579° 2.468°', 0.0001 ),
267
			array( '1.00001° 2.00001°', 0.00001 ),
268
			array( '1.000001° 2.000001°', 0.000001 ),
269
			array( '1.0000001° 2.0000001°', 0.0000001 ),
270
			array( '1.00000001° 2.00000001°', 0.00000001 ),
271
			array( '1.000000001° 2.000000001°', 1 ),
272
			array( '1.555555555° 2.555555555°', 0.00000001 ),
273
274
			// Dm
275
			array( '1°3\' 2°4\'', 1 / 60 ),
276
			array( '1°3\' 2°0\'', 1 / 60 ),
277
			array( '1°0\' 2°4\'', 1 / 60 ),
278
			array( '1°3.5\' 2°4.6\'', 1 / 3600 ),
279
			array( '1°3.57\' 2°4.68\'', 1 / 36000 ),
280
			array( '1°3.579\' 2°4.68\'', 1 / 360000 ),
281
			array( '1°3.0001\' 2°4.0001\'', 1 / 3600000 ),
282
			array( '1°3.00001\' 2°4.00001\'', 1 / 36000000 ),
283
			array( '1°3.000001\' 2°4.000001\'', 1 / 36000000 ),
284
			array( '1°3.0000001\' 2°4.0000001\'', 1 / 60 ),
285
			array( '1°3.5555555\' 2°4.5555555\'', 1 / 36000000 ),
286
287
			// Dms
288
			array( '1°3\'5" 2°4\'6"', 1 / 3600 ),
289
			array( '1°3\'5" 2°0\'0"', 1 / 3600 ),
290
			array( '1°0\'0" 2°4\'6"', 1 / 3600 ),
291
			array( '1°3\'0" 2°4\'0"', 1 / 3600 ),
292
			array( '1°3\'5.7" 2°4\'6.8"', 1 / 36000 ),
293
			array( '1°3\'5.79" 2°4\'6.8"', 1 / 360000 ),
294
			array( '1°3\'5.001" 2°4\'6.001"', 1 / 3600000 ),
295
			array( '1°3\'5.0001" 2°4\'6.0001"', 1 / 36000000 ),
296
			array( '1°3\'5.00001" 2°4\'6.00001"', 1 / 3600 ),
297
			array( '1°3\'5.55555" 2°4\'6.55555"', 1 / 36000000 ),
298
299
			/**
300
			 * @fixme What do the users expect in this case, 1/3600 or 1/360000?
301
			 * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=64820
302
			 */
303
			array( '47°42\'0.00"N, 15°27\'0.00"E', 1 / 3600 ),
304
		);
305
	}
306
307
}
308