Completed
Push — MediaWikiFileUrlFinderTest ( d58bee...394f02 )
by Jeroen De
08:48
created

MapsDistanceParserTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 1
dl 0
loc 164
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 2
A testParseDistance() 0 16 3
A testFormatDistance() 0 9 2
A testParseAndFormat() 0 16 2
A testIsDistance() 0 12 3
A testGetUnitRatio() 0 6 2
A testGetValidUnit() 0 17 3
A testGetUnits() 0 3 1
1
<?php
2
3
namespace Maps\Tests\Integration;
4
5
use Maps\Presentation\MapsDistanceParser;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * @covers MapsDistanceParser
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class MapsDistanceParserTest extends TestCase {
15
16
	public static $distances = [
17
		'1' => 1,
18
		'1m' => 1,
19
		'1 m' => 1,
20
		'   1   	  m ' => 1,
21
		'1.1' => 1.1,
22
		'1,1' => 1.1,
23
		'1 km' => 1000,
24
		'42 km' => 42000,
25
		'4.2 km' => 4200,
26
		'4,20km' => 4200,
27
		'1 mile' => 1609.344,
28
		'10 nauticalmiles' => 18520,
29
		'1.0nautical mile' => 1852,
30
	];
31
	public static $formatTests = [
32
		'm' => [
33
			'1 m' => 1,
34
			'1000 m' => 1000.00,
35
			'42.42 m' => 42.42,
36
			'42.4242 m' => 42.4242,
37
		],
38
		'km' => [
39
			//'0.001 km' => 1,
40
			'1 km' => 1000,
41
			'4.24 km' => 4242,
42
		],
43
		'kilometers' => [
44
			'0.001 kilometers' => 1,
45
			'1 kilometers' => 1000,
46
			'4.24 kilometers' => 4242,
47
		],
48
	];
49
	/**
50
	 * Invalid distances.
51
	 *
52
	 * @var array
53
	 */
54
	public static $fakeDistances = [
55
		'IN YOUR CODE, BEING TOTALLY RIDICULOUS',
56
		'0x20 km',
57
		'km 42',
58
		'42 42 km',
59
		'42 km km',
60
		'42 foo',
61
		'3.4.2 km'
62
	];
63
64
	public function setUp() {
65
		if ( !defined( 'MEDIAWIKI' ) ) {
66
			$this->markTestSkipped( 'MediaWiki is not available' );
67
		}
68
	}
69
70
	/**
71
	 * Tests Maps\Presentation\MapsDistanceParser::parseDistance()
72
	 */
73
	public function testParseDistance() {
74
		foreach ( self::$distances as $rawValue => $parsedValue ) {
75
			$this->assertEquals(
76
				$parsedValue,
77
				MapsDistanceParser::parseDistance( $rawValue ),
78
				"'$rawValue' was not parsed to '$parsedValue':"
79
			);
80
		}
81
82
		foreach ( self::$fakeDistances as $fakeDistance ) {
83
			$this->assertFalse(
84
				MapsDistanceParser::parseDistance( $fakeDistance ),
85
				"'$fakeDistance' should not be recognized:"
86
			);
87
		}
88
	}
89
90
	/**
91
	 * Tests Maps\Presentation\MapsDistanceParser::formatDistance()
92
	 */
93
	public function testFormatDistance() {
94
		foreach ( self::$formatTests['km'] as $rawValue => $parsedValue ) {
95
			$this->assertEquals(
96
				$rawValue,
97
				MapsDistanceParser::formatDistance( $parsedValue, 'km' ),
98
				"'$parsedValue' was not formatted to '$rawValue':"
99
			);
100
		}
101
	}
102
103
	/**
104
	 * Tests Maps\Presentation\MapsDistanceParser::parseAndFormat()
105
	 */
106
	public function testParseAndFormat() {
107
		$conversions = [
108
			'42 km' => '42000 m'
109
		];
110
111
		foreach ( array_merge( $conversions, array_reverse( $conversions ) ) as $source => $target ) {
112
			global $wgContLang;
113
			$unit = explode( ' ', $target, 2 );
114
			$unit = $unit[1];
115
			$this->assertEquals(
116
				$wgContLang->formatNum( $target ),
117
				MapsDistanceParser::parseAndFormat( $source, $unit ),
118
				"'$source' was not parsed and formatted to '$target':"
119
			);
120
		}
121
	}
122
123
	/**
124
	 * Tests Maps\Presentation\MapsDistanceParser::isDistance()
125
	 */
126
	public function testIsDistance() {
127
		foreach ( self::$fakeDistances as $fakeDistance ) {
128
			$this->assertFalse(
129
				MapsDistanceParser::isDistance( $fakeDistance ),
130
				"'$fakeDistance' should not be recognized:"
131
			);
132
		}
133
134
		foreach ( self::$distances as $distance ) {
135
			$this->assertTrue( MapsDistanceParser::isDistance( $distance ), "'$distance' was not be recognized:" );
136
		}
137
	}
138
139
	/**
140
	 * Tests Maps\Presentation\MapsDistanceParser::getUnitRatio()
141
	 */
142
	public function testGetUnitRatio() {
0 ignored issues
show
Coding Style introduced by
testGetUnitRatio uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
143
		foreach ( $GLOBALS['egMapsDistanceUnits'] as $unit => $ratio ) {
144
			$r = MapsDistanceParser::getUnitRatio( $unit );
145
			$this->assertEquals( $ratio, $r, "The ratio for '$unit' should be '$ratio' but was '$r'" );
146
		}
147
	}
148
149
	/**
150
	 * Tests Maps\Presentation\MapsDistanceParser::getValidUnit()
151
	 */
152
	public function testGetValidUnit() {
0 ignored issues
show
Coding Style introduced by
testGetValidUnit uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
153
		foreach ( $GLOBALS['egMapsDistanceUnits'] as $unit => $ratio ) {
154
			$u = MapsDistanceParser::getValidUnit( $unit );
155
			$this->assertEquals( $unit, $u, "The valid unit for '$unit' should be '$unit' but was '$u'" );
156
		}
157
158
		global $egMapsDistanceUnit;
159
160
		foreach ( [ '0', 'swfwdffdhy', 'dxwgdrfh' ] as $unit ) {
161
			$u = MapsDistanceParser::getValidUnit( $unit );
162
			$this->assertEquals(
163
				$egMapsDistanceUnit,
164
				$u,
165
				"The valid unit for '$unit' should be '$egMapsDistanceUnit' but was '$u'"
166
			);
167
		}
168
	}
169
170
	/**
171
	 * Tests Maps\Presentation\MapsDistanceParser::getUnits()
172
	 */
173
	public function testGetUnits() {
0 ignored issues
show
Coding Style introduced by
testGetUnits uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
174
		$this->assertEquals( array_keys( $GLOBALS['egMapsDistanceUnits'] ), MapsDistanceParser::getUnits() );
175
	}
176
177
}
178