From 8c17532a0cd368f8fe988a19e56b045310d5b45b Mon Sep 17 00:00:00 2001 From: Tito Duarte Date: Tue, 15 Aug 2023 16:55:26 +0100 Subject: [PATCH 1/2] [BUGFIX] - Partial-Data; Return correct partial data if requested --- src/Service/Inertia.php | 1 - test/Service/InertiaTest.php | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/Service/Inertia.php b/src/Service/Inertia.php index d449d8b..ae4bb50 100644 --- a/src/Service/Inertia.php +++ b/src/Service/Inertia.php @@ -35,7 +35,6 @@ 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')) { diff --git a/test/Service/InertiaTest.php b/test/Service/InertiaTest.php index 92074bd..b800c2c 100644 --- a/test/Service/InertiaTest.php +++ b/test/Service/InertiaTest.php @@ -91,4 +91,57 @@ 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(Argument::type('string')); + $request->getHeader('X-Inertia-Partial-Data')->willReturn([ + 'key2' + ]); + $json = '{"component":"type(string)","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( + Argument::type('string'), + [ + 'key1' => fn() => 'value1', + 'key2' => fn() => 'value2' + ] + ); + + $this->assertInstanceOf(ResponseInterface::class, $returnedResponse); + $this->assertSame($json, $jsonResponse); + } + } \ No newline at end of file From 010f0b933cb2510544572d2019cf04e72b91accd Mon Sep 17 00:00:00 2001 From: Tito Duarte Date: Tue, 15 Aug 2023 19:21:10 +0100 Subject: [PATCH 2/2] [BUGFIX] - Partial-Data; Fixed correct handling of header data [BUGFIX] - Partial-Data; check component for valid partial request --- src/Service/Inertia.php | 4 ++-- test/Service/InertiaTest.php | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Service/Inertia.php b/src/Service/Inertia.php index ae4bb50..ec25876 100644 --- a/src/Service/Inertia.php +++ b/src/Service/Inertia.php @@ -38,8 +38,8 @@ public function render(string $component, array $props = []): ResponseInterface ->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 b800c2c..ff7aa1d 100644 --- a/test/Service/InertiaTest.php +++ b/test/Service/InertiaTest.php @@ -98,11 +98,9 @@ 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(Argument::type('string')); - $request->getHeader('X-Inertia-Partial-Data')->willReturn([ - 'key2' - ]); - $json = '{"component":"type(string)","props":{"key2":"value2"},"url":"callback()","version":null}'; + $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); @@ -133,7 +131,7 @@ public function testRenderReturnPartialDataWhenHeaderContainsPartialData() ); $returnedResponse = $inertia->render( - Argument::type('string'), + 'component', [ 'key1' => fn() => 'value1', 'key2' => fn() => 'value2'