Skip to content

Commit

Permalink
Merge pull request #742 from bskl/issue/588
Browse files Browse the repository at this point in the history
Unset classCastCache for given key
  • Loading branch information
MortenDHansen authored Oct 6, 2023
2 parents 430e9cc + 1bec0f6 commit 8efe5b3
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
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 @@ -9,6 +9,7 @@
use OwenIt\Auditing\Redactors\LeftRedactor;
use OwenIt\Auditing\Resolvers\UrlResolver;
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 @@ -149,6 +150,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
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

0 comments on commit 8efe5b3

Please sign in to comment.