Skip to content

Commit 158e7c1

Browse files
Fix spatial distance error messages and improve validation for geometry types
1 parent 46f1a46 commit 158e7c1

3 files changed

Lines changed: 22 additions & 23 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ protected function handleDistanceSpatialQueries(string $spatialAttributeType, Qu
13931393
$wktType = $this->getSpatialTypeFromWKT($wkt);
13941394
$attrType = strtolower($spatialAttributeType);
13951395
if ($wktType != Database::VAR_POINT || $attrType != Database::VAR_POINT) {
1396-
throw new DatabaseException('Distance in meters is not supported between '.$attrType . ' and '. $wkt);
1396+
throw new DatabaseException('Distance in meters is not supported between '.$attrType . ' and '. $wktType);
13971397
}
13981398
return "ST_DISTANCE_SPHERE({$alias}.{$attribute}, ST_GeomFromText(:{$placeholder}_0), 6371000) {$operator} :{$placeholder}_1";
13991399
}

src/Database/Adapter/SQL.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,6 +2662,9 @@ public function getSpatialTypeFromWKT(string $wkt): string
26622662
{
26632663
$wkt = trim($wkt);
26642664
$pos = strpos($wkt, '(');
2665+
if ($pos === false) {
2666+
throw new Exception("Not a valid spatialtype");
2667+
}
26652668
return strtolower(trim(substr($wkt, 0, $pos)));
26662669
}
26672670
}

tests/e2e/Adapter/Scopes/SpatialTests.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,7 +2289,7 @@ public function testSpatialDistanceInMeterForMultiDimensionGeometry(): void
22892289
}
22902290
}
22912291

2292-
public function testSpatialDistanceInMeterError()
2292+
public function testSpatialDistanceInMeterError(): void
22932293
{
22942294
/** @var Database $database */
22952295
$database = static::getDatabase();
@@ -2316,37 +2316,33 @@ public function testSpatialDistanceInMeterError()
23162316
]));
23172317
$this->assertInstanceOf(Document::class, $doc);
23182318

2319-
// Invalid geometry pairs (all combinations except POINT vs POINT)
2319+
// Invalid geometry pairs
23202320
$cases = [
2321-
// Point compared against wrong types
2322-
['attr' => 'line', 'geom' => [0.002, 0.0], 'msg' => 'Point vs LineString'],
2323-
['attr' => 'poly', 'geom' => [0.002, 0.0], 'msg' => 'Point vs Polygon'],
2324-
2325-
// LineString compared against wrong types
2326-
['attr' => 'loc', 'geom' => [[0.0, 0.0], [0.001, 0.001]], 'msg' => 'LineString vs Point'],
2327-
['attr' => 'poly', 'geom' => [[0.0, 0.0], [0.001, 0.001]], 'msg' => 'LineString vs Polygon'],
2328-
2329-
// Polygon compared against wrong types
2330-
['attr' => 'loc', 'geom' => [[[0.0, 0.0], [0.001, 0.0], [0.001, 0.001], [0.0, 0.0]]], 'msg' => 'Polygon vs Point'],
2331-
['attr' => 'line', 'geom' => [[[0.0, 0.0], [0.001, 0.0], [0.001, 0.001], [0.0, 0.0]]], 'msg' => 'Polygon vs LineString'],
2332-
2333-
// Polygon vs Polygon (still invalid for "meters")
2334-
['attr' => 'poly', 'geom' => [[[0.002, -0.001], [0.002, 0.001], [0.004, 0.001], [0.002, -0.001]]], 'msg' => 'Polygon vs Polygon'],
2335-
2336-
// LineString vs LineString (invalid for "meters")
2337-
['attr' => 'line', 'geom' => [[0.002, 0.0], [0.003, 0.0]], 'msg' => 'LineString vs LineString'],
2321+
['attr' => 'line', 'geom' => [0.002, 0.0], 'expected' => ['linestring', 'point']],
2322+
['attr' => 'poly', 'geom' => [0.002, 0.0], 'expected' => ['polygon', 'point']],
2323+
['attr' => 'loc', 'geom' => [[0.0, 0.0], [0.001, 0.001]], 'expected' => ['point', 'linestring']],
2324+
['attr' => 'poly', 'geom' => [[0.0, 0.0], [0.001, 0.001]], 'expected' => ['polygon', 'linestring']],
2325+
['attr' => 'loc', 'geom' => [[[0.0, 0.0], [0.001, 0.0], [0.001, 0.001], [0.0, 0.0]]], 'expected' => ['point', 'polygon']],
2326+
['attr' => 'line', 'geom' => [[[0.0, 0.0], [0.001, 0.0], [0.001, 0.001], [0.0, 0.0]]], 'expected' => ['linestring', 'polygon']],
2327+
['attr' => 'poly', 'geom' => [[[0.002, -0.001], [0.002, 0.001], [0.004, 0.001], [0.002, -0.001]]], 'expected' => ['polygon', 'polygon']],
2328+
['attr' => 'line', 'geom' => [[0.002, 0.0], [0.003, 0.0]], 'expected' => ['linestring', 'linestring']],
23382329
];
23392330

23402331
foreach ($cases as $case) {
23412332
try {
23422333
$database->find($collection, [
23432334
Query::distanceLessThan($case['attr'], $case['geom'], 1000, true)
23442335
]);
2345-
$this->fail('Expected Exception not thrown for ' . $case['msg']);
2336+
$this->fail('Expected Exception not thrown for ' . implode(' vs ', $case['expected']));
23462337
} catch (\Exception $e) {
2347-
$this->assertInstanceOf(\Exception::class, $e, $case['msg']);
2338+
$this->assertInstanceOf(\Exception::class, $e);
2339+
2340+
// Validate exception message contains correct type names
2341+
$msg = strtolower($e->getMessage());
2342+
var_dump($msg);
2343+
$this->assertStringContainsString($case['expected'][0], $msg, 'Attr type missing in exception');
2344+
$this->assertStringContainsString($case['expected'][1], $msg, 'Geom type missing in exception');
23482345
}
23492346
}
23502347
}
2351-
23522348
}

0 commit comments

Comments
 (0)