From c2f87295916b925e443b38a82e1ec559779e6396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Kartal?= Date: Thu, 15 Sep 2022 21:26:30 +0300 Subject: [PATCH 1/2] Unset classCastCache for given key --- src/Audit.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Audit.php b/src/Audit.php index d33ce6fc..84e1b787 100644 --- a/src/Audit.php +++ b/src/Audit.php @@ -136,6 +136,8 @@ protected function getFormattedValue(Model $model, string $key, $value) // Cast to native PHP type if ($model->hasCast($key)) { + unset($model->classCastCache[$key]); + return $model->castAttribute($key, $value); } From 1bec0f6f690246eb37ca0291c84bdc18a4ff70c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Kartal?= Date: Mon, 1 May 2023 23:02:27 +0300 Subject: [PATCH 2/2] Add test --- src/Audit.php | 4 +-- tests/Casts/Money.php | 26 ++++++++++++++ tests/Models/Article.php | 2 ++ tests/Models/Money.php | 26 ++++++++++++++ tests/Unit/AuditTest.php | 34 +++++++++++++++++++ ...0_00_000003_create_articles_test_table.php | 1 + 6 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/Casts/Money.php create mode 100644 tests/Models/Money.php diff --git a/src/Audit.php b/src/Audit.php index 8b94ebc1..899c4765 100644 --- a/src/Audit.php +++ b/src/Audit.php @@ -141,12 +141,12 @@ protected function getFormattedValue(Model $model, string $key, $value) // Cast to native PHP type if ($model->hasCast($key)) { - unset($model->classCastCache[$key]); - if ($model->getCastType($key) == 'datetime' ) { $value = $this->castDatetimeUTC($model, $value); } + unset($model->classCastCache[$key]); + return $model->castAttribute($key, $value); } diff --git a/tests/Casts/Money.php b/tests/Casts/Money.php new file mode 100644 index 00000000..48df70fd --- /dev/null +++ b/tests/Casts/Money.php @@ -0,0 +1,26 @@ + 'bool', 'config' => 'json', 'published_at' => 'datetime', + 'price' => Money::class, ]; /** diff --git a/tests/Models/Money.php b/tests/Models/Money.php new file mode 100644 index 00000000..82287df4 --- /dev/null +++ b/tests/Models/Money.php @@ -0,0 +1,26 @@ +formatted = $formatter->formatCurrency($this->amount, $this->currency); + } +} + diff --git a/tests/Unit/AuditTest.php b/tests/Unit/AuditTest.php index 0bd2b44c..d8181de1 100644 --- a/tests/Unit/AuditTest.php +++ b/tests/Unit/AuditTest.php @@ -8,6 +8,7 @@ use OwenIt\Auditing\Models\Audit; use OwenIt\Auditing\Redactors\LeftRedactor; use OwenIt\Auditing\Tests\Models\Article; +use OwenIt\Auditing\Tests\Models\Money; use OwenIt\Auditing\Tests\Models\User; class AuditTest extends AuditingTestCase @@ -148,6 +149,39 @@ public function itReturnsTheAppropriateAuditableDataValues() $this->assertNull($audit->getDataValue('invalid_key')); } + /** + * @group Audit::resolveData + * @group Audit::getDataValue + * @test + */ + public function itReturnsTheAppropriateAuditableDataValuesWithCustomCastValueObject() + { + $user = factory(User::class)->create([ + 'is_admin' => 1, + 'first_name' => 'rick', + 'last_name' => 'Sanchez', + 'email' => 'rick@wubba-lubba-dub.dub', + ]); + + $this->actingAs($user); + + $article = factory(Article::class)->create([ + 'title' => 'How To Audit Eloquent Models', + 'content' => 'First step: install the laravel-auditing package.', + 'reviewed' => 1, + 'published_at' => Carbon::now(), + 'price' => '12.45', + ]); + + $article->price = '24.68'; + $article->save(); + + $lastAudit = $article->audits()->skip(1)->first(); + + $this->assertEquals(new Money('24.68', 'USD'), $lastAudit->getModified()['price']['new']); + $this->assertEquals(new Money('12.45', 'USD'), $lastAudit->getModified()['price']['old']); + } + /** * @group Audit::getMetadata * @test diff --git a/tests/database/migrations/0000_00_00_000003_create_articles_test_table.php b/tests/database/migrations/0000_00_00_000003_create_articles_test_table.php index 6a47eade..4e0107af 100644 --- a/tests/database/migrations/0000_00_00_000003_create_articles_test_table.php +++ b/tests/database/migrations/0000_00_00_000003_create_articles_test_table.php @@ -20,6 +20,7 @@ public function up() $table->boolean('reviewed'); $table->timestamp('published_at')->nullable(); $table->json('config')->nullable(); + $table->string('price')->nullable(); $table->timestamps(); $table->softDeletes(); });