From 141256b2ed1833158852c9d239d00abec8ff4942 Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Thu, 10 Oct 2024 09:16:21 -0400 Subject: [PATCH] fixed dot config merging --- src/Response.php | 4 +++ tests/ResponseFactoryTest.php | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/Response.php b/src/Response.php index bdeff879..914831ec 100644 --- a/src/Response.php +++ b/src/Response.php @@ -172,6 +172,10 @@ public function resolveArrayableProperties(array $props, Request $request, bool $value = $this->resolveArrayableProperties($value, $request, false); } + if ($value instanceof Closure) { + $value = $value(); + } + if ($unpackDotProps && str_contains($key, '.')) { Arr::set($props, $key, $value); unset($props[$key]); diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index 61f79c2c..68473d22 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -146,6 +146,66 @@ public function test_shared_data_can_be_shared_from_anywhere(): void ]); } + public function test_dot_props_are_merged_from_shared(): void + { + Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () { + Inertia::share('auth.user', [ + 'name' => 'Jonathan', + ]); + + return Inertia::render('User/Edit', [ + 'auth.user.can.create_group' => false, + ]); + }); + + $response = $this->withoutExceptionHandling()->get('/', ['X-Inertia' => 'true']); + + $response->assertSuccessful(); + $response->assertJson([ + 'component' => 'User/Edit', + 'props' => [ + 'auth' => [ + 'user' => [ + 'name' => 'Jonathan', + 'can' => [ + 'create_group' => false, + ], + ], + ], + ], + ]); + } + + public function test_dot_props_with_callbacks_are_merged_from_shared(): void + { + Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () { + Inertia::share('auth.user', fn () => [ + 'name' => 'Jonathan', + ]); + + return Inertia::render('User/Edit', [ + 'auth.user.can.create_group' => false, + ]); + }); + + $response = $this->withoutExceptionHandling()->get('/', ['X-Inertia' => 'true']); + + $response->assertSuccessful(); + $response->assertJson([ + 'component' => 'User/Edit', + 'props' => [ + 'auth' => [ + 'user' => [ + 'name' => 'Jonathan', + 'can' => [ + 'create_group' => false, + ], + ], + ], + ], + ]); + } + public function test_can_flush_shared_data(): void { Inertia::share('foo', 'bar');