Skip to content

Commit

Permalink
Bugfix: Model spatial column update using Expression (#81)
Browse files Browse the repository at this point in the history
* add test

* Fix

---------

Co-authored-by: Matan Yadaev <[email protected]>
  • Loading branch information
MatanYadaev and Matan Yadaev authored Feb 16, 2023
1 parent 30b6cff commit eec4775
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public function set($model, string $key, $value, array $attributes): ExpressionC
$value = Geometry::fromArray($value);
}

if ($value instanceof ExpressionContract) {
return $value;
}

if (! ($value instanceof $this->className)) {
$geometryType = is_object($value) ? $value::class : gettype($value);
throw new InvalidArgumentException(
Expand Down
12 changes: 12 additions & 0 deletions tests/GeometryCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit eec4775

Please sign in to comment.