|
2 | 2 |
|
3 | 3 | namespace Lomkit\Rest\Tests\Feature\Controllers; |
4 | 4 |
|
| 5 | +use Illuminate\Support\Arr; |
5 | 6 | use Illuminate\Support\Facades\Gate; |
6 | 7 | use Lomkit\Rest\Http\Requests\RestRequest; |
| 8 | +use Lomkit\Rest\Relations\Relation; |
7 | 9 | use Lomkit\Rest\Tests\Feature\TestCase; |
8 | 10 | use Lomkit\Rest\Tests\Support\Database\Factories\BelongsToManyRelationFactory; |
9 | 11 | use Lomkit\Rest\Tests\Support\Database\Factories\BelongsToRelationFactory; |
@@ -259,6 +261,76 @@ public function test_getting_a_list_of_resources_including_belongs_to_has_many_r |
259 | 261 | ); |
260 | 262 | } |
261 | 263 |
|
| 264 | + public function test_getting_a_list_of_resources_including_distant_relation_with_intermediary_search_query_condition(): void |
| 265 | + { |
| 266 | + $matchingModel = ModelFactory::new()->create( |
| 267 | + ['number' => 1] |
| 268 | + )->fresh(); |
| 269 | + |
| 270 | + $belongsToMany = BelongsToManyRelationFactory::new() |
| 271 | + ->for($matchingModel) |
| 272 | + ->create(); |
| 273 | + |
| 274 | + $matchingModel2 = ModelFactory::new() |
| 275 | + ->afterCreating(function (Model $model) use ($belongsToMany) { |
| 276 | + $model->belongsToManyQueryChangesRelation() |
| 277 | + ->attach($belongsToMany); |
| 278 | + }) |
| 279 | + ->create()->fresh(); |
| 280 | + |
| 281 | + Gate::policy(Model::class, GreenPolicy::class); |
| 282 | + Gate::policy(BelongsToManyRelation::class, GreenPolicy::class); |
| 283 | + |
| 284 | + $response = $this->post( |
| 285 | + '/api/models/search', |
| 286 | + [ |
| 287 | + 'search' => [ |
| 288 | + 'includes' => [ |
| 289 | + ['relation' => 'belongsToManyRelation.model'], |
| 290 | + ], |
| 291 | + ], |
| 292 | + ], |
| 293 | + ['Accept' => 'application/json'] |
| 294 | + ); |
| 295 | + |
| 296 | + $matchingModelBelongsToManyQueryChangesRelations = $matchingModel2->belongsToManyQueryChangesRelation; |
| 297 | + |
| 298 | + $this->assertResourcePaginated( |
| 299 | + $response, |
| 300 | + [$matchingModel, $matchingModel2], |
| 301 | + new ModelResource(), |
| 302 | + [ |
| 303 | + [ |
| 304 | + 'belongs_to_many_relation' => [], |
| 305 | + ], |
| 306 | + [ |
| 307 | + 'belongs_to_many_relation' => $matchingModelBelongsToManyQueryChangesRelations |
| 308 | + ->map(function (BelongsToManyRelation $belongsToManyRelation) { |
| 309 | + return array_merge( |
| 310 | + $belongsToManyRelation |
| 311 | + ->only((new BelongsToManyResource())->getFields(app()->make(RestRequest::class))), |
| 312 | + [ |
| 313 | + 'model' => null, |
| 314 | + 'belongs_to_many_pivot' => Arr::only( |
| 315 | + $belongsToManyRelation |
| 316 | + ->belongs_to_many_pivot |
| 317 | + ->toArray(), |
| 318 | + Arr::first( |
| 319 | + (new ModelResource())->getRelations(app()->make(RestRequest::class)), |
| 320 | + function (Relation $relation) { |
| 321 | + return $relation->relation === 'belongsToManyRelation'; |
| 322 | + } |
| 323 | + )->getPivotFields() |
| 324 | + ), |
| 325 | + ] |
| 326 | + ); |
| 327 | + }) |
| 328 | + ->toArray(), |
| 329 | + ], |
| 330 | + ] |
| 331 | + ); |
| 332 | + } |
| 333 | + |
262 | 334 | public function test_getting_a_list_of_resources_including_has_one_relation(): void |
263 | 335 | { |
264 | 336 | $matchingModel = ModelFactory::new() |
|
0 commit comments