diff --git a/src/Collectors/InertiaSharedData.php b/src/Collectors/InertiaSharedData.php index 104b9a3..8182909 100644 --- a/src/Collectors/InertiaSharedData.php +++ b/src/Collectors/InertiaSharedData.php @@ -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; @@ -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); } } diff --git a/src/Components/InertiaSharedData.php b/src/Components/InertiaSharedData.php index 76c4619..0d7b959 100644 --- a/src/Components/InertiaSharedData.php +++ b/src/Components/InertiaSharedData.php @@ -8,6 +8,7 @@ class InertiaSharedData { public function __construct( public readonly ArrayType $data, + public readonly bool $withAllErrors = false, ) { // } diff --git a/tests/Feature/InertiaSharedDataTest.php b/tests/Feature/InertiaSharedDataTest.php new file mode 100644 index 0000000..7d14923 --- /dev/null +++ b/tests/Feature/InertiaSharedDataTest.php @@ -0,0 +1,40 @@ +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); +}); diff --git a/workbench/app/Http/Middleware/HandleInertiaRequests.php b/workbench/app/Http/Middleware/HandleInertiaRequests.php new file mode 100644 index 0000000..2360342 --- /dev/null +++ b/workbench/app/Http/Middleware/HandleInertiaRequests.php @@ -0,0 +1,22 @@ + fn () => [ + 'user' => $request->user(), + ], + 'flash' => fn () => [ + 'success' => $request->session()->get('success'), + ], + ]; + } +} diff --git a/workbench/app/Http/Middleware/HandleInertiaRequestsWithAllErrors.php b/workbench/app/Http/Middleware/HandleInertiaRequestsWithAllErrors.php new file mode 100644 index 0000000..248793e --- /dev/null +++ b/workbench/app/Http/Middleware/HandleInertiaRequestsWithAllErrors.php @@ -0,0 +1,21 @@ + fn () => [ + 'user' => $request->user(), + ], + ]; + } +}