Test Failed
Push — php71 ( 89b822...49b1d4 )
by no
02:21
created

GlobeCoordinateParserTest::validInputProvider()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 86
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 86
rs 8.6583
cc 2
eloc 71
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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