testGivenInvalidFormat_defaultFormatGetsUsed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\Tests\Integration\Parser;
6
7
use MediaWiki\MediaWikiServices;
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class CoordinatesTest extends TestCase {
15
16
	private function parse( string $textToParse ): string {
17
		return MediaWikiServices::getInstance()->getParser()
18
			->parse( $textToParse, \Title::newMainPage(), new \ParserOptions() )->getText();
19
	}
20
21
	public function testGivenInvalidCoordinates_errorIsShown() {
22
		$this->assertStringContainsString(
23
			'<span class="errorbox">',
24
			$this->parse( '{{#coordinates:nope}}' )
25
		);
26
	}
27
28
	public function testGivenNoCoordinates_errorIsShown() {
29
		$this->assertStringContainsString(
30
			'<span class="errorbox">',
31
			$this->parse( '{{#coordinates:}}' )
32
		);
33
	}
34
35
	public function testGivenValidCoordinates_theyAreFormatted() {
36
		$this->assertStringContainsString(
37
			'1° 0\' 0.00" N, 1° 0\' 0.00" E',
38
			$this->parse( '{{#coordinates:1,1}}' )
39
		);
40
	}
41
42
	public function testGivenFormat_coordinatesAreConvertedToIt() {
43
		$this->assertStringContainsString(
44
			'1.000000° N, 1.000000° E',
45
			$this->parse( '{{#coordinates:1,1|format=dd}}' )
46
		);
47
	}
48
49
	public function testGivenDirectionalParameter_itGetsUsed() {
50
		$this->assertStringContainsString(
51
			'1° 0\' 0.00", 1° 0\' 0.00"',
52
			$this->parse( '{{#coordinates:1,1|directional=no}}' )
53
		);
54
	}
55
56
	public function testCoordinatesInNonDms_theyGetParsed() {
57
		$this->assertStringContainsString(
58
			'1° 20\' 13.20" N, 4° 12\' 0.00" W',
59
			$this->parse( '{{#coordinates:1.337°, -4.2°}}' )
60
		);
61
	}
62
63
	public function testGivenInvalidFormat_defaultFormatGetsUsed() {
64
		$this->assertStringContainsString(
65
			'1° 0\' 0.00" N, 1° 0\' 0.00" E',
66
			$this->parse( '{{#coordinates:1,1|format=such}}' )
67
		);
68
	}
69
70
	public function testRoundingWhenFormattingAsFloat() {
71
		$this->assertStringContainsString(
72
			'52.136945 N, 0.466722 W',
73
			$this->parse( '{{#coordinates:52.136945,-0.466722|format=float}}' )
74
		);
75
	}
76
77
	public function testRoundingWhenFormattingAsDMS() {
78
		$this->assertStringContainsString(
79
			'52° 8\' 13.00" N, 0° 28\' 0.20" W',
80
			$this->parse( '{{#coordinates:52.136945,-0.466722|format=dms}}' )
81
		);
82
	}
83
84
	public function testRoundingWhenFormattingAsDD() {
85
		$this->assertStringContainsString(
86
			'52.136945° N, 0.466722° W',
87
			$this->parse( '{{#coordinates:52.136945,-0.466722|format=dd}}' )
88
		);
89
	}
90
91
	public function testRoundingWhenFormattingAsDM() {
92
		$this->assertStringContainsString(
93
			'52° 8.2167\' N, 0° 28.0033\' W',
94
			$this->parse( '{{#coordinates:52.136945,-0.466722|format=dm}}' )
95
		);
96
	}
97
98
}
99