From e4703f7c4b7009b44f272e1c69446a6756e6bb4f Mon Sep 17 00:00:00 2001 From: Gautier DELEGLISE Date: Wed, 4 Dec 2024 14:40:47 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20morph=20to=20force=20model=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ludingMorphRelationshipsOperationsTest.php | 39 +++++++++++++++++++ tests/Support/Models/Model.php | 7 ++++ tests/Support/Models/MorphToRelation.php | 2 +- .../Support/Rest/Resources/ModelResource.php | 1 + 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Controllers/SearchIncludingMorphRelationshipsOperationsTest.php b/tests/Feature/Controllers/SearchIncludingMorphRelationshipsOperationsTest.php index 9eec0ee..627d591 100644 --- a/tests/Feature/Controllers/SearchIncludingMorphRelationshipsOperationsTest.php +++ b/tests/Feature/Controllers/SearchIncludingMorphRelationshipsOperationsTest.php @@ -69,6 +69,45 @@ public function test_getting_a_list_of_resources_including_morph_to_relation(): ); } + public function test_getting_a_list_of_resources_including_morph_to_relation_with_concrete_relation(): void + { + $morphTo = MorphToRelationFactory::new()->create(); + $matchingModel = ModelFactory::new() + ->for($morphTo, 'morphToForceModelRelation') + ->create()->fresh(); + + $matchingModel2 = ModelFactory::new()->create()->fresh(); + + Gate::policy(Model::class, GreenPolicy::class); + Gate::policy(MorphToRelation::class, GreenPolicy::class); + + $response = $this->post( + '/api/models/search', + [ + 'search' => [ + 'includes' => [ + ['relation' => 'morphToForceModelRelation'], + ], + ], + ], + ['Accept' => 'application/json'] + ); + + $this->assertResourcePaginated( + $response, + [$matchingModel, $matchingModel2], + new ModelResource(), + [ + [ + 'morph_to_force_model_relation' => $matchingModel->morphToForceModelRelation->only((new MorphToResource())->getFields(app()->make(RestRequest::class))), + ], + [ + 'morph_to_force_model_relation' => null, + ], + ] + ); + } + public function test_getting_a_list_of_resources_including_morph_one_relation(): void { $matchingModel = ModelFactory::new() diff --git a/tests/Support/Models/Model.php b/tests/Support/Models/Model.php index a602163..d5e47bb 100644 --- a/tests/Support/Models/Model.php +++ b/tests/Support/Models/Model.php @@ -79,6 +79,13 @@ public function morphToRelation() return $this->morphTo(); } + public function morphToForceModelRelation() + { + return $this->morphTo('morphToForceModelRelation', 'morph_to_relation_type', 'morph_to_relation_id')->whereHas('model', function (Builder $query) { + $query->where('morph_to_relation_type', MorphToRelation::class); + }); + } + public function morphManyRelation() { return $this->morphMany(MorphManyRelation::class, 'morph_many_relation'); diff --git a/tests/Support/Models/MorphToRelation.php b/tests/Support/Models/MorphToRelation.php index 3666bb5..1ad7949 100644 --- a/tests/Support/Models/MorphToRelation.php +++ b/tests/Support/Models/MorphToRelation.php @@ -8,6 +8,6 @@ class MorphToRelation extends BaseModel { public function model() { - return $this->morphOne(Model::class, 'morph_to'); + return $this->morphOne(Model::class, 'morph_to_relation'); } } diff --git a/tests/Support/Rest/Resources/ModelResource.php b/tests/Support/Rest/Resources/ModelResource.php index 0c9ee60..98b0305 100644 --- a/tests/Support/Rest/Resources/ModelResource.php +++ b/tests/Support/Rest/Resources/ModelResource.php @@ -65,6 +65,7 @@ public function relations(RestRequest $request): array // Morph relationships MorphTo::make('morphToRelation', MorphToResource::class), + MorphTo::make('morphToForceModelRelation', MorphToResource::class), MorphOne::make('morphOneRelation', MorphOneResource::class), MorphOneOfMany::make('morphOneOfManyRelation', MorphOneOfManyResource::class), MorphMany::make('morphManyRelation', MorphManyResource::class),