diff --git a/src/Service/Inertia.php b/src/Service/Inertia.php index d449d8b..ec25876 100644 --- a/src/Service/Inertia.php +++ b/src/Service/Inertia.php @@ -35,12 +35,11 @@ public function render(string $component, array $props = []): ResponseInterface { $this->page = $this->page ->withComponent($component) - ->withProps($props) ->withUrl((string)$this->request->getUri()); if ($this->request->hasHeader('X-Inertia-Partial-Data')) { - $only = $this->request->getHeader('X-Inertia-Partial-Data'); - $props = ($only && $this->request->getHeaderLine('X-Inertia-Partial-Component')) + $only = explode(',', $this->request->getHeaderLine('X-Inertia-Partial-Data')); + $props = ($only && $this->request->getHeaderLine('X-Inertia-Partial-Component') === $component) ? array_intersect_key($props, array_flip((array) $only)) : $props; } diff --git a/test/Service/InertiaTest.php b/test/Service/InertiaTest.php index 92074bd..ff7aa1d 100644 --- a/test/Service/InertiaTest.php +++ b/test/Service/InertiaTest.php @@ -91,4 +91,55 @@ public function testRenderReturnPsr7ResponseWithHtmlWhenInertiaHeaderIsNotPresen $this->assertInstanceOf(ResponseInterface::class, $returnedResponse); $this->assertNotSame($response, $returnedResponse); } + + + public function testRenderReturnPartialDataWhenHeaderContainsPartialData() + { + $request = $this->prophesize(ServerRequestInterface::class); + $request->hasHeader('X-Inertia')->willReturn(true); + $request->hasHeader('X-Inertia-Partial-Data')->willReturn(true); + $request->getHeaderLine('X-Inertia-Partial-Component')->willReturn('component'); + $request->getHeaderLine('X-Inertia-Partial-Data')->willReturn('key2'); + $json = '{"component":"component","props":{"key2":"value2"},"url":"callback()","version":null}'; + $jsonResponse = null; + + $uri = $this->prophesize(UriInterface::class); + $request->getUri()->willReturn(Argument::that([$uri, 'reveal'])); + + $response = $this->prophesize(ResponseInterface::class); + $responseFactory = $this->prophesize(ResponseFactoryInterface::class); + $responseFactory->createResponse()->willReturn($response); + + $stream = $this->prophesize(StreamInterface::class); + $streamFactory = $this->prophesize(StreamFactoryInterface::class); + $streamFactory->createStream(Argument::type('string'))->will(function ($args) use (&$jsonResponse, $stream){ + $jsonResponse = $args[0]; + return $stream; + }); + + $rootViewProvider = $this->prophesize(RootViewProviderInterface::class); + + $response->withBody($stream->reveal())->willReturn($response); + $response->withHeader('X-Inertia', true)->willReturn($response); + $response->withHeader('Content-Type', 'application/json')->willReturn($response); + + $inertia = new Inertia( + $request->reveal(), + $responseFactory->reveal(), + $streamFactory->reveal(), + $rootViewProvider->reveal() + ); + + $returnedResponse = $inertia->render( + 'component', + [ + 'key1' => fn() => 'value1', + 'key2' => fn() => 'value2' + ] + ); + + $this->assertInstanceOf(ResponseInterface::class, $returnedResponse); + $this->assertSame($json, $jsonResponse); + } + } \ No newline at end of file