|
10 | 10 |
|
11 | 11 | use Spatie\QueryBuilder\AllowedFilter;
|
12 | 12 | use Spatie\QueryBuilder\Enums\FilterOperator;
|
13 |
| -use Spatie\QueryBuilder\Exceptions\InvalidFilterProperty; |
14 | 13 | use Spatie\QueryBuilder\Exceptions\InvalidFilterQuery;
|
15 | 14 | use Spatie\QueryBuilder\Filters\Filter as CustomFilter;
|
16 | 15 | use Spatie\QueryBuilder\Filters\Filter as FilterInterface;
|
|
349 | 348 | expect($modelsResult)->toHaveCount(2);
|
350 | 349 | });
|
351 | 350 |
|
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); |
354 | 353 |
|
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)) |
361 | 356 | ->get();
|
362 | 357 |
|
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 | +]); |
364 | 368 |
|
365 | 369 | it('can filter results by scope', function () {
|
366 | 370 | $testModel = TestModel::create(['name' => 'John Testing Doe']);
|
|
0 commit comments