From 461ac1cb8ecb27c0448baf8b73a9f5833a8c97de Mon Sep 17 00:00:00 2001 From: Hitesh Date: Thu, 9 Mar 2023 18:04:27 +0530 Subject: [PATCH] Bugfix: Model spatial column update using Expression v2 branch --- src/GeometryCast.php | 4 ++++ tests/GeometryCastTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/GeometryCast.php b/src/GeometryCast.php index ee7227f..4a7ca1f 100644 --- a/src/GeometryCast.php +++ b/src/GeometryCast.php @@ -65,6 +65,10 @@ public function set($model, string $key, $value, array $attributes): Expression| $value = Geometry::fromArray($value); } + if ($value instanceof Expression) { + return $value; + } + if (! ($value instanceof $this->className)) { $geometryType = is_object($value) ? $value::class : gettype($value); throw new InvalidArgumentException( diff --git a/tests/GeometryCastTest.php b/tests/GeometryCastTest.php index 4d0f4ce..e22d6d1 100644 --- a/tests/GeometryCastTest.php +++ b/tests/GeometryCastTest.php @@ -27,6 +27,18 @@ expect($testPlace->point)->toEqual($point2); }); +it('updates a model record with expression', function (): void { + $point = new Point(0, 180); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); + $pointFromAttributes = $testPlace->getAttributes()['point']; + + expect(function () use ($testPlace, $pointFromAttributes): void { + $testPlace->update(['point' => $pointFromAttributes]); + })->not->toThrow(InvalidArgumentException::class); + expect(true)->toBeTrue(); // because of Pest's bug: https://github.com/pestphp/pest/issues/657 +}); + it('updates a model record with null geometry', function (): void { $point = new Point(0, 180); /** @var TestPlace $testPlace */