Skip to content

Commit

Permalink
Fixes MySQL 5.5 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
grimzy committed Mar 26, 2017
1 parent 67427a0 commit 321bf9e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"jmikola/geojson": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "5.6.5",
"phpunit/phpunit": "~4.5||5.6.5",
"mockery/mockery": "^0.9.9",
"laravel/laravel": "^5.2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public function update(array $values)

protected function asWKT(GeometryInterface $geometry)
{
return $this->getQuery()->raw("ST_GeomFromText('" . $geometry->toWKT() . "')");
return $this->getQuery()->raw("GeomFromText('" . $geometry->toWKT() . "')");
}
}
5 changes: 3 additions & 2 deletions src/Eloquent/SpatialTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function performInsert(EloquentBuilder $query, array $options = [])
foreach ($this->attributes as $key => $value) {
if ($value instanceof GeometryInterface) {
$this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert
$this->attributes[$key] = $this->getConnection()->raw(sprintf("ST_GeomFromText('%s')", $value->toWKT()));
$this->attributes[$key] = $this->getConnection()->raw(sprintf("GeomFromText('%s')", $value->toWKT()));
}
}

Expand Down Expand Up @@ -73,12 +73,13 @@ public function getSpatialFields()

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

public function scopeBounding($query, Geometry $bounds, $column_name) {
return $query->whereRaw("MBRIntersects(ST_GeomFromText('{$bounds->toWkt()}'), `{$column_name}`)");
return $query->whereRaw("MBRIntersects(GeomFromText('{$bounds->toWkt()}'), `{$column_name}`)");
}
}
10 changes: 9 additions & 1 deletion tests/Feature/SpatialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function createApplication()
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

$app['config']->set('database.default', 'mysql');
$app['config']->set('database.connections.mysql.host', env('DOCKER_DB_HOST', '127.0.0.1'));
$app['config']->set('database.connections.mysql.host', env('DB_HOST', '127.0.0.1'));
$app['config']->set('database.connections.mysql.database', 'test');
$app['config']->set('database.connections.mysql.username', 'root');
$app['config']->set('database.connections.mysql.password', '');
Expand Down Expand Up @@ -50,6 +50,14 @@ public function tearDown()
parent::tearDown();
}

protected function assertDatabaseHas($table, array $data, $connection = null)
{
if(method_exists($this, 'seeInDatabase')) {
$this->seeInDatabase($table, $data, $connection);
}
parent::assertDatabaseHas($table, $data, $connection);
}

private function onMigrations(\Closure $closure)
{
$fileSystem = new Filesystem();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Eloquent/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function testUpdate()
{
$this->queryBuilder
->shouldReceive('raw')
->with("ST_GeomFromText('POINT(2 1)')")
->andReturn(new Expression("ST_GeomFromText('POINT(2 1)')"));
->with("GeomFromText('POINT(2 1)')")
->andReturn(new Expression("GeomFromText('POINT(2 1)')"));

$this->queryBuilder
->shouldReceive('update');
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Eloquent/SpatialTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testInsertPointHasCorrectSql()
$this->model->point = new Point(1, 2);
$this->model->save();

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

public function testUpdatePointHasCorrectSql()
Expand All @@ -42,7 +42,7 @@ public function testUpdatePointHasCorrectSql()
$this->model->point = new Point(2, 4);
$this->model->save();

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

Expand Down

0 comments on commit 321bf9e

Please sign in to comment.