Skip to content

Commit

Permalink
Merge pull request #146 from Lomkit/fix/force-morph-to-relation-test
Browse files Browse the repository at this point in the history
✅ morph to force model type
  • Loading branch information
GautierDele authored Dec 4, 2024
2 parents 35d0021 + e4703f7 commit eb13f44
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Models/MorphToRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
1 change: 1 addition & 0 deletions tests/Support/Rest/Resources/ModelResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit eb13f44

Please sign in to comment.