Skip to content

Commit

Permalink
fix: dont run properties through accessors when serialising
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangjchandler committed Nov 7, 2024
1 parent f5bce3f commit 756ff1c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
17 changes: 8 additions & 9 deletions src/Support/ModelAttributeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
11 changes: 11 additions & 0 deletions tests/Feature/Orbital/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
4 changes: 2 additions & 2 deletions tests/Feature/Orbital/SoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
->toBeFile()
->and(file_get_contents(base_path("content/categories/{$category->id}.md")))
->toContain(<<<MD
deleted_at: {$category->deleted_at->toIso8601String()}
deleted_at: '{$category->deleted_at->toIso8601String()}'
MD);
});

Expand All @@ -34,7 +34,7 @@
->toBeFile()
->and(file_get_contents(base_path("content/categories/{$category->id}.md")))
->toContain(<<<MD
deleted_at: {$category->deleted_at->toIso8601String()}
deleted_at: '{$category->deleted_at->toIso8601String()}'
MD);

$category->restore();
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
}

0 comments on commit 756ff1c

Please sign in to comment.