Skip to content

Commit fa76ace

Browse files
committed
Small test refactors
1 parent 4d13c49 commit fa76ace

File tree

3 files changed

+89
-52
lines changed

3 files changed

+89
-52
lines changed

tests/HasSpatialTest.php

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace TarfinLabs\LaravelSpatial\Tests;
44

5-
use Illuminate\Support\Collection;
65
use TarfinLabs\LaravelSpatial\Tests\TestModels\Address;
76
use TarfinLabs\LaravelSpatial\Types\Point;
87

98
class HasSpatialTest extends TestCase
109
{
11-
public function test_scopeSelectDistanceTo(): void
10+
/**
11+
* @test
12+
* @see
13+
*/
14+
public function it_generates_sql_query_for_selectDistanceTo_scope(): void
1215
{
1316
// Arrange
1417
$address = new Address();
@@ -18,58 +21,85 @@ public function test_scopeSelectDistanceTo(): void
1821
$query = $address->selectDistanceTo($castedAttr, new Point());
1922

2023
// Assert
21-
$this->assertEquals("select *, CONCAT(ST_AsText(addresses.{$castedAttr}), ',', ST_SRID(addresses.{$castedAttr})) as {$castedAttr}, ST_Distance(
22-
ST_SRID({$castedAttr}, ?),
24+
$this->assertEquals(
25+
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr, ST_Distance(
26+
ST_SRID($castedAttr, ?),
2327
ST_SRID(Point(?, ?), ?)
24-
) as distance from `addresses`", $query->toSql());
28+
) as distance from `addresses`",
29+
actual: $query->toSql()
30+
);
2531
}
2632

27-
public function test_scopeWithinDistanceTo(): void
33+
/**
34+
* @test
35+
* @see
36+
*/
37+
public function it_generates_sql_query_for_withinDistanceTo_scope(): void
2838
{
29-
// Arrange
39+
// 1. Arrange
3040
$address = new Address();
3141
$castedAttr = $address->getLocationCastedAttributes()->first();
3242

33-
// Act
43+
// 2. Act
3444
$query = $address->withinDistanceTo($castedAttr, new Point(), 10000);
3545

36-
// Assert
37-
$this->assertEquals("select *, CONCAT(ST_AsText(addresses.{$castedAttr}), ',', ST_SRID(addresses.{$castedAttr})) as {$castedAttr} from `addresses` where ST_Distance(
38-
ST_SRID({$castedAttr}, ?),
46+
// 3. Assert
47+
$this->assertEquals(
48+
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` where ST_Distance(
49+
ST_SRID($castedAttr, ?),
3950
ST_SRID(Point(?, ?), ?)
40-
) <= ?", $query->toSql());
51+
) <= ?",
52+
actual: $query->toSql()
53+
);
4154
}
4255

43-
public function test_scopeOrderByDistanceTo(): void
56+
/**
57+
* @test
58+
* @see
59+
*/
60+
public function it_generates_sql_query_for_orderByDistanceTo_scope(): void
4461
{
45-
// Arrange
62+
// 1. Arrange
4663
$address = new Address();
4764
$castedAttr = $address->getLocationCastedAttributes()->first();
4865

49-
// Act
66+
// 2. Act
5067
$queryForAsc = $address->orderByDistanceTo($castedAttr, new Point());
5168
$queryForDesc = $address->orderByDistanceTo($castedAttr, new Point(), 'desc');
5269

53-
// Assert
54-
$this->assertEquals("select *, CONCAT(ST_AsText(addresses.{$castedAttr}), ',', ST_SRID(addresses.{$castedAttr})) as {$castedAttr} from `addresses` order by ST_Distance(
55-
ST_SRID({$castedAttr}, ?),
70+
// 3. Assert
71+
$this->assertEquals(
72+
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` order by ST_Distance(
73+
ST_SRID($castedAttr, ?),
5674
ST_SRID(Point(?, ?), ?)
57-
) asc", $queryForAsc->toSql());
75+
) asc",
76+
actual: $queryForAsc->toSql()
77+
);
5878

59-
$this->assertEquals("select *, CONCAT(ST_AsText(addresses.{$castedAttr}), ',', ST_SRID(addresses.{$castedAttr})) as {$castedAttr} from `addresses` order by ST_Distance(
60-
ST_SRID({$castedAttr}, ?),
79+
$this->assertEquals(
80+
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` order by ST_Distance(
81+
ST_SRID($castedAttr, ?),
6182
ST_SRID(Point(?, ?), ?)
62-
) desc", $queryForDesc->toSql());
83+
) desc",
84+
actual: $queryForDesc->toSql()
85+
);
6386
}
6487

65-
public function test_newQuery(): void
88+
/**
89+
* @test
90+
* @see
91+
*/
92+
public function it_generates_sql_query_for_location_casted_attributes(): void
6693
{
67-
// Arrange
94+
// 1. Arrange
6895
$address = new Address();
6996
$castedAttr = $address->getLocationCastedAttributes()->first();
7097

71-
// Assert
72-
$this->assertEquals("select *, CONCAT(ST_AsText(addresses.{$castedAttr}), ',', ST_SRID(addresses.{$castedAttr})) as {$castedAttr} from `addresses`", $address->query()->toSql());
98+
// 2. Act & Assert
99+
$this->assertEquals(
100+
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses`",
101+
actual: $address->query()->toSql()
102+
);
73103
}
74104

75105
/**
@@ -78,14 +108,13 @@ public function test_newQuery(): void
78108
*/
79109
public function it_returns_location_casted_attributes(): void
80110
{
81-
// Arrange
111+
// 1. Arrange
82112
$address = new Address();
83113

84-
// Act
114+
// 2. Act
85115
$locationCastedAttributres = $address->getLocationCastedAttributes();
86116

87-
// Assert
88-
$this->assertInstanceOf(Collection::class, $locationCastedAttributres);
117+
// 3. Assert
89118
$this->assertEquals(collect(['location']), $locationCastedAttributres);
90119
}
91120
}

tests/LocationCastTest.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,65 @@
1010

1111
class LocationCastTest extends TestCase
1212
{
13-
public function test_setting_location_to_a_non_point_value(): void
13+
/** @test */
14+
public function it_throws_an_exception_if_casted_attribute_set_to_a_non_point_value(): void
1415
{
15-
// Arrange
16+
// 1. Arrange
1617
$address = new Address();
1718

19+
// 3. Expect
1820
$this->expectException(Exception::class);
1921

20-
// Act
22+
// 2. Act
2123
$address->location = 'dummy';
2224
}
2325

24-
public function test_setting_location_to_a_point(): void
26+
/** @test */
27+
public function it_can_set_the_casted_attribute_to_a_point(): void
2528
{
29+
// 1. Arrange
2630
$address = new Address();
2731
$point = new Point(27.1234, 39.1234);
2832

2933
$cast = new LocationCast();
34+
35+
// 2. Act
3036
$response = $cast->set($address, 'location', $point, $address->getAttributes());
3137

32-
// Assert
38+
// 3. Assert
3339
$this->assertEquals(DB::raw("ST_GeomFromText('POINT({$point->getLng()} {$point->getLat()})')"), $response);
3440
}
3541

36-
public function test_getting_location(): void
42+
/** @test */
43+
public function it_can_get_a_casted_attribute(): void
3744
{
38-
// Arrange
45+
// 1. Arrange
3946
$address = new Address();
4047
$point = new Point(27.1234, 39.1234);
4148

42-
// Act
49+
// 2. Act
4350
$address->location = $point;
4451
$address->save();
4552

46-
// Assert
53+
// 3. Assert
4754
$this->assertInstanceOf(Point::class, $address->location);
4855
$this->assertEquals($point->getLat(), $address->location->getLat());
4956
$this->assertEquals($point->getLng(), $address->location->getLng());
5057
$this->assertEquals($point->getSrid(), $address->location->getSrid());
5158
}
5259

53-
public function test_serialize_location(): void
60+
/** @test */
61+
public function it_can_serialize_a_casted_attribute(): void
5462
{
55-
// Arrange
63+
// 1. Arrange
5664
$address = new Address();
5765
$point = new Point(27.1234, 39.1234);
5866

59-
// Act
67+
// 2. Act
6068
$address->location = $point;
6169
$address->save();
6270

63-
// Assert
71+
// 3. Assert
6472
$array = $address->toArray();
6573
$this->assertIsArray($array);
6674
$this->assertArrayHasKey('location', $array);

tests/PointTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ class PointTest extends TestCase
1212
/** @test */
1313
public function it_sets_lat_lng_and_srid_in_constructor(): void
1414
{
15-
// Arrange
15+
// 1. Arrange
1616
$lat = 25.1515;
1717
$lng = 36.1212;
1818
$srid = 4326;
1919

20-
// Act
20+
// 2. Act
2121
$point = new Point(lat: $lat, lng: $lng, srid: $srid);
2222

23-
// Assert
23+
// 3. Assert
2424
$this->assertSame(expected: $lat, actual: $point->getLat());
2525
$this->assertSame(expected: $lng, actual: $point->getLng());
2626
$this->assertSame(expected: $srid, actual: $point->getSrid());
2727
}
2828

2929
/** @test */
30-
public function it_returns_default_lat_lng_and_srid_if_they_are_not_given_to_constructor(): void
30+
public function it_returns_default_lat_lng_and_srid_if_they_are_not_given_in_the_constructor(): void
3131
{
32-
// Act
32+
// 1. Act
3333
$point = new Point();
3434

35-
// Assert
35+
// 2. Assert
3636
$this->assertSame(expected: 0.0, actual: $point->getLat());
3737
$this->assertSame(expected: 0.0, actual: $point->getLng());
3838
$this->assertSame(expected: 0, actual: $point->getSrid());
@@ -41,13 +41,13 @@ public function it_returns_default_lat_lng_and_srid_if_they_are_not_given_to_con
4141
/** @test */
4242
public function it_returns_default_srid_in_config_if_it_is_not_null(): void
4343
{
44-
// Arrange
44+
// 1. Arrange
4545
Config::set('laravel-spatial.default_srid', 4326);
4646

47-
// Act
47+
// 2. Act
4848
$point = new Point();
4949

50-
// Assert
50+
// 3. Assert
5151
$this->assertSame(expected: 0.0, actual: $point->getLat());
5252
$this->assertSame(expected: 0.0, actual: $point->getLng());
5353
$this->assertSame(expected: 4326, actual: $point->getSrid());

0 commit comments

Comments
 (0)