|
17 | 17 | use Inertia\Inertia;
|
18 | 18 | use Inertia\LazyProp;
|
19 | 19 | use Inertia\MergeProp;
|
| 20 | +use Inertia\ProvidesInertiaProps; |
| 21 | +use Inertia\RenderContext; |
20 | 22 | use Inertia\Response;
|
21 | 23 | use Inertia\Tests\Stubs\FakeResource;
|
22 | 24 | use Inertia\Tests\Stubs\MergeWithSharedProp;
|
@@ -759,6 +761,33 @@ public function test_always_props_are_included_on_partial_reload(): void
|
759 | 761 | $this->assertFalse(isset($page->props->user));
|
760 | 762 | }
|
761 | 763 |
|
| 764 | + public function test_inertia_responsable_objects(): void |
| 765 | + { |
| 766 | + $request = Request::create('/user/123', 'GET'); |
| 767 | + |
| 768 | + $response = new Response('User/Edit', [ |
| 769 | + 'foo' => 'bar', |
| 770 | + new class implements ProvidesInertiaProps |
| 771 | + { |
| 772 | + public function toInertiaProps(RenderContext $context): iterable |
| 773 | + { |
| 774 | + return collect([ |
| 775 | + 'baz' => 'qux', |
| 776 | + ]); |
| 777 | + } |
| 778 | + }, |
| 779 | + 'quux' => 'corge', |
| 780 | + |
| 781 | + ], 'app', '123'); |
| 782 | + $response = $response->toResponse($request); |
| 783 | + $view = $response->getOriginalContent(); |
| 784 | + $page = $view->getData()['page']; |
| 785 | + |
| 786 | + $this->assertSame('bar', $page['props']['foo']); |
| 787 | + $this->assertSame('qux', $page['props']['baz']); |
| 788 | + $this->assertSame('corge', $page['props']['quux']); |
| 789 | + } |
| 790 | + |
762 | 791 | public function test_inertia_response_type_prop(): void
|
763 | 792 | {
|
764 | 793 | $request = Request::create('/user/123', 'GET');
|
@@ -836,6 +865,30 @@ public function test_nested_dot_props_do_not_get_unpacked(): void
|
836 | 865 | $this->assertFalse(array_key_exists('can', $auth));
|
837 | 866 | }
|
838 | 867 |
|
| 868 | + public function test_props_can_be_added_using_the_with_method(): void |
| 869 | + { |
| 870 | + $request = Request::create('/user/123', 'GET'); |
| 871 | + $response = new Response('User/Edit', [], 'app', '123'); |
| 872 | + |
| 873 | + $response->with(['foo' => 'bar', 'baz' => 'qux']) |
| 874 | + ->with(['quux' => 'corge']) |
| 875 | + ->with(new class implements ProvidesInertiaProps |
| 876 | + { |
| 877 | + public function toInertiaProps(RenderContext $context): iterable |
| 878 | + { |
| 879 | + return collect(['grault' => 'garply']); |
| 880 | + } |
| 881 | + }); |
| 882 | + |
| 883 | + $response = $response->toResponse($request); |
| 884 | + $view = $response->getOriginalContent(); |
| 885 | + $page = $view->getData()['page']; |
| 886 | + |
| 887 | + $this->assertSame('bar', $page['props']['foo']); |
| 888 | + $this->assertSame('qux', $page['props']['baz']); |
| 889 | + $this->assertSame('corge', $page['props']['quux']); |
| 890 | + } |
| 891 | + |
839 | 892 | public function test_responsable_with_invalid_key(): void
|
840 | 893 | {
|
841 | 894 | $request = Request::create('/user/123', 'GET');
|
|
0 commit comments