Skip to content

Commit

Permalink
Merge pull request #3 from aiglesiasn/master
Browse files Browse the repository at this point in the history
Add $url parameter to Inertia render method.
  • Loading branch information
cherifGsoul authored Jul 2, 2024
2 parents 6898036 + b010dd4 commit c9e8deb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Service/Inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public function __construct(
$this->page = Page::create();
}

public function render(string $component, array $props = []): ResponseInterface
public function render(string $component, array $props = [], string $url = null): ResponseInterface
{
$this->page = $this->page
->withComponent($component)
->withUrl((string)$this->request->getUri());
->withComponent($component)
->withUrl($url ?? (string)$this->request->getUri());

if ($this->request->hasHeader('X-Inertia-Partial-Data')) {
$only = explode(',', $this->request->getHeaderLine('X-Inertia-Partial-Data'));
Expand Down Expand Up @@ -85,4 +85,4 @@ private function createResponse(string $data, string $contentType)
->withBody($stream)
->withHeader('Content-Type', $contentType);
}
}
}
52 changes: 51 additions & 1 deletion test/Service/InertiaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,54 @@ public function testRenderReturnPartialDataWhenHeaderContainsPartialData()
$this->assertSame($json, $jsonResponse);
}

}
public function testRenderReturnResponseWithRequestedUrl()
{
$request = $this->prophesize(ServerRequestInterface::class);
$request->hasHeader('X-Inertia')->willReturn(true);
$request->hasHeader('X-Inertia-Partial-Data')->willReturn(false);
$invalidJson = '{"component":"component","props":{"key1":"value1","key2":"value2"},"url":"callback()","version":null}';
$validJson = '{"component":"component","props":{"key1":"value1","key2":"value2"},"url":"\/test\/url","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'
]
,'/test/url'
);

$this->assertInstanceOf(ResponseInterface::class, $returnedResponse);
$this->assertNotSame($invalidJson, $jsonResponse);
$this->assertSame($validJson, $jsonResponse);
}

}

0 comments on commit c9e8deb

Please sign in to comment.