Completed
Push — dropOldAliases ( 17840d...e22dca )
by Jeroen De
12:52 queued 07:28
created

precisionDetectionProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 66
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

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