Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Collectors/InertiaSharedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Laravel\Ranger\Components\InertiaSharedData as SharedDataComponent;
use Laravel\Surveyor\Analyzer\Analyzer;
use Laravel\Surveyor\Types\ArrayType;
use Laravel\Surveyor\Types\BoolType;
use Laravel\Surveyor\Types\Type;
use Laravel\Surveyor\Types\UnionType;
use Spatie\StructureDiscoverer\Discover;
Expand Down Expand Up @@ -62,6 +63,10 @@ protected function processSharedData(string $class): SharedDataComponent
$data = new ArrayType($finalArray);
}

return new SharedDataComponent($data);
$withAllErrors = $result->hasProperty('withAllErrors')
&& $result->getProperty('withAllErrors')->type instanceof BoolType
&& $result->getProperty('withAllErrors')->type->value === true;

return new SharedDataComponent($data, $withAllErrors);
}
}
1 change: 1 addition & 0 deletions src/Components/InertiaSharedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class InertiaSharedData
{
public function __construct(
public readonly ArrayType $data,
public readonly bool $withAllErrors = false,
) {
//
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Feature/InertiaSharedDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Laravel\Ranger\Collectors\InertiaSharedData;
use Laravel\Ranger\Components\InertiaSharedData as InertiaSharedDataComponent;

beforeEach(function () {
$this->collector = app(InertiaSharedData::class);
});

it('collects all inertia middleware classes', function () {
$sharedData = $this->collector->collect();

expect($sharedData)->toHaveCount(2);
});

it('creates shared data components with correct structure', function () {
$sharedData = $this->collector->collect();

expect($sharedData->first())->toBeInstanceOf(InertiaSharedDataComponent::class);
});

it('sets withAllErrors to false when property is not overridden', function () {
$sharedData = $this->collector->collect();

$component = $sharedData->first(
fn (InertiaSharedDataComponent $c) => $c->withAllErrors === false
);

expect($component)->toBeInstanceOf(InertiaSharedDataComponent::class);
});

it('sets withAllErrors to true when property is set to true', function () {
$sharedData = $this->collector->collect();

$component = $sharedData->first(
fn (InertiaSharedDataComponent $c) => $c->withAllErrors === true
);

expect($component)->toBeInstanceOf(InertiaSharedDataComponent::class);
});
22 changes: 22 additions & 0 deletions workbench/app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
public function share(Request $request): array
{
return [
...parent::share($request),
'auth' => fn () => [
'user' => $request->user(),
],
'flash' => fn () => [
'success' => $request->session()->get('success'),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Inertia\Middleware;

class HandleInertiaRequestsWithAllErrors extends Middleware
{
protected $withAllErrors = true;

public function share(Request $request): array
{
return [
...parent::share($request),
'auth' => fn () => [
'user' => $request->user(),
],
];
}
}