Skip to content

Commit

Permalink
change RelationNotFoundException instead of InvalidFilterProperty, ad…
Browse files Browse the repository at this point in the history
…d tests
  • Loading branch information
gpibarra committed Oct 21, 2024
1 parent 4ce4259 commit 4b3f57e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
13 changes: 0 additions & 13 deletions src/Exceptions/InvalidFilterProperty.php

This file was deleted.

17 changes: 2 additions & 15 deletions src/Filters/FiltersBelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\RelationNotFoundException;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Spatie\QueryBuilder\Exceptions\InvalidFilterProperty;

/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
Expand Down Expand Up @@ -44,36 +44,23 @@ protected function getRelatedModel(Model $modelQuery, string $relationName, stri
{
if ($relationParent) {
$modelParent = $this->getModelFromRelation($modelQuery, $relationParent);
if (! $modelParent) {
throw InvalidFilterProperty::make($relationParent.'.'.$relationName);
}
} else {
$modelParent = $modelQuery;
}

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

return $relatedModel;
}

protected function getRelatedModelFromRelation(Model $model, string $relationName): ?Model
{
if (! method_exists($model, $relationName)) {
return null;
}

$relationObject = $model->$relationName();
if (! is_subclass_of($relationObject, Relation::class)) {
return null;
throw RelationNotFoundException::make($model, $relationName);
}

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

return $relatedModel;
}
Expand Down
24 changes: 14 additions & 10 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\Enums\FilterOperator;
use Spatie\QueryBuilder\Exceptions\InvalidFilterProperty;
use Spatie\QueryBuilder\Exceptions\InvalidFilterQuery;
use Spatie\QueryBuilder\Filters\Filter as CustomFilter;
use Spatie\QueryBuilder\Filters\Filter as FilterInterface;
Expand Down Expand Up @@ -349,18 +348,23 @@
expect($modelsResult)->toHaveCount(2);
});

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

$testModel = TestModel::create(['name' => 'John Test Doe']);
$relatedModel = RelatedModel::create(['name' => 'John Related Doe', 'test_model_id' => $testModel->id]);
$nestedModel = NestedRelatedModel::create(['name' => 'John Nested Doe', 'related_model_id' => $relatedModel->id]);

$modelsResult = createQueryFromFilterRequest(['test_filter' => $testModel->id], NestedRelatedModel::class)
->allowedFilters(AllowedFilter::belongsTo('test_filter', 'inexistentRelation.testModel'))
$modelsResult = createQueryFromFilterRequest(['test_filter' => 1], RelatedModel::class)
->allowedFilters(AllowedFilter::belongsTo('test_filter', $relationName))
->get();

});
})->with([
['inexistentRelation', \BadMethodCallException::class],
['testModel.inexistentRelation', \BadMethodCallException::class], // existing 'testModel' belongsTo relation
['inexistentRelation.inexistentRelation', \BadMethodCallException::class],
['getTable', \Illuminate\Database\Eloquent\RelationNotFoundException::class],
['testModel.getTable', \Illuminate\Database\Eloquent\RelationNotFoundException::class], // existing 'testModel' belongsTo relation
['getTable.getTable', \Illuminate\Database\Eloquent\RelationNotFoundException::class],
['nestedRelatedModels', \Illuminate\Database\Eloquent\RelationNotFoundException::class], // existing 'nestedRelatedModels' relation but not a belongsTo relation
['testModel.relatedModels', \Illuminate\Database\Eloquent\RelationNotFoundException::class], // existing 'testModel' belongsTo relation and existing 'relatedModels' relation but not a belongsTo relation
]);

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

0 comments on commit 4b3f57e

Please sign in to comment.