MapsDistanceParserTest::testFormatDistance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\Tests\Integration;
6
7
use Maps\Presentation\MapsDistanceParser;
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * @covers MapsDistanceParser
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class MapsDistanceParserTest extends TestCase {
17
18
	public static $distances = [
19
		'1' => 1,
20
		'1m' => 1,
21
		'1 m' => 1,
22
		'   1   	  m ' => 1,
23
		'1.1' => 1.1,
24
		'1,1' => 1.1,
25
		'1 km' => 1000,
26
		'42 km' => 42000,
27
		'4.2 km' => 4200,
28
		'4,20km' => 4200,
29
		'1 mile' => 1609.344,
30
		'10 nauticalmiles' => 18520,
31
		'1.0nautical mile' => 1852,
32
	];
33
	public static $formatTests = [
34
		'm' => [
35
			'1 m' => 1,
36
			'1000 m' => 1000.00,
37
			'42.42 m' => 42.42,
38
			'42.4242 m' => 42.4242,
39
		],
40
		'km' => [
41
			//'0.001 km' => 1,
42
			'1 km' => 1000,
43
			'4.24 km' => 4242,
44
		],
45
		'kilometers' => [
46
			'0.001 kilometers' => 1,
47
			'1 kilometers' => 1000,
48
			'4.24 kilometers' => 4242,
49
		],
50
	];
51
	/**
52
	 * Invalid distances.
53
	 *
54
	 * @var array
55
	 */
56
	public static $fakeDistances = [
57
		'IN YOUR CODE, BEING TOTALLY RIDICULOUS',
58
		'0x20 km',
59
		'km 42',
60
		'42 42 km',
61
		'42 km km',
62
		'42 foo',
63
		'3.4.2 km'
64
	];
65
66
	public function setUp(): void {
67
		if ( !defined( 'MEDIAWIKI' ) ) {
68
			$this->markTestSkipped( 'MediaWiki is not available' );
69
		}
70
	}
71
72
	/**
73
	 * Tests Maps\Presentation\MapsDistanceParser::parseDistance()
74
	 */
75
	public function testParseDistance() {
76
		foreach ( self::$distances as $rawValue => $parsedValue ) {
77
			$this->assertEquals(
78
				$parsedValue,
79
				MapsDistanceParser::parseDistance( (string)$rawValue ),
80
				"'$rawValue' was not parsed to '$parsedValue':"
81
			);
82
		}
83
84
		foreach ( self::$fakeDistances as $fakeDistance ) {
85
			$this->assertFalse(
86
				MapsDistanceParser::parseDistance( $fakeDistance ),
87
				"'$fakeDistance' should not be recognized:"
88
			);
89
		}
90
	}
91
92
	/**
93
	 * Tests Maps\Presentation\MapsDistanceParser::formatDistance()
94
	 */
95
	public function testFormatDistance() {
96
		foreach ( self::$formatTests['km'] as $rawValue => $parsedValue ) {
97
			$this->assertEquals(
98
				$rawValue,
99
				MapsDistanceParser::formatDistance( $parsedValue, 'km' ),
100
				"'$parsedValue' was not formatted to '$rawValue':"
101
			);
102
		}
103
	}
104
105
	/**
106
	 * Tests Maps\Presentation\MapsDistanceParser::parseAndFormat()
107
	 */
108
	public function testParseAndFormat() {
109
		$this->assertSame(
110
			'42,000 m',
111
			MapsDistanceParser::parseAndFormat( '42 km', 'm' )
112
		);
113
114
		$this->assertSame(
115
			'42 km',
116
			MapsDistanceParser::parseAndFormat( '42000 m', 'km' )
117
		);
118
	}
119
120
	/**
121
	 * Tests Maps\Presentation\MapsDistanceParser::isDistance()
122
	 */
123
	public function testIsDistance() {
124
		foreach ( self::$fakeDistances as $fakeDistance ) {
125
			$this->assertFalse(
126
				MapsDistanceParser::isDistance( $fakeDistance ),
127
				"'$fakeDistance' should not be recognized:"
128
			);
129
		}
130
131
		foreach ( self::$distances as $distance ) {
132
			$this->assertTrue( MapsDistanceParser::isDistance( (string)$distance ), "'$distance' was not be recognized:" );
133
		}
134
	}
135
136
	/**
137
	 * Tests Maps\Presentation\MapsDistanceParser::getUnitRatio()
138
	 */
139
	public function testGetUnitRatio() {
140
		foreach ( $GLOBALS['egMapsDistanceUnits'] as $unit => $ratio ) {
141
			$r = MapsDistanceParser::getUnitRatio( $unit );
142
			$this->assertEquals( $ratio, $r, "The ratio for '$unit' should be '$ratio' but was '$r'" );
143
		}
144
	}
145
146
	/**
147
	 * Tests Maps\Presentation\MapsDistanceParser::getValidUnit()
148
	 */
149
	public function testGetValidUnit() {
150
		foreach ( $GLOBALS['egMapsDistanceUnits'] as $unit => $ratio ) {
151
			$u = MapsDistanceParser::getValidUnit( $unit );
152
			$this->assertEquals( $unit, $u, "The valid unit for '$unit' should be '$unit' but was '$u'" );
153
		}
154
155
		global $egMapsDistanceUnit;
156
157
		foreach ( [ '0', 'swfwdffdhy', 'dxwgdrfh' ] as $unit ) {
158
			$u = MapsDistanceParser::getValidUnit( $unit );
159
			$this->assertEquals(
160
				$egMapsDistanceUnit,
161
				$u,
162
				"The valid unit for '$unit' should be '$egMapsDistanceUnit' but was '$u'"
163
			);
164
		}
165
	}
166
167
	/**
168
	 * Tests Maps\Presentation\MapsDistanceParser::getUnits()
169
	 */
170
	public function testGetUnits() {
171
		$this->assertEquals( array_keys( $GLOBALS['egMapsDistanceUnits'] ), MapsDistanceParser::getUnits() );
172
	}
173
174
}
175