Skip to content

Commit

Permalink
fixed dot config merging
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Oct 10, 2024
1 parent 31fed76 commit 141256b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
60 changes: 60 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 141256b

Please sign in to comment.