Completed
Push — merge-sm ( 44b830...c70fa4 )
by Jeroen De
10:03 queued 07:12
created

MapsDistanceParserTest::testGetUnitRatio()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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