Skip to content

Commit 4b3f57e

Browse files
committed
change RelationNotFoundException instead of InvalidFilterProperty, add tests
1 parent 4ce4259 commit 4b3f57e

File tree

3 files changed

+16
-38
lines changed

3 files changed

+16
-38
lines changed

src/Exceptions/InvalidFilterProperty.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Filters/FiltersBelongsTo.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\RelationNotFoundException;
78
use Illuminate\Database\Eloquent\Relations\Relation;
89
use Illuminate\Support\Arr;
9-
use Spatie\QueryBuilder\Exceptions\InvalidFilterProperty;
1010

1111
/**
1212
* @template TModelClass of \Illuminate\Database\Eloquent\Model
@@ -44,36 +44,23 @@ protected function getRelatedModel(Model $modelQuery, string $relationName, stri
4444
{
4545
if ($relationParent) {
4646
$modelParent = $this->getModelFromRelation($modelQuery, $relationParent);
47-
if (! $modelParent) {
48-
throw InvalidFilterProperty::make($relationParent.'.'.$relationName);
49-
}
5047
} else {
5148
$modelParent = $modelQuery;
5249
}
5350

5451
$relatedModel = $this->getRelatedModelFromRelation($modelParent, $relationName);
55-
if (! $relatedModel) {
56-
throw InvalidFilterProperty::make($relationParent.'.'.$relationName);
57-
}
5852

5953
return $relatedModel;
6054
}
6155

6256
protected function getRelatedModelFromRelation(Model $model, string $relationName): ?Model
6357
{
64-
if (! method_exists($model, $relationName)) {
65-
return null;
66-
}
67-
6858
$relationObject = $model->$relationName();
6959
if (! is_subclass_of($relationObject, Relation::class)) {
70-
return null;
60+
throw RelationNotFoundException::make($model, $relationName);
7161
}
7262

7363
$relatedModel = $relationObject->getRelated();
74-
if (! is_subclass_of($relatedModel, Model::class)) {
75-
return null;
76-
}
7764

7865
return $relatedModel;
7966
}

tests/FilterTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use Spatie\QueryBuilder\AllowedFilter;
1212
use Spatie\QueryBuilder\Enums\FilterOperator;
13-
use Spatie\QueryBuilder\Exceptions\InvalidFilterProperty;
1413
use Spatie\QueryBuilder\Exceptions\InvalidFilterQuery;
1514
use Spatie\QueryBuilder\Filters\Filter as CustomFilter;
1615
use Spatie\QueryBuilder\Filters\Filter as FilterInterface;
@@ -349,18 +348,23 @@
349348
expect($modelsResult)->toHaveCount(2);
350349
});
351350

352-
it('throws an exception when trying to filter by belongs to with an inexistent relation', function () {
353-
$this->expectException(InvalidFilterProperty::class);
351+
it('throws an exception when trying to filter by belongs to with an inexistent relation', function ($relationName, $exceptionClass) {
352+
$this->expectException($exceptionClass);
354353

355-
$testModel = TestModel::create(['name' => 'John Test Doe']);
356-
$relatedModel = RelatedModel::create(['name' => 'John Related Doe', 'test_model_id' => $testModel->id]);
357-
$nestedModel = NestedRelatedModel::create(['name' => 'John Nested Doe', 'related_model_id' => $relatedModel->id]);
358-
359-
$modelsResult = createQueryFromFilterRequest(['test_filter' => $testModel->id], NestedRelatedModel::class)
360-
->allowedFilters(AllowedFilter::belongsTo('test_filter', 'inexistentRelation.testModel'))
354+
$modelsResult = createQueryFromFilterRequest(['test_filter' => 1], RelatedModel::class)
355+
->allowedFilters(AllowedFilter::belongsTo('test_filter', $relationName))
361356
->get();
362357

363-
});
358+
})->with([
359+
['inexistentRelation', \BadMethodCallException::class],
360+
['testModel.inexistentRelation', \BadMethodCallException::class], // existing 'testModel' belongsTo relation
361+
['inexistentRelation.inexistentRelation', \BadMethodCallException::class],
362+
['getTable', \Illuminate\Database\Eloquent\RelationNotFoundException::class],
363+
['testModel.getTable', \Illuminate\Database\Eloquent\RelationNotFoundException::class], // existing 'testModel' belongsTo relation
364+
['getTable.getTable', \Illuminate\Database\Eloquent\RelationNotFoundException::class],
365+
['nestedRelatedModels', \Illuminate\Database\Eloquent\RelationNotFoundException::class], // existing 'nestedRelatedModels' relation but not a belongsTo relation
366+
['testModel.relatedModels', \Illuminate\Database\Eloquent\RelationNotFoundException::class], // existing 'testModel' belongsTo relation and existing 'relatedModels' relation but not a belongsTo relation
367+
]);
364368

365369
it('can filter results by scope', function () {
366370
$testModel = TestModel::create(['name' => 'John Testing Doe']);

0 commit comments

Comments
 (0)