Skip to content

Bugfix: isDirty is always true after creation #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function set($model, string $key, $value, array $attributes): ?Expression
if (! ($value instanceof $this->className)) {
$geometryType = is_object($value) ? $value::class : gettype($value);
throw new InvalidArgumentException(
sprintf('Expected %s, %s given.', static::class, $geometryType)
sprintf('Expected %s, %s given.', $this->className, $geometryType)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated fix.

);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Traits/HasSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@

trait HasSpatial
{
public function originalIsEquivalent($key)
{
if (! array_key_exists($key, $this->original)) {
return false;
}

$casts = $this->getCasts();

if (array_key_exists($key, $casts)) {
$original = $this->getOriginal($key);
$attribute = $this->getAttributeValue($key);

if ($original instanceof Geometry && $attribute instanceof Geometry) {
return $original->getWktData() === $attribute->getWktData();
}
}

return parent::originalIsEquivalent($key);
}

public function scopeWithDistance(
Builder $query,
ExpressionContract|Geometry|string $column,
Expand Down
63 changes: 63 additions & 0 deletions tests/GeometryCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,66 @@

expect($testPlace->point)->toEqual($point);
});

it('checks a model record is not dirty after creation', function (): void {
$point = new Point(0, 180);

/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);

expect($testPlace->isDirty())->toBeFalse();
});

it('checks a model record is not dirty after fetch', function (): void {
$point = new Point(0, 180);
TestPlace::factory()->create(['point' => $point]);

/** @var TestPlace $testPlace */
$testPlace = TestPlace::firstOrFail();

expect($testPlace->isDirty())->toBeFalse();
});

it('checks a model record is dirty after update from null before save', function (): void {
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create([]);

$testPlace->point = $point;

expect($testPlace->isDirty())->toBeTrue();
});

it('checks a model record is dirty after update before save', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 0);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);

$testPlace->point = $point2;

expect($testPlace->isDirty())->toBeTrue();
});

it('checks a model record is not dirty after update and save', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 0);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);

$testPlace->point = $point2;
$testPlace->save();

expect($testPlace->isDirty())->toBeFalse();
});

it('checks a model record is not dirty after update to same value before save', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);

$testPlace->point = $point2;

expect($testPlace->isDirty())->toBeFalse();
});