Skip to content

Commit

Permalink
fix errors, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
v1r0x committed Jul 14, 2017
1 parent 525efc6 commit 3545a4c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 18 deletions.
15 changes: 11 additions & 4 deletions src/Eloquent/PostgisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Arr;
use Phaza\LaravelPostgis\Exceptions\PostgisFieldsNotDefinedException;
use Phaza\LaravelPostgis\Exceptions\PostgisTypesMalformedException;
use Phaza\LaravelPostgis\Exceptions\UnsupportedGeomtypeException;
use Phaza\LaravelPostgis\Geometries\Geometry;
use Phaza\LaravelPostgis\Geometries\GeometryInterface;
use Phaza\LaravelPostgis\Schema\Grammars\PostgisGrammar;
Expand Down Expand Up @@ -68,8 +70,16 @@ public function setRawAttributes(array $attributes, $sync = false)

public function getPostgisType($key)
{
$default = [
'geomtype' => 'geography',
'srid' => 4326
];

if (property_exists($this, 'postgisTypes')) {
if (Arr::isAssoc($this->postgisTypes)) {
if(!array_key_exists($key, $this->postgisTypes)) {
return $default;
}
$column = $this->postgisTypes[$key];
if (isset($column['geomtype']) && in_array(strtoupper($column['geomtype']), PostgisGrammar::$allowed_geom_types)) {
return $column;
Expand All @@ -82,10 +92,7 @@ public function getPostgisType($key)
}

// Return default geography if postgisTypes does not exist (for backward compatibility)
return [
'geomtype' => 'geography',
'srid' => 4326
];
return $default;
}

public function getPostgisFields()
Expand Down
60 changes: 47 additions & 13 deletions src/Schema/Grammars/PostgisGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ class PostgisGrammar extends PostgresGrammar
*/
public function typePoint(Fluent $column)
{
if ((in_array(strtoupper($column->geomtype), PostgisGrammar::$allowed_geom_types)) && (is_int((int) $column->srid))) {
return strtoupper($column->geomtype) . '(POINT, ' . $column->srid . ')';
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return $type . '(POINT, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid! (If geom type is GEOGRAPHY then the SRID must be 4326)');
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
}

Expand All @@ -33,10 +37,14 @@ public function typePoint(Fluent $column)
*/
public function typeMultipoint(Fluent $column)
{
if ((in_array(strtoupper($column->geomtype), PostgisGrammar::$allowed_geom_types)) && (is_int((int) $column->srid))) {
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(MULTIPOINT, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid! (If geom type is GEOGRAPHY then the SRID must be 4326)');
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
}

Expand All @@ -48,10 +56,14 @@ public function typeMultipoint(Fluent $column)
*/
public function typePolygon(Fluent $column)
{
if ((in_array(strtoupper($column->geomtype), PostgisGrammar::$allowed_geom_types)) && (is_int((int) $column->srid))) {
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(POLYGON, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid! (If geom type is GEOGRAPHY then the SRID must be 4326)');
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
}

Expand All @@ -63,10 +75,14 @@ public function typePolygon(Fluent $column)
*/
public function typeMultipolygon(Fluent $column)
{
if ((in_array(strtoupper($column->geomtype), PostgisGrammar::$allowed_geom_types)) && (is_int((int) $column->srid))) {
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(MULTIPOLYGON, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid! (If geom type is GEOGRAPHY then the SRID must be 4326)');
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
}

Expand All @@ -78,10 +94,14 @@ public function typeMultipolygon(Fluent $column)
*/
public function typeLinestring(Fluent $column)
{
if ((in_array(strtoupper($column->geomtype), PostgisGrammar::$allowed_geom_types)) && (is_int((int) $column->srid))) {
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(LINESTRING, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid! (If geom type is GEOGRAPHY then the SRID must be 4326)');
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
}

Expand All @@ -93,10 +113,14 @@ public function typeLinestring(Fluent $column)
*/
public function typeMultilinestring(Fluent $column)
{
if ((in_array(strtoupper($column->geomtype), PostgisGrammar::$allowed_geom_types)) && (is_int((int) $column->srid))) {
$type = strtoupper($column->geomtype);
if ($this->isValid($column)) {
if ($type == 'GEOGRAPHY' && $column->srid != 4326) {
throw new UnsupportedGeomtypeException('Error with validation of srid! SRID of GEOGRAPHY must be 4326)');
}
return strtoupper($column->geomtype) . '(MULTILINESTRING, ' . $column->srid . ')';
} else {
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid! (If geom type is GEOGRAPHY then the SRID must be 4326)');
throw new UnsupportedGeomtypeException('Error with validation of geom type or srid!');
}
}

Expand Down Expand Up @@ -180,4 +204,14 @@ protected function compileGeometry(Blueprint $blueprint, Fluent $command)
$typmod
);
}

/**
* Checks if the given $column is a valid geometry type
*
* @param \Illuminate\Support\Fluent $column
* @return boolean
*/
protected function isValid($column) {
return in_array(strtoupper($column->geomtype), PostgisGrammar::$allowed_geom_types) && is_int((int) $column->srid);
}
}
18 changes: 17 additions & 1 deletion tests/Eloquent/PostgisTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public function testInsertPointHasCorrectSql()
$this->assertContains("public.ST_GeogFromText('POINT(2 1)')", $this->queries[0]);
}

public function testInsertPointGeometryHasCorrectSql()
{
$this->model->point2 = new Point(1, 2);
$this->model->save();

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

public function testUpdatePointHasCorrectSql()
{
$this->model->exists = true;
Expand All @@ -52,7 +60,15 @@ class TestModel extends Model
use PostgisTrait;

protected $postgisFields = [
'point' => Point::class
'point' => Point::class,
'point2' => Polygon::class,
];

protected $postgisTypes = [
'point2' => [
'geomtype' => 'geometry',
'srid' => 27700
]
];


Expand Down
20 changes: 20 additions & 0 deletions tests/Schema/Grammars/PostgisGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Phaza\LaravelPostgis\PostgisConnection;
use Phaza\LaravelPostgis\Schema\Blueprint;
use Phaza\LaravelPostgis\Schema\Grammars\PostgisGrammar;
use Phaza\LaravelPostgis\Exceptions\PostgisTypesMalformedException;
use Phaza\LaravelPostgis\Exceptions\UnsupportedGeomtypeException;

class PostgisGrammarBaseTest extends BaseTestCase
{
Expand All @@ -26,6 +28,24 @@ public function testAddingPointGeom()
$this->assertContains('GEOMETRY(POINT, 27700)', $statements[0]);
}

public function testAddingPointWrongSrid()
{
$this->setExpectedException(UnsupportedGeomtypeException::class);
$blueprint = new Blueprint('test');
$blueprint->point('foo', 'GEOGRAPHY', 27700);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertEquals(1, count($statements));
}

public function testAddingPointUnsupported()
{
$this->setExpectedException(UnsupportedGeomtypeException::class);
$blueprint = new Blueprint('test');
$blueprint->point('foo', 'UNSUPPORTED_ENTRY', 27700);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertEquals(1, count($statements));
}

public function testAddingLinestring()
{
$blueprint = new Blueprint('test');
Expand Down

0 comments on commit 3545a4c

Please sign in to comment.