Skip to content

Commit 8e5c421

Browse files
committed
Add more tests for improved coverage
- GeoCoordinate: comprehensive tests for all methods - Calculator: exception paths, GeoCoordinate support, blur with level 0 - GeoCalculator: empty array exception path - GeoapifyProvider: named colors, hex colors, invalid size, marker options
1 parent f7577eb commit 8e5c421

File tree

3 files changed

+166
-3
lines changed

3 files changed

+166
-3
lines changed

tests/TestCase/Geocoder/CalculatorTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Geo\Test\TestCase\Geocoder;
44

55
use Cake\TestSuite\TestCase;
6+
use Geo\Exception\CalculatorException;
67
use Geo\Geocoder\Calculator;
8+
use Geo\Geocoder\GeoCoordinate;
79

810
class CalculatorTest extends TestCase {
911

@@ -93,6 +95,60 @@ public function testConvert() {
9395
}
9496
}
9597

98+
/**
99+
* @return void
100+
*/
101+
public function testBlurWithZeroLevel(): void {
102+
$coordinate = 48.1391;
103+
$result = Calculator::blur($coordinate, 0);
104+
$this->assertSame($coordinate, $result);
105+
}
106+
107+
/**
108+
* @return void
109+
*/
110+
public function testConvertInvalidFromUnit(): void {
111+
$this->expectException(CalculatorException::class);
112+
$this->expectExceptionMessage('Invalid Unit');
113+
114+
$this->Calculator->convert(100, 'INVALID', 'K');
115+
}
116+
117+
/**
118+
* @return void
119+
*/
120+
public function testConvertInvalidToUnit(): void {
121+
$this->expectException(CalculatorException::class);
122+
$this->expectExceptionMessage('Invalid Unit');
123+
124+
$this->Calculator->convert(100, 'K', 'INVALID');
125+
}
126+
127+
/**
128+
* @return void
129+
*/
130+
public function testDistanceInvalidUnit(): void {
131+
$this->expectException(CalculatorException::class);
132+
$this->expectExceptionMessage('Invalid Unit: INVALID');
133+
134+
$pointX = ['lat' => 48.1391, 'lng' => 11.5802];
135+
$pointY = ['lat' => 48.8934, 'lng' => 8.70492];
136+
$this->Calculator->distance($pointX, $pointY, 'INVALID');
137+
}
138+
139+
/**
140+
* @return void
141+
*/
142+
public function testCalculateDistanceWithGeoCoordinates(): void {
143+
$pointX = new GeoCoordinate(48.1391, 11.5802);
144+
$pointY = new GeoCoordinate(48.8934, 8.70492);
145+
146+
$result = Calculator::calculateDistance($pointX, $pointY);
147+
148+
$this->assertGreaterThan(140, $result);
149+
$this->assertLessThan(150, $result);
150+
}
151+
96152
/**
97153
* @return void
98154
*/

tests/TestCase/Geocoder/GeoCalculatorTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@
55
use Cake\TestSuite\TestCase;
66
use Geo\Geocoder\GeoCalculator;
77
use Geo\Geocoder\GeoCoordinate;
8+
use RuntimeException;
89

910
class GeoCalculatorTest extends TestCase {
1011

1112
/**
12-
* @var \Geocoder\Provider\Provider
13+
* @return void
1314
*/
14-
protected $Geocoder;
15+
public function testGetCentralGeoCoordinateEmpty(): void {
16+
$this->expectException(RuntimeException::class);
17+
$this->expectExceptionMessage('No geo coordinates provided');
18+
19+
GeoCalculator::getCentralGeoCoordinate([]);
20+
}
1521

1622
/**
1723
* @return void
1824
*/
19-
public function testGetCentralGeoCoordinate() {
25+
public function testGetCentralGeoCoordinate(): void {
2026
$coordinates = [
2127
new GeoCoordinate(48.1, 17.2),
2228
];

tests/TestCase/StaticMap/Provider/GeoapifyProviderTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,105 @@ public function testBuildUrlAutoCalculatesBoundsWithMarkers(): void {
263263
$this->assertStringContainsString('marker=', $url);
264264
}
265265

266+
/**
267+
* Test with various named colors.
268+
*
269+
* @return void
270+
*/
271+
public function testBuildUrlWithNamedColors(): void {
272+
$url = $this->provider->buildUrl(
273+
['lat' => 48.2082, 'lng' => 16.3738],
274+
[
275+
['lat' => 48.2082, 'lng' => 16.3738, 'color' => 'green'],
276+
['lat' => 48.195, 'lng' => 16.37, 'color' => 'yellow'],
277+
['lat' => 48.21, 'lng' => 16.38, 'color' => 'purple'],
278+
],
279+
);
280+
281+
$this->assertStringContainsString('color:%2300ff00', $url); // green
282+
$this->assertStringContainsString('color:%23ffff00', $url); // yellow
283+
$this->assertStringContainsString('color:%23800080', $url); // purple
284+
}
285+
286+
/**
287+
* Test with hex colors with # prefix.
288+
*
289+
* @return void
290+
*/
291+
public function testBuildUrlWithHexColors(): void {
292+
$url = $this->provider->buildUrl(
293+
['lat' => 48.2082, 'lng' => 16.3738],
294+
[
295+
['lat' => 48.2082, 'lng' => 16.3738, 'color' => '#FF5500'],
296+
],
297+
);
298+
299+
$this->assertStringContainsString('color:%23ff5500', $url);
300+
}
301+
302+
/**
303+
* Test with 0x prefix colors.
304+
*
305+
* @return void
306+
*/
307+
public function testBuildUrlWith0xColors(): void {
308+
$url = $this->provider->buildUrl(
309+
['lat' => 48.2082, 'lng' => 16.3738],
310+
[
311+
['lat' => 48.2082, 'lng' => 16.3738, 'color' => '0xABCDEF'],
312+
],
313+
);
314+
315+
$this->assertStringContainsString('color:%23abcdef', $url);
316+
}
317+
318+
/**
319+
* Test invalid size format.
320+
*
321+
* @return void
322+
*/
323+
public function testBuildUrlWithInvalidSize(): void {
324+
$url = $this->provider->buildUrl([
325+
'lat' => 48.2082,
326+
'lng' => 16.3738,
327+
'size' => 'invalid',
328+
]);
329+
330+
// Should fallback to defaults
331+
$this->assertStringContainsString('width=400', $url);
332+
$this->assertStringContainsString('height=300', $url);
333+
}
334+
335+
/**
336+
* Test marker with custom icon type.
337+
*
338+
* @return void
339+
*/
340+
public function testBuildUrlWithMarkerIcon(): void {
341+
$url = $this->provider->buildUrl(
342+
['lat' => 48.2082, 'lng' => 16.3738],
343+
[
344+
['lat' => 48.2082, 'lng' => 16.3738, 'icon' => 'awesome'],
345+
],
346+
);
347+
348+
$this->assertStringContainsString('type:awesome', $url);
349+
}
350+
351+
/**
352+
* Test marker with custom size.
353+
*
354+
* @return void
355+
*/
356+
public function testBuildUrlWithMarkerSize(): void {
357+
$url = $this->provider->buildUrl(
358+
['lat' => 48.2082, 'lng' => 16.3738],
359+
[
360+
['lat' => 48.2082, 'lng' => 16.3738, 'size' => 'large'],
361+
],
362+
);
363+
364+
$this->assertStringContainsString('size:large', $url);
365+
}
366+
266367
}

0 commit comments

Comments
 (0)