Completed
Branch AUTOMATED_TESTING (fff10f)
by Gordon
11:14
created

MapAPITest::testSetKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
class MapAPITest extends SapphireTest {
4
5
	public function setUpOnce() {
6
		$this->requiredExtensions = array(
7
			'Member' => array('MapExtension')
8
		);
9
		parent::setupOnce();
10
	}
11
12
	public function setUp() {
13
		MapUtil::reset();
14
		parent::setUp();
15
	}
16
17
	public function testSetClusterer() {
18
		$map = $this->getMap();
19
		$map->setClusterer(true);
20
		$html = $map->forTemplate();
21
		$this->assertContains('data-clusterergridsize=50', $html);
22
		$this->assertContains('data-clusterermaxzoom=17', $html);
23
		$this->assertContains('data-useclusterer=1', $html);
24
25
		$map = $this->getMap();
26
		$map->setClusterer(true, 60, 14);
27
		$html = $map->forTemplate();
28
		$this->assertContains('data-clusterergridsize=60', $html);
29
		$this->assertContains('data-clusterermaxzoom=14', $html);
30
		$this->assertContains('data-useclusterer=1', $html);
31
32
		$map = $this->getMap();
33
		$map->setClusterer(false);
34
		$html = $map->forTemplate();
35
		$this->assertContains('data-useclusterer=false', $html);
36
		$this->assertContains('data-clusterergridsize=50', $html);
37
		$this->assertContains('data-clusterermaxzoom=17', $html);
38
	}
39
40
	/*
41
	Toggle as to whether or not to include a style= attribute with width/height
42
	 */
43
	public function testSetShowInlineMapDivStyle() {
44
		$map = $this->getMap();
45
		$map->setShowInlineMapDivStyle(true);
46
		$html = $map->forTemplate();
47
		$expected = 'style="width:100%; height: 400px;"';
48
		$this->assertContains($expected, $html);
49
50
		$map->setShowInlineMapDivStyle(false);
51
		$html = $map->forTemplate();
52
		$this->assertNotContains($expected, $html);
53
	}
54
55
	public function testSetAdditionalCSSClasses() {
56
		$map = $this->getMap();
57
		$map->setAdditionalCSSClasses('bigMap shadowMap');
58
		$html = $map->forTemplate();
59
		$expected = 'class="bigMap shadowMap mappable"';
60
		$this->assertContains($expected, $html);
61
		$map->setAdditionalCSSClasses('bigMap shadowMap');
62
	}
63
64
65
	public function testSetMapStyle() {
66
		$style = <<<STYLE
67
[{
68
	"featureType": "landscape",
69
	"stylers": [{
70
		"hue": "#FFBB00"
71
	}, {
72
		"saturation": 43.400000000000006
73
	}, {
74
		"lightness": 37.599999999999994
75
	}, {
76
		"gamma": 1
77
	}]
78
}]
79
STYLE;
80
		$map = $this->getMap();
81
		$map->setMapStyle($style);
82
		$html = $map->forTemplate()->getValue();
83
		$expected = <<<HTML
84
85
86
<div id="google_map_1" data-google-map-lang="en"  style="width:100%; height: 400px;"
87
 class=" mappable"
88
data-map
89
data-centre='{"lat":48.856614,"lng":2.3522219}'
90
data-zoom=9
91
data-maptype='road'
92
data-allowfullscreen='1'
93
data-clusterergridsize=50,
94
data-clusterermaxzoom=17,
95
data-enableautocentrezoom=false
96
data-enablewindowzoom=false
97
data-infowindowzoom=13
98
data-mapmarkers='[]'
99
data-defaulthidemarker=false
100
data-lines='[]'
101
data-kmlfiles='[]'
102
data-mapstyles='[{
103
	"featureType": "landscape",
104
	"stylers": [{
105
		"hue": "#FFBB00"
106
	}, {
107
		"saturation": 43.400000000000006
108
	}, {
109
		"lightness": 37.599999999999994
110
	}, {
111
		"gamma": 1
112
	}]
113
}]'
114
data-useclusterer=false
115
>
116
</div>
117
118
HTML;
119
		$this->assertEquals($expected, $html);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MapAPITest>.

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...
120
		$map->setMapStyle(null);
121
	}
122
123
	public function testSetDivId() {
124
		$map = $this->getMap();
125
		$map->setDivId('mymapid');
126
		$html = $map->forTemplate();
127
		$expected = '<div id="mymapid" data-google-map-lang="en"  style=';
128
		$this->assertContains($expected, $html);
129
	}
130
131
	public function testSetSize() {
132
		$map = $this->getMap();
133
		$map->setSize('432px', '1234px');
134
		$html = $map->forTemplate();
135
		$this->assertContains('style="width:432px; height: 1234px;"', $html);
136
	}
137
138
	public function testSetLang() {
139
		Config::inst()->update('Mappable', 'language', 'fr');
140
		$map = $this->getMap();
141
		$html = $map->forTemplate();
142
		$this->assertContains(
143
			'<div id="google_map_1" data-google-map-lang="fr" ',
144
			$html
145
		);
146
	}
147
148
149
	public function testSetZoom() {
150
		$map = $this->getMap();
151
		$map->setZoom(4);
152
		$html = $map->forTemplate();
153
		$this->assertContains('data-zoom=4', $html);
154
		$map->setZoom(12);
155
		$html = $map->forTemplate();
156
		$this->assertContains('data-zoom=12', $html);
157
	}
158
159
	public function testSetInfoWindowZoom() {
160
		$map = $this->getMap();
161
		$map->setInfoWindowZoom(4);
162
		$html = $map->forTemplate();
163
		$this->assertContains('data-infowindowzoom=4', $html);
164
		$map->setInfoWindowZoom(12);
165
		$html = $map->forTemplate();
166
		$this->assertContains('data-infowindowzoom=12', $html);
167
168
	}
169
170
	public function testSetEnableWindowZoom() {
171
		$map = $this->getMap();
172
		$map->setEnableWindowZoom(false);
173
		$html = $map->forTemplate();
174
		$this->assertContains('data-enablewindowzoom=false', $html);
175
		$map->setEnableWindowZoom(true);
176
		$html = $map->forTemplate();
177
		$this->assertContains('data-enablewindowzoom=1', $html);
178
	}
179
180
	public function testSetEnableAutomaticCenterZoom() {
181
		$map = $this->getMap();
182
		$map->setEnableAutomaticCenterZoom(true);
183
		$html = $map->forTemplate();
184
		$this->assertContains('data-enableautocentrezoom=1', $html);
185
	}
186
187
	public function testSetNoLocation() {
188
		$map = $this->getMap();
189
		$html = $map->forTemplate();
190
		$this->assertContains(
191
			'data-centre=\'{"lat":48.856614,"lng":2.3522219}\'',
192
			$html
193
		);
194
	}
195
196
	/**
197
	 * setCentre is mis-named, as the method expects text for a geocoder
198
	 */
199
	public function testSetCenter() {
200
		$map = $this->getMap();
201
		$map->setCenter('Klong Tan, Bangkok, Thailand');
202
		$html = $map->forTemplate();
203
204
		//coordinates of Klong Tan in Bangkok
205
		$expected = 'data-centre=\'{"lat":13.7243075,"lng":100.5718086}';
206
		$this->assertContains($expected, $html);
207
		$map->setCenter('Paris, France');
208
	}
209
210
211
	public function testSetLatLongCenter() {
212
		$map = $this->getMap();
213
		$llc = array('lat' => -23.714, 'lng' => 47.419);
214
		$map->setLatLongCenter($llc);
215
		$html = $map->forTemplate();
216
		$expected = "data-centre='{\"lat\":-23.714,\"lng\":47.419}'";
217
		$this->assertContains($expected, $html);
218
219
		// now test error conditions
220
		try {
221
			$map->setLatLongCenter('This is not a coordinate');
222
			$this->fail('Should not be able to set coordinate as text');
223
		} catch (InvalidArgumentException $e) {
224
			$message = $e->getMessage();
225
			$this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MapAPITest>.

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...
226
				'Center must be an associative array containing lat,lng',
227
				$message
228
			);
229
		}
230
231
		try {
232
			$badKeys = array('lat' => 47.2, 'wibble' => 76.10);
233
			$map->setLatLongCenter($badKeys);
234
			$this->fail('Should not be able to set coordinate as text');
235
		} catch (InvalidArgumentException $e) {
236
			$message = $e->getMessage();
237
238
			$this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MapAPITest>.

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...
239
				'Keys provided must be lat, lng',
240
				$message
241
			);
242
		}
243
244
245
	}
246
247
248
	public function testSetMapType() {
249
		$map = $this->getMap();
250
251
		$mapTypes = array(
252
			'road' => 'road',
253
			'satellite' => 'satellite',
254
			'hybrid' => 'hybrid',
255
			'terrain' => 'terrain',
256
			'google.maps.MapTypeId.ROADMAP' => 'road',
257
			'google.maps.MapTypeId.SATELLITE' => 'satellite',
258
			'google.maps.MapTypeId.G_HYBRID_MAP' => 'hybrid',
259
			'google.maps.MapTypeId.G_PHYSICAL_MAP' => 'terrain',
260
			'custom_layer' => 'custom_layer'
261
		);
262
263
		foreach (array_keys($mapTypes) as $mapType) {
264
			$map->setMapType($mapType);
265
			$expected = "data-maptype='".$mapTypes[$mapType]."'";
266
			$html = $map->forTemplate();
267
			$this->assertContains($expected, $html);
268
		}
269
	}
270
271
272
	public function testSetAllowFullScreen() {
273
		$map = $this->getMap();
274
		$map->setAllowFullScreen(false);
275
		$html = $map->forTemplate();
276
277
		$this->assertContains("data-allowfullscreen='false'", $html);
278
279
		$map->setAllowFullScreen(true);
280
		$html = $map->forTemplate();
281
		$this->assertContains("data-allowfullscreen='1'", $html);
282
283
		// deal with the null calse
284
		$map->setAllowFullScreen(null);
285
		$html = $map->forTemplate();
286
		$expected = Config::inst()->get('Mappable', 'allow_full_screen');
287
		$this->assertContains("data-allowfullscreen='{$expected}'", $html);
288
	}
289
290
	public function testMapWithMarkers() {
291
		$config = Config::inst();
0 ignored issues
show
Unused Code introduced by
$config 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...
292
293
		$map = $this->getMapMultipleItems();
294
		$html = $map->forTemplate();
295
		$expected = 'data-mapmarkers=\'[{"latitude":23,"longitude":78,"html":"'
296
				  . 'MEMBER: Fred Bloggs","category":"default","icon":false},{"latitude'
297
				  . '":-12,"longitude":42.1,"html":"MEMBER: Kane Williamson","category"'
298
				  . ':"default","icon":false}]\'';
299
		$this->assertContains($expected, $html);
300
	}
301
302
303
	public function testMapWithMarkersDifferentCategory() {
304
		$this->markTestSkipped('TODO');
305
	}
306
307
308
	public function testSetDefaultHideMarker() {
309
		$map = $this->getMapMultipleItems();
310
		$map->setDefaultHideMarker(false);
311
		$html = $map->forTemplate();
312
		$this->assertContains(
313
			'data-defaulthidemarker=false',
314
			$html
315
		);
316
317
		$map = $this->getMapMultipleItems();
318
		$map->setDefaultHideMarker(true);
319
		$html = $map->forTemplate();
320
		$this->assertContains(
321
			'data-defaulthidemarker=1',
322
			$html
323
		);
324
	}
325
326
	public function testGetContent() {
327
		$map = $this->getMap();
328
		$filepath = 'file://'.Director::baseFolder()
329
				  . '/mappable/tests/kml/example.kml';
330
		$content = $map->getContent($filepath);
331
		$textHash = hash('ripemd160', $content);
332
		$fileHash = hash_file('ripemd160', $filepath);
333
		$this->assertEquals($fileHash, $textHash);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MapAPITest>.

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...
334
	}
335
336
	public function testGeocoding() {
337
		$map = $this->getMap();
338
		$location = $map->geocoding("Nonthaburi, Thailand");
339
		$expected = array(
340
			'lat' => 13.8621125,
341
			'lon' => 100.5143528,
342
    		'geocoded' => true
343
		);
344
		$this->assertEquals($expected, $location);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MapAPITest>.

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...
345
	}
346
347
	public function testGeocodingNoResultsFound() {
348
		$map = $this->getMap();
349
		$location = $map->geocoding("aasdfsafsfdsfasdf");
350
		$expected = array();
351
		$this->assertEquals($expected, $location);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MapAPITest>.

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...
352
	}
353
354
	public function testAddMarkerByAddress() {
355
		//$address, $content = '', $category = '', $icon = ''
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
356
		$map = $this->getMap();
357
		$map->addMarkerByAddress(
358
			'Koh Kred, Nonthaburi, Thailand',
359
			'Small island in the Chao Phraya river',
360
			'testing',
361
			'http://www.test.com/icon.png'
362
		);
363
		$html = $map->forTemplate();
364
		$expected = 'data-mapmarkers=\'[{"latitude":13.9114455,"longitude":100.4761897,"html":"Small island in the Chao Phraya river","category":"testing","icon":"http://www.test.com/icon.png"}]\'';
365
		$this->assertContains($expected, $html);
366
	}
367
368
369
	public function testAddArrayMarkerByCoords() {
370
		$map = $this->getMap();
371
372
		$markerArray = array();
373
		$marker1 = array(48.2, 27, 'Description marker 1', 'Marker Test', '');
374
		$marker2 = array(-12.2, 47, 'Description marker 2', 'Marker Test', '');
375
376
		array_push($markerArray, $marker1);
377
		array_push($markerArray, $marker2);
378
379
		$map->addArrayMarkerByCoords($markerArray);
380
		$html = $map->forTemplate();
381
		$expected = 'data-mapmarkers=\'[{"latitude":48.2,"longitude":27,"html":"Description marker 1","category":"","icon":""},{"latitude":-12.2,"longitude":47,"html":"Description marker 2","category":"","icon":""}]\'';
382
		$this->assertContains($expected, $html);
383
	}
384
385
	public function testAddMarkerByCoords() {
386
		$map = $this->getMap();
387
		$map->addMarkerByCoords(
388
			13.91,
389
			100.47,
390
			'Description of marker',
391
			'testing',
392
			'http://www.test.com/icon.png'
393
		);
394
		$html = $map->forTemplate();
395
		$expected =
396
		'data-mapmarkers=\'[{"latitude":13.91,"longitude":100.47,"html":"Description of marker","category":"testing","icon":"http://www.test.com/icon.png"}]';
397
398
		$this->assertContains($expected, $html);
399
	}
400
401
402
	public function testAddMarkerAsObject() {
403
		$map = $this->getMap();
404
		$member = new Member();
405
		$member->FirstName = 'Test';
406
		$member->Surname = 'User';
407
		$member->Lat = 24.2;
408
		$member->Lon = -40;
409
		$member->write();
410
		$params = array();
411
		$map->addMarkerAsObject(
412
			$member,
413
			$params
414
		);
415
416
		$html = $map->forTemplate();
417
		$expected = 'data-mapmarkers=\'[{"latitude":24.2,"longitude":-40,"html":"MEMBER: Test User","category":"default","icon":false}]\'';
418
		$this->assertContains($expected, $html);
419
	}
420
421
422
	public function testAddMarkerThatIsMappableAsObject() {
423
		$map = $this->getMap();
424
		$member = new MappableExampleClass();
425
		//$member->write();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% 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...
426
		$params = array();
427
		$map->addMarkerAsObject(
428
			$member,
429
			$params
430
		);
431
432
		$html = $map->forTemplate();
433
		$expected = 'data-mapmarkers=\'[{"latitude":13.4,"longitude":100.7,"html":"example content","category":"default","icon":null}]\'';
434
		$this->assertContains($expected, $html);
435
	}
436
437
438
	public function testConnectPoints() {
439
		$members = $this->getGeolocatedMembers();
440
		$member1 = $members->pop();
441
		$member2 = $members->pop();
442
		$map = $this->getMap();
443
		$map->connectPoints($member1, $member2);
444
		$html = $map->forTemplate();
445
		$expected = 'data-lines=\'[{"lat1":-12,"lon1":42.1,"lat2":23,"lon2":78,"color":"#FF3300"}]\'';
446
		$this->assertContains($expected, $html);
447
	}
448
449
450
	public function testAddKML() {
451
		$map = $this->getMap();
452
		$map->addKml('http://www.test.com/route1.kml');
453
		$map->addKml('http://www.test.com/route2.kml');
454
		$html = $map->forTemplate();
455
		$expected = 'data-kmlfiles=\'["http://www.test.com/route1.kml","http://www.test.com/route2.kml"]\'';
456
		$this->assertContains($expected, $html);
457
	}
458
459
460
	public function testAddLine() {
461
		$map = $this->getMap();
462
		$map->addLine(
463
			array(13, 101),
464
			array(13.2, 101.4),
465
			'#F32'
466
		);
467
468
		$map->addLine(
469
			array(13.2, 101.4),
470
			array(14.2, 99.8)
471
		);
472
473
		$html = $map->forTemplate();
474
		$expected = 'data-lines=\'[{"lat1":13,"lon1":101,"lat2":13.2,"lon2":101.4,"color":"#F32"},{"lat1":13.2,"lon1":101.4,"lat2":14.2,"lon2":99.8,"color":"#FF3300"}]\'';
475
		$this->assertContains($expected, $html);
476
	}
477
478
	/**
479
	 * This tests out a specific case of passing null for template values
480
	 */
481
	public function testProcessTemplate() {
482
		$map = $this->getMap();
483
		$html = $map->processTemplateHTML('Map', null);
484
		$expected = <<<HTML
485
486
487
<div id=""
488
data-map
489
data-centre=''
490
data-zoom=
491
data-maptype=''
492
data-allowfullscreen=''
493
data-clusterergridsize=,
494
data-clusterermaxzoom=,
495
data-enableautocentrezoom=
496
data-enablewindowzoom=
497
data-infowindowzoom=
498
data-mapmarkers=''
499
data-defaulthidemarker=
500
data-lines=''
501
data-kmlfiles=''
502
data-mapstyles=''
503
data-useclusterer=
504
>
505
</div>
506
507
HTML;
508
		$this->assertEquals($expected, $html->getValue());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MapAPITest>.

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...
509
	}
510
511
	private function getMap() {
512
		$instance = new Member();
513
		return $instance->getRenderableMap();
514
	}
515
516
	private function getMapMultipleItems() {
517
		$members = $this->getGeolocatedMembers();
518
		return $members->getRenderableMap();
519
	}
520
521
	private function getGeolocatedMembers() {
522
		$members = new ArrayList();
523
524
		$member1 = new Member();
525
		$member1->Lat = 23;
526
		$member1->Lon = 78;
527
		$member1->MapPinEdited = true;
528
		$member1->FirstName = 'Fred';
529
		$member1->Surname = 'Bloggs';
530
		$member1->write();
531
		$members->push($member1);
532
533
		$member2 = new Member();
534
		$member2->Lat = -12;
535
		$member2->Lon = 42.1;
536
		$member2->MapPinEdited = true;
537
		$member2->FirstName = 'Kane';
538
		$member2->Surname = 'Williamson';
539
		$member2->write();
540
		$members->push($member2);
541
542
		return $members;
543
	}
544
545
}
546
547
548
549
// basic implementation of Mappable interface for some of the tests
550
class MappableExampleClass extends ViewableData implements TestOnly, Mappable {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
551
552
	public function getMappableLatitude() {
553
		return 13.4;
554
	}
555
556
	public function getMappableLongitude() {
557
		return 100.7;
558
	}
559
560
	public function getMappableMapPin() {
561
		return null;
562
	}
563
564
	public function getMappableMapContent() {
565
		return 'example content';
566
	}
567
}
568