|
10 | 10 | use Lomkit\Rest\Tests\Support\Database\Factories\BelongsToManyRelationFactory; |
11 | 11 | use Lomkit\Rest\Tests\Support\Database\Factories\BelongsToRelationFactory; |
12 | 12 | use Lomkit\Rest\Tests\Support\Database\Factories\HasManyRelationFactory; |
| 13 | +use Lomkit\Rest\Tests\Support\Database\Factories\HasManyThroughRelationFactory; |
13 | 14 | use Lomkit\Rest\Tests\Support\Database\Factories\HasOneOfManyRelationFactory; |
14 | 15 | use Lomkit\Rest\Tests\Support\Database\Factories\HasOneRelationFactory; |
15 | 16 | use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory; |
16 | 17 | use Lomkit\Rest\Tests\Support\Database\Factories\ModelWithFactory; |
17 | 18 | use Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation; |
18 | 19 | use Lomkit\Rest\Tests\Support\Models\BelongsToRelation; |
19 | 20 | use Lomkit\Rest\Tests\Support\Models\HasManyRelation; |
| 21 | +use Lomkit\Rest\Tests\Support\Models\HasManyThroughRelation; |
20 | 22 | use Lomkit\Rest\Tests\Support\Models\HasOneOfManyRelation; |
21 | 23 | use Lomkit\Rest\Tests\Support\Models\HasOneRelation; |
22 | 24 | use Lomkit\Rest\Tests\Support\Models\Model; |
|
25 | 27 | use Lomkit\Rest\Tests\Support\Rest\Resources\BelongsToManyResource; |
26 | 28 | use Lomkit\Rest\Tests\Support\Rest\Resources\BelongsToResource; |
27 | 29 | use Lomkit\Rest\Tests\Support\Rest\Resources\HasManyResource; |
| 30 | +use Lomkit\Rest\Tests\Support\Rest\Resources\HasManyThroughResource; |
28 | 31 | use Lomkit\Rest\Tests\Support\Rest\Resources\HasOneOfManyResource; |
29 | 32 | use Lomkit\Rest\Tests\Support\Rest\Resources\HasOneResource; |
30 | 33 | use Lomkit\Rest\Tests\Support\Rest\Resources\ModelResource; |
@@ -578,6 +581,69 @@ public function test_getting_a_list_of_resources_including_has_many_relation(): |
578 | 581 | ); |
579 | 582 | } |
580 | 583 |
|
| 584 | + public function test_getting_a_list_of_resources_including_has_many_relation_with_eager_loading_relations(): void |
| 585 | + { |
| 586 | + $matchingModel = ModelFactory::new() |
| 587 | + ->createOne()->fresh(); |
| 588 | + |
| 589 | + $hasMany = HasManyRelationFactory::new() |
| 590 | + ->for($matchingModel) |
| 591 | + ->createOne(); |
| 592 | + |
| 593 | + HasManyThroughRelationFactory::new() |
| 594 | + ->for($hasMany) |
| 595 | + ->createOne(); |
| 596 | + |
| 597 | + Gate::policy(Model::class, GreenPolicy::class); |
| 598 | + Gate::policy(HasManyRelation::class, GreenPolicy::class); |
| 599 | + Gate::policy(HasManyThroughRelation::class, GreenPolicy::class); |
| 600 | + |
| 601 | + $response = $this->post( |
| 602 | + '/api/models/search', |
| 603 | + [ |
| 604 | + 'search' => [ |
| 605 | + 'includes' => [ |
| 606 | + [ |
| 607 | + 'relation' => 'hasManyRelationWithEagerLoadingRelation', |
| 608 | + ], |
| 609 | + ], |
| 610 | + ], |
| 611 | + ], |
| 612 | + ['Accept' => 'application/json'] |
| 613 | + ); |
| 614 | + |
| 615 | + $this->assertResourcePaginated( |
| 616 | + $response, |
| 617 | + [$matchingModel], |
| 618 | + new ModelResource(), |
| 619 | + [ |
| 620 | + [ |
| 621 | + 'has_many_relation_with_eager_loading_relation' => $matchingModel->hasManyRelationWithEagerLoadingRelation() |
| 622 | + ->orderBy('id') |
| 623 | + ->get() |
| 624 | + ->map(function ($relation) { |
| 625 | + $relation->has_many_through_relation = $relation->hasManyThroughRelation |
| 626 | + ->map(function ($relation) { |
| 627 | + $relation->has_many_relation = $relation->hasManyRelation->only( |
| 628 | + (new HasManyResource())->getFields(app()->make(RestRequest::class)) |
| 629 | + ); |
| 630 | + |
| 631 | + return $relation->only(array_merge( |
| 632 | + (new HasManyThroughResource())->getFields(app()->make(RestRequest::class)), |
| 633 | + ['has_many_relation'] |
| 634 | + )); |
| 635 | + })->toArray(); |
| 636 | + |
| 637 | + return $relation->only(array_merge( |
| 638 | + (new HasManyResource())->getFields(app()->make(RestRequest::class)), |
| 639 | + ['has_many_through_relation'] |
| 640 | + )); |
| 641 | + })->toArray(), |
| 642 | + ], |
| 643 | + ] |
| 644 | + ); |
| 645 | + } |
| 646 | + |
581 | 647 | public function test_getting_a_list_of_resources_including_belongs_to_many_relation(): void |
582 | 648 | { |
583 | 649 | $matchingModel = ModelFactory::new() |
|
0 commit comments