Skip to content

Commit eb13f44

Browse files
authored
Merge pull request #146 from Lomkit/fix/force-morph-to-relation-test
✅ morph to force model type
2 parents 35d0021 + e4703f7 commit eb13f44

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

tests/Feature/Controllers/SearchIncludingMorphRelationshipsOperationsTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,45 @@ public function test_getting_a_list_of_resources_including_morph_to_relation():
6969
);
7070
}
7171

72+
public function test_getting_a_list_of_resources_including_morph_to_relation_with_concrete_relation(): void
73+
{
74+
$morphTo = MorphToRelationFactory::new()->create();
75+
$matchingModel = ModelFactory::new()
76+
->for($morphTo, 'morphToForceModelRelation')
77+
->create()->fresh();
78+
79+
$matchingModel2 = ModelFactory::new()->create()->fresh();
80+
81+
Gate::policy(Model::class, GreenPolicy::class);
82+
Gate::policy(MorphToRelation::class, GreenPolicy::class);
83+
84+
$response = $this->post(
85+
'/api/models/search',
86+
[
87+
'search' => [
88+
'includes' => [
89+
['relation' => 'morphToForceModelRelation'],
90+
],
91+
],
92+
],
93+
['Accept' => 'application/json']
94+
);
95+
96+
$this->assertResourcePaginated(
97+
$response,
98+
[$matchingModel, $matchingModel2],
99+
new ModelResource(),
100+
[
101+
[
102+
'morph_to_force_model_relation' => $matchingModel->morphToForceModelRelation->only((new MorphToResource())->getFields(app()->make(RestRequest::class))),
103+
],
104+
[
105+
'morph_to_force_model_relation' => null,
106+
],
107+
]
108+
);
109+
}
110+
72111
public function test_getting_a_list_of_resources_including_morph_one_relation(): void
73112
{
74113
$matchingModel = ModelFactory::new()

tests/Support/Models/Model.php

+7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ public function morphToRelation()
7979
return $this->morphTo();
8080
}
8181

82+
public function morphToForceModelRelation()
83+
{
84+
return $this->morphTo('morphToForceModelRelation', 'morph_to_relation_type', 'morph_to_relation_id')->whereHas('model', function (Builder $query) {
85+
$query->where('morph_to_relation_type', MorphToRelation::class);
86+
});
87+
}
88+
8289
public function morphManyRelation()
8390
{
8491
return $this->morphMany(MorphManyRelation::class, 'morph_many_relation');

tests/Support/Models/MorphToRelation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class MorphToRelation extends BaseModel
88
{
99
public function model()
1010
{
11-
return $this->morphOne(Model::class, 'morph_to');
11+
return $this->morphOne(Model::class, 'morph_to_relation');
1212
}
1313
}

tests/Support/Rest/Resources/ModelResource.php

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function relations(RestRequest $request): array
6565

6666
// Morph relationships
6767
MorphTo::make('morphToRelation', MorphToResource::class),
68+
MorphTo::make('morphToForceModelRelation', MorphToResource::class),
6869
MorphOne::make('morphOneRelation', MorphOneResource::class),
6970
MorphOneOfMany::make('morphOneOfManyRelation', MorphOneOfManyResource::class),
7071
MorphMany::make('morphManyRelation', MorphManyResource::class),

0 commit comments

Comments
 (0)