From 756ff1c8e86b156a1423e8c5bd95a879a870a661 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 7 Nov 2024 13:53:01 +0000 Subject: [PATCH] fix: dont run properties through accessors when serialising --- src/Support/ModelAttributeFormatter.php | 17 ++++++++--------- tests/Feature/Orbital/CreateTest.php | 11 +++++++++++ tests/Feature/Orbital/SoftDeleteTest.php | 4 ++-- tests/Fixtures/Models/Post.php | 7 +++++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Support/ModelAttributeFormatter.php b/src/Support/ModelAttributeFormatter.php index c696a70..5b8c12f 100644 --- a/src/Support/ModelAttributeFormatter.php +++ b/src/Support/ModelAttributeFormatter.php @@ -3,24 +3,23 @@ namespace Orbit\Support; use BackedEnum; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Arr; use Orbit\Contracts\Orbit; class ModelAttributeFormatter { public static function format(Orbit&Model $model, array $attributes): array { - $formatted = []; + return Arr::map($attributes, static function (mixed $value, string $key) use ($model) { + $cast = $model->{$key}; - foreach ($attributes as $key => $value) { - $value = $model->{$key}; - - $formatted[$key] = match (true) { - $value instanceof BackedEnum => $value->value, + return match (true) { + $cast instanceof BackedEnum => $cast->value, + $cast instanceof Carbon => $cast->toIso8601String(), default => $value, }; - } - - return $formatted; + }); } } diff --git a/tests/Feature/Orbital/CreateTest.php b/tests/Feature/Orbital/CreateTest.php index fe46528..2c9369b 100644 --- a/tests/Feature/Orbital/CreateTest.php +++ b/tests/Feature/Orbital/CreateTest.php @@ -15,3 +15,14 @@ title: 'Example Post' MD); }); + +it('does not use accessors when serialising saved data', function () { + $post = Post::create([ + 'title' => 'hello', + ]); + + expect(base_path("content/posts/{$post->id}.md")) + ->toBeFile() + ->and(file_get_contents(base_path("content/posts/{$post->id}.md"))) + ->not->toContain('Hello'); +}); diff --git a/tests/Feature/Orbital/SoftDeleteTest.php b/tests/Feature/Orbital/SoftDeleteTest.php index 64a1ecb..57dc6ed 100644 --- a/tests/Feature/Orbital/SoftDeleteTest.php +++ b/tests/Feature/Orbital/SoftDeleteTest.php @@ -16,7 +16,7 @@ ->toBeFile() ->and(file_get_contents(base_path("content/categories/{$category->id}.md"))) ->toContain(<<deleted_at->toIso8601String()} + deleted_at: '{$category->deleted_at->toIso8601String()}' MD); }); @@ -34,7 +34,7 @@ ->toBeFile() ->and(file_get_contents(base_path("content/categories/{$category->id}.md"))) ->toContain(<<deleted_at->toIso8601String()} + deleted_at: '{$category->deleted_at->toIso8601String()}' MD); $category->restore(); diff --git a/tests/Fixtures/Models/Post.php b/tests/Fixtures/Models/Post.php index c881d9f..3c2ea09 100644 --- a/tests/Fixtures/Models/Post.php +++ b/tests/Fixtures/Models/Post.php @@ -2,8 +2,10 @@ namespace Tests\Fixtures\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Str; use Orbit\Concerns\Orbital; use Orbit\Contracts\Orbit; @@ -19,4 +21,9 @@ public function schema(Blueprint $table): void $table->string('title'); $table->longText('content')->nullable(); } + + public function title(): Attribute + { + return Attribute::get(fn (string $value) => Str::headline($value)); + } }