From 6508a7ffb22993e2818e1d92353dd73d0da4286b Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Tue, 3 Mar 2020 01:09:16 -0500 Subject: [PATCH 1/4] Port of validation when creating spatial collections --- composer.json | 2 + src/Types/GeometryCollection.php | 70 ++++++++++++++++++--- src/Types/LineString.php | 7 +++ src/Types/MultiLineString.php | 26 ++------ src/Types/MultiPolygon.php | 20 ++---- src/Types/PointCollection.php | 25 ++------ tests/Unit/BaseTestCase.php | 6 +- tests/Unit/Eloquent/SpatialTraitTest.php | 3 +- tests/Unit/Types/GeometryCollectionTest.php | 10 ++- tests/Unit/Types/GeometryTest.php | 5 +- tests/Unit/Types/MultiLineStringTest.php | 15 ++++- tests/Unit/Types/MultiPointTest.php | 20 ++++-- tests/Unit/Types/MultiPolygonTest.php | 10 ++- 13 files changed, 138 insertions(+), 81 deletions(-) diff --git a/composer.json b/composer.json index 8e345e42..84b7921d 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,8 @@ ], "require": { "php": ">=5.5.9", + "ext-pdo": "*", + "ext-json": "*", "illuminate/database": "^5.2||^6.0", "geo-io/wkb-parser": "^1.0", "jmikola/geojson": "^1.0" diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index 2e1f0321..69d7fe29 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -14,6 +14,20 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAccess, Arrayable, Countable { + /** + * The minimum number of items required to create this collection. + * + * @var int + */ + protected $minimumCollectionItems = 1; + + /** + * The class of the items in the collection. + * + * @var string + */ + protected $collectionItemType = GeometryInterface::class; + /** * The items contained in the spatial collection. * @@ -28,13 +42,7 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc */ public function __construct(array $geometries) { - $validated = array_filter($geometries, function ($value) { - return $value instanceof GeometryInterface; - }); - - if (count($geometries) !== count($validated)) { - throw new InvalidArgumentException('$geometries must be an array of Geometry objects'); - } + $this->validateItems($geometries); $this->items = $geometries; } @@ -89,9 +97,7 @@ public function offsetGet($offset) public function offsetSet($offset, $value) { - if (!($value instanceof GeometryInterface)) { - throw new InvalidArgumentException('$value must be an instance of GeometryInterface'); - } + $this->validateItemType($value); if (is_null($offset)) { $this->items[] = $value; @@ -142,4 +148,48 @@ public function jsonSerialize() return new \GeoJson\Geometry\GeometryCollection($geometries); } + + /** + * Checks whether the items are valid to create this collection. + * + * @param array $items + */ + protected function validateItems(array $items) { + $this->validateItemCount($items); + + foreach ($items as $item) { + $this->validateItemType($item); + } + } + + /** + * Checks whether the array has enough items to generate a valid WKT. + * + * @param array $items + * + * @see $minimumCollectionItems + */ + protected function validateItemCount(array $items) { + if (count($items) < $this->minimumCollectionItems) { + $entries = $this->minimumCollectionItems === 1 ? 'entry' : 'entries'; + throw new InvalidArgumentException(sprintf( + '%s must contain at least %d %s', get_class($this), $this->minimumCollectionItems, $entries + )); + } + } + + /** + * Checks the type of the items in the array. + * + * @param $item + * + * @see $collectionItemType + */ + protected function validateItemType($item) { + if (!$item instanceof $this->collectionItemType) { + throw new InvalidArgumentException(sprintf( + '%s must be a collection of %s', get_class($this), $this->collectionItemType + )); + } + } } diff --git a/src/Types/LineString.php b/src/Types/LineString.php index 02097edd..8c226444 100644 --- a/src/Types/LineString.php +++ b/src/Types/LineString.php @@ -8,6 +8,13 @@ class LineString extends PointCollection { + /** + * The minimum number of items required to create this collection. + * + * @var int + */ + protected $minimumCollectionItems = 2; + public function toWKT() { return sprintf('LINESTRING(%s)', $this->toPairList()); diff --git a/src/Types/MultiLineString.php b/src/Types/MultiLineString.php index dd3342fd..64707d6a 100644 --- a/src/Types/MultiLineString.php +++ b/src/Types/MultiLineString.php @@ -5,29 +5,15 @@ use GeoJson\GeoJson; use GeoJson\Geometry\MultiLineString as GeoJsonMultiLineString; use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; -use InvalidArgumentException; class MultiLineString extends GeometryCollection { /** - * @param LineString[] $lineStrings + * The class of the items in the collection. + * + * @var string */ - public function __construct(array $lineStrings) - { - if (count($lineStrings) < 1) { - throw new InvalidArgumentException('$lineStrings must contain at least one entry'); - } - - $validated = array_filter($lineStrings, function ($value) { - return $value instanceof LineString; - }); - - if (count($lineStrings) !== count($validated)) { - throw new InvalidArgumentException('$lineStrings must be an array of LineString'); - } - - parent::__construct($lineStrings); - } + protected $collectionItemType = LineString::class; public function getLineStrings() { @@ -58,9 +44,7 @@ public function __toString() public function offsetSet($offset, $value) { - if (!($value instanceof LineString)) { - throw new InvalidArgumentException('$value must be an instance of LineString'); - } + $this->validateItemType($value); parent::offsetSet($offset, $value); } diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php index aba2b6d1..16f2769c 100644 --- a/src/Types/MultiPolygon.php +++ b/src/Types/MultiPolygon.php @@ -10,19 +10,11 @@ class MultiPolygon extends GeometryCollection { /** - * @param Polygon[] $polygons + * The class of the items in the collection. + * + * @var string */ - public function __construct(array $polygons) - { - $validated = array_filter($polygons, function ($value) { - return $value instanceof Polygon; - }); - - if (count($polygons) !== count($validated)) { - throw new InvalidArgumentException('$polygons must be an array of Polygon'); - } - parent::__construct($polygons); - } + protected $collectionItemType = Polygon::class; public function toWKT() { @@ -93,9 +85,7 @@ protected static function assembleParts(array $parts) public function offsetSet($offset, $value) { - if (!($value instanceof Polygon)) { - throw new InvalidArgumentException('$value must be an instance of Polygon'); - } + $this->validateItemType($value); parent::offsetSet($offset, $value); } diff --git a/src/Types/PointCollection.php b/src/Types/PointCollection.php index af586b28..30d1b8de 100755 --- a/src/Types/PointCollection.php +++ b/src/Types/PointCollection.php @@ -8,24 +8,11 @@ abstract class PointCollection extends GeometryCollection { /** - * @param Point[] $points + * The class of the items in the collection. + * + * @var string */ - public function __construct(array $points) - { - if (count($points) < 2) { - throw new InvalidArgumentException('$points must contain at least two entries'); - } - - $validated = array_filter($points, function ($value) { - return $value instanceof Point; - }); - - if (count($points) !== count($validated)) { - throw new InvalidArgumentException('$points must be an array of Points'); - } - - parent::__construct($points); - } + protected $collectionItemType = Point::class; public function toPairList() { @@ -36,9 +23,7 @@ public function toPairList() public function offsetSet($offset, $value) { - if (!($value instanceof Point)) { - throw new InvalidArgumentException('$value must be an instance of Point'); - } + $this->validateItemType($value); parent::offsetSet($offset, $value); } diff --git a/tests/Unit/BaseTestCase.php b/tests/Unit/BaseTestCase.php index 44f323c5..0b53a414 100644 --- a/tests/Unit/BaseTestCase.php +++ b/tests/Unit/BaseTestCase.php @@ -7,12 +7,14 @@ public function tearDown() Mockery::close(); } - protected function assertException($exceptionName) + protected function assertException($exceptionName, $exceptionMessage = '', $exceptionCode = 0) { if (method_exists(parent::class, 'expectException')) { parent::expectException($exceptionName); + parent::expectExceptionMessage($exceptionMessage); + parent::expectExceptionCode($exceptionCode); } else { - $this->setExpectedException($exceptionName); + $this->setExpectedException($exceptionName, $exceptionMessage, $exceptionCode); } } } diff --git a/tests/Unit/Eloquent/SpatialTraitTest.php b/tests/Unit/Eloquent/SpatialTraitTest.php index f70b8cbb..2678cc07 100644 --- a/tests/Unit/Eloquent/SpatialTraitTest.php +++ b/tests/Unit/Eloquent/SpatialTraitTest.php @@ -46,6 +46,7 @@ public function testInsertUpdatePointHasCorrectSql() $this->assertStringStartsWith('update', $this->queries[1]); $this->assertContains('update `test_models` set `point` = ST_GeomFromText(?) where `id` = ?', $this->queries[1]); + // TODO: assert bindings in query } public function testInsertUpdateLineStringHasCorrectSql() @@ -421,7 +422,7 @@ class TestModel extends Model { use \Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait; - protected $spatialFields = ['point']; // only required when fetching, not saving + protected $spatialFields = ['point']; // TODO: only required when fetching, not saving public $timestamps = false; diff --git a/tests/Unit/Types/GeometryCollectionTest.php b/tests/Unit/Types/GeometryCollectionTest.php index 07759c5f..8f5b9081 100644 --- a/tests/Unit/Types/GeometryCollectionTest.php +++ b/tests/Unit/Types/GeometryCollectionTest.php @@ -35,7 +35,10 @@ public function testJsonSerialize() public function testInvalidArgumentExceptionNotArrayGeometries() { - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\GeometryCollection must be a collection of Grimzy\LaravelMysqlSpatial\Types\GeometryInterface' + ); $geometrycollection = new GeometryCollection([ $this->getPoint(), 1, @@ -85,7 +88,10 @@ public function testArrayAccess() $this->assertEquals($point100, $geometryCollection[100]); // assert invalid - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\GeometryCollection must be a collection of Grimzy\LaravelMysqlSpatial\Types\GeometryInterface' + ); $geometryCollection[] = 1; } diff --git a/tests/Unit/Types/GeometryTest.php b/tests/Unit/Types/GeometryTest.php index 8bd765a7..e51022d5 100644 --- a/tests/Unit/Types/GeometryTest.php +++ b/tests/Unit/Types/GeometryTest.php @@ -32,7 +32,10 @@ public function testGetWKTClass() $this->assertEquals(MultiLineString::class, Geometry::getWKTClass('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))')); $this->assertEquals(MultiPolygon::class, Geometry::getWKTClass('MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))')); $this->assertEquals(GeometryCollection::class, Geometry::getWKTClass('GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))')); - $this->assertException(UnknownWKTTypeException::class); + $this->assertException( + UnknownWKTTypeException::class, + 'Type was TRIANGLE' + ); Geometry::getWKTClass('TRIANGLE((0 0, 0 9, 9 0, 0 0))'); } diff --git a/tests/Unit/Types/MultiLineStringTest.php b/tests/Unit/Types/MultiLineStringTest.php index df7b42e4..563ecbd1 100644 --- a/tests/Unit/Types/MultiLineStringTest.php +++ b/tests/Unit/Types/MultiLineStringTest.php @@ -59,13 +59,19 @@ public function testJsonSerialize() public function testInvalidArgumentExceptionAtLeastOneEntry() { - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\MultiLineString must contain at least 1 entry' + ); $multilinestring = new MultiLineString([]); } public function testInvalidArgumentExceptionNotArrayOfLineString() { - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\MultiLineString must be a collection of Grimzy\LaravelMysqlSpatial\Types\LineString' + ); $multilinestring = new MultiLineString([ new LineString([new Point(0, 0), new Point(1, 1)]), new Point(0, 1), @@ -98,7 +104,10 @@ public function testArrayAccess() $this->assertEquals($linestring2, $multilinestring[2]); // assert invalid - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\MultiLineString must be a collection of Grimzy\LaravelMysqlSpatial\Types\LineString' + ); $multilinestring[] = 1; } } diff --git a/tests/Unit/Types/MultiPointTest.php b/tests/Unit/Types/MultiPointTest.php index 1e9bf7f4..6e94af87 100644 --- a/tests/Unit/Types/MultiPointTest.php +++ b/tests/Unit/Types/MultiPointTest.php @@ -58,13 +58,19 @@ public function testJsonSerialize() public function testInvalidArgumentExceptionAtLeastOneEntry() { - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\MultiPoint must contain at least 1 entry' + ); $multipoint = new MultiPoint([]); } public function testInvalidArgumentExceptionNotArrayOfLineString() { - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\MultiPoint must be a collection of Grimzy\LaravelMysqlSpatial\Types\Point' + ); $multipoint = new MultiPoint([ new Point(0, 0), 1, @@ -87,7 +93,10 @@ public function testArrayAccess() $this->assertEquals($point2, $multipoint[2]); // assert invalid - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\MultiPoint must be a collection of Grimzy\LaravelMysqlSpatial\Types\Point' + ); $multipoint[] = 1; } @@ -132,7 +141,10 @@ public function testDeprecatedInsertPoint() $this->assertEquals($point2, $multipoint[1]); $this->assertEquals($point3, $multipoint[2]); - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + '$index is greater than the size of the array' + ); $multipoint->insertPoint(100, new Point(100, 100)); } } diff --git a/tests/Unit/Types/MultiPolygonTest.php b/tests/Unit/Types/MultiPolygonTest.php index adcac1e1..f4df9983 100644 --- a/tests/Unit/Types/MultiPolygonTest.php +++ b/tests/Unit/Types/MultiPolygonTest.php @@ -70,7 +70,10 @@ public function testJsonSerialize() public function testInvalidArgumentExceptionNotArrayOfLineString() { - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\MultiPolygon must be a collection of Grimzy\LaravelMysqlSpatial\Types\Polygon' + ); $multipolygon = new MultiPolygon([ $this->getPolygon1(), $this->getLineString1(), @@ -94,7 +97,10 @@ public function testArrayAccess() $this->assertEquals($polygon2, $multipolygon[2]); // assert invalid - $this->assertException(InvalidArgumentException::class); + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\MultiPolygon must be a collection of Grimzy\LaravelMysqlSpatial\Types\Polygon' + ); $multipolygon[] = 1; } From ce1d086b365fd984a10921f46c8c6cedec362cda Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Tue, 3 Mar 2020 06:09:34 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI [ci skip] [skip ci] --- src/Types/GeometryCollection.php | 10 +++++++--- src/Types/MultiPolygon.php | 1 - 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index 69d7fe29..28251a38 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -154,7 +154,8 @@ public function jsonSerialize() * * @param array $items */ - protected function validateItems(array $items) { + protected function validateItems(array $items) + { $this->validateItemCount($items); foreach ($items as $item) { @@ -169,9 +170,11 @@ protected function validateItems(array $items) { * * @see $minimumCollectionItems */ - protected function validateItemCount(array $items) { + protected function validateItemCount(array $items) + { if (count($items) < $this->minimumCollectionItems) { $entries = $this->minimumCollectionItems === 1 ? 'entry' : 'entries'; + throw new InvalidArgumentException(sprintf( '%s must contain at least %d %s', get_class($this), $this->minimumCollectionItems, $entries )); @@ -185,7 +188,8 @@ protected function validateItemCount(array $items) { * * @see $collectionItemType */ - protected function validateItemType($item) { + protected function validateItemType($item) + { if (!$item instanceof $this->collectionItemType) { throw new InvalidArgumentException(sprintf( '%s must be a collection of %s', get_class($this), $this->collectionItemType diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php index 16f2769c..cd03f8bb 100644 --- a/src/Types/MultiPolygon.php +++ b/src/Types/MultiPolygon.php @@ -5,7 +5,6 @@ use GeoJson\GeoJson; use GeoJson\Geometry\MultiPolygon as GeoJsonMultiPolygon; use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; -use InvalidArgumentException; class MultiPolygon extends GeometryCollection { From 26e6bdefea3bfff88f4f9e5868b2a378c5c70fdf Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Sun, 8 Mar 2020 21:45:44 +0000 Subject: [PATCH 3/4] Apply fixes from StyleCI [ci skip] [skip ci] --- src/Types/GeometryCollection.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index 28251a38..b199feff 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -176,7 +176,10 @@ protected function validateItemCount(array $items) $entries = $this->minimumCollectionItems === 1 ? 'entry' : 'entries'; throw new InvalidArgumentException(sprintf( - '%s must contain at least %d %s', get_class($this), $this->minimumCollectionItems, $entries + '%s must contain at least %d %s', + get_class($this), + $this->minimumCollectionItems, + $entries )); } } @@ -192,7 +195,9 @@ protected function validateItemType($item) { if (!$item instanceof $this->collectionItemType) { throw new InvalidArgumentException(sprintf( - '%s must be a collection of %s', get_class($this), $this->collectionItemType + '%s must be a collection of %s', + get_class($this), + $this->collectionItemType )); } } From 8439802e62a0394b9a30c688ac4080c6a9723f44 Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Sun, 8 Mar 2020 17:31:46 -0400 Subject: [PATCH 4/4] Set $minimumCollectionItems to 0 for GeometryCollection and MultiPolygon. Adjusted child classes. Added/update tests. (cherry picked from commit 534dddc9d2f43550618196c4bfa4f4751acbb983) --- src/Types/GeometryCollection.php | 6 +++++- src/Types/MultiLineString.php | 7 +++++++ src/Types/MultiPoint.php | 7 +++++++ src/Types/MultiPolygon.php | 7 +++++++ tests/Integration/SpatialTest.php | 11 +++++++++++ tests/Unit/Types/GeometryCollectionTest.php | 22 +++++++++++++++++++++ tests/Unit/Types/MultiPolygonTest.php | 11 ++++++++++- 7 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index b199feff..4311685a 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -19,7 +19,7 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc * * @var int */ - protected $minimumCollectionItems = 1; + protected $minimumCollectionItems = 0; /** * The class of the items in the collection. @@ -66,6 +66,10 @@ public function __toString() public static function fromString($wktArgument) { + if (empty($wktArgument)) { + return new static([]); + } + $geometry_strings = preg_split('/,\s*(?=[A-Za-z])/', $wktArgument); return new static(array_map(function ($geometry_string) { diff --git a/src/Types/MultiLineString.php b/src/Types/MultiLineString.php index 64707d6a..dd815ebf 100644 --- a/src/Types/MultiLineString.php +++ b/src/Types/MultiLineString.php @@ -8,6 +8,13 @@ class MultiLineString extends GeometryCollection { + /** + * The minimum number of items required to create this collection. + * + * @var int + */ + protected $minimumCollectionItems = 1; + /** * The class of the items in the collection. * diff --git a/src/Types/MultiPoint.php b/src/Types/MultiPoint.php index fb55f9e8..eafa7c0e 100644 --- a/src/Types/MultiPoint.php +++ b/src/Types/MultiPoint.php @@ -8,6 +8,13 @@ class MultiPoint extends PointCollection { + /** + * The minimum number of items required to create this collection. + * + * @var int + */ + protected $minimumCollectionItems = 1; + public function toWKT() { return sprintf('MULTIPOINT(%s)', (string) $this); diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php index cd03f8bb..0a5b6784 100644 --- a/src/Types/MultiPolygon.php +++ b/src/Types/MultiPolygon.php @@ -8,6 +8,13 @@ class MultiPolygon extends GeometryCollection { + /** + * The minimum number of items required to create this collection. + * + * @var int + */ + protected $minimumCollectionItems = 1; + /** * The class of the items in the collection. * diff --git a/tests/Integration/SpatialTest.php b/tests/Integration/SpatialTest.php index 1d1eb2a5..f8ebe62b 100644 --- a/tests/Integration/SpatialTest.php +++ b/tests/Integration/SpatialTest.php @@ -175,6 +175,17 @@ public function testInsertGeometryCollection() $this->assertDatabaseHas('geometry', ['id' => $geo->id]); } + public function testInsertEmptyGeometryCollection() + { + $geo = new GeometryModel(); + + $geo->location = new Point(1, 2); + + $geo->multi_geometries = new GeometryCollection([]); + $geo->save(); + $this->assertDatabaseHas('geometry', ['id' => $geo->id]); + } + public function testUpdate() { $geo = new GeometryModel(); diff --git a/tests/Unit/Types/GeometryCollectionTest.php b/tests/Unit/Types/GeometryCollectionTest.php index 8f5b9081..4576a77b 100644 --- a/tests/Unit/Types/GeometryCollectionTest.php +++ b/tests/Unit/Types/GeometryCollectionTest.php @@ -33,6 +33,28 @@ public function testJsonSerialize() $this->assertSame('{"type":"GeometryCollection","geometries":[{"type":"LineString","coordinates":[[0,0],[1,0],[1,1],[0,1],[0,0]]},{"type":"Point","coordinates":[200,100]}]}', json_encode($this->getGeometryCollection()->jsonSerialize())); } + public function testCanCreateEmptyGeometryCollection() + { + $geometryCollection = new GeometryCollection([]); + $this->assertInstanceOf(GeometryCollection::class, $geometryCollection); + } + + public function testFromWKTWithEmptyGeometryCollection() + { + /** + * @var GeometryCollection + */ + $geometryCollection = GeometryCollection::fromWKT('GEOMETRYCOLLECTION()'); + $this->assertInstanceOf(GeometryCollection::class, $geometryCollection); + + $this->assertEquals(0, $geometryCollection->count()); + } + + public function testToWKTWithEmptyGeometryCollection() + { + $this->assertEquals('GEOMETRYCOLLECTION()', (new GeometryCollection([]))->toWKT()); + } + public function testInvalidArgumentExceptionNotArrayGeometries() { $this->assertException( diff --git a/tests/Unit/Types/MultiPolygonTest.php b/tests/Unit/Types/MultiPolygonTest.php index f4df9983..98e3ba2e 100644 --- a/tests/Unit/Types/MultiPolygonTest.php +++ b/tests/Unit/Types/MultiPolygonTest.php @@ -68,7 +68,16 @@ public function testJsonSerialize() $this->assertSame('{"type":"MultiPolygon","coordinates":[[[[0,0],[1,0],[1,1],[0,1],[0,0]],[[10,10],[20,10],[20,20],[10,20],[10,10]]],[[[100,100],[200,100],[200,200],[100,200],[100,100]]]]}', json_encode($this->getMultiPolygon())); } - public function testInvalidArgumentExceptionNotArrayOfLineString() + public function testInvalidArgumentExceptionAtLeastOneEntry() + { + $this->assertException( + InvalidArgumentException::class, + 'Grimzy\LaravelMysqlSpatial\Types\MultiPolygon must contain at least 1 entry' + ); + $multipolygon = new MultiPolygon([]); + } + + public function testInvalidArgumentExceptionNotArrayOfPolygon() { $this->assertException( InvalidArgumentException::class,