Passed
Push — zeroRounding ( 491d2a...2f1015 )
by no
35:13 queued 21:46
created

GlobeCoordinateFormatterTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 7
dl 0
loc 118
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 3 1
A validProvider() 0 68 3
A testFormatWithInvalidPrecision_fallsBackToDefaultPrecision() 0 8 1
A testFormatterRoundTrip() 0 19 1
1
<?php
2
3
namespace Tests\DataValues\Geo\Formatters;
4
5
use DataValues\Geo\Formatters\LatLongFormatter;
6
use DataValues\Geo\Formatters\GlobeCoordinateFormatter;
7
use DataValues\Geo\Parsers\GlobeCoordinateParser;
8
use DataValues\Geo\Values\GlobeCoordinateValue;
9
use DataValues\Geo\Values\LatLongValue;
10
use ValueFormatters\FormatterOptions;
11
use ValueFormatters\Test\ValueFormatterTestBase;
12
use ValueParsers\ParserOptions;
13
14
/**
15
 * @covers DataValues\Geo\Formatters\GlobeCoordinateFormatter
16
 *
17
 * @group ValueFormatters
18
 * @group DataValueExtensions
19
 *
20
 * @license GPL-2.0+
21
 * @author Jeroen De Dauw < [email protected] >
22
 */
23
class GlobeCoordinateFormatterTest extends ValueFormatterTestBase {
24
25
	/**
26
	 * @see ValueFormatterTestBase::getInstance
27
	 *
28
	 * @param FormatterOptions|null $options
29
	 *
30
	 * @return GlobeCoordinateFormatter
31
	 */
32
	protected function getInstance( FormatterOptions $options = null ) {
33
		return new GlobeCoordinateFormatter( $options );
34
	}
35
36
	/**
37
	 * @see ValueFormatterTestBase::validProvider
38
	 */
39
	public function validProvider() {
40
		$floats = [
41
			'55.755786, -37.617633' => [ 55.755786, -37.617633, 0.000001 ],
42
			'-55.7558, 37.6176' => [ -55.755786, 37.617633, 0.0001 ],
43
			'-55, -38' => [ -55, -37.617633, 1 ],
44
			'5.5, 37' => [ 5.5, 37, 0.1 ],
45
			'0, 0' => [ 0, 0, 1 ],
46
		];
47
48
		$decimalDegrees = [
49
			'55.755786°, 37.617633°' => [ 55.755786, 37.617633, 0.000001 ],
50
			'55.7558°, -37.6176°' => [ 55.755786, -37.617633, 0.0001 ],
51
			'-55°, -38°' => [ -55, -37.617633, 1 ],
52
			'-5.5°, -37.0°' => [ -5.5, -37, 0.1 ],
53
			'0°, 0°' => [ 0, 0, 1 ],
54
		];
55
56
		$dmsCoordinates = [
57
			'55° 45\' 20.830", 37° 37\' 3.479"' => [ 55.755786, 37.617633, 0.000001 ],
58
			'55° 45\' 20.830", -37° 37\' 3.479"' => [ 55.755786, -37.617633, 0.000001 ],
59
			'-55° 45\' 20.9", -37° 37\' 3.4"' => [ -55.755786, -37.617633, 0.0001 ],
60
			'-55° 45\' 20.9", 37° 37\' 3.4"' => [ -55.755786, 37.617633, 0.0001 ],
61
62
			'55°, 37°' => [ 55, 37, 1 ],
63
			'55° 30\' 0", 37° 30\' 0"' => [ 55.5, 37.5, 0.01 ],
64
			'55° 0\' 18", 37° 0\' 18"' => [ 55.005, 37.005, 0.001 ],
65
			'0° 0\' 0", 0° 0\' 0"' => [ 0, 0, 0.001 ],
66
			'0° 0\' 18", 0° 0\' 18"' => [ 0.005, 0.005, 0.001 ],
67
			'-0° 0\' 18", -0° 0\' 18"' => [ -0.005, -0.005, 0.001 ],
68
		];
69
70
		$dmCoordinates = [
71
			'55°, 37°' => [ 55, 37, 1 ],
72
			'0°, 0°' => [ 0, 0, 1 ],
73
			'55° 31\', 37° 31\'' => [ 55.5, 37.5, 0.04 ],
74
			'-55° 31\', -37° 31\'' => [ -55.5, -37.5, 0.04 ],
75
			'-0° 0.3\', -0° 0.3\'' => [ -0.005, -0.005, 0.005 ],
76
		];
77
78
		$argLists = [];
79
80
		$tests = [
81
			LatLongFormatter::TYPE_FLOAT => $floats,
82
			LatLongFormatter::TYPE_DD => $decimalDegrees,
83
			LatLongFormatter::TYPE_DMS => $dmsCoordinates,
84
			LatLongFormatter::TYPE_DM => $dmCoordinates,
85
		];
86
87
		$i = 0;
88
		foreach ( $tests as $format => $coords ) {
89
			foreach ( $coords as $expectedOutput => $arguments ) {
90
				$options = new FormatterOptions();
91
				$options->setOption( LatLongFormatter::OPT_FORMAT, $format );
92
93
				$input = new GlobeCoordinateValue(
94
					new LatLongValue( $arguments[0], $arguments[1] ),
95
					$arguments[2]
96
				);
97
98
				$key = "[$i] $format: $expectedOutput";
99
				$argLists[$key] = [ $input, $expectedOutput, $options ];
100
101
				$i++;
102
			}
103
		}
104
105
		return $argLists;
106
	}
107
108
	public function testFormatWithInvalidPrecision_fallsBackToDefaultPrecision() {
109
		$options = new FormatterOptions();
110
		$options->setOption( LatLongFormatter::OPT_PRECISION, 0 );
111
		$formatter = new GlobeCoordinateFormatter( $options );
112
113
		$formatted = $formatter->format( new GlobeCoordinateValue( new LatLongValue( 1.2, 3.4 ), null ) );
114
		$this->assertSame( '1.2, 3.4', $formatted );
115
	}
116
117
	/**
118
	 * @dataProvider validProvider
119
	 */
120
	public function testFormatterRoundTrip(
121
		GlobeCoordinateValue $coord,
122
		$expectedValue,
0 ignored issues
show
Unused Code introduced by
The parameter $expectedValue 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...
123
		FormatterOptions $options
124
	) {
125
		$formatter = new GlobeCoordinateFormatter( $options );
126
127
		$parser = new GlobeCoordinateParser(
128
			new ParserOptions( [ 'precision' => $coord->getPrecision() ] )
129
		);
130
131
		$formatted = $formatter->format( $coord );
132
		$parsed = $parser->parse( $formatted );
133
134
		// NOTE: $parsed may be != $coord, because of rounding, so we can't compare directly.
135
		$formattedParsed = $formatter->format( $parsed );
136
137
		$this->assertSame( $formatted, $formattedParsed );
138
	}
139
140
}
141