MapExtensionTest   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 302
Duplicated Lines 22.85 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 97.35%

Importance

Changes 0
Metric Value
wmc 26
lcom 2
cbo 6
dl 69
loc 302
ccs 184
cts 189
cp 0.9735
rs 10
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpOnce() 0 7 1
A testUpdateCMSFields() 0 3 1
A testGetMappableLatitude() 0 10 1
A testGetMappableLongitude() 0 9 1
A testgetMappableMapContent() 0 3 1
A testonBeforeWriteMapPinNotEdited() 0 8 1
A testonBeforeWriteMapPinEdited() 0 28 1
A testGetMappableMapPin() 0 26 1
A testHasGeoWest() 0 6 1
A testHasGeoEast() 0 6 1
A testHasGeoNorth() 0 6 1
A testHasGeoNorthWest() 6 7 1
A testHasGeoNortEast() 6 7 1
A testHasGeoSouth() 0 6 1
A testHasGeoSouthWest() 6 7 1
A testHasGeoSouthEast() 6 7 1
A testHasGeoOrigin() 0 7 1
A testHasGeoOriginMapLayerExtension() 0 14 1
A testBasicMap() 17 18 1
A testBasicMapWithLayer() 19 20 1
A testGetMapField() 0 9 1
A testUseCompressedAssets() 0 14 1
A getInstance() 0 6 1
A addLayerToInstance() 0 13 1
A addMapPinToInstance() 9 10 1
A showMapPinEdited() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class MapExtensionTest extends SapphireTest
4
{
5
    protected static $fixture_file = 'mappable/tests/mapextensions.yml';
6
7
    public function setUpOnce()
8
    {
9
        $this->requiredExtensions = array(
10
            'Member' => array('MapExtension', 'MapLayerExtension'),
11
        );
12
        parent::setupOnce();
13
    }
14
15 1
    public function testUpdateCMSFields()
16
    {
17 1
    }
18
19 1
    public function testGetMappableLatitude()
20
    {
21 1
        $instance = $this->getInstance();
22 1
        $instance->Lat = 42.1;
23 1
        $instance->write();
24 1
        $this->assertEquals(
25 1
            42.1,
26 1
            $instance->getMappableLatitude()
27 1
        );
28 1
    }
29
30 1
    public function testGetMappableLongitude()
31
    {
32 1
        $instance = $this->getInstance();
33 1
        $instance->Lon = 42.1;
34 1
        $this->assertEquals(
35 1
            42.1,
36 1
            $instance->getMappableLongitude()
37 1
        );
38 1
    }
39
40 1
    public function testgetMappableMapContent()
41
    {
42 1
    }
43
44 1
    public function testonBeforeWriteMapPinNotEdited()
45
    {
46 1
        $instance = $this->getInstance();
47 1
        $this->Lat = 0;
48 1
        $this->Lon = 0;
49 1
        $instance->write();
50 1
        $this->assertFalse($instance->MapPinEdited);
51 1
    }
52
53 1
    public function testonBeforeWriteMapPinEdited()
54
    {
55 1
        $instance = $this->getInstance();
56
57
        // north
58 1
        $this->showMapPinEdited($instance, 10, 0);
59
60
        // north west
61 1
        $this->showMapPinEdited($instance, 10, -10);
62
63
        // west
64 1
        $this->showMapPinEdited($instance, 0, -10);
65
66
        // south west
67 1
        $this->showMapPinEdited($instance, -10, -10);
68
69
        // south
70 1
        $this->showMapPinEdited($instance, -10, 0);
71
72
        // south east
73 1
        $this->showMapPinEdited($instance, -10, 10);
74
75
        // east
76 1
        $this->showMapPinEdited($instance, 0, 10);
77
78
        // north east
79 1
        $this->showMapPinEdited($instance, 10, 10);
80 1
    }
81
82
    // FIXME - use an actual file here
83 1
    public function testGetMappableMapPin()
84
    {
85 1
        $instance = $this->getInstance();
86
87
        // no icon, so return false
88 1
        $this->assertFalse($instance->getMappableMapPin());
89
90
        // add a default map icon
91 1
        $this->addMapPinToInstance($instance);
92
93 1
        $pin1 = $instance->getMappableMapPin();
94 1
        $this->assertStringEndsWith('assets/mapicontest.png', $pin1);
0 ignored issues
show
Bug introduced by
The method assertStringEndsWith() does not seem to exist on object<MapExtensionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
96
        // Test the cached pin - this is the case of a layer providing a cached pin
97
        // so that repeated calls are not made to load the same icon
98
        $iconURL =
99 1
            'https://cdn3.iconfinder.com/data/icons/iconic-1/32/map_pin_fill-512.png';
100
101
        // Set mappable as having no icon, use the cached one instead
102
        // This simulates using a common icon to avoid DB queries
103 1
        $instance->MapPinIconID = 0;
104 1
        $instance->write();
105 1
        $instance->CachedMapPinURL = $iconURL;
106 1
        $pin2 = $instance->getMappableMapPin();
107 1
        $this->assertEquals($iconURL, $pin2);
108 1
    }
109
110 1
    public function testHasGeoWest()
111
    {
112 1
        $instance = $this->getInstance();
113 1
        $instance->Lon = -20;
114 1
        $this->assertTrue($instance->HasGeo());
115 1
    }
116
117 1
    public function testHasGeoEast()
118
    {
119 1
        $instance = $this->getInstance();
120 1
        $instance->Lon = 20;
121 1
        $this->assertTrue($instance->HasGeo());
122 1
    }
123
124 1
    public function testHasGeoNorth()
125
    {
126 1
        $instance = $this->getInstance();
127 1
        $instance->Lat = 20;
128 1
        $this->assertTrue($instance->HasGeo());
129 1
    }
130
131 1 View Code Duplication
    public function testHasGeoNorthWest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133 1
        $instance = $this->getInstance();
134 1
        $instance->Lat = 20;
135 1
        $instance->Lon = -20;
136 1
        $this->assertTrue($instance->HasGeo());
137 1
    }
138
139 1 View Code Duplication
    public function testHasGeoNortEast()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141 1
        $instance = $this->getInstance();
142 1
        $instance->Lat = 20;
143 1
        $instance->Lon = 20;
144 1
        $this->assertTrue($instance->HasGeo());
145 1
    }
146
147 1
    public function testHasGeoSouth()
148
    {
149 1
        $instance = $this->getInstance();
150 1
        $instance->Lat = -20;
151 1
        $this->assertTrue($instance->HasGeo());
152 1
    }
153
154 1 View Code Duplication
    public function testHasGeoSouthWest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156 1
        $instance = $this->getInstance();
157 1
        $instance->Lat = -20;
158 1
        $instance->Lon = -20;
159 1
        $this->assertTrue($instance->HasGeo());
160 1
    }
161
162 1 View Code Duplication
    public function testHasGeoSouthEast()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164 1
        $instance = $this->getInstance();
165 1
        $instance->Lat = -20;
166 1
        $instance->Lon = 20;
167 1
        $this->assertTrue($instance->HasGeo());
168 1
    }
169
170 1
    public function testHasGeoOrigin()
171
    {
172 1
        $instance = $this->getInstance();
173 1
        $instance->Lat = 0;
174 1
        $instance->Lon = 0;
175 1
        $this->assertFalse($instance->HasGeo());
176 1
    }
177
178 2
    public function testHasGeoOriginMapLayerExtension()
179
    {
180 1
        $instance = $this->getInstance();
181
182
        // assert that origin has no geo (until layers added)
183 1
        $instance->Lat = 0;
184 1
        $instance->Lon = 0;
185 1
        $this->assertFalse($instance->HasGeo());
186
187 1
        $this->addLayerToInstance($instance);
188
189
        // assert has geo even when at the origin of 0,0
190 1
        $this->assertTrue($instance->HasGeo());
191 2
    }
192
193 1 View Code Duplication
    public function testBasicMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195 1
        $instance = $this->getInstance();
196 1
        $instance->Lat = 37.1;
197 1
        $instance->Lon = 28;
198 1
        $instance->Zoom = 12;
199 1
        $instance->MapPinEdited = true;
200 1
        $html = $instance->BasicMap()->forTemplate();
201
202 1
        $expected = "data-centre='{\"lat\":37.1,\"lng\":28}'";
203 1
        $this->assertContains($expected, $html);
204 1
        $expected = "data-mapmarkers='[{\"latitude\":37.1,\"longitude\":28,\"html\":\"MEMBER: \",\"category\":\"default\",\"icon\":false}]'";
205 1
        $this->assertContains($expected, $html);
206
207
        // This is only set automatically with layers
208 1
        $expected = 'data-enableautocentrezoom=false';
209 1
        $this->assertContains($expected, $html);
210 1
    }
211
212 1 View Code Duplication
    public function testBasicMapWithLayer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
    {
214 1
        $instance = $this->getInstance();
215 1
        $instance->Lat = 37.1;
216 1
        $instance->Lon = 28;
217 1
        $instance->Zoom = 12;
218 1
        $instance->MapPinEdited = true;
219 1
        $instance->write();
220 1
        $this->addLayerToInstance($instance);
221 1
        $html = $instance->BasicMap()->forTemplate();
222
223 1
        $expected = "data-centre='{\"lat\":37.1,\"lng\":28}'";
224 1
        $this->assertContains($expected, $html);
225 1
        $expected = "data-mapmarkers='[{\"latitude\":37.1,\"longitude\":28,\"html\":\"MEMBER: \",\"category\":\"default\",\"icon\":false}]'";
226 1
        $this->assertContains($expected, $html);
227
228
        // This is only set automatically with layers
229 1
        $expected = 'data-enableautocentrezoom=1';
230 1
        $this->assertContains($expected, $html);
231 1
    }
232
233 1
    public function testGetMapField()
234
    {
235 1
        $instance = $this->getInstance();
236 1
        $this->Lat = 37.1;
237 1
        $this->Lon = 28;
238 1
        $this->Zoom = 12;
239 1
        $mapField = $instance->getMapField();
240 1
        $this->assertInstanceOf('LatLongField', $mapField);
241 1
    }
242
243 1
    public function testUseCompressedAssets()
244
    {
245 1
        $original = Config::inst()->get('Mappable', 'use_compressed_assets');
246
247 1
        $instance = $this->getInstance();
248 1
        Config::inst()->update('Mappable', 'use_compressed_assets', false);
249 1
        $this->assertFalse($instance->UseCompressedAssets());
250
251 1
        $instance = $this->getInstance();
252 1
        Config::inst()->update('Mappable', 'use_compressed_assets', true);
253 1
        $this->assertTrue($instance->UseCompressedAssets());
254
255 1
        Config::inst()->update('Mappable', 'use_compressed_assets', $original);
256 1
    }
257
258 19
    private function getInstance()
259
    {
260 19
        $instance = new Member();
261
262 19
        return $instance;
263
    }
264
265
    /*
266
    Add a map layer to an existing instance
267
     */
268 2
    private function addLayerToInstance(&$instance)
269
    {
270
        // Create a layer
271 2
        $kmlFile = new File(array('Name' => 'example.kml', 'Filename' => 'mappable/tests/kml/example.kml'));
272 2
        $layer = new MapLayer();
273 2
        $layer->Title = 'Example KML Layer';
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<MapLayer>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
274 2
        $layer->KmlFile = $kmlFile;
0 ignored issues
show
Documentation introduced by
The property KmlFile does not exist on object<MapLayer>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
275 2
        $layer->write();
276
277
        // add the layer to the Member object
278 2
        $layers = $instance->MapLayers();
279 2
        $layers->add($layer);
280 2
    }
281
282 1 View Code Duplication
    private function addMapPinToInstance(&$instance)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
283
    {
284
        // Create a pin
285 1
        $copied = copy('mappable/tests/images/mapicontest.png', 'assets/mapicontest.png');
286 1
        $this->assertTrue($copied);
287 1
        $imageFile = new Image(array('Name' => 'mapicontest.png', 'Filename' => 'assets/mapicontest.png'));
288 1
        $imageFile->write();
289 1
        $instance->MapPinIconID = $imageFile->ID;
290 1
        $instance->write();
291 1
    }
292
293
    /**
294
     * @param int $lat
295
     * @param int $lon
296
     */
297 1
    private function showMapPinEdited(&$instance, $lat, $lon)
298
    {
299 1
        $instance->Lat = $lat;
300 1
        $instance->Long = $lon;
301 1
        $instance->write();
302 1
        $this->assertTrue($instance->MapPinEdited);
303 1
    }
304
}
305