diff --git a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php index 1decb30..7d03c8b 100644 --- a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php +++ b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php @@ -521,4 +521,146 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat ] ); } + + public function test_getting_a_list_of_resources_including_belongs_to_many_relation_and_filtering_on_pivot(): void + { + $matchingModel = ModelFactory::new() + ->hasAttached( + BelongsToManyRelationFactory::new()->count(1), + ['number' => 3], + 'belongsToManyRelation' + ) + ->create()->fresh(); + + $matchingModel2 = ModelFactory::new() + ->hasAttached( + BelongsToManyRelationFactory::new()->count(1), + ['number' => 1], + 'belongsToManyRelation' + ) + ->create()->fresh(); + + $pivotAccessor = $matchingModel->belongsToManyRelation()->getPivotAccessor(); + + Gate::policy(Model::class, GreenPolicy::class); + Gate::policy(BelongsToManyRelation::class, GreenPolicy::class); + + $response = $this->post( + '/api/models/search', + [ + 'search' => [ + 'includes' => [ + [ + 'relation' => 'belongsToManyRelation', + 'filters' => [ + ['field' => 'models.pivot.number', 'operator' => '>', 'value' => 2], + ], + ], + ], + ], + ], + ['Accept' => 'application/json'] + ); + + $this->assertResourcePaginated( + $response, + [$matchingModel, $matchingModel2], + new ModelResource(), + [ + [ + 'belongs_to_many_relation' => $matchingModel->belongsToManyRelation() + ->orderBy('id', 'desc') + ->get() + ->map(function ($relation) use ($pivotAccessor) { + return collect($relation->only( + array_merge((new BelongsToManyResource())->getFields(app()->make(RestRequest::class)), [$pivotAccessor]) + )) + ->pipe(function ($relation) use ($pivotAccessor) { + $relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray()) + ->only( + (new ModelResource())->relation('belongsToManyRelation')->getPivotFields() + ); + + return $relation; + }); + }) + ->toArray(), + ], + [ + 'belongs_to_many_relation' => [], + ], + ] + ); + } + + public function test_getting_a_list_of_resources_including_belongs_to_many_relation_and_filtering_on_pivot_with_null_value(): void + { + $matchingModel = ModelFactory::new() + ->hasAttached( + BelongsToManyRelationFactory::new()->count(1), + ['number' => null], + 'belongsToManyRelation' + ) + ->create()->fresh(); + + $matchingModel2 = ModelFactory::new() + ->hasAttached( + BelongsToManyRelationFactory::new()->count(1), + ['number' => 1], + 'belongsToManyRelation' + ) + ->create()->fresh(); + + $pivotAccessor = $matchingModel->belongsToManyRelation()->getPivotAccessor(); + + Gate::policy(Model::class, GreenPolicy::class); + Gate::policy(BelongsToManyRelation::class, GreenPolicy::class); + + $response = $this->post( + '/api/models/search', + [ + 'search' => [ + 'includes' => [ + [ + 'relation' => 'belongsToManyRelation', + 'filters' => [ + ['field' => 'models.pivot.number', 'operator' => '=', 'value' => null], + ], + ], + ], + ], + ], + ['Accept' => 'application/json'] + ); + + $this->assertResourcePaginated( + $response, + [$matchingModel, $matchingModel2], + new ModelResource(), + [ + [ + 'belongs_to_many_relation' => $matchingModel->belongsToManyRelation() + ->orderBy('id', 'desc') + ->get() + ->map(function ($relation) use ($pivotAccessor) { + return collect($relation->only( + array_merge((new BelongsToManyResource())->getFields(app()->make(RestRequest::class)), [$pivotAccessor]) + )) + ->pipe(function ($relation) use ($pivotAccessor) { + $relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray()) + ->only( + (new ModelResource())->relation('belongsToManyRelation')->getPivotFields() + ); + + return $relation; + }); + }) + ->toArray(), + ], + [ + 'belongs_to_many_relation' => [], + ], + ] + ); + } } diff --git a/tests/Support/Database/migrations/2023_05_00_000000_create_belongs_to_many_relation_model_table.php b/tests/Support/Database/migrations/2023_05_00_000000_create_belongs_to_many_relation_model_table.php index f9eb9fb..230f935 100644 --- a/tests/Support/Database/migrations/2023_05_00_000000_create_belongs_to_many_relation_model_table.php +++ b/tests/Support/Database/migrations/2023_05_00_000000_create_belongs_to_many_relation_model_table.php @@ -16,7 +16,7 @@ public function up() $table->foreignIdFor(\Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation::class)->constrained(indexName: 'belongs_to_many_relation_self_id_foreign'); $table->foreignIdFor(\Lomkit\Rest\Tests\Support\Models\Model::class)->constrained(); - $table->integer('number')->default(0); + $table->integer('number')->default(0)->nullable(); $table->timestamps(); diff --git a/tests/Support/Rest/Resources/BelongsToManyResource.php b/tests/Support/Rest/Resources/BelongsToManyResource.php index 0f0ead9..e57cb52 100644 --- a/tests/Support/Rest/Resources/BelongsToManyResource.php +++ b/tests/Support/Rest/Resources/BelongsToManyResource.php @@ -6,6 +6,7 @@ use Lomkit\Rest\Http\Requests\RestRequest; use Lomkit\Rest\Http\Resource; use Lomkit\Rest\Relations\BelongsTo; +use Lomkit\Rest\Relations\BelongsToMany; use Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation; class BelongsToManyResource extends Resource @@ -18,6 +19,7 @@ public function relations(RestRequest $request): array { return [ BelongsTo::make('model', ModelQueryChangedResource::class), + BelongsToMany::make('models', ModelResource::class)->withPivotFields(['number']), ]; }