Skip to content

Unset classCastCache for given key #742

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 3 commits into from
Oct 6, 2023
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: 2 additions & 0 deletions src/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ protected function getFormattedValue(Model $model, string $key, $value)
$value = $this->castDatetimeUTC($model, $value);
}

unset($model->classCastCache[$key]);

return $model->castAttribute($key, $value);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Casts/Money.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace OwenIt\Auditing\Tests\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Tests\Models\Money as MoneyValueObject;

class Money implements CastsAttributes
{
/**
* {@inheritdoc}
*/
public function get(Model $model, string $key, mixed $value, array $attributes): MoneyValueObject
{
return new MoneyValueObject($value, 'USD');
}

/**
* {@inheritdoc}
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}
}
2 changes: 2 additions & 0 deletions tests/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
use OwenIt\Auditing\Tests\Casts\Money;

class Article extends Model implements Auditable
{
Expand All @@ -18,6 +19,7 @@ class Article extends Model implements Auditable
'reviewed' => 'bool',
'config' => 'json',
'published_at' => 'datetime',
'price' => Money::class,
];

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/Models/Money.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace OwenIt\Auditing\Tests\Models;

use NumberFormatter;

final class Money
{
/**
* Formatted value.
*/
public string $formatted;

/**
* Create a new money instance.
*/
public function __construct(
public string $amount,
public string $currency,
) {
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);

$this->formatted = $formatter->formatCurrency($this->amount, $this->currency);
}
}

34 changes: 34 additions & 0 deletions tests/Unit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' => '[email protected]',
]);

$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down