From 38cb1c6d65455e270fea231a83d391ea9fcd6f9b Mon Sep 17 00:00:00 2001 From: Gautier DELEGLISE Date: Mon, 18 Nov 2024 21:50:07 +0100 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=85=20include=20filtering=20on=20pivo?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...chIncludingRelationshipsOperationsTest.php | 72 +++++++++++++++++++ .../Rest/Resources/BelongsToManyResource.php | 2 + 2 files changed, 74 insertions(+) diff --git a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php index 1decb30..966c707 100644 --- a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php +++ b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php @@ -491,6 +491,78 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat ['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(): 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], 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']), ]; } From c63d9759f07250dac962a702d241f88b576ecd43 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 18 Nov 2024 20:50:30 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- .../SearchIncludingRelationshipsOperationsTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php index 966c707..92e0d64 100644 --- a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php +++ b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php @@ -552,9 +552,9 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat 'includes' => [ [ 'relation' => 'belongsToManyRelation', - 'filters' => [ - ['field' => 'models.pivot.number', 'operator' => '>', 'value' => 2] - ] + 'filters' => [ + ['field' => 'models.pivot.number', 'operator' => '>', 'value' => 2], + ], ], ], ], @@ -562,7 +562,6 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat ['Accept' => 'application/json'] ); - $this->assertResourcePaginated( $response, [$matchingModel, $matchingModel2], From 03d2533a5b66482d7b45b9facf111657a41d1316 Mon Sep 17 00:00:00 2001 From: Gautier DELEGLISE Date: Mon, 2 Dec 2024 10:58:27 +0100 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=85=20nullable=20pivot=20filtering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...chIncludingRelationshipsOperationsTest.php | 72 +++++++++++++++++++ ...e_belongs_to_many_relation_model_table.php | 2 +- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php index 92e0d64..d199c42 100644 --- a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php +++ b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php @@ -562,6 +562,78 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat ['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], 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(); From 648154019822bbe61ffd82321131130b9a4e19a1 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 2 Dec 2024 09:58:39 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- .../SearchIncludingRelationshipsOperationsTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php index d199c42..7d03c8b 100644 --- a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php +++ b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php @@ -623,9 +623,9 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat 'includes' => [ [ 'relation' => 'belongsToManyRelation', - 'filters' => [ - ['field' => 'models.pivot.number', 'operator' => '=', 'value' => null] - ] + 'filters' => [ + ['field' => 'models.pivot.number', 'operator' => '=', 'value' => null], + ], ], ], ], @@ -633,7 +633,6 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat ['Accept' => 'application/json'] ); - $this->assertResourcePaginated( $response, [$matchingModel, $matchingModel2],