MapUtilTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 169
ccs 109
cts 109
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A test_set_api_key_string() 0 15 1
A test_set_api_key_host_array() 0 22 1
A test_set_map_size() 0 6 1
A testSanitizeEmptyString() 0 7 1
A testSanitizeAlreadySanitized() 0 7 1
A testSanitizeSlashN() 0 7 1
A testSanitizeSlashT() 0 7 1
A testSanitizeSlashR() 0 7 1
A testSingularMappableItemMarkerUJSExists() 0 17 1
A htmlForMap() 0 7 1
A test_set_center() 0 8 1
A test_set_map_type() 0 22 2
1
<?php
2
3
class MapUtilTest extends SapphireTest
4
{
5
    /*
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 11
    public function setUp()
20
    {
21 11
        MapUtil::reset();
22 11
        parent::setUp();
23 11
    }
24
25 1
    public function test_set_api_key_string()
0 ignored issues
show
Coding Style introduced by
Method name "MapUtilTest::test_set_api_key_string" is not in camel caps format
Loading history...
26
    {
27 1
        MapUtil::set_api_key('PRETENDAPIKEY');
28 1
        $html = $this->htmlForMap();
29 1
        $this->assertContains(
30 1
            ' data-google-map-key="PRETENDAPIKEY" data-google-map-lang="en"',
31
            $html
32 1
        );
33
34 1
        $html = $this->htmlForMap();
35 1
        $this->assertNotContains(
36 1
            ' data-google-map-key="PRETENDAPIKEY" data-google-map-lang="en"',
37
            $html
38 1
        );
39 1
    }
40
41 1
    public function test_set_api_key_host_array()
0 ignored issues
show
Coding Style introduced by
Method name "MapUtilTest::test_set_api_key_host_array" is not in camel caps format
Loading history...
42
    {
43 1
        $url = Director::absoluteBaseURL();
44
        // remove http and https
45 1
        $url = str_replace('http://', '', $url);
46 1
        $url = str_replace('https://', '', $url);
47 1
        $parts = explode('/', $url);
48 1
        $host = $parts[0];
49 1
        $key = array($host => 'PRETENDAPIKEY');
50 1
        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...
51 1
        $html = $this->htmlForMap();
52 1
        $this->assertContains(
53 1
            ' data-google-map-key="PRETENDAPIKEY" data-google-map-lang="en"',
54
            $html
55 1
        );
56
57 1
        $html = $this->htmlForMap();
58 1
        $this->assertNotContains(
59 1
            ' data-google-map-key="PRETENDAPIKEY" data-google-map-lang="en"',
60
            $html
61 1
        );
62 1
    }
63
64 1
    public function test_set_map_size()
0 ignored issues
show
Coding Style introduced by
Method name "MapUtilTest::test_set_map_size" is not in camel caps format
Loading history...
65
    {
66 1
        MapUtil::set_map_size('890px', '24em');
67 1
        $html = $this->htmlForMap();
68 1
        $this->assertContains(' style="width:890px; height: 24em;"', $html);
69 1
    }
70
71 1
    public function testSanitizeEmptyString()
72
    {
73 1
        $this->assertEquals(
74 1
            '',
75 1
            MapUtil::sanitize('')
76 1
        );
77 1
    }
78
79 1
    public function testSanitizeAlreadySanitized()
80
    {
81 1
        $this->assertEquals(
82 1
            'This is already sanitized',
83 1
            MapUtil::sanitize('This is already sanitized')
84 1
        );
85 1
    }
86
87 1
    public function testSanitizeSlashN()
88
    {
89 1
        $this->assertEquals(
90 1
            'String to be sanitized',
91 1
            MapUtil::sanitize("String\n to be sanitized")
92 1
        );
93 1
    }
94
95 1
    public function testSanitizeSlashT()
96
    {
97 1
        $this->assertEquals(
98 1
            'String to be sanitized',
99 1
            MapUtil::sanitize("String\t to be sanitized")
100 1
        );
101 1
    }
102
103 1
    public function testSanitizeSlashR()
104
    {
105 1
        $this->assertEquals(
106 1
            'String to be sanitized',
107 1
            MapUtil::sanitize("String\r to be sanitized")
108 1
        );
109 1
    }
110
111
    /**
112
     * A single marker for the Member should appear in the UJS map data.
113
     */
114 1
    public function testSingularMappableItemMarkerUJSExists()
115
    {
116 1
        Member::add_extension('MapExtension');
117 1
        $member = new Member();
118 1
        $member->Lat = 12.847;
119 1
        $member->Lon = 29.24;
120
121
        // because we are not writing, set this manually
122 1
        $member->MapPinEdited = true;
123 1
        $list = new ArrayList();
124 1
        $list->push($member);
125 1
        $map = MapUtil::get_map($list, array());
126 1
        $html = $map->forTemplate();
127 1
        $markerExpected = 'data-mapmarkers=\'[{"latitude":12.847,"longitude":29.24,"html":"MEMBER: ","category":"default","icon":false}]\'';
128 1
        $this->assertContains($markerExpected, $html);
129 1
        Member::remove_extension('MapExtension');
130 1
    }
131
132 5
    private function htmlForMap()
133
    {
134 5
        $map = MapUtil::get_map(new ArrayList(), array());
135 5
        $html = $map->forTemplate();
136
137 5
        return $html;
138
    }
139
140 1
    public function test_set_center()
0 ignored issues
show
Coding Style introduced by
Method name "MapUtilTest::test_set_center" is not in camel caps format
Loading history...
141
    {
142 1
        MapUtil::set_center('Klong Tan, Bangkok, Thailand');
143 1
        $html = $this->htmlForMap();
144
        //coordinates of Klong Tan in Bangkok
145 1
        $expected = 'data-centre=\'{"lat":13.7243075,"lng":100.5718086}';
146 1
        $this->assertContains($expected, $html);
147 1
    }
148
149 1
    public function test_set_map_type()
0 ignored issues
show
Coding Style introduced by
Method name "MapUtilTest::test_set_map_type" is not in camel caps format
Loading history...
150
    {
151
        $mapTypes = array(
152 1
            'road' => 'road',
153 1
            'satellite' => 'satellite',
154 1
            'hybrid' => 'hybrid',
155 1
            'terrain' => 'terrain',
156 1
            'google.maps.MapTypeId.ROADMAP' => 'road',
157 1
            'google.maps.MapTypeId.SATELLITE' => 'satellite',
158 1
            'google.maps.MapTypeId.G_HYBRID_MAP' => 'hybrid',
159 1
            'google.maps.MapTypeId.G_PHYSICAL_MAP' => 'terrain',
160 1
            'custom_layer' => 'custom_layer',
161
162 1
        );
163
164 1
        foreach (array_keys($mapTypes) as $mapType) {
165 1
            MapUtil::set_map_type($mapType);
166 1
            $expected = "data-maptype='".$mapTypes[$mapType]."'";
167 1
            $html = $this->htmlForMap();
168 1
            $this->assertContains($expected, $html);
169 1
        }
170 1
    }
171
}
172