Completed
Push — AUTOMATED_TESTING ( b401db...19cb04 )
by Gordon
11:46
created

MapUtilTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
class MapUtilTest extends SapphireTest {
4
5
	/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
6
	Other tests:
7
	1) List, ArrayList, DataList, null for get_map
8
	2) Negative and zero map sizes
9
	3) Invalid map type
10
11
12
	public function setUpOnce() {
13
		$this->requiredExtensions = array(
14
			'Member' => array('MapExtension')
15
		);
16
		parent::setupOnce();
17
	}
18
*/
19
	public function setUp() {
20
		MapUtil::reset();
21
		parent::setUp();
22
	}
23
24
	public function test_set_api_key_string() {
25
		MapUtil::set_api_key('PRETENDAPIKEY');
26
		$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27
		$this->fail('Where is this used?');
28
	}
29
30
	public function test_set_api_key_host_array() {
31
		$url = Director::absoluteBaseURL();
32
		// remove http and https
33
		$url = str_replace('http://', '', $url);
34
		$url = str_replace('https://', '', $url);
35
		$parts = explode('/', $url);
36
		$host = $parts[0];
37
		$key = array($host => 'PRETENDAPIKEY');
38
		MapUtil::set_api_key($key);
0 ignored issues
show
Documentation introduced by
$key is of type array<?,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
		$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
		$this->fail('Where is this used?');
41
	}
42
43
	public function test_get_set_map_already_rendered() {
44
		MapUtil::set_map_already_rendered(false);
45
		$this->assertFalse(MapUtil::get_map_already_rendered());
46
		MapUtil::set_map_already_rendered(true);
47
		$this->assertTrue(MapUtil::get_map_already_rendered());
48
	}
49
50
	public function test_set_map_size() {
51
		MapUtil::set_map_size('890px', '24em');
52
		$html = $this->htmlForMap();
53
		$this->assertContains(' style="width:890px; height: 24em;"', $html);
54
	}
55
56
	public function testSanitizeEmptyString() {
57
		$this->assertEquals(
58
			'',
59
			MapUtil::sanitize('')
60
		);
61
	}
62
63
	public function testSanitizeAlreadySanitized() {
64
		$this->assertEquals(
65
			'This is already sanitized',
66
			MapUtil::sanitize('This is already sanitized')
67
		);
68
	}
69
70
	public function testSanitizeSlashN() {
71
		$this->assertEquals(
72
			'String to be sanitized',
73
			MapUtil::sanitize("String\n to be sanitized")
74
		);
75
	}
76
77
	public function testSanitizeSlashT() {
78
		$this->assertEquals(
79
			'String to be sanitized',
80
			MapUtil::sanitize("String\t to be sanitized")
81
		);
82
	}
83
84
	public function testSanitizeSlashR() {
85
		$this->assertEquals(
86
			'String to be sanitized',
87
			MapUtil::sanitize("String\r to be sanitized")
88
		);
89
	}
90
91
	/**
92
	 * A single marker for the Member should appear in the UJS map data
93
	 */
94
	public function testSingularMappableItemMarkerUJSExists() {
95
		Member::add_extension('MapExtension');
96
		$member = new Member();
97
		$member->Lat = 12.847;
98
		$member->Lon = 29.24;
99
100
		// because we are not writing, set this manually
101
		$member->MapPinEdited = true;
102
		$list = new ArrayList();
103
		$list->push($member);
104
		$map = MapUtil::get_map($list, array());
105
		$html = $map->forTemplate();
106
		$markerExpected = 'data-mapmarkers=\'[{"latitude":12.847,"longitude":29.24,"html":"MEMBER: ","category":"default","icon":false}]\'';
107
		$this->assertContains($markerExpected, $html);
108
		Member::remove_extension('MapExtension');
109
	}
110
111
	private function htmlForMap() {
112
		$map = MapUtil::get_map(new ArrayList(), array());
113
		$html = $map->forTemplate();
114
		return $html;
115
	}
116
117
118
	// These appear to test code that's not used
119
	public function test_set_center() {
120
		MapUtil::set_center('Klong Tan, Bangkok, Thailand');
121
		$html = $this->htmlForMap();
122
		//coordinates of Klong Tan in Bangkok
123
		$expected = 'data-centre=\'{"lat":13.7243075,"lng":100.5718086}';
124
		$this->assertContains($expected, $html);
125
	}
126
127
	 public function test_set_map_type() {
128
		MapUtil::set_map_type('google.maps.MapTypeId.G_HYBRID_MAP');
129
		$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130
		$this->fail('No effect for set map type');
131
132
	}
133
134
	 public function test_set_info_window_width() {
135
	 	MapUtil::set_info_window_width(420);
136
	 	$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
137
	 	$this->fail('No evidence of set info width being used');
138
	}
139
140
	 public function test_set_icon_size() {
141
	 	MapUtil::set_icon_size(14, 37);
142
	 	$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
143
	 	$html = $this->htmlForMap();
0 ignored issues
show
Unused Code introduced by
$html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
144
	 	$this->fail('No effect for set icon size');
145
	}
146
147
}
148