Passed
Push — php71 ( 95a24c...2c708a )
by Jeroen De
07:19
created

GlobeCoordinateFormatterTest::testValidFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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