|
11 | 11 | use Lomkit\Rest\Tests\Support\Database\Factories\HasOneOfManyRelationFactory; |
12 | 12 | use Lomkit\Rest\Tests\Support\Database\Factories\HasOneRelationFactory; |
13 | 13 | use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory; |
| 14 | +use Lomkit\Rest\Tests\Support\Database\Factories\ModelWithFactory; |
14 | 15 | use Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation; |
15 | 16 | use Lomkit\Rest\Tests\Support\Models\BelongsToRelation; |
16 | 17 | use Lomkit\Rest\Tests\Support\Models\HasManyRelation; |
17 | 18 | use Lomkit\Rest\Tests\Support\Models\HasOneOfManyRelation; |
18 | 19 | use Lomkit\Rest\Tests\Support\Models\HasOneRelation; |
19 | 20 | use Lomkit\Rest\Tests\Support\Models\Model; |
| 21 | +use Lomkit\Rest\Tests\Support\Models\ModelWith; |
20 | 22 | use Lomkit\Rest\Tests\Support\Policies\GreenPolicy; |
21 | 23 | use Lomkit\Rest\Tests\Support\Rest\Resources\BelongsToManyResource; |
22 | 24 | use Lomkit\Rest\Tests\Support\Rest\Resources\BelongsToResource; |
@@ -171,6 +173,41 @@ public function test_getting_a_list_of_resources_including_belongs_to_relation() |
171 | 173 | ); |
172 | 174 | } |
173 | 175 |
|
| 176 | + public function test_getting_a_list_of_resources_with_auto_loaded_relation(): void |
| 177 | + { |
| 178 | + $belongsTo = BelongsToRelationFactory::new()->create(); |
| 179 | + $matchingModel = ModelWithFactory::new() |
| 180 | + ->for($belongsTo) |
| 181 | + ->create()->fresh(); |
| 182 | + |
| 183 | + $matchingModel2 = ModelWithFactory::new()->create()->fresh(); |
| 184 | + |
| 185 | + Gate::policy(ModelWith::class, GreenPolicy::class); |
| 186 | + Gate::policy(BelongsToRelation::class, GreenPolicy::class); |
| 187 | + |
| 188 | + $response = $this->post( |
| 189 | + '/api/model-withs/search', |
| 190 | + [ |
| 191 | + 'search' => [], |
| 192 | + ], |
| 193 | + ['Accept' => 'application/json'] |
| 194 | + ); |
| 195 | + |
| 196 | + $this->assertResourcePaginated( |
| 197 | + $response, |
| 198 | + [$matchingModel, $matchingModel2], |
| 199 | + new ModelResource(), |
| 200 | + [ |
| 201 | + [ |
| 202 | + 'belongs_to_relation' => $matchingModel->belongsToRelation->only((new BelongsToResource())->getFields(app()->make(RestRequest::class))), |
| 203 | + ], |
| 204 | + [ |
| 205 | + 'belongs_to_relation' => null, |
| 206 | + ], |
| 207 | + ] |
| 208 | + ); |
| 209 | + } |
| 210 | + |
174 | 211 | public function test_getting_a_list_of_resources_including_belongs_to_has_many_relation(): void |
175 | 212 | { |
176 | 213 | $belongsTo = BelongsToRelationFactory::new()->create(); |
|
0 commit comments