Skip to content

Commit

Permalink
Merge pull request #2 from fduarte42/master
Browse files Browse the repository at this point in the history
[BUGFIX] - Partial-Data; Return correct partial data if requested
  • Loading branch information
cherifGsoul authored Aug 24, 2023
2 parents 675605b + 010f0b9 commit 6898036
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Service/Inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
51 changes: 51 additions & 0 deletions test/Service/InertiaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 6898036

Please sign in to comment.