Passed
Push — master ( 9d3cd0...e94f7b )
by Amir
01:44 queued 11s
created

unit/Formatters/GlobeCoordinateFormatterTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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