Passed
Push — master ( 4564ad...000d15 )
by Bekh-Ivanov
02:24
created

testDirectionalOptionGetsAppliedForDecimalMinutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Tests\DataValues\Geo\Formatters;
4
5
use DataValues\Geo\Formatters\LatLongFormatter;
6
use DataValues\Geo\Parsers\LatLongParser;
7
use DataValues\Geo\Values\LatLongValue;
8
use DataValues\StringValue;
9
use ValueFormatters\FormatterOptions;
10
11
/**
12
 * @covers DataValues\Geo\Formatters\LatLongFormatter
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 LatLongFormatterTest 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 / 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
			'precision option must support strings' => array(
82
				new LatLongValue( -0.05, 0.05 ),
83
				'0.1',
84
				'-0.1, 0.1'
85
			),
86
		);
87
	}
88
89
	/**
90
	 * @param string $format One of the LatLongFormatter::TYPE_… constants
91
	 * @param float|int $precision
92
	 *
93
	 * @return FormatterOptions
94
	 */
95
	private function makeOptions( $format, $precision ) {
96
		$options = new FormatterOptions();
97
		$options->setOption( LatLongFormatter::OPT_FORMAT, $format );
98
		$options->setOption( LatLongFormatter::OPT_DIRECTIONAL, false );
99
		$options->setOption( LatLongFormatter::OPT_PRECISION, $precision );
100
101
		return $options;
102
	}
103
104
	/**
105
	 * @dataProvider floatNotationProvider
106
	 */
107
	public function testFloatNotationFormatting( LatLongValue $latLong, $precision, $expected ) {
108
		$options = $this->makeOptions( LatLongFormatter::TYPE_FLOAT, $precision );
109
		$this->assertFormatsCorrectly( $latLong, $options, $expected );
110
	}
111
112
	/**
113
	 * @dataProvider floatNotationProvider
114
	 */
115
	public function testFloatNotationRoundTrip( LatLongValue $value, $precision, $expected ) {
0 ignored issues
show
Unused Code introduced by
The parameter $expected is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
		$options = $this->makeOptions( LatLongFormatter::TYPE_FLOAT, $precision );
117
		$this->assertRoundTrip( $value, $options );
118
	}
119
120
	public function decimalDegreeNotationProvider() {
121
		return array(
122
			'0, degree' => array(
123
				new LatLongValue( 0, 0 ),
124
				1,
125
				'0°, 0°'
126
			),
127
			'negative zero' => array(
128
				new LatLongValue( -0.25, 0.25 ),
129
				1,
130
				'0°, 0°'
131
			),
132
			'signed, minute' => array(
133
				new LatLongValue( -55.755786, 37.25633 ),
134
				1 / 60,
135
				'-55.75°, 37.25°'
136
			),
137
			'signed, degree' => array(
138
				new LatLongValue( -55.755786, 37.25633 ),
139
				1,
140
				'-56°, 37°'
141
			),
142
			'three degrees' => array(
143
				new LatLongValue( -55.755786, 37.25633 ),
144
				3,
145
				'-57°, 36°'
146
			),
147
			'seven degrees' => array(
148
				new LatLongValue( -55.755786, 37.25633 ),
149
				7,
150
				'-56°, 35°'
151
			),
152
			'ten degrees' => array(
153
				new LatLongValue( -55.755786, 37.25633 ),
154
				10,
155
				'-60°, 40°'
156
			),
157
			'rounding degrees down' => array(
158
				new LatLongValue( -14.9, 14.9 ),
159
				10,
160
				'-10°, 10°'
161
			),
162
			'rounding degrees up' => array(
163
				new LatLongValue( -15, 15 ),
164
				10,
165
				'-20°, 20°'
166
			),
167
			'rounding fractions down' => array(
168
				new LatLongValue( -0.049, 0.049 ),
169
				0.1,
170
				'0.0°, 0.0°'
171
			),
172
			'rounding fractions up' => array(
173
				new LatLongValue( -0.05, 0.05 ),
174
				0.1,
175
				'-0.1°, 0.1°'
176
			),
177
			'precision option must support strings' => array(
178
				new LatLongValue( -0.05, 0.05 ),
179
				'0.1',
180
				'-0.1°, 0.1°'
181
			),
182
		);
183
	}
184
185
	/**
186
	 * @dataProvider decimalDegreeNotationProvider
187
	 */
188
	public function testDecimalDegreeNotationFormatting( LatLongValue $latLong, $precision, $expected ) {
189
		$options = $this->makeOptions( LatLongFormatter::TYPE_DD, $precision );
190
		$this->assertFormatsCorrectly( $latLong, $options, $expected );
191
	}
192
193
	/**
194
	 * @dataProvider decimalDegreeNotationProvider
195
	 */
196
	public function testDecimalDegreeNotationRoundTrip( LatLongValue $latLong, $precision, $expected ) {
0 ignored issues
show
Unused Code introduced by
The parameter $expected is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
197
		$options = $this->makeOptions( LatLongFormatter::TYPE_DD, $precision );
198
		$this->assertRoundTrip( $latLong, $options );
199
	}
200
201
	public function decimalMinuteNotationProvider() {
202
		return array(
203
			'0, degree' => array(
204
				new LatLongValue( 0, 0 ),
205
				1,
206
				'0°, 0°'
207
			),
208
			'0, minute' => array(
209
				new LatLongValue( 0, 0 ),
210
				1 / 60,
211
				'0° 0\', 0° 0\''
212
			),
213
			'0, second' => array(
214
				new LatLongValue( 0, 0 ),
215
				1 / 3600,
216
				'0° 0.00\', 0° 0.00\''
217
			),
218
			'negative zero' => array(
219
				new LatLongValue( -1 / 128, 1 / 128 ),
220
				1 / 60,
221
				'0° 0\', 0° 0\''
222
			),
223
			'negative, not zero' => array(
224
				new LatLongValue( -0.25, 0.25 ),
225
				1 / 60,
226
				'-0° 15\', 0° 15\''
227
			),
228
			'second' => array(
229
				new LatLongValue( -55.755786, 37.25633 ),
230
				1 / 3600,
231
				'-55° 45.35\', 37° 15.38\''
232
			),
233
			'minute' => array(
234
				new LatLongValue( -55.755786, 37.25633 ),
235
				1 / 60,
236
				'-55° 45\', 37° 15\''
237
			),
238
			'ten minutes' => array(
239
				new LatLongValue( -55.755786, 37.25633 ),
240
				10 / 60,
241
				'-55° 50\', 37° 20\''
242
			),
243
			'fifty minutes' => array(
244
				new LatLongValue( -55.755786, 37.25633 ),
245
				50 / 60,
246
				'-55° 50\', 37° 30\''
247
			),
248
			'degree' => array(
249
				new LatLongValue( -55.755786, 37.25633 ),
250
				1,
251
				'-56°, 37°'
252
			),
253
			'ten degrees' => array(
254
				new LatLongValue( -55.755786, 37.25633 ),
255
				10,
256
				'-60°, 40°'
257
			),
258
			'rounding minutes down' => array(
259
				new LatLongValue( -14.9 / 60, 14.9 / 60 ),
260
				10 / 60,
261
				'-0° 10\', 0° 10\''
262
			),
263
			'rounding minutes up' => array(
264
				new LatLongValue( -15 / 60, 15 / 60 ),
265
				10 / 60,
266
				'-0° 20\', 0° 20\''
267
			),
268
			'rounding fractions down' => array(
269
				new LatLongValue( -0.049 / 60, 0.049 / 60 ),
270
				0.1 / 60,
271
				'0° 0.0\', 0° 0.0\''
272
			),
273
			'rounding fractions up' => array(
274
				new LatLongValue( -0.05 / 60, 0.05 / 60 ),
275
				0.1 / 60,
276
				'-0° 0.1\', 0° 0.1\''
277
			),
278
			'round to degree when it does not make a difference' => array(
279
				new LatLongValue( 1.5, 2.5 ),
280
				1 - 1 / 60,
281
				'2°, 3°'
282
			),
283
			'round to minutes when it starts making a difference' => array(
284
				new LatLongValue( 1.5, 2.5 ),
285
				1 - 2 / 60,
286
				'1° 56\', 2° 54\''
287
			),
288
			'precision option must support strings' => array(
289
				new LatLongValue( -0.05, 0.05 ),
290
				'0.1',
291
				'-0° 6\', 0° 6\''
292
			),
293
		);
294
	}
295
296
	/**
297
	 * @dataProvider decimalMinuteNotationProvider
298
	 */
299
	public function testDecimalMinuteNotationFormatting( LatLongValue $latLong, $precision, $expected ) {
300
		$options = $this->makeOptions( LatLongFormatter::TYPE_DM, $precision );
301
		$this->assertFormatsCorrectly( $latLong, $options, $expected );
302
	}
303
304
	/**
305
	 * @dataProvider decimalMinuteNotationProvider
306
	 */
307
	public function testDecimalMinuteNotationRoundTrip( LatLongValue $latLong, $precision, $expected ) {
0 ignored issues
show
Unused Code introduced by
The parameter $expected is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
308
		$options = $this->makeOptions( LatLongFormatter::TYPE_DM, $precision );
309
		$this->assertRoundTrip( $latLong, $options );
310
	}
311
312
	public function decimalMinuteSecondNotationProvider() {
313
		return array(
314
			'0, degree' => array(
315
				new LatLongValue( 0, 0 ),
316
				1,
317
				'0°, 0°'
318
			),
319
			'0, minute' => array(
320
				new LatLongValue( 0, 0 ),
321
				1 / 60,
322
				'0° 0\', 0° 0\''
323
			),
324
			'0, second' => array(
325
				new LatLongValue( 0, 0 ),
326
				1 / 3600,
327
				'0° 0\' 0", 0° 0\' 0"'
328
			),
329
			'negative zero' => array(
330
				new LatLongValue( -1 / 8192, 1 / 8192 ),
331
				1 / 3600,
332
				'0° 0\' 0", 0° 0\' 0"'
333
			),
334
			'negative, not zero' => array(
335
				new LatLongValue( -1 / 4096, 1 / 4096 ),
336
				1 / 7200,
337
				'-0° 0\' 1.0", 0° 0\' 1.0"'
338
			),
339
			'second' => array(
340
				new LatLongValue( -55.755786, 37.25 ),
341
				1 / 3600,
342
				'-55° 45\' 21", 37° 15\' 0"'
343
			),
344
			'second/100' => array(
345
				new LatLongValue( -55.755786, 37.25633 ),
346
				1 / 360000,
347
				'-55° 45\' 20.83", 37° 15\' 22.79"'
348
			),
349
			'ten seconds' => array(
350
				new LatLongValue( -55.755786, 37.25633 ),
351
				10 / 3600,
352
				'-55° 45\' 20", 37° 15\' 20"'
353
			),
354
			'fifty seconds' => array(
355
				new LatLongValue( -55.755786, 37.25633 ),
356
				50 / 3600,
357
				'-55° 45\' 0", 37° 15\' 0"'
358
			),
359
			'minute' => array(
360
				new LatLongValue( -55.755786, 37.25633 ),
361
				1 / 60,
362
				'-55° 45\', 37° 15\''
363
			),
364
			'degree' => array(
365
				new LatLongValue( -55.755786, 37.25633 ),
366
				1,
367
				'-56°, 37°'
368
			),
369
			'degree/100, case A' => array(
370
				new LatLongValue( 52.01, 10.01 ),
371
				0.01,
372
				'52° 0\' 36", 10° 0\' 36"'
373
			),
374
			'degree/100, case B' => array(
375
				new LatLongValue( 52.02, 10.02 ),
376
				0.01,
377
				'52° 1\' 12", 10° 1\' 12"'
378
			),
379
			'degree/1000' => array(
380
				new LatLongValue( 52.4, 6.7667 ),
381
				0.001,
382
				'52° 24\' 0", 6° 46\' 1"'
383
			),
384
			'ten degrees' => array(
385
				new LatLongValue( -55.755786, 37.25633 ),
386
				10,
387
				'-60°, 40°'
388
			),
389
			'rounding seconds down' => array(
390
				new LatLongValue( -14.9 / 3600, 14.9 / 3600 ),
391
				10 / 3600,
392
				'-0° 0\' 10", 0° 0\' 10"'
393
			),
394
			'rounding seconds up' => array(
395
				new LatLongValue( -15 / 3600, 15 / 3600 ),
396
				10 / 3600,
397
				'-0° 0\' 20", 0° 0\' 20"'
398
			),
399
			'rounding fractions down' => array(
400
				new LatLongValue( -0.049 / 3600, 0.049 / 3600 ),
401
				0.1 / 3600,
402
				'0° 0\' 0.0", 0° 0\' 0.0"'
403
			),
404
			'rounding fractions up' => array(
405
				new LatLongValue( -0.05 / 3600, 0.05 / 3600 ),
406
				0.1 / 3600,
407
				'-0° 0\' 0.1", 0° 0\' 0.1"'
408
			),
409
			'round to degree when it does not make a difference' => array(
410
				new LatLongValue( 1.5, 2.5 ),
411
				1 - 1 / 60,
412
				'2°, 3°'
413
			),
414
			'round to minutes when it starts making a difference' => array(
415
				new LatLongValue( 1.5, 2.5 ),
416
				1 - 2 / 60,
417
				'1° 56\', 2° 54\''
418
			),
419
			'round to minutes when it does not make a difference' => array(
420
				new LatLongValue( 1.926, 2.926 ),
421
				1 / 60 - 1 / 3600,
422
				'1° 56\', 2° 56\''
423
			),
424
			'round to seconds when it starts making a difference' => array(
425
				new LatLongValue( 1.926, 2.926 ),
426
				1 / 60 - 2 / 3600,
427
				'1° 56\' 0", 2° 55\' 56"'
428
			),
429
			'unexpected rounding to 36°, 36°' => array(
430
				new LatLongValue( 36.5867, 37.0458 ),
431
				1.1187604885913,
432
				'37°, 37°'
433
			),
434
			'precision option must support strings' => array(
435
				new LatLongValue( -0.05, 0.05 ),
436
				'0.1',
437
				'-0° 6\', 0° 6\''
438
			),
439
			'Bug T150085 with 1 second precision' => array(
440
				new LatLongValue( 42.1206, 2.76944 ),
441
				1 / 3600,
442
				'42° 7\' 14", 2° 46\' 10"'
443
			),
444
			'Bug T150085 with 1 minute precision' => array(
445
				new LatLongValue( 42.1206, 2.76944 ),
446
				1 / 60,
447
				'42° 7\', 2° 46\''
448
			),
449
			'Bug T150085 with ~0.7 minute precision' => array(
450
				new LatLongValue( 42.1206, 2.76944 ),
451
				0.012111004438894,
452
				'42° 7\' 19", 2° 46\' 24"'
453
			),
454
		);
455
	}
456
457
	/**
458
	 * @dataProvider decimalMinuteSecondNotationProvider
459
	 */
460
	public function testDecimalMinuteSecondNotationFormatting( LatLongValue $latLong, $precision, $expected ) {
461
		$options = $this->makeOptions( LatLongFormatter::TYPE_DMS, $precision );
462
		$this->assertFormatsCorrectly( $latLong, $options, $expected );
463
	}
464
465
	/**
466
	 * @dataProvider decimalMinuteSecondNotationProvider
467
	 */
468
	public function testDecimalMinuteSecondNotationRoundTrip( LatLongValue $latLong, $precision, $expected ) {
0 ignored issues
show
Unused Code introduced by
The parameter $expected is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
469
		$options = $this->makeOptions( LatLongFormatter::TYPE_DMS, $precision );
470
		$this->assertRoundTrip( $latLong, $options );
471
	}
472
473
	/**
474
	 * @param LatLongValue $latLong
475
	 * @param FormatterOptions $options
476
	 * @param string $expected
477
	 */
478
	private function assertFormatsCorrectly( LatLongValue $latLong, FormatterOptions $options, $expected ) {
479
		$formatter = new LatLongFormatter( $options );
480
481
		$this->assertSame(
482
			$expected,
483
			$formatter->format( $latLong ),
484
			'format()'
485
		);
486
487
		$precision = $options->getOption( LatLongFormatter::OPT_PRECISION );
488
		$this->assertSame(
489
			$expected,
490
			$formatter->formatLatLongValue( $latLong, $precision ),
491
			'formatLatLongValue()'
492
		);
493
	}
494
495
	private function assertRoundTrip( LatLongValue $value, FormatterOptions $options ) {
496
		$formatter = new LatLongFormatter( $options );
497
		$parser = new LatLongParser();
498
499
		$formatted = $formatter->format( $value );
500
		$parsed = $parser->parse( $formatted );
501
502
		// NOTE: $parsed may be != $coord, because of rounding, so we can't compare directly.
503
		$formattedParsed = $formatter->format( $parsed );
504
505
		$this->assertSame( $formatted, $formattedParsed );
506
	}
507
508
	public function testDirectionalOptionGetsAppliedForDecimalMinutes() {
509
		$coordinates = array(
510
			'55° 0\' N, 37° 0\' E' => array( 55, 37 ),
511
			'55° 30\' N, 37° 30\' W' => array( 55.5, -37.5 ),
512
			'55° 30\' S, 37° 30\' E' => array( -55.5, 37.5 ),
513
			'55° 30\' S, 37° 30\' W' => array( -55.5, -37.5 ),
514
			'0° 0\' N, 0° 0\' E' => array( 0, 0 ),
515
		);
516
517
		$this->assertIsDirectionalFormatMap( $coordinates, LatLongFormatter::TYPE_DM );
518
	}
519
520
	/**
521
	 * @param array[] $coordinates
522
	 * @param string $format One of the LatLongFormatter::TYPE_… constants
523
	 */
524
	private function assertIsDirectionalFormatMap( array $coordinates, $format ) {
525
		foreach ( $coordinates as $expected => $arguments ) {
526
			$options = new FormatterOptions();
527
			$options->setOption( LatLongFormatter::OPT_FORMAT, $format );
528
			$options->setOption( LatLongFormatter::OPT_DIRECTIONAL, true );
529
			$options->setOption( LatLongFormatter::OPT_PRECISION, 1 / 60 );
530
531
			$this->assertFormatsCorrectly(
532
				new LatLongValue( $arguments[0], $arguments[1] ),
533
				$options,
534
				$expected
535
			);
536
		}
537
	}
538
539
	public function testDirectionalOptionGetsAppliedForFloats() {
540
		$coordinates = array(
541
			'55.75 N, 37.25 W' => array( 55.755786, -37.25633 ),
542
			'55.75 S, 37.25 E' => array( -55.755786, 37.25633 ),
543
			'55 S, 37.25 W' => array( -55, -37.25633 ),
544
			'5.5 N, 37 E' => array( 5.5, 37 ),
545
			'0 N, 0 E' => array( 0, 0 ),
546
		);
547
548
		$this->assertIsDirectionalFormatMap( $coordinates, LatLongFormatter::TYPE_FLOAT );
549
	}
550
551
	private function provideSpacingLevelOptions() {
552
		return array(
553
			'none' => array(),
554
			'latlong' => array( LatLongFormatter::OPT_SPACE_LATLONG ),
555
			'direction' => array( LatLongFormatter::OPT_SPACE_DIRECTION ),
556
			'coordparts' => array( LatLongFormatter::OPT_SPACE_COORDPARTS ),
557
			'latlong_direction' => array(
558
				LatLongFormatter::OPT_SPACE_LATLONG,
559
				LatLongFormatter::OPT_SPACE_DIRECTION
560
			),
561
			'all' => array(
562
				LatLongFormatter::OPT_SPACE_LATLONG,
563
				LatLongFormatter::OPT_SPACE_DIRECTION,
564
				LatLongFormatter::OPT_SPACE_COORDPARTS,
565
			),
566
		);
567
	}
568
569
	public function testSpacingOptionGetsAppliedForDecimalMinutes() {
570
		$coordinates = array(
571
			'none' => array(
572
				'55°0\'N,37°0\'E' => array( 55, 37 ),
573
				'55°30\'N,37°30\'W' => array( 55.5, -37.5 ),
574
				'0°0\'N,0°0\'E' => array( 0, 0 ),
575
			),
576
			'latlong' => array(
577
				'55°0\'N, 37°0\'E' => array( 55, 37 ),
578
				'55°30\'N, 37°30\'W' => array( 55.5, -37.5 ),
579
				'0°0\'N, 0°0\'E' => array( 0, 0 ),
580
			),
581
			'direction' => array(
582
				'55°0\' N,37°0\' E' => array( 55, 37 ),
583
				'55°30\' N,37°30\' W' => array( 55.5, -37.5 ),
584
				'0°0\' N,0°0\' E' => array( 0, 0 ),
585
			),
586
			'coordparts' => array(
587
				'55° 0\'N,37° 0\'E' => array( 55, 37 ),
588
				'55° 30\'N,37° 30\'W' => array( 55.5, -37.5 ),
589
				'0° 0\'N,0° 0\'E' => array( 0, 0 ),
590
			),
591
			'latlong_direction' => array(
592
				'55°0\' N, 37°0\' E' => array( 55, 37 ),
593
				'55°30\' N, 37°30\' W' => array( 55.5, -37.5 ),
594
				'0°0\' N, 0°0\' E' => array( 0, 0 ),
595
			),
596
		);
597
598
		$this->assertSpacingCorrect( $coordinates, LatLongFormatter::TYPE_DM );
599
	}
600
601
	/**
602
	 * @param array[] $coordSets
603
	 * @param string $format One of the LatLongFormatter::TYPE_… constants
604
	 */
605
	private function assertSpacingCorrect( array $coordSets, $format ) {
606
		$spacingLevelOptions = $this->provideSpacingLevelOptions();
607
		foreach ( $coordSets as $spacingKey => $coordinates ) {
608
			foreach ( $coordinates as $expected => $arguments ) {
609
				$options = new FormatterOptions();
610
				$options->setOption( LatLongFormatter::OPT_FORMAT, $format );
611
				$options->setOption( LatLongFormatter::OPT_DIRECTIONAL, true );
612
				$options->setOption( LatLongFormatter::OPT_PRECISION, 1 / 60 );
613
				$options->setOption( LatLongFormatter::OPT_SPACING_LEVEL, $spacingLevelOptions[$spacingKey] );
614
615
				$this->assertFormatsCorrectly(
616
					new LatLongValue( $arguments[0], $arguments[1] ),
617
					$options,
618
					$expected
619
				);
620
			}
621
		}
622
	}
623
624
	public function testSpacingOptionGetsAppliedForFloats() {
625
		$coordinates = array(
626
			'none' => array(
627
				'55.75N,37.25W' => array( 55.755786, -37.25633 ),
628
				'0N,0E' => array( 0, 0 ),
629
			),
630
			'latlong' => array(
631
				'55.75N, 37.25W' => array( 55.755786, -37.25633 ),
632
				'0N, 0E' => array( 0, 0 ),
633
			),
634
			'direction' => array(
635
				'55.75 N,37.25 W' => array( 55.755786, -37.25633 ),
636
				'0 N,0 E' => array( 0, 0 ),
637
			),
638
			'coordparts' => array(
639
				'55.75N,37.25W' => array( 55.755786, -37.25633 ),
640
				'0N,0E' => array( 0, 0 ),
641
			),
642
			'latlong_direction' => array(
643
				'55.75 N, 37.25 W' => array( 55.755786, -37.25633 ),
644
				'0 N, 0 E' => array( 0, 0 ),
645
			),
646
			'all' => array(
647
				'55.75 N, 37.25 W' => array( 55.755786, -37.25633 ),
648
				'0 N, 0 E' => array( 0, 0 ),
649
			),
650
		);
651
652
		$this->assertSpacingCorrect( $coordinates, LatLongFormatter::TYPE_FLOAT );
653
	}
654
655
	public function testWrongType() {
656
		$this->setExpectedException( 'InvalidArgumentException' );
657
658
		$formatter = new LatLongFormatter( new FormatterOptions() );
659
660
		$formatter->format( new StringValue( 'Evil' ) );
0 ignored issues
show
Documentation introduced by
new \DataValues\StringValue('Evil') is of type object<DataValues\StringValue>, but the function expects a object<DataValues\Geo\Values\LatLongValue>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
661
	}
662
663
	public function testGivenInvalidFormattingOption_formatThrowsException() {
664
		$options = new FormatterOptions();
665
		$options->setOption( LatLongFormatter::OPT_FORMAT, 'not a format' );
666
		$formatter = new LatLongFormatter( $options );
667
668
		$this->setExpectedException( 'InvalidArgumentException' );
669
		$formatter->format( new LatLongValue( 0, 0 ) );
670
	}
671
672
	/**
673
	 * @dataProvider invalidPrecisionProvider
674
	 */
675
	public function testFormatWithInvalidPrecision_fallsBackToDefaultPrecision( $precision ) {
676
		$options = new FormatterOptions();
677
		$options->setOption( LatLongFormatter::OPT_PRECISION, $precision );
678
		$formatter = new LatLongFormatter( $options );
679
680
		$formatted = $formatter->format( new LatLongValue( 1.2, 3.4 ) );
681
		$this->assertSame( '1.2, 3.4', $formatted );
682
	}
683
684
	/**
685
	 * @dataProvider invalidPrecisionProvider
686
	 */
687
	public function testFormatLatLongValueWithInvalidPrecision_fallsBackToDefaultPrecision( $precision ) {
688
		$formatter = new LatLongFormatter( new FormatterOptions() );
689
690
		$formatted = $formatter->formatLatLongValue( new LatLongValue( 1.2, 3.4 ), $precision );
691
		$this->assertSame( '1.2, 3.4', $formatted );
692
	}
693
694
	public function invalidPrecisionProvider() {
695
		return array(
696
			array( null ),
697
			array( '' ),
698
			array( 0 ),
699
			array( -1 ),
700
			array( NAN ),
701
			array( INF ),
702
		);
703
	}
704
705
}
706