From 972d3931ccdf5f4010ac928b9b4affd37ae4eb2e Mon Sep 17 00:00:00 2001 From: Craig Harley Date: Thu, 18 Jun 2020 10:01:04 +0100 Subject: [PATCH 1/2] with test --- .../Eloquent/Concerns/HasAttributes.php | 5 +++++ tests/Database/DatabaseEloquentModelTest.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 986e8a72121f..1ec1642cfbea 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -1178,6 +1178,11 @@ public function originalIsEquivalent($key, $current) } elseif ($this->hasCast($key, ['object', 'collection'])) { return $this->castAttribute($key, $current) == $this->castAttribute($key, $original); + } elseif ($this->hasCast($key, 'float')){ + return bccomp( + $this->castAttribute($key, $current), + $this->castAttribute($key, $original) + ) === 0; } elseif ($this->hasCast($key)) { return $this->castAttribute($key, $current) === $this->castAttribute($key, $original); diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index b5c0bc8689f2..d3a6f9e70c8d 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -148,6 +148,23 @@ public function testCleanAttributes() $this->assertFalse($model->isClean(['foo', 'bar'])); } + public function testCleanWhenFloatUpdateAttribute() + { + $original = -16.666347; + $new = 20.1-36.766347; + + // PHP isn't able to compare two floats using === reliably. + // See warning here: + // https://www.php.net/manual/en/language.types.float.php + + $this->assertTrue($original !== $new); + $this->assertTrue(bccomp($original, $new) === 0); + + $model = new EloquentModelStub(['castedFloat' => $original]); + $model->syncOriginal(); + $this->assertTrue($model->originalIsEquivalent('castedFloat', $new)); + } + public function testCalculatedAttributes() { $model = new EloquentModelStub; @@ -1992,6 +2009,7 @@ class EloquentModelStub extends Model protected $table = 'stub'; protected $guarded = []; protected $morph_to_stub_type = EloquentModelSaveStub::class; + protected $casts = ['castedFloat' => 'float']; public function getListItemsAttribute($value) { From 3b0ec9bfdce303c525a001346ca6c9322d4d4ec1 Mon Sep 17 00:00:00 2001 From: Craig Harley Date: Thu, 18 Jun 2020 10:10:38 +0100 Subject: [PATCH 2/2] styleci --- src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php | 2 +- tests/Database/DatabaseEloquentModelTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 1ec1642cfbea..6f2adac99443 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -1178,7 +1178,7 @@ public function originalIsEquivalent($key, $current) } elseif ($this->hasCast($key, ['object', 'collection'])) { return $this->castAttribute($key, $current) == $this->castAttribute($key, $original); - } elseif ($this->hasCast($key, 'float')){ + } elseif ($this->hasCast($key, 'float')) { return bccomp( $this->castAttribute($key, $current), $this->castAttribute($key, $original) diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index d3a6f9e70c8d..d7a6c9c1975d 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -151,7 +151,7 @@ public function testCleanAttributes() public function testCleanWhenFloatUpdateAttribute() { $original = -16.666347; - $new = 20.1-36.766347; + $new = 20.1 - 36.766347; // PHP isn't able to compare two floats using === reliably. // See warning here: