Skip to content

Commit 321bf9e

Browse files
committed
Fixes MySQL 5.5 compatibility
1 parent 67427a0 commit 321bf9e

File tree

6 files changed

+18
-9
lines changed

6 files changed

+18
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"jmikola/geojson": "^1.0"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit": "5.6.5",
19+
"phpunit/phpunit": "~4.5||5.6.5",
2020
"mockery/mockery": "^0.9.9",
2121
"laravel/laravel": "^5.2"
2222
},

src/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public function update(array $values)
2525

2626
protected function asWKT(GeometryInterface $geometry)
2727
{
28-
return $this->getQuery()->raw("ST_GeomFromText('" . $geometry->toWKT() . "')");
28+
return $this->getQuery()->raw("GeomFromText('" . $geometry->toWKT() . "')");
2929
}
3030
}

src/Eloquent/SpatialTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function performInsert(EloquentBuilder $query, array $options = [])
3636
foreach ($this->attributes as $key => $value) {
3737
if ($value instanceof GeometryInterface) {
3838
$this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert
39-
$this->attributes[$key] = $this->getConnection()->raw(sprintf("ST_GeomFromText('%s')", $value->toWKT()));
39+
$this->attributes[$key] = $this->getConnection()->raw(sprintf("GeomFromText('%s')", $value->toWKT()));
4040
}
4141
}
4242

@@ -73,12 +73,13 @@ public function getSpatialFields()
7373

7474
public function scopeDistance($query, $distance, $point, $column_name) {
7575
// TODO: check if array, and transform to string delimited by ,
76+
// TODO: what about mysql 5.5? distance_sphere? need test
7677
return $query
7778
->whereRaw("st_distance_sphere(`{$column_name}`, POINT({$point->getLng()}, {$point->getLat()})) <= {$distance}")
7879
->whereRaw("st_distance_sphere(`{$column_name}`, POINT({$point->getLng()}, {$point->getLat()})) != 0");
7980
}
8081

8182
public function scopeBounding($query, Geometry $bounds, $column_name) {
82-
return $query->whereRaw("MBRIntersects(ST_GeomFromText('{$bounds->toWkt()}'), `{$column_name}`)");
83+
return $query->whereRaw("MBRIntersects(GeomFromText('{$bounds->toWkt()}'), `{$column_name}`)");
8384
}
8485
}

tests/Feature/SpatialTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function createApplication()
1919
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
2020

2121
$app['config']->set('database.default', 'mysql');
22-
$app['config']->set('database.connections.mysql.host', env('DOCKER_DB_HOST', '127.0.0.1'));
22+
$app['config']->set('database.connections.mysql.host', env('DB_HOST', '127.0.0.1'));
2323
$app['config']->set('database.connections.mysql.database', 'test');
2424
$app['config']->set('database.connections.mysql.username', 'root');
2525
$app['config']->set('database.connections.mysql.password', '');
@@ -50,6 +50,14 @@ public function tearDown()
5050
parent::tearDown();
5151
}
5252

53+
protected function assertDatabaseHas($table, array $data, $connection = null)
54+
{
55+
if(method_exists($this, 'seeInDatabase')) {
56+
$this->seeInDatabase($table, $data, $connection);
57+
}
58+
parent::assertDatabaseHas($table, $data, $connection);
59+
}
60+
5361
private function onMigrations(\Closure $closure)
5462
{
5563
$fileSystem = new Filesystem();

tests/Unit/Eloquent/BuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function testUpdate()
4747
{
4848
$this->queryBuilder
4949
->shouldReceive('raw')
50-
->with("ST_GeomFromText('POINT(2 1)')")
51-
->andReturn(new Expression("ST_GeomFromText('POINT(2 1)')"));
50+
->with("GeomFromText('POINT(2 1)')")
51+
->andReturn(new Expression("GeomFromText('POINT(2 1)')"));
5252

5353
$this->queryBuilder
5454
->shouldReceive('update');

tests/Unit/Eloquent/SpatialTraitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testInsertPointHasCorrectSql()
3333
$this->model->point = new Point(1, 2);
3434
$this->model->save();
3535

36-
$this->assertContains("ST_GeomFromText('POINT(2 1)')", $this->queries[0]);
36+
$this->assertContains("GeomFromText('POINT(2 1)')", $this->queries[0]);
3737
}
3838

3939
public function testUpdatePointHasCorrectSql()
@@ -42,7 +42,7 @@ public function testUpdatePointHasCorrectSql()
4242
$this->model->point = new Point(2, 4);
4343
$this->model->save();
4444

45-
$this->assertContains("ST_GeomFromText('POINT(4 2)')", $this->queries[0]);
45+
$this->assertContains("GeomFromText('POINT(4 2)')", $this->queries[0]);
4646
}
4747
}
4848

0 commit comments

Comments
 (0)