1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\DataValues\Geo\Formatters; |
4
|
|
|
|
5
|
|
|
use DataValues\Geo\Formatters\GeoCoordinateFormatter; |
6
|
|
|
use DataValues\Geo\Parsers\GeoCoordinateParser; |
7
|
|
|
use DataValues\Geo\Values\LatLongValue; |
8
|
|
|
use DataValues\StringValue; |
9
|
|
|
use ValueFormatters\FormatterOptions; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers DataValues\Geo\Formatters\GeoCoordinateFormatter |
13
|
|
|
* |
14
|
|
|
* @group ValueFormatters |
15
|
|
|
* @group DataValueExtensions |
16
|
|
|
* |
17
|
|
|
* @license GPL-2.0+ |
18
|
|
|
* @author Jeroen De Dauw < [email protected] > |
19
|
|
|
* @author Addshore |
20
|
|
|
* @author Daniel Kinzler |
21
|
|
|
*/ |
22
|
|
|
class GeoCoordinateFormatterTest extends \PHPUnit_Framework_TestCase { |
23
|
|
|
|
24
|
|
|
public function floatNotationProvider() { |
25
|
|
|
return array( |
26
|
|
|
'0, degree' => array( |
27
|
|
|
new LatLongValue( 0, 0 ), |
28
|
|
|
1, |
29
|
|
|
'0, 0' |
30
|
|
|
), |
31
|
|
|
'negative zero' => array( |
32
|
|
|
new LatLongValue( -0.25, 0.25 ), |
33
|
|
|
1, |
34
|
|
|
'0, 0' |
35
|
|
|
), |
36
|
|
|
'signed, minute' => array( |
37
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
38
|
|
|
1.0/60, |
39
|
|
|
'-55.75, 37.25' |
40
|
|
|
), |
41
|
|
|
'signed, degree' => array( |
42
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
43
|
|
|
1, |
44
|
|
|
'-56, 37' |
45
|
|
|
), |
46
|
|
|
'three degrees' => array( |
47
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
48
|
|
|
3, |
49
|
|
|
'-57, 36' |
50
|
|
|
), |
51
|
|
|
'seven degrees' => array( |
52
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
53
|
|
|
7, |
54
|
|
|
'-56, 35' |
55
|
|
|
), |
56
|
|
|
'ten degrees' => array( |
57
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
58
|
|
|
10, |
59
|
|
|
'-60, 40' |
60
|
|
|
), |
61
|
|
|
'rounding degrees down' => array( |
62
|
|
|
new LatLongValue( -14.9, 14.9 ), |
63
|
|
|
10, |
64
|
|
|
'-10, 10' |
65
|
|
|
), |
66
|
|
|
'rounding degrees up' => array( |
67
|
|
|
new LatLongValue( -15, 15 ), |
68
|
|
|
10, |
69
|
|
|
'-20, 20' |
70
|
|
|
), |
71
|
|
|
'rounding fractions down' => array( |
72
|
|
|
new LatLongValue( -0.049, 0.049 ), |
73
|
|
|
0.1, |
74
|
|
|
'0, 0' |
75
|
|
|
), |
76
|
|
|
'rounding fractions up' => array( |
77
|
|
|
new LatLongValue( -0.05, 0.05 ), |
78
|
|
|
0.1, |
79
|
|
|
'-0.1, 0.1' |
80
|
|
|
), |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function makeOptions( $format, $precision ) { |
85
|
|
|
$options = new FormatterOptions(); |
86
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_FORMAT, $format ); |
87
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_DIRECTIONAL, false ); |
88
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_PRECISION, $precision ); |
89
|
|
|
|
90
|
|
|
return $options; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @dataProvider floatNotationProvider |
95
|
|
|
*/ |
96
|
|
|
public function testFloatNotationFormatting( LatLongValue $latLong, $precision, $expected ) { |
97
|
|
|
$options = $this->makeOptions( GeoCoordinateFormatter::TYPE_FLOAT, $precision ); |
98
|
|
|
$this->assertFormatsCorrectly( $latLong, $options, $expected ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @dataProvider floatNotationProvider |
103
|
|
|
*/ |
104
|
|
|
public function testFloatNotationRoundTrip( LatLongValue $value, $precision, $expected ) { |
105
|
|
|
$options = $this->makeOptions( GeoCoordinateFormatter::TYPE_FLOAT, $precision ); |
106
|
|
|
$this->assertRoundTrip( $value, $options ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function decimalDegreeNotationProvider() { |
110
|
|
|
return array( |
111
|
|
|
'0, degree' => array( |
112
|
|
|
new LatLongValue( 0, 0 ), |
113
|
|
|
1, |
114
|
|
|
'0°, 0°' |
115
|
|
|
), |
116
|
|
|
'negative zero' => array( |
117
|
|
|
new LatLongValue( -0.25, 0.25 ), |
118
|
|
|
1, |
119
|
|
|
'0°, 0°' |
120
|
|
|
), |
121
|
|
|
'signed, minute' => array( |
122
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
123
|
|
|
1.0/60, |
124
|
|
|
'-55.75°, 37.25°' |
125
|
|
|
), |
126
|
|
|
'signed, degree' => array( |
127
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
128
|
|
|
1, |
129
|
|
|
'-56°, 37°' |
130
|
|
|
), |
131
|
|
|
'three degrees' => array( |
132
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
133
|
|
|
3, |
134
|
|
|
'-57°, 36°' |
135
|
|
|
), |
136
|
|
|
'seven degrees' => array( |
137
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
138
|
|
|
7, |
139
|
|
|
'-56°, 35°' |
140
|
|
|
), |
141
|
|
|
'ten degrees' => array( |
142
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
143
|
|
|
10, |
144
|
|
|
'-60°, 40°' |
145
|
|
|
), |
146
|
|
|
'rounding degrees down' => array( |
147
|
|
|
new LatLongValue( -14.9, 14.9 ), |
148
|
|
|
10, |
149
|
|
|
'-10°, 10°' |
150
|
|
|
), |
151
|
|
|
'rounding degrees up' => array( |
152
|
|
|
new LatLongValue( -15, 15 ), |
153
|
|
|
10, |
154
|
|
|
'-20°, 20°' |
155
|
|
|
), |
156
|
|
|
'rounding fractions down' => array( |
157
|
|
|
new LatLongValue( -0.049, 0.049 ), |
158
|
|
|
0.1, |
159
|
|
|
'0.0°, 0.0°' |
160
|
|
|
), |
161
|
|
|
'rounding fractions up' => array( |
162
|
|
|
new LatLongValue( -0.05, 0.05 ), |
163
|
|
|
0.1, |
164
|
|
|
'-0.1°, 0.1°' |
165
|
|
|
), |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @dataProvider decimalDegreeNotationProvider |
171
|
|
|
*/ |
172
|
|
|
public function testDecimalDegreeNotationFormatting( LatLongValue $latLong, $precision, $expected ) { |
173
|
|
|
$options = $this->makeOptions( GeoCoordinateFormatter::TYPE_DD, $precision ); |
174
|
|
|
$this->assertFormatsCorrectly( $latLong, $options, $expected ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @dataProvider decimalDegreeNotationProvider |
179
|
|
|
*/ |
180
|
|
|
public function testDecimalDegreeNotationRoundTrip( LatLongValue $latLong, $precision, $expected ) { |
181
|
|
|
$options = $this->makeOptions( GeoCoordinateFormatter::TYPE_DD, $precision ); |
182
|
|
|
$this->assertRoundTrip( $latLong, $options ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function decimalMinuteNotationProvider() { |
186
|
|
|
return array( |
187
|
|
|
'0, degree' => array( |
188
|
|
|
new LatLongValue( 0, 0 ), |
189
|
|
|
1, |
190
|
|
|
'0°, 0°' |
191
|
|
|
), |
192
|
|
|
'0, minute' => array( |
193
|
|
|
new LatLongValue( 0, 0 ), |
194
|
|
|
1.0/60, |
195
|
|
|
'0° 0\', 0° 0\'' |
196
|
|
|
), |
197
|
|
|
'0, second' => array( |
198
|
|
|
new LatLongValue( 0, 0 ), |
199
|
|
|
1.0/3600, |
200
|
|
|
'0° 0.00\', 0° 0.00\'' |
201
|
|
|
), |
202
|
|
|
'negative zero' => array( |
203
|
|
|
new LatLongValue( -1.0/128, 1.0/128 ), |
204
|
|
|
1.0/60, |
205
|
|
|
'0° 0\', 0° 0\'' |
206
|
|
|
), |
207
|
|
|
'negative, not zero' => array( |
208
|
|
|
new LatLongValue( -0.25, 0.25 ), |
209
|
|
|
1.0/60, |
210
|
|
|
'-0° 15\', 0° 15\'' |
211
|
|
|
), |
212
|
|
|
'second' => array( |
213
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
214
|
|
|
1.0/3600, |
215
|
|
|
'-55° 45.35\', 37° 15.38\'' |
216
|
|
|
), |
217
|
|
|
'minute' => array( |
218
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
219
|
|
|
1.0/60, |
220
|
|
|
'-55° 45\', 37° 15\'' |
221
|
|
|
), |
222
|
|
|
'ten minutes' => array( |
223
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
224
|
|
|
10.0/60, |
225
|
|
|
'-55° 50\', 37° 20\'' |
226
|
|
|
), |
227
|
|
|
'fifty minutes' => array( |
228
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
229
|
|
|
50.0/60, |
230
|
|
|
'-55° 50\', 37° 30\'' |
231
|
|
|
), |
232
|
|
|
'degree' => array( |
233
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
234
|
|
|
1, |
235
|
|
|
'-56°, 37°' |
236
|
|
|
), |
237
|
|
|
'ten degrees' => array( |
238
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
239
|
|
|
10, |
240
|
|
|
'-60°, 40°' |
241
|
|
|
), |
242
|
|
|
'rounding minutes down' => array( |
243
|
|
|
new LatLongValue( -14.9 / 60, 14.9 / 60 ), |
244
|
|
|
10 / 60, |
245
|
|
|
'-0° 10\', 0° 10\'' |
246
|
|
|
), |
247
|
|
|
'rounding minutes up' => array( |
248
|
|
|
new LatLongValue( -15 / 60, 15 / 60 ), |
249
|
|
|
10 / 60, |
250
|
|
|
'-0° 20\', 0° 20\'' |
251
|
|
|
), |
252
|
|
|
'rounding fractions down' => array( |
253
|
|
|
new LatLongValue( -0.049 / 60, 0.049 / 60 ), |
254
|
|
|
0.1 / 60, |
255
|
|
|
'0° 0.0\', 0° 0.0\'' |
256
|
|
|
), |
257
|
|
|
'rounding fractions up' => array( |
258
|
|
|
new LatLongValue( -0.05 / 60, 0.05 / 60 ), |
259
|
|
|
0.1 / 60, |
260
|
|
|
'-0° 0.1\', 0° 0.1\'' |
261
|
|
|
), |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @dataProvider decimalMinuteNotationProvider |
267
|
|
|
*/ |
268
|
|
|
public function testDecimalMinuteNotationFormatting( LatLongValue $latLong, $precision, $expected ) { |
269
|
|
|
$options = $this->makeOptions( GeoCoordinateFormatter::TYPE_DM, $precision ); |
270
|
|
|
$this->assertFormatsCorrectly( $latLong, $options, $expected ); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @dataProvider decimalMinuteNotationProvider |
275
|
|
|
*/ |
276
|
|
|
public function testDecimalMinuteNotationRoundTrip( LatLongValue $latLong, $precision, $expected ) { |
277
|
|
|
$options = $this->makeOptions( GeoCoordinateFormatter::TYPE_DM, $precision ); |
278
|
|
|
$this->assertRoundTrip( $latLong, $options ); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function decimalMinuteSecondNotationProvider() { |
282
|
|
|
return array( |
283
|
|
|
'0, degree' => array( |
284
|
|
|
new LatLongValue( 0, 0 ), |
285
|
|
|
1, |
286
|
|
|
'0°, 0°' |
287
|
|
|
), |
288
|
|
|
'0, minute' => array( |
289
|
|
|
new LatLongValue( 0, 0 ), |
290
|
|
|
1.0/60, |
291
|
|
|
'0° 0\', 0° 0\'' |
292
|
|
|
), |
293
|
|
|
'0, second' => array( |
294
|
|
|
new LatLongValue( 0, 0 ), |
295
|
|
|
1.0/3600, |
296
|
|
|
'0° 0\' 0", 0° 0\' 0"' |
297
|
|
|
), |
298
|
|
|
'negative zero' => array( |
299
|
|
|
new LatLongValue( -1.0/8192, 1.0/8192 ), |
300
|
|
|
1.0/3600, |
301
|
|
|
'0° 0\' 0", 0° 0\' 0"' |
302
|
|
|
), |
303
|
|
|
'negative, not zero' => array( |
304
|
|
|
new LatLongValue( -1.0/4096, 1.0/4096 ), |
305
|
|
|
1.0/7200, |
306
|
|
|
'-0° 0\' 1.0", 0° 0\' 1.0"' |
307
|
|
|
), |
308
|
|
|
'second' => array( |
309
|
|
|
new LatLongValue( -55.755786, 37.25 ), |
310
|
|
|
1.0/3600, |
311
|
|
|
'-55° 45\' 21", 37° 15\' 0"' |
312
|
|
|
), |
313
|
|
|
'second/100' => array( |
314
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
315
|
|
|
1.0/360000, |
316
|
|
|
'-55° 45\' 20.83", 37° 15\' 22.79"' |
317
|
|
|
), |
318
|
|
|
'ten seconds' => array( |
319
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
320
|
|
|
10.0/3600, |
321
|
|
|
'-55° 45\' 20", 37° 15\' 20"' |
322
|
|
|
), |
323
|
|
|
'fifty seconds' => array( |
324
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
325
|
|
|
50.0/3600, |
326
|
|
|
'-55° 45\' 0", 37° 15\' 0"' |
327
|
|
|
), |
328
|
|
|
'minute' => array( |
329
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
330
|
|
|
1.0/60, |
331
|
|
|
'-55° 45\', 37° 15\'' |
332
|
|
|
), |
333
|
|
|
'degree' => array( |
334
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
335
|
|
|
1, |
336
|
|
|
'-56°, 37°' |
337
|
|
|
), |
338
|
|
|
'degree/100, case A' => array( |
339
|
|
|
new LatLongValue( 52.01, 10.01 ), |
340
|
|
|
0.01, |
341
|
|
|
'52° 0\' 36", 10° 0\' 36"' |
342
|
|
|
), |
343
|
|
|
'degree/100, case B' => array( |
344
|
|
|
new LatLongValue( 52.02, 10.02 ), |
345
|
|
|
0.01, |
346
|
|
|
'52° 1\' 12", 10° 1\' 12"' |
347
|
|
|
), |
348
|
|
|
'ten degrees' => array( |
349
|
|
|
new LatLongValue( -55.755786, 37.25633 ), |
350
|
|
|
10, |
351
|
|
|
'-60°, 40°' |
352
|
|
|
), |
353
|
|
|
'rounding seconds down' => array( |
354
|
|
|
new LatLongValue( -14.9 / 3600, 14.9 / 3600 ), |
355
|
|
|
10 / 3600, |
356
|
|
|
'-0° 0\' 10", 0° 0\' 10"' |
357
|
|
|
), |
358
|
|
|
'rounding seconds up' => array( |
359
|
|
|
new LatLongValue( -15 / 3600, 15 / 3600 ), |
360
|
|
|
10 / 3600, |
361
|
|
|
'-0° 0\' 20", 0° 0\' 20"' |
362
|
|
|
), |
363
|
|
|
'rounding fractions down' => array( |
364
|
|
|
new LatLongValue( -0.049 / 3600, 0.049 / 3600 ), |
365
|
|
|
0.1 / 3600, |
366
|
|
|
'0° 0\' 0.0", 0° 0\' 0.0"' |
367
|
|
|
), |
368
|
|
|
'rounding fractions up' => array( |
369
|
|
|
new LatLongValue( -0.05 / 3600, 0.05 / 3600 ), |
370
|
|
|
0.1 / 3600, |
371
|
|
|
'-0° 0\' 0.1", 0° 0\' 0.1"' |
372
|
|
|
), |
373
|
|
|
); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @dataProvider decimalMinuteSecondNotationProvider |
378
|
|
|
*/ |
379
|
|
|
public function testDecimalMinuteSecondNotationFormatting( LatLongValue $latLong, $precision, $expected ) { |
380
|
|
|
$options = $this->makeOptions( GeoCoordinateFormatter::TYPE_DMS, $precision ); |
381
|
|
|
$this->assertFormatsCorrectly( $latLong, $options, $expected ); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @dataProvider decimalMinuteSecondNotationProvider |
386
|
|
|
*/ |
387
|
|
|
public function testDecimalMinuteSecondNotationRoundTrip( LatLongValue $latLong, $precision, $expected ) { |
388
|
|
|
$options = $this->makeOptions( GeoCoordinateFormatter::TYPE_DMS, $precision ); |
389
|
|
|
$this->assertRoundTrip( $latLong, $options ); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
private function assertFormatsCorrectly( LatLongValue $latLong, FormatterOptions $options, $expected ) { |
393
|
|
|
$formatter = new GeoCoordinateFormatter( $options ); |
394
|
|
|
|
395
|
|
|
$this->assertSame( |
396
|
|
|
$expected, |
397
|
|
|
$formatter->format( $latLong ), |
398
|
|
|
'format()' |
399
|
|
|
); |
400
|
|
|
|
401
|
|
|
$precision = $options->getOption( GeoCoordinateFormatter::OPT_PRECISION ); |
402
|
|
|
$this->assertSame( |
403
|
|
|
$expected, |
404
|
|
|
$formatter->formatLatLongValue( $latLong, $precision ), |
405
|
|
|
'formatLatLongValue()' |
406
|
|
|
); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
private function assertRoundTrip( LatLongValue $value, FormatterOptions $options ) { |
410
|
|
|
$formatter = new GeoCoordinateFormatter( $options ); |
411
|
|
|
$parser = new GeoCoordinateParser(); |
412
|
|
|
|
413
|
|
|
$formatted = $formatter->format( $value ); |
414
|
|
|
$parsed = $parser->parse( $formatted ); |
415
|
|
|
|
416
|
|
|
// NOTE: $parsed may be != $coord, because of rounding, so we can't compare directly. |
417
|
|
|
$formattedParsed = $formatter->format( $parsed ); |
418
|
|
|
|
419
|
|
|
$this->assertSame( $formatted, $formattedParsed ); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
public function testDirectionalOptionGetsAppliedForDecimalMinutes() { |
423
|
|
|
$coordinates = array( |
424
|
|
|
'55° 0\' N, 37° 0\' E' => array( 55, 37 ), |
425
|
|
|
'55° 30\' N, 37° 30\' W' => array( 55.5, -37.5 ), |
426
|
|
|
'55° 30\' S, 37° 30\' E' => array( -55.5, 37.5 ), |
427
|
|
|
'55° 30\' S, 37° 30\' W' => array( -55.5, -37.5 ), |
428
|
|
|
'0° 0\' N, 0° 0\' E' => array( 0, 0 ), |
429
|
|
|
); |
430
|
|
|
|
431
|
|
|
$this->assertIsDirectionalFormatMap( $coordinates, GeoCoordinateFormatter::TYPE_DM ); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
private function assertIsDirectionalFormatMap( array $coordinates, $format ) { |
435
|
|
|
foreach ( $coordinates as $expected => $arguments ) { |
436
|
|
|
$options = new FormatterOptions(); |
437
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_FORMAT, $format ); |
438
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_DIRECTIONAL, true ); |
439
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_PRECISION, 1.0/60 ); |
440
|
|
|
|
441
|
|
|
$this->assertFormatsCorrectly( |
442
|
|
|
new LatLongValue( $arguments[0], $arguments[1] ), |
443
|
|
|
$options, |
444
|
|
|
$expected |
445
|
|
|
); |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
public function testDirectionalOptionGetsAppliedForFloats() { |
450
|
|
|
$coordinates = array( |
451
|
|
|
'55.75 N, 37.25 W' => array( 55.755786, -37.25633 ), |
452
|
|
|
'55.75 S, 37.25 E' => array( -55.755786, 37.25633 ), |
453
|
|
|
'55 S, 37.25 W' => array( -55, -37.25633 ), |
454
|
|
|
'5.5 N, 37 E' => array( 5.5, 37 ), |
455
|
|
|
'0 N, 0 E' => array( 0, 0 ), |
456
|
|
|
); |
457
|
|
|
|
458
|
|
|
$this->assertIsDirectionalFormatMap( $coordinates, GeoCoordinateFormatter::TYPE_FLOAT ); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
private function provideSpacingLevelOptions() { |
462
|
|
|
return array( |
463
|
|
|
'none' => array(), |
464
|
|
|
'latlong' => array( GeoCoordinateFormatter::OPT_SPACE_LATLONG ), |
465
|
|
|
'direction' => array( GeoCoordinateFormatter::OPT_SPACE_DIRECTION ), |
466
|
|
|
'coordparts' => array( GeoCoordinateFormatter::OPT_SPACE_COORDPARTS ), |
467
|
|
|
'latlong_direction' => array( |
468
|
|
|
GeoCoordinateFormatter::OPT_SPACE_LATLONG, |
469
|
|
|
GeoCoordinateFormatter::OPT_SPACE_DIRECTION |
470
|
|
|
), |
471
|
|
|
'all' => array( |
472
|
|
|
GeoCoordinateFormatter::OPT_SPACE_LATLONG, |
473
|
|
|
GeoCoordinateFormatter::OPT_SPACE_DIRECTION, |
474
|
|
|
GeoCoordinateFormatter::OPT_SPACE_COORDPARTS, |
475
|
|
|
), |
476
|
|
|
); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
public function testSpacingOptionGetsAppliedForDecimalMinutes() { |
480
|
|
|
$coordinates = array( |
481
|
|
|
'none' => array( |
482
|
|
|
'55°0\'N,37°0\'E' => array( 55, 37 ), |
483
|
|
|
'55°30\'N,37°30\'W' => array( 55.5, -37.5 ), |
484
|
|
|
'0°0\'N,0°0\'E' => array( 0, 0 ), |
485
|
|
|
), |
486
|
|
|
'latlong' => array( |
487
|
|
|
'55°0\'N, 37°0\'E' => array( 55, 37 ), |
488
|
|
|
'55°30\'N, 37°30\'W' => array( 55.5, -37.5 ), |
489
|
|
|
'0°0\'N, 0°0\'E' => array( 0, 0 ), |
490
|
|
|
), |
491
|
|
|
'direction' => array( |
492
|
|
|
'55°0\' N,37°0\' E' => array( 55, 37 ), |
493
|
|
|
'55°30\' N,37°30\' W' => array( 55.5, -37.5 ), |
494
|
|
|
'0°0\' N,0°0\' E' => array( 0, 0 ), |
495
|
|
|
), |
496
|
|
|
'coordparts' => array( |
497
|
|
|
'55° 0\'N,37° 0\'E' => array( 55, 37 ), |
498
|
|
|
'55° 30\'N,37° 30\'W' => array( 55.5, -37.5 ), |
499
|
|
|
'0° 0\'N,0° 0\'E' => array( 0, 0 ), |
500
|
|
|
), |
501
|
|
|
'latlong_direction' => array( |
502
|
|
|
'55°0\' N, 37°0\' E' => array( 55, 37 ), |
503
|
|
|
'55°30\' N, 37°30\' W' => array( 55.5, -37.5 ), |
504
|
|
|
'0°0\' N, 0°0\' E' => array( 0, 0 ), |
505
|
|
|
), |
506
|
|
|
); |
507
|
|
|
|
508
|
|
|
$this->assertSpacingCorrect( $coordinates, GeoCoordinateFormatter::TYPE_DM ); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
private function assertSpacingCorrect( array $coordSets, $format ) { |
512
|
|
|
$spacingLevelOptions = $this->provideSpacingLevelOptions(); |
513
|
|
|
foreach( $coordSets as $spacingKey => $coordinates ) { |
514
|
|
|
foreach ( $coordinates as $expected => $arguments ) { |
515
|
|
|
$options = new FormatterOptions(); |
516
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_FORMAT, $format ); |
517
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_DIRECTIONAL, true ); |
518
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_PRECISION, 1.0/60 ); |
519
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_SPACING_LEVEL, $spacingLevelOptions[$spacingKey] ); |
520
|
|
|
|
521
|
|
|
$this->assertFormatsCorrectly( |
522
|
|
|
new LatLongValue( $arguments[0], $arguments[1] ), |
523
|
|
|
$options, |
524
|
|
|
$expected |
525
|
|
|
); |
526
|
|
|
} |
527
|
|
|
} |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
public function testSpacingOptionGetsAppliedForFloats() { |
531
|
|
|
$coordinates = array( |
532
|
|
|
'none' => array( |
533
|
|
|
'55.75N,37.25W' => array( 55.755786, -37.25633 ), |
534
|
|
|
'0N,0E' => array( 0, 0 ), |
535
|
|
|
), |
536
|
|
|
'latlong' => array( |
537
|
|
|
'55.75N, 37.25W' => array( 55.755786, -37.25633 ), |
538
|
|
|
'0N, 0E' => array( 0, 0 ), |
539
|
|
|
), |
540
|
|
|
'direction' => array( |
541
|
|
|
'55.75 N,37.25 W' => array( 55.755786, -37.25633 ), |
542
|
|
|
'0 N,0 E' => array( 0, 0 ), |
543
|
|
|
), |
544
|
|
|
'coordparts' => array( |
545
|
|
|
'55.75N,37.25W' => array( 55.755786, -37.25633 ), |
546
|
|
|
'0N,0E' => array( 0, 0 ), |
547
|
|
|
), |
548
|
|
|
'latlong_direction' => array( |
549
|
|
|
'55.75 N, 37.25 W' => array( 55.755786, -37.25633 ), |
550
|
|
|
'0 N, 0 E' => array( 0, 0 ), |
551
|
|
|
), |
552
|
|
|
'all' => array( |
553
|
|
|
'55.75 N, 37.25 W' => array( 55.755786, -37.25633 ), |
554
|
|
|
'0 N, 0 E' => array( 0, 0 ), |
555
|
|
|
), |
556
|
|
|
); |
557
|
|
|
|
558
|
|
|
$this->assertSpacingCorrect( $coordinates, GeoCoordinateFormatter::TYPE_FLOAT ); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
public function testWrongType() { |
562
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
563
|
|
|
|
564
|
|
|
$formatter = new GeoCoordinateFormatter( new FormatterOptions() ); |
565
|
|
|
|
566
|
|
|
$formatter->format( new StringValue( 'Evil' ) ); |
|
|
|
|
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
public function testGivenInvalidFormattingOption_formatThrowsException() { |
570
|
|
|
$options = new FormatterOptions(); |
571
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_FORMAT, 'not a format' ); |
572
|
|
|
$formatter = new GeoCoordinateFormatter( $options ); |
573
|
|
|
|
574
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
575
|
|
|
$formatter->format( new LatLongValue( 0, 0 ) ); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* @dataProvider invalidPrecisionProvider |
580
|
|
|
*/ |
581
|
|
|
public function testFormatWithInvalidPrecision_fallsBackToDefaultPrecision( $precision ) { |
582
|
|
|
$options = new FormatterOptions(); |
583
|
|
|
$options->setOption( GeoCoordinateFormatter::OPT_PRECISION, $precision ); |
584
|
|
|
$formatter = new GeoCoordinateFormatter( $options ); |
585
|
|
|
|
586
|
|
|
$formatted = $formatter->format( new LatLongValue( 1.2, 3.4 ) ); |
587
|
|
|
$this->assertSame( '1.2, 3.4', $formatted ); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* @dataProvider invalidPrecisionProvider |
592
|
|
|
*/ |
593
|
|
|
public function testFormatLatLongValueWithInvalidPrecision_fallsBackToDefaultPrecision( $precision ) { |
594
|
|
|
$formatter = new GeoCoordinateFormatter( new FormatterOptions() ); |
595
|
|
|
|
596
|
|
|
$formatted = $formatter->formatLatLongValue( new LatLongValue( 1.2, 3.4 ), $precision ); |
597
|
|
|
$this->assertSame( '1.2, 3.4', $formatted ); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
public function invalidPrecisionProvider() { |
601
|
|
|
return array( |
602
|
|
|
array( null ), |
603
|
|
|
array( '' ), |
604
|
|
|
array( 0 ), |
605
|
|
|
array( -1 ), |
606
|
|
|
array( NAN ), |
607
|
|
|
array( INF ), |
608
|
|
|
); |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
} |
612
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: