From f16e304223c1dd299a0a275e328bd070d278d29e Mon Sep 17 00:00:00 2001 From: Matan Yadaev Date: Wed, 24 Jul 2024 19:04:12 +0300 Subject: [PATCH] fixes --- ...MySQL 8.0.run.xml => Test - MySQL.run.xml} | 4 +- CHANGELOG.md | 12 --- README.md | 96 +++++++++++-------- src/EloquentSpatial.php | 28 +++--- src/GeometryCast.php | 2 +- src/GeometryExpression.php | 4 +- tests/HasSpatialTest.php | 6 +- tests/Objects/GeometryCollectionTest.php | 36 +++++-- tests/Objects/GeometryTest.php | 3 - tests/Objects/LineStringTest.php | 22 ++++- tests/Objects/MultiLineStringTest.php | 24 ++++- tests/Objects/MultiPointTest.php | 21 +++- tests/Objects/MultiPolygonTest.php | 29 +++++- tests/Objects/PointTest.php | 19 +++- tests/Objects/PolygonTest.php | 27 +++++- tests/TestModels/TestExtendedPlace.php | 2 - .../ExtendedGeometryCollection.php | 4 +- tests/TestObjects/ExtendedLineString.php | 4 +- tests/TestObjects/ExtendedMultiLineString.php | 4 +- tests/TestObjects/ExtendedMultiPoint.php | 4 +- tests/TestObjects/ExtendedMultiPolygon.php | 4 +- tests/TestObjects/ExtendedPoint.php | 4 +- tests/TestObjects/ExtendedPolygon.php | 4 +- 23 files changed, 262 insertions(+), 101 deletions(-) rename .run/{Test - MySQL 8.0.run.xml => Test - MySQL.run.xml} (69%) diff --git a/.run/Test - MySQL 8.0.run.xml b/.run/Test - MySQL.run.xml similarity index 69% rename from .run/Test - MySQL 8.0.run.xml rename to .run/Test - MySQL.run.xml index 4a8f694..a49ed0d 100644 --- a/.run/Test - MySQL 8.0.run.xml +++ b/.run/Test - MySQL.run.xml @@ -1,7 +1,9 @@ - + + + diff --git a/CHANGELOG.md b/CHANGELOG.md index d52f6bb..0457e89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,18 +2,6 @@ All notable changes to `laravel-eloquent-spatial` will be documented in this file. -## v4.2.1 - 2024-04-02 - -### What's Changed - -* `Geometry::fromArray` added optional`$srid` paramter by @ju-gow in https://github.com/MatanYadaev/laravel-eloquent-spatial/pull/118 - -### New Contributors - -* @ju-gow made their first contribution in https://github.com/MatanYadaev/laravel-eloquent-spatial/pull/118 - -**Full Changelog**: https://github.com/MatanYadaev/laravel-eloquent-spatial/compare/4.2.0...4.2.1 - ## v4.2.0 - 2024-03-13 ### What's Changed diff --git a/README.md b/README.md index 49ab972..1655894 100644 --- a/README.md +++ b/README.md @@ -152,33 +152,58 @@ For more comprehensive documentation on the API, please refer to the [API](API.m ## Extension +### Extend Geometry class with macros + +You can add new methods to the `Geometry` class through macros. + +Here's an example of how to register a macro in your service provider's `boot` method: + +```php +class AppServiceProvider extends ServiceProvider +{ + public function boot(): void + { + Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + return class_basename($this); + }); + } +} +``` + +Use the method in your code: + +```php +$londonEyePoint = new Point(51.5032973, -0.1217424); + +echo $londonEyePoint->getName(); // Point +``` + ### Extend with custom geometry classes -You can extend the geometry classes by creating custom geometry classes and add or overwrite certain functionality. +You can extend the geometry classes by creating custom geometry classes and add functionality. You can also override existing methods, although it is not recommended, as it may lead to unexpected behavior. -Here's an example how to overwrite the `toArray()` method by adding array keys to the `coordinates` array and removing the `type` key. +1. Create a custom geometry class that extends the base geometry class. ```php -use MatanYadaev\EloquentSpatial\Objects\Point as EloquentSpatialPoint; +use MatanYadaev\EloquentSpatial\Objects\Point; -class Point extends EloquentSpatialPoint +class ExtendedPoint extends Point { - public function toArray(): array + public function toCustomArray(): array { - return [ - 'coordinates' => [ - 'longitude' => $this->longitude, - 'latitude' => $this->latitude, - ], - ]; + return 'coordinates' => [ + 'latitude' => $this->latitude, + 'longitude' => $this->longitude + ] } } ``` -All we have to do is tell the package that we want to use are our custom Point class, we can do this in the AppServiceProvider under the `boot()` method. +2. Update the geometry class mapping in a service provider file. ```php -use App\ValueObjects\Point; +use App\ValueObjects\ExtendedPoint; use Illuminate\Support\ServiceProvider; use MatanYadaev\EloquentSpatial\EloquentSpatial; @@ -186,56 +211,49 @@ class AppServiceProvider extends ServiceProvider { public function boot(): void { - EloquentSpatial::usePoint(Point::class); + EloquentSpatial::usePoint(ExtendedPoint::class); } } ``` -Update your model to use the custom geometry class in the `casts()` method +3. Update your model to use the custom geometry class in the `$casts` property or `casts()` method. ```php -use App\ValueObjects\Point; +use App\ValueObjects\ExtendedPoint; use Illuminate\Database\Eloquent\Model; use MatanYadaev\EloquentSpatial\Traits\HasSpatial; -class Location extends Model +class Place extends Model { use HasSpatial; + + protected $casts = [ + 'coordinates' => ExtendedPoint::class, + ]; + + // Or: protected function casts(): array { return [ - 'coordinates' => Point::class, + 'coordinates' => ExtendedPoint::class, ]; } } ``` -### Extend Geometry class with macros - -You can add new methods to the `Geometry` class through macros. - -Here's an example of how to register a macro in your service provider's `boot` method: +4. Use the custom geometry class in your code. ```php -class AppServiceProvider extends ServiceProvider -{ - public function boot(): void - { - Geometry::macro('getName', function (): string { - /** @var Geometry $this */ - return class_basename($this); - }); - } -} -``` +use App\Models\Location; +use App\ValueObjects\ExtendedPoint; -Use the method in your code: - -```php -$londonEyePoint = new Point(51.5032973, -0.1217424); +$place = Place::create([ + 'name' => 'London Eye', + 'coordinates' => new ExtendedPoint(51.5032973, -0.1217424), +]); -echo $londonEyePoint->getName(); // Point +echo $place->coordinates->toCustomArray(); // ['longitude' => -0.1217424, 'latitude' => 51.5032973] ``` ## Development diff --git a/src/EloquentSpatial.php b/src/EloquentSpatial.php index 74c7cf7..39995ee 100644 --- a/src/EloquentSpatial.php +++ b/src/EloquentSpatial.php @@ -14,29 +14,29 @@ class EloquentSpatial { - /** @var class-string<\MatanYadaev\EloquentSpatial\Objects\GeometryCollection> */ + /** @var class-string */ public static string $geometryCollection = GeometryCollection::class; - /** @var class-string<\MatanYadaev\EloquentSpatial\Objects\LineString> */ + /** @var class-string */ public static string $lineString = LineString::class; - /** @var class-string<\MatanYadaev\EloquentSpatial\Objects\MultiLineString> */ + /** @var class-string */ public static string $multiLineString = MultiLineString::class; - /** @var class-string<\MatanYadaev\EloquentSpatial\Objects\MultiPoint> */ + /** @var class-string */ public static string $multiPoint = MultiPoint::class; - /** @var class-string<\MatanYadaev\EloquentSpatial\Objects\MultiPolygon> */ + /** @var class-string */ public static string $multiPolygon = MultiPolygon::class; - /** @var class-string<\MatanYadaev\EloquentSpatial\Objects\Point> */ + /** @var class-string */ public static string $point = Point::class; - /** @var class-string<\MatanYadaev\EloquentSpatial\Objects\Polygon> */ + /** @var class-string */ public static string $polygon = Polygon::class; /** - * @param class-string<\MatanYadaev\EloquentSpatial\Objects\GeometryCollection> $class + * @param class-string $class */ public static function useGeometryCollection(string $class): string { @@ -46,7 +46,7 @@ public static function useGeometryCollection(string $class): string } /** - * @param class-string<\MatanYadaev\EloquentSpatial\Objects\LineString> $class + * @param class-string $class */ public static function useLineString(string $class): string { @@ -56,7 +56,7 @@ public static function useLineString(string $class): string } /** - * @param class-string<\MatanYadaev\EloquentSpatial\Objects\MultiLineString> $class + * @param class-string $class */ public static function useMultiLineString(string $class): string { @@ -66,7 +66,7 @@ public static function useMultiLineString(string $class): string } /** - * @param class-string<\MatanYadaev\EloquentSpatial\Objects\MultiPoint> $class + * @param class-string $class */ public static function useMultiPoint(string $class): string { @@ -76,7 +76,7 @@ public static function useMultiPoint(string $class): string } /** - * @param class-string<\MatanYadaev\EloquentSpatial\Objects\MultiPolygon> $class + * @param class-string $class */ public static function useMultiPolygon(string $class): string { @@ -86,7 +86,7 @@ public static function useMultiPolygon(string $class): string } /** - * @param class-string<\MatanYadaev\EloquentSpatial\Objects\Point> $class + * @param class-string $class */ public static function usePoint(string $class): string { @@ -96,7 +96,7 @@ public static function usePoint(string $class): string } /** - * @param class-string<\MatanYadaev\EloquentSpatial\Objects\Polygon> $class + * @param class-string $class */ public static function usePolygon(string $class): string { diff --git a/src/GeometryCast.php b/src/GeometryCast.php index 213f4f6..fbe00c6 100644 --- a/src/GeometryCast.php +++ b/src/GeometryCast.php @@ -66,7 +66,7 @@ public function set($model, string $key, $value, array $attributes): ?Expression return $value; } - if (! ($value instanceof $this->className)) { + if (! ($value instanceof $this->className) || get_class($value) !== $this->className) { $geometryType = is_object($value) ? $value::class : gettype($value); throw new InvalidArgumentException( sprintf('Expected %s, %s given.', $this->className, $geometryType) diff --git a/src/GeometryExpression.php b/src/GeometryExpression.php index fbf2607..2b4fed7 100644 --- a/src/GeometryExpression.php +++ b/src/GeometryExpression.php @@ -10,7 +10,9 @@ /** @codeCoverageIgnore */ class GeometryExpression { - public function __construct(readonly private string $expression) {} + public function __construct(readonly private string $expression) + { + } public function normalize(ConnectionInterface $connection): string { diff --git a/tests/HasSpatialTest.php b/tests/HasSpatialTest.php index a949ae2..1a967a2 100644 --- a/tests/HasSpatialTest.php +++ b/tests/HasSpatialTest.php @@ -447,7 +447,7 @@ }); it('toExpressionString can handle a Expression input', function (): void { - $spatialBuilder = new TestPlace; + $spatialBuilder = new TestPlace(); $toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString'); $result = $toExpressionStringMethod->invoke($spatialBuilder, DB::raw('POINT(longitude, latitude)')); @@ -456,7 +456,7 @@ }); it('toExpressionString can handle a Geometry input', function (): void { - $testPlace = new TestPlace; + $testPlace = new TestPlace(); $toExpressionStringMethod = (new ReflectionClass($testPlace))->getMethod('toExpressionString'); $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'); @@ -469,7 +469,7 @@ }); it('toExpressionString can handle a string input', function (): void { - $spatialBuilder = new TestPlace; + $spatialBuilder = new TestPlace(); $toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString'); $result = $toExpressionStringMethod->invoke($spatialBuilder, 'test_places.point'); diff --git a/tests/Objects/GeometryCollectionTest.php b/tests/Objects/GeometryCollectionTest.php index 54a0d75..5c579db 100644 --- a/tests/Objects/GeometryCollectionTest.php +++ b/tests/Objects/GeometryCollectionTest.php @@ -486,6 +486,7 @@ it('adds a macro toGeometryCollection', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ + // @phpstan-ignore-next-line return class_basename($this); }); @@ -507,8 +508,8 @@ }); it('uses an extended GeometryCollection class', function (): void { + // Arrange EloquentSpatial::useGeometryCollection(ExtendedGeometryCollection::class); - $geometryCollection = new ExtendedGeometryCollection([ new Polygon([ new LineString([ @@ -522,18 +523,18 @@ new Point(0, 180), ], 4326); + // Act /** @var TestExtendedPlace $testPlace */ $testPlace = TestExtendedPlace::factory()->create(['geometry_collection' => $geometryCollection])->fresh(); - expect($testPlace->geometry_collection) - ->toBeInstanceOf(ExtendedGeometryCollection::class) - ->and($testPlace->geometry_collection) - ->toEqual($geometryCollection); + // Assert + expect($testPlace->geometry_collection)->toBeInstanceOf(ExtendedGeometryCollection::class); + expect($testPlace->geometry_collection)->toEqual($geometryCollection); }); it('throws exception when storing a record with regular GeometryCollection instead of the extended one', function (): void { + // Arrange EloquentSpatial::useGeometryCollection(ExtendedGeometryCollection::class); - $geometryCollection = new GeometryCollection([ new Polygon([ new LineString([ @@ -547,7 +548,30 @@ new Point(0, 180), ], 4326); + // Act & Assert expect(function () use ($geometryCollection): void { TestExtendedPlace::factory()->create(['geometry_collection' => $geometryCollection]); })->toThrow(InvalidArgumentException::class); }); + +it('throws exception when storing a record with extended GeometryCollection instead of the regular one', function (): void { + // Arrange + EloquentSpatial::useGeometryCollection(ExtendedGeometryCollection::class); + $geometryCollection = new ExtendedGeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ], 4326); + + // Act & Assert + expect(function () use ($geometryCollection): void { + TestPlace::factory()->create(['geometry_collection' => $geometryCollection]); + })->toThrow(InvalidArgumentException::class); +}); diff --git a/tests/Objects/GeometryTest.php b/tests/Objects/GeometryTest.php index f636c48..d157f4b 100644 --- a/tests/Objects/GeometryTest.php +++ b/tests/Objects/GeometryTest.php @@ -2,7 +2,6 @@ use Illuminate\Database\PostgresConnection; use Illuminate\Database\QueryException; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\DB; use MatanYadaev\EloquentSpatial\AxisOrder; use MatanYadaev\EloquentSpatial\Enums\Srid; @@ -12,8 +11,6 @@ use MatanYadaev\EloquentSpatial\Objects\Point; use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; -uses(DatabaseMigrations::class); - it('throws exception when generating geometry from other geometry WKB', function (): void { expect(function (): void { $pointWkb = (new Point(0, 180))->toWkb(); diff --git a/tests/Objects/LineStringTest.php b/tests/Objects/LineStringTest.php index 7f89954..7a8959e 100644 --- a/tests/Objects/LineStringTest.php +++ b/tests/Objects/LineStringTest.php @@ -200,6 +200,7 @@ it('adds a macro toLineString', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ + // @phpstan-ignore-next-line return class_basename($this); }); @@ -213,29 +214,46 @@ }); it('uses an extended LineString class', function (): void { + // Arrange EloquentSpatial::useLineString(ExtendedLineString::class); - $lineString = new ExtendedLineString([ new Point(0, 180), new Point(1, 179), ], 4326); + // Act /** @var TestExtendedPlace $testPlace */ $testPlace = TestExtendedPlace::factory()->create(['line_string' => $lineString])->fresh(); + // Assert expect($testPlace->line_string)->toBeInstanceOf(ExtendedLineString::class); expect($testPlace->line_string)->toEqual($lineString); }); it('throws exception when storing a record with regular LineString instead of the extended one', function (): void { + // Arrange EloquentSpatial::useLineString(ExtendedLineString::class); - $lineString = new LineString([ new Point(0, 180), new Point(1, 179), ], 4326); + // Act & Assert expect(function () use ($lineString): void { TestExtendedPlace::factory()->create(['line_string' => $lineString]); })->toThrow(InvalidArgumentException::class); }); + +it('throws exception when storing a record with extended LineString instead of the regular one', function (): void { + // Arrange + EloquentSpatial::useLineString(LineString::class); + $lineString = new ExtendedLineString([ + new Point(0, 180), + new Point(1, 179), + ], 4326); + + // Act & Assert + expect(function () use ($lineString): void { + TestPlace::factory()->create(['line_string' => $lineString]); + })->toThrow(InvalidArgumentException::class); +}); diff --git a/tests/Objects/MultiLineStringTest.php b/tests/Objects/MultiLineStringTest.php index 0634d0f..73bcde9 100644 --- a/tests/Objects/MultiLineStringTest.php +++ b/tests/Objects/MultiLineStringTest.php @@ -228,6 +228,7 @@ it('adds a macro toMultiLineString', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ + // @phpstan-ignore-next-line return class_basename($this); }); @@ -243,8 +244,8 @@ }); it('uses an extended MultiLineString class', function (): void { + // Arrange EloquentSpatial::useMultiLineString(ExtendedMultiLineString::class); - $multiLineString = new ExtendedMultiLineString([ new LineString([ new Point(0, 180), @@ -252,16 +253,18 @@ ]), ], 4326); + // Act /** @var TestExtendedPlace $testPlace */ $testPlace = TestExtendedPlace::factory()->create(['multi_line_string' => $multiLineString])->fresh(); + // Assert expect($testPlace->multi_line_string)->toBeInstanceOf(ExtendedMultiLineString::class); expect($testPlace->multi_line_string)->toEqual($multiLineString); }); it('throws exception when storing a record with regular MultiLineString instead of the extended one', function (): void { + // Arrange EloquentSpatial::useMultiLineString(ExtendedMultiLineString::class); - $multiLineString = new MultiLineString([ new LineString([ new Point(0, 180), @@ -269,7 +272,24 @@ ]), ], 4326); + // Act & Assert expect(function () use ($multiLineString): void { TestExtendedPlace::factory()->create(['multi_line_string' => $multiLineString]); })->toThrow(InvalidArgumentException::class); }); + +it('throws exception when storing a record with extended MultiLineString instead of the regular one', function (): void { + // Arrange + EloquentSpatial::useMultiLineString(MultiLineString::class); + $multiLineString = new ExtendedMultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ], 4326); + + // Act & Assert + expect(function () use ($multiLineString): void { + TestPlace::factory()->create(['multi_line_string' => $multiLineString]); + })->toThrow(InvalidArgumentException::class); +}); diff --git a/tests/Objects/MultiPointTest.php b/tests/Objects/MultiPointTest.php index ae9b92d..39667d6 100644 --- a/tests/Objects/MultiPointTest.php +++ b/tests/Objects/MultiPointTest.php @@ -183,6 +183,7 @@ it('adds a macro toMultiPoint', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ + // @phpstan-ignore-next-line return class_basename($this); }); @@ -195,27 +196,43 @@ }); it('uses an extended MultiPoint class', function (): void { + // Arrange EloquentSpatial::useMultiPoint(ExtendedMultiPoint::class); - $multiPoint = new ExtendedMultiPoint([ new Point(0, 180), ], 4326); + // Act /** @var TestExtendedPlace $testPlace */ $testPlace = TestExtendedPlace::factory()->create(['multi_point' => $multiPoint])->fresh(); + // Assert expect($testPlace->multi_point)->toBeInstanceOf(ExtendedMultiPoint::class); expect($testPlace->multi_point)->toEqual($multiPoint); }); it('throws exception when storing a record with regular MultiPoint instead of the extended one', function (): void { + // Arrange EloquentSpatial::useMultiPoint(ExtendedMultiPoint::class); - $multiPoint = new MultiPoint([ new Point(0, 180), ], 4326); + // Act & Assert expect(function () use ($multiPoint): void { TestExtendedPlace::factory()->create(['multi_point' => $multiPoint]); })->toThrow(InvalidArgumentException::class); }); + +it('throws exception when storing a record with extended MultiPoint instead of the regular one', function (): void { + // Arrange + EloquentSpatial::useMultiPoint(MultiPoint::class); + $multiPoint = new ExtendedMultiPoint([ + new Point(0, 180), + ], 4326); + + // Act & Assert + expect(function () use ($multiPoint): void { + TestPlace::factory()->create(['multi_point' => $multiPoint]); + })->toThrow(InvalidArgumentException::class); +}); diff --git a/tests/Objects/MultiPolygonTest.php b/tests/Objects/MultiPolygonTest.php index 76321f1..ed9eea9 100644 --- a/tests/Objects/MultiPolygonTest.php +++ b/tests/Objects/MultiPolygonTest.php @@ -304,6 +304,7 @@ it('adds a macro toMultiPolygon', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ + // @phpstan-ignore-next-line return class_basename($this); }); @@ -324,8 +325,8 @@ }); it('uses an extended MultiPolygon class', function (): void { + // Arrange EloquentSpatial::useMultiPolygon(ExtendedMultiPolygon::class); - $multiPolygon = new ExtendedMultiPolygon([ new Polygon([ new LineString([ @@ -338,16 +339,18 @@ ]), ], 4326); + // Act /** @var TestExtendedPlace $testPlace */ $testPlace = TestExtendedPlace::factory()->create(['multi_polygon' => $multiPolygon])->fresh(); + // Assert expect($testPlace->multi_polygon)->toBeInstanceOf(ExtendedMultiPolygon::class); expect($testPlace->multi_polygon)->toEqual($multiPolygon); }); it('throws exception when storing a record with regular MultiPolygon instead of the extended one', function (): void { + // Arrange EloquentSpatial::useMultiPolygon(ExtendedMultiPolygon::class); - $multiPolygon = new MultiPolygon([ new Polygon([ new LineString([ @@ -360,7 +363,29 @@ ]), ], 4326); + // Act & Assert expect(function () use ($multiPolygon): void { TestExtendedPlace::factory()->create(['multi_polygon' => $multiPolygon]); })->toThrow(InvalidArgumentException::class); }); + +it('throws exception when storing a record with extended MultiPolygon instead of the regular one', function (): void { + // Arrange + EloquentSpatial::useMultiPolygon(MultiPolygon::class); + $multiPolygon = new ExtendedMultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ], 4326); + + // Act & Assert + expect(function () use ($multiPolygon): void { + TestPlace::factory()->create(['multi_polygon' => $multiPolygon]); + })->toThrow(InvalidArgumentException::class); +}); diff --git a/tests/Objects/PointTest.php b/tests/Objects/PointTest.php index 1462b29..d35645f 100644 --- a/tests/Objects/PointTest.php +++ b/tests/Objects/PointTest.php @@ -133,6 +133,7 @@ it('adds a macro toPoint', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ + // @phpstan-ignore-next-line return class_basename($this); }); @@ -143,23 +144,37 @@ }); it('uses an extended Point class', function (): void { + // Arrange EloquentSpatial::usePoint(ExtendedPoint::class); - $point = new ExtendedPoint(0, 180, 4326); + // Act /** @var TestExtendedPlace $testPlace */ $testPlace = TestExtendedPlace::factory()->create(['point' => $point])->fresh(); + // Assert expect($testPlace->point)->toBeInstanceOf(ExtendedPoint::class); expect($testPlace->point)->toEqual($point); }); it('throws exception when storing a record with regular Point instead of the extended one', function (): void { + // Arrange EloquentSpatial::usePoint(ExtendedPoint::class); - $point = new Point(0, 180, 4326); + // Act & Assert expect(function () use ($point): void { TestExtendedPlace::factory()->create(['point' => $point]); })->toThrow(InvalidArgumentException::class); }); + +it('throws exception when storing a record with extended Point instead of the regular one', function (): void { + // Arrange + EloquentSpatial::usePoint(Point::class); + $point = new ExtendedPoint(0, 180, 4326); + + // Act & Assert + expect(function () use ($point): void { + TestPlace::factory()->create(['point' => $point]); + })->toThrow(InvalidArgumentException::class); +}); diff --git a/tests/Objects/PolygonTest.php b/tests/Objects/PolygonTest.php index c101369..316ee0f 100644 --- a/tests/Objects/PolygonTest.php +++ b/tests/Objects/PolygonTest.php @@ -273,6 +273,7 @@ it('adds a macro toPolygon', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ + // @phpstan-ignore-next-line return class_basename($this); }); @@ -291,8 +292,8 @@ }); it('uses an extended Polygon class', function (): void { + // Arrange EloquentSpatial::usePolygon(ExtendedPolygon::class); - $polygon = new ExtendedPolygon([ new LineString([ new Point(0, 180), @@ -303,16 +304,18 @@ ]), ], 4326); + // Act /** @var TestExtendedPlace $testPlace */ $testPlace = TestExtendedPlace::factory()->create(['polygon' => $polygon])->fresh(); + // Assert expect($testPlace->polygon)->toBeInstanceOf(ExtendedPolygon::class); expect($testPlace->polygon)->toEqual($polygon); }); it('throws exception when storing a record with regular Polygon instead of the extended one', function (): void { + // Arrange EloquentSpatial::usePolygon(ExtendedPolygon::class); - $polygon = new Polygon([ new LineString([ new Point(0, 180), @@ -323,7 +326,27 @@ ]), ], 4326); + // Act & Assert expect(function () use ($polygon): void { TestExtendedPlace::factory()->create(['polygon' => $polygon]); })->toThrow(InvalidArgumentException::class); }); + +it('throws exception when storing a record with extended Polygon instead of the regular one', function (): void { + // Arrange + EloquentSpatial::usePolygon(Polygon::class); + $polygon = new ExtendedPolygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ], 4326); + + // Act & Assert + expect(function () use ($polygon): void { + TestPlace::factory()->create(['polygon' => $polygon]); + })->toThrow(InvalidArgumentException::class); +}); diff --git a/tests/TestModels/TestExtendedPlace.php b/tests/TestModels/TestExtendedPlace.php index 12f2d11..5e0d9ba 100644 --- a/tests/TestModels/TestExtendedPlace.php +++ b/tests/TestModels/TestExtendedPlace.php @@ -4,8 +4,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -use MatanYadaev\EloquentSpatial\Objects\Point; -use MatanYadaev\EloquentSpatial\Objects\Polygon; use MatanYadaev\EloquentSpatial\Tests\TestFactories\TestExtendedPlaceFactory; use MatanYadaev\EloquentSpatial\Tests\TestObjects\ExtendedGeometryCollection; use MatanYadaev\EloquentSpatial\Tests\TestObjects\ExtendedLineString; diff --git a/tests/TestObjects/ExtendedGeometryCollection.php b/tests/TestObjects/ExtendedGeometryCollection.php index ddc5412..87036a2 100644 --- a/tests/TestObjects/ExtendedGeometryCollection.php +++ b/tests/TestObjects/ExtendedGeometryCollection.php @@ -4,4 +4,6 @@ use MatanYadaev\EloquentSpatial\Objects\GeometryCollection; -class ExtendedGeometryCollection extends GeometryCollection {} +class ExtendedGeometryCollection extends GeometryCollection +{ +} diff --git a/tests/TestObjects/ExtendedLineString.php b/tests/TestObjects/ExtendedLineString.php index e4e1e43..aaa946b 100644 --- a/tests/TestObjects/ExtendedLineString.php +++ b/tests/TestObjects/ExtendedLineString.php @@ -4,4 +4,6 @@ use MatanYadaev\EloquentSpatial\Objects\LineString; -class ExtendedLineString extends LineString {} +class ExtendedLineString extends LineString +{ +} diff --git a/tests/TestObjects/ExtendedMultiLineString.php b/tests/TestObjects/ExtendedMultiLineString.php index 82f1f9c..a63f68e 100644 --- a/tests/TestObjects/ExtendedMultiLineString.php +++ b/tests/TestObjects/ExtendedMultiLineString.php @@ -4,4 +4,6 @@ use MatanYadaev\EloquentSpatial\Objects\MultiLineString; -class ExtendedMultiLineString extends MultiLineString {} +class ExtendedMultiLineString extends MultiLineString +{ +} diff --git a/tests/TestObjects/ExtendedMultiPoint.php b/tests/TestObjects/ExtendedMultiPoint.php index 86914cf..6ec5ca2 100644 --- a/tests/TestObjects/ExtendedMultiPoint.php +++ b/tests/TestObjects/ExtendedMultiPoint.php @@ -4,4 +4,6 @@ use MatanYadaev\EloquentSpatial\Objects\MultiPoint; -class ExtendedMultiPoint extends MultiPoint {} +class ExtendedMultiPoint extends MultiPoint +{ +} diff --git a/tests/TestObjects/ExtendedMultiPolygon.php b/tests/TestObjects/ExtendedMultiPolygon.php index 9b32cad..e729f07 100644 --- a/tests/TestObjects/ExtendedMultiPolygon.php +++ b/tests/TestObjects/ExtendedMultiPolygon.php @@ -4,4 +4,6 @@ use MatanYadaev\EloquentSpatial\Objects\MultiPolygon; -class ExtendedMultiPolygon extends MultiPolygon {} +class ExtendedMultiPolygon extends MultiPolygon +{ +} diff --git a/tests/TestObjects/ExtendedPoint.php b/tests/TestObjects/ExtendedPoint.php index 7ad879d..d8aff43 100644 --- a/tests/TestObjects/ExtendedPoint.php +++ b/tests/TestObjects/ExtendedPoint.php @@ -4,4 +4,6 @@ use MatanYadaev\EloquentSpatial\Objects\Point; -class ExtendedPoint extends Point {} +class ExtendedPoint extends Point +{ +} diff --git a/tests/TestObjects/ExtendedPolygon.php b/tests/TestObjects/ExtendedPolygon.php index f175e17..d148a20 100644 --- a/tests/TestObjects/ExtendedPolygon.php +++ b/tests/TestObjects/ExtendedPolygon.php @@ -4,4 +4,6 @@ use MatanYadaev\EloquentSpatial\Objects\Polygon; -class ExtendedPolygon extends Polygon {} +class ExtendedPolygon extends Polygon +{ +}