Skip to content

Commit a61953e

Browse files
committed
Added more tests. Fixed some unit tests.
1 parent b197f2f commit a61953e

File tree

8 files changed

+491
-23
lines changed

8 files changed

+491
-23
lines changed

phpunit.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
file="vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php">
2222
</listener>
2323
</listeners>
24+
<filter>
25+
<whitelist processUncoveredFilesFromWhitelist="true">
26+
<directory suffix=".php">./src</directory>
27+
</whitelist>
28+
</filter>
2429
<php>
2530
<env name="APP_ENV" value="testing"/>
2631
<env name="APP_DEBUG" value="true"/>

tests/Feature/Migrations/CreateTables.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function up()
2525
$table->geometryCollection('multi_geometries')->default(null)->nullable();
2626
$table->timestamps();
2727
});
28+
29+
Schema::create('no_spatial_fields', function (Blueprint $table) {
30+
$table->increments('id');
31+
$table->geometry('geometry')->default(null)->nullable();
32+
});
2833
}
2934

3035
/**
@@ -35,5 +40,6 @@ public function up()
3540
public function down()
3641
{
3742
Schema::drop('geometry');
43+
Schema::drop('no_spatial_fields');
3844
}
3945
}

tests/Feature/Migrations/UpdateTables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function up()
2626
public function down()
2727
{
2828
Schema::table('geometry', function (Blueprint $table) {
29-
$table->dropSpatial(['location']);
29+
$table->dropSpatialIndex(['location']); // either an array of column names or the index name
3030
});
3131
}
3232
}

tests/Feature/Models/GeometryModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ class GeometryModel extends Model
88

99
protected $table = 'geometry';
1010

11-
protected $spatialFields = ['location'];
11+
protected $spatialFields = ['location', 'line'];
1212
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
use Grimzy\LaravelSpatial\Eloquent\SpatialTrait;
3+
use Illuminate\Database\Eloquent\Model;
4+
5+
class NoSpatialFieldsModel extends Model
6+
{
7+
use SpatialTrait;
8+
9+
protected $table = 'no_spatial_fields';
10+
11+
public $timestamps = false;
12+
}

tests/Feature/SpatialTest.php

Lines changed: 178 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22
use Grimzy\LaravelSpatial\SpatialServiceProvider;
3+
use Grimzy\LaravelSpatial\Types\GeometryCollection;
4+
use Grimzy\LaravelSpatial\Types\LineString;
5+
use Grimzy\LaravelSpatial\Types\MultiPoint;
6+
use Grimzy\LaravelSpatial\Types\MultiPolygon;
37
use Grimzy\LaravelSpatial\Types\Point;
8+
use Grimzy\LaravelSpatial\Types\Polygon;
49
use Illuminate\Filesystem\Filesystem;
510
use Illuminate\Foundation\Testing\TestCase;
611

@@ -19,7 +24,7 @@ public function createApplication()
1924
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
2025

2126
$app['config']->set('database.default', 'mysql');
22-
$app['config']->set('database.connections.mysql.host', env('DB_HOST', '127.0.0.1'));
27+
$app['config']->set('database.connections.mysql.host', env('DB_HOST', '172.17.0.2')); // TODO: do not commit ip change
2328
$app['config']->set('database.connections.mysql.database', 'test');
2429
$app['config']->set('database.connections.mysql.username', 'root');
2530
$app['config']->set('database.connections.mysql.password', '');
@@ -75,12 +80,79 @@ private function onMigrations(\Closure $closure, $reverse_sort = false)
7580
}
7681
}
7782

78-
// TODO: Test with a model missing $spatialFields to expect SpatialFieldNotDefinedException
83+
public function testSpatialFieldsNotDefinedException() {
84+
$geo = new NoSpatialFieldsModel();
85+
$geo->geometry = new Point(1, 2);
86+
$geo->save();
87+
88+
$this->setExpectedException(\Grimzy\LaravelSpatial\Exceptions\SpatialFieldsNotDefinedException::class);
89+
NoSpatialFieldsModel::all();
90+
91+
}
92+
93+
public function testInsertPoint()
94+
{
95+
$geo = new GeometryModel();
96+
$geo->location = new Point(1, 2);
97+
$geo->save();
98+
$this->assertDatabaseHas('geometry', ['id' => $geo->id]);
99+
}
100+
101+
public function testInsertLineString()
102+
{
103+
$geo = new GeometryModel();
79104

80-
public function testInsert()
105+
$geo->location = new Point(1, 2);
106+
$geo->line = new LineString([new Point(1, 1), new Point(2, 2)]);
107+
$geo->save();
108+
$this->assertDatabaseHas('geometry', ['id' => $geo->id]);
109+
}
110+
111+
public function testInsertPolygon()
81112
{
82113
$geo = new GeometryModel();
114+
83115
$geo->location = new Point(1, 2);
116+
$geo->shape = Polygon::fromWKT("POLYGON((0 10,10 10,10 0,0 0,0 10))");
117+
$geo->save();
118+
$this->assertDatabaseHas('geometry', ['id' => $geo->id]);
119+
}
120+
121+
public function testInsertMultiPoint()
122+
{
123+
$geo = new GeometryModel();
124+
125+
$geo->location = new Point(1, 2);
126+
$geo->multi_locations = new MultiPoint([new Point(1, 1), new Point(2, 2)]);
127+
$geo->save();
128+
$this->assertDatabaseHas('geometry', ['id' => $geo->id]);
129+
}
130+
131+
public function testInsertMultiPolygon()
132+
{
133+
$geo = new GeometryModel();
134+
135+
$geo->location = new Point(1, 2);
136+
137+
$geo->multi_shapes = new MultiPolygon([
138+
Polygon::fromWKT("POLYGON((0 10,10 10,10 0,0 0,0 10))"),
139+
Polygon::fromWKT("POLYGON((0 0,0 5,5 5,5 0,0 0))")
140+
]);
141+
$geo->save();
142+
$this->assertDatabaseHas('geometry', ['id' => $geo->id]);
143+
}
144+
145+
public function testInsertGeometryCollection()
146+
{
147+
$geo = new GeometryModel();
148+
149+
$geo->location = new Point(1, 2);
150+
151+
$geo->multi_geometries = new GeometryCollection([
152+
Polygon::fromWKT("POLYGON((0 10,10 10,10 0,0 0,0 10))"),
153+
Polygon::fromWKT("POLYGON((0 0,0 5,5 5,5 0,0 0))"),
154+
new Point(0, 0)
155+
]);
84156
$geo->save();
85157
$this->assertDatabaseHas('geometry', ['id' => $geo->id]);
86158
}
@@ -107,35 +179,133 @@ public function testUpdate()
107179
}
108180

109181
public function testDistance()
182+
{
183+
$loc1 = new GeometryModel();
184+
$loc1->location = new Point(1, 1);
185+
$loc1->save();
186+
187+
$loc2 = new GeometryModel();
188+
$loc2->location = new Point(2, 2); // Distance from loc1: 1.4142135623731
189+
$loc2->save();
190+
191+
$loc3 = new GeometryModel();
192+
$loc3->location = new Point(3, 3); // Distance from loc1: 2.8284271247462
193+
$loc3->save();
194+
195+
$a = GeometryModel::distance(2, $loc1->location, 'location')->get();
196+
$this->assertCount(2, $a);
197+
$this->assertTrue($a->contains($loc1));
198+
$this->assertTrue($a->contains($loc2));
199+
$this->assertFalse($a->contains($loc3));
200+
201+
// Excluding self
202+
$b = GeometryModel::distance(2, $loc1->location, 'location', true)->get();
203+
$this->assertCount(1, $b);
204+
$this->assertFalse($b->contains($loc1));
205+
$this->assertTrue($b->contains($loc2));
206+
$this->assertFalse($b->contains($loc3));
207+
208+
$c = GeometryModel::distance(1, $loc1->location, 'location')->get();
209+
$this->assertCount(1, $c);
210+
$this->assertTrue($c->contains($loc1));
211+
$this->assertFalse($c->contains($loc2));
212+
$this->assertFalse($c->contains($loc3));
213+
}
214+
215+
public function testDistanceSphere()
110216
{
111217
$loc1 = new GeometryModel();
112218
$loc1->location = new Point(40.767864, -73.971732);
113219
$loc1->save();
114220

115221
$loc2 = new GeometryModel();
116-
$loc2->location = new Point(40.767664, -73.971271); // Distance from loc1: 44.7414064845878
222+
$loc2->location = new Point(40.767664, -73.971271); // Distance from loc1: 44.741406484588
117223
$loc2->save();
118224

119225
$loc3 = new GeometryModel();
120-
$loc3->location = new Point(40.761434, -73.977619);
226+
$loc3->location = new Point(40.761434, -73.977619); // Distance from loc1: 870.06424066202
121227
$loc3->save();
122228

123-
$a = GeometryModel::distance(45, $loc1->location, 'location')->get();
229+
$a = GeometryModel::distanceSphere(200, $loc1->location, 'location')->get();
124230
$this->assertCount(2, $a);
125231
$this->assertTrue($a->contains($loc1));
126232
$this->assertTrue($a->contains($loc2));
127233
$this->assertFalse($a->contains($loc3));
128234

129-
$b = GeometryModel::distance(45, $loc1->location, 'location', true)->get();
235+
// Excluding self
236+
$b = GeometryModel::distanceSphere(200, $loc1->location, 'location', true)->get();
130237
$this->assertCount(1, $b);
131238
$this->assertFalse($b->contains($loc1));
132239
$this->assertTrue($b->contains($loc2));
133240
$this->assertFalse($b->contains($loc3));
134241

135-
$c = GeometryModel::distance(44.741406484587, $loc1->location, 'location')->get();
242+
$c = GeometryModel::distanceSphere(44.741406484587, $loc1->location, 'location')->get();
136243
$this->assertCount(1, $c);
137244
$this->assertTrue($c->contains($loc1));
138245
$this->assertFalse($c->contains($loc2));
139246
$this->assertFalse($c->contains($loc3));
140247
}
248+
249+
public function testDistanceValue()
250+
{
251+
$loc1 = new GeometryModel();
252+
$loc1->location = new Point(1, 1);
253+
$loc1->save();
254+
255+
$loc2 = new GeometryModel();
256+
$loc2->location = new Point(2, 2); // Distance from loc1: 1.4142135623731
257+
$loc2->save();
258+
259+
$a = GeometryModel::distanceValue($loc1->location, 'location')->get();
260+
$this->assertCount(2, $a);
261+
$this->assertEquals(0, $a[0]->distance);
262+
$this->assertEquals(1.4142135623, $a[1]->distance); // PHP floats' 11th+ digits don't matter
263+
}
264+
265+
public function testDistanceSphereValue() {
266+
$loc1 = new GeometryModel();
267+
$loc1->location = new Point(40.767864, -73.971732);
268+
$loc1->save();
269+
270+
$loc2 = new GeometryModel();
271+
$loc2->location = new Point(40.767664, -73.971271); // Distance from loc1: 44.741406484588
272+
$loc2->save();
273+
274+
$a = GeometryModel::distanceSphereValue($loc1->location, 'location')->get();
275+
$this->assertCount(2, $a);
276+
$this->assertEquals(0, $a[0]->distance);
277+
$this->assertEquals(44.7414064845, $a[1]->distance); // PHP floats' 11th+ digits don't matter
278+
}
279+
280+
public function testBounding() {
281+
$point = new Point(0, 0);
282+
283+
$linestring1 = \Grimzy\LaravelSpatial\Types\LineString::fromWkt("LINESTRING(1 1, 2 2)");
284+
$linestring2 = \Grimzy\LaravelSpatial\Types\LineString::fromWkt("LINESTRING(20 20, 24 24)");
285+
$linestring3 = \Grimzy\LaravelSpatial\Types\LineString::fromWkt("LINESTRING(0 10, 10 10)");
286+
287+
$geo1 = new GeometryModel();
288+
$geo1->location = $point;
289+
$geo1->line = $linestring1;
290+
$geo1->save();
291+
292+
$geo2 = new GeometryModel();
293+
$geo2->location = $point;
294+
$geo2->line = $linestring2;
295+
$geo2->save();
296+
297+
$geo3 = new GeometryModel();
298+
$geo3->location = $point;
299+
$geo3->line = $linestring3;
300+
$geo3->save();
301+
302+
$polygon = Polygon::fromWKT("POLYGON((0 10,10 10,10 0,0 0,0 10))");
303+
304+
$result = GeometryModel::Bounding($polygon, 'line')->get();
305+
$this->assertCount(2, $result);
306+
$this->assertTrue($result->contains($geo1));
307+
$this->assertFalse($result->contains($geo2));
308+
$this->assertTrue($result->contains($geo3));
309+
310+
}
141311
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
<?php
22

3-
use Illuminate\Container\Container;
43
use Grimzy\LaravelSpatial\Connectors\ConnectionFactory;
54
use Grimzy\LaravelSpatial\MysqlConnection;
5+
use Illuminate\Container\Container;
66
use Stubs\PDOStub;
77

88
class ConnectionFactoryBaseTest extends BaseTestCase
99
{
1010
public function testMakeCallsCreateConnection()
1111
{
12-
$mysql_config = ['driver' => 'mysql', 'prefix' => 'prefix', 'database' => 'database', 'name' => 'foo'];
1312
$pdo = new PDOStub();
1413

1514
$factory = Mockery::mock(ConnectionFactory::class, [new Container()])->makePartial();
1615
$factory->shouldAllowMockingProtectedMethods();
17-
$conn = $factory->createConnection('mysql', $pdo, 'database', 'prefix', $mysql_config);
16+
$conn = $factory->createConnection('mysql', $pdo, 'database');
1817

1918
$this->assertInstanceOf(MysqlConnection::class, $conn);
2019
}
20+
21+
public function testCreateConnectionDifferentDriver()
22+
{
23+
$pdo = new PDOStub();
24+
25+
$factory = Mockery::mock(ConnectionFactory::class, [new Container()])->makePartial();
26+
$factory->shouldAllowMockingProtectedMethods();
27+
$conn = $factory->createConnection('pgsql', $pdo, 'database');
28+
29+
$this->assertInstanceOf(\Illuminate\Database\PostgresConnection::class, $conn);
30+
}
2131
}

0 commit comments

Comments
 (0)