Skip to content

Commit f6aaa1f

Browse files
author
Matan Yadaev
committed
clean tests
1 parent 30cb65a commit f6aaa1f

File tree

2 files changed

+27
-39
lines changed

2 files changed

+27
-39
lines changed

src/SpatialBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public function whereSrid(
318318

319319
protected function toExpressionString(ExpressionContract|Geometry|string $geometryOrColumnOrExpression): string
320320
{
321-
$grammar = $this->getQuery()->getGrammar();
321+
$grammar = $this->getGrammar();
322322

323323
if ($geometryOrColumnOrExpression instanceof ExpressionContract) {
324324
$expression = $geometryOrColumnOrExpression;

tests/SpatialBuilderTest.php

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,7 @@
1010

1111
uses(DatabaseMigrations::class);
1212

13-
it('calculates distance between column and column', function (): void {
14-
TestPlace::factory()->create(['point' => new Point(0, 0, 4326)]);
15-
16-
/** @var TestPlace $testPlaceWithDistance */
17-
$testPlaceWithDistance = TestPlace::query()
18-
->withDistance('point', 'point')
19-
->firstOrFail();
20-
21-
expect($testPlaceWithDistance->distance)->toBe(0.0);
22-
});
23-
24-
it('calculates distance between column and geometry', function (): void {
13+
it('calculates distance', function (): void {
2514
TestPlace::factory()->create(['point' => new Point(0, 0, 4326)]);
2615

2716
/** @var TestPlace $testPlaceWithDistance */
@@ -32,7 +21,7 @@
3221
expect($testPlaceWithDistance->distance)->toBe(156897.79947260793);
3322
})->skip(fn () => ! (new AxisOrder)->supported(DB::connection()));
3423

35-
it('calculates distance between column and geometry - without axis-order', function (): void {
24+
it('calculates distance - without axis-order', function (): void {
3625
TestPlace::factory()->create(['point' => new Point(0, 0, 4326)]);
3726

3827
/** @var TestPlace $testPlaceWithDistance */
@@ -121,18 +110,7 @@
121110
expect($testPlacesOrderedByDistance[0]->id)->toBe($fartherTestPlace->id);
122111
});
123112

124-
it('calculates distance sphere column and column', function (): void {
125-
TestPlace::factory()->create(['point' => new Point(0, 0, 4326)]);
126-
127-
/** @var TestPlace $testPlaceWithDistance */
128-
$testPlaceWithDistance = TestPlace::query()
129-
->withDistanceSphere('point', 'point')
130-
->firstOrFail();
131-
132-
expect($testPlaceWithDistance->distance)->toBe(0.0);
133-
});
134-
135-
it('calculates distance sphere column and geometry', function (): void {
113+
it('calculates distance sphere', function (): void {
136114
TestPlace::factory()->create(['point' => new Point(0, 0, 4326)]);
137115

138116
/** @var TestPlace $testPlaceWithDistance */
@@ -143,7 +121,7 @@
143121
expect($testPlaceWithDistance->distance)->toBe(157249.59776850493);
144122
})->skip(fn () =>! (new AxisOrder)->supported(DB::connection()));
145123

146-
it('calculates distance sphere column and geometry - without axis-order', function (): void {
124+
it('calculates distance sphere - without axis-order', function (): void {
147125
TestPlace::factory()->create(['point' => new Point(0, 0, 4326)]);
148126

149127
/** @var TestPlace $testPlaceWithDistance */
@@ -393,7 +371,18 @@
393371
expect($testPlaces[0]->point)->toEqual($point1);
394372
});
395373

396-
it('uses spatial function on column that contains its table name', function (): void {
374+
it('uses spatial function with column', function (): void {
375+
TestPlace::factory()->create(['point' => new Point(0, 0, 4326)]);
376+
377+
/** @var TestPlace $testPlaceWithDistance */
378+
$testPlaceWithDistance = TestPlace::query()
379+
->withDistance('point', 'point')
380+
->firstOrFail();
381+
382+
expect($testPlaceWithDistance->distance)->toBe(0.0);
383+
});
384+
385+
it('uses spatial function with column that contains table name', function (): void {
397386
TestPlace::factory()->create(['point' => new Point(0, 0, 4326)]);
398387

399388
/** @var TestPlace $testPlaceWithDistance */
@@ -404,23 +393,23 @@
404393
expect($testPlaceWithDistance->distance)->toBe(0.0);
405394
});
406395

407-
it('uses spatial function on raw expression', function (): void {
396+
it('uses spatial function with expression', function (): void {
408397
$polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}');
409398
TestPlace::factory()->create([
399+
'polygon' => $polygon,
410400
'longitude' => 0,
411401
'latitude' => 0,
412-
'polygon' => $polygon,
413402
]);
414403

415404
/** @var TestPlace $testPlaceWithDistance */
416405
$testPlaceWithDistance = TestPlace::query()
417-
->whereWithin(DB::raw('POINT(longitude, latitude)'), 'polygon')
406+
->whereWithin(DB::raw('POINT(longitude, latitude)'), DB::raw('polygon'))
418407
->firstOrFail();
419408

420409
expect($testPlaceWithDistance)->not()->toBeNull();
421410
});
422411

423-
it('toExpressionString can handle Expression', function (): void {
412+
it('toExpressionString can handle a Expression input', function (): void {
424413
$spatialBuilder = TestPlace::query();
425414
$toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString');
426415

@@ -429,21 +418,20 @@
429418
expect($result)->toBe('POINT(longitude, latitude)');
430419
});
431420

432-
it('toExpressionString can handle Geometry', function (): void {
433-
$polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}');
434-
421+
it('toExpressionString can handle a Geometry input', function (): void {
435422
$spatialBuilder = TestPlace::query();
436-
$grammar = $spatialBuilder->getQuery()->getGrammar();
437423
$toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString');
424+
$polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}');
438425

439426
$result = $toExpressionStringMethod->invoke($spatialBuilder, $polygon);
440427

441-
$sqlSerializedPolygon = $polygon->toSqlExpression($spatialBuilder->getConnection())->getValue($grammar);
442-
428+
$grammar = $spatialBuilder->getGrammar();
429+
$connection = $spatialBuilder->getConnection();
430+
$sqlSerializedPolygon = $polygon->toSqlExpression($connection)->getValue($grammar);
443431
expect($result)->toBe($sqlSerializedPolygon);
444432
});
445433

446-
it('toExpressionString can handle string', function (): void {
434+
it('toExpressionString can handle a string input', function (): void {
447435
$spatialBuilder = TestPlace::query();
448436
$toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString');
449437

0 commit comments

Comments
 (0)