-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathHasSpatialTest.php
93 lines (76 loc) · 3.5 KB
/
HasSpatialTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace TarfinLabs\LaravelSpatial\Tests;
use TarfinLabs\LaravelSpatial\Tests\TestModels\Address;
use TarfinLabs\LaravelSpatial\Types\Point;
class HasSpatialTest extends TestCase
{
/** @test */
public function it_generates_sql_query_for_selectDistanceTo_scope(): void
{
// Arrange
$address = new Address();
$castedAttr = $address->getLocationCastedAttributes()->first();
// Act
$query = $address->selectDistanceTo($castedAttr, new Point());
// Assert
$this->assertEquals(
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr, ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) as distance from `addresses`",
actual: $query->toSql()
);
}
/** @test */
public function it_generates_sql_query_for_withinDistanceTo_scope(): void
{
// 1. Arrange
$address = new Address();
$castedAttr = $address->getLocationCastedAttributes()->first();
// 2. Act
$query = $address->withinDistanceTo($castedAttr, new Point(), 10000);
// 3. Assert
$this->assertEquals(
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` where ST_AsText(location) != ? and ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) <= ?",
actual: $query->toSql()
);
}
/** @test */
public function it_generates_sql_query_for_orderByDistanceTo_scope(): void
{
// 1. Arrange
$address = new Address();
$castedAttr = $address->getLocationCastedAttributes()->first();
// 2. Act
$queryForAsc = $address->orderByDistanceTo($castedAttr, new Point());
$queryForDesc = $address->orderByDistanceTo($castedAttr, new Point(), 'desc');
// 3. Assert
$this->assertEquals(
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` order by ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) asc",
actual: $queryForAsc->toSql()
);
$this->assertEquals(
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` order by ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) desc",
actual: $queryForDesc->toSql()
);
}
/** @test */
public function it_generates_sql_query_for_location_casted_attributes(): void
{
// 1. Arrange
$address = new Address();
$castedAttr = $address->getLocationCastedAttributes()->first();
// 2. Act & Assert
$this->assertEquals(
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses`",
actual: $address->query()->toSql()
);
}
/** @test */
public function it_returns_location_casted_attributes(): void
{
// 1. Arrange
$address = new Address();
// 2. Act
$locationCastedAttributres = $address->getLocationCastedAttributes();
// 3. Assert
$this->assertEquals(collect(['location']), $locationCastedAttributres);
}
}