|
17 | 17 | use Inertia\Inertia;
|
18 | 18 | use Inertia\LazyProp;
|
19 | 19 | use Inertia\MergeProp;
|
| 20 | +use Inertia\ProvidesInertiaProperties; |
| 21 | +use Inertia\RenderContext; |
20 | 22 | use Inertia\Response;
|
21 | 23 | use Inertia\Tests\Stubs\FakeResource;
|
22 | 24 | use Inertia\Tests\Stubs\MergeWithSharedProp;
|
@@ -822,6 +824,33 @@ public function test_always_props_are_included_on_partial_reload(): void
|
822 | 824 | $this->assertFalse(isset($page->props->user));
|
823 | 825 | }
|
824 | 826 |
|
| 827 | + public function test_inertia_responsable_objects(): void |
| 828 | + { |
| 829 | + $request = Request::create('/user/123', 'GET'); |
| 830 | + |
| 831 | + $response = new Response('User/Edit', [ |
| 832 | + 'foo' => 'bar', |
| 833 | + new class implements ProvidesInertiaProperties |
| 834 | + { |
| 835 | + public function toInertiaProperties(RenderContext $context): iterable |
| 836 | + { |
| 837 | + return collect([ |
| 838 | + 'baz' => 'qux', |
| 839 | + ]); |
| 840 | + } |
| 841 | + }, |
| 842 | + 'quux' => 'corge', |
| 843 | + |
| 844 | + ], 'app', '123'); |
| 845 | + $response = $response->toResponse($request); |
| 846 | + $view = $response->getOriginalContent(); |
| 847 | + $page = $view->getData()['page']; |
| 848 | + |
| 849 | + $this->assertSame('bar', $page['props']['foo']); |
| 850 | + $this->assertSame('qux', $page['props']['baz']); |
| 851 | + $this->assertSame('corge', $page['props']['quux']); |
| 852 | + } |
| 853 | + |
825 | 854 | public function test_inertia_response_type_prop(): void
|
826 | 855 | {
|
827 | 856 | $request = Request::create('/user/123', 'GET');
|
@@ -899,6 +928,30 @@ public function test_nested_dot_props_do_not_get_unpacked(): void
|
899 | 928 | $this->assertFalse(array_key_exists('can', $auth));
|
900 | 929 | }
|
901 | 930 |
|
| 931 | + public function test_props_can_be_added_using_the_with_method(): void |
| 932 | + { |
| 933 | + $request = Request::create('/user/123', 'GET'); |
| 934 | + $response = new Response('User/Edit', [], 'app', '123'); |
| 935 | + |
| 936 | + $response->with(['foo' => 'bar', 'baz' => 'qux']) |
| 937 | + ->with(['quux' => 'corge']) |
| 938 | + ->with(new class implements ProvidesInertiaProperties |
| 939 | + { |
| 940 | + public function toInertiaProperties(RenderContext $context): iterable |
| 941 | + { |
| 942 | + return collect(['grault' => 'garply']); |
| 943 | + } |
| 944 | + }); |
| 945 | + |
| 946 | + $response = $response->toResponse($request); |
| 947 | + $view = $response->getOriginalContent(); |
| 948 | + $page = $view->getData()['page']; |
| 949 | + |
| 950 | + $this->assertSame('bar', $page['props']['foo']); |
| 951 | + $this->assertSame('qux', $page['props']['baz']); |
| 952 | + $this->assertSame('corge', $page['props']['quux']); |
| 953 | + } |
| 954 | + |
902 | 955 | public function test_responsable_with_invalid_key(): void
|
903 | 956 | {
|
904 | 957 | $request = Request::create('/user/123', 'GET');
|
|
0 commit comments