Skip to content

Commit

Permalink
Set supported Laravel version.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldemeery committed Oct 25, 2024
1 parent 2145892 commit e5e844a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 163 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function index(Request $request)
## Installation

> [!IMPORTANT]
> This package requires Laravel 9.0 or higher.
> This package requires Laravel 11.0 or higher and PHP 8.2 or higher.
You can install the package via composer:

Expand Down
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
],
"minimum-stability": "stable",
"require": {
"php": ">=8.0",
"illuminate/support": ">=9.0",
"illuminate/database": ">=9.0",
"illuminate/console": ">=9.00",
"aldemeery/onion": "^1.0",
"azjezz/psl": "^3.0"
"php": "^8.2",
"illuminate/support": "^11.0",
"illuminate/database": "^11.0",
"illuminate/console": "^11.0",
"aldemeery/onion": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
155 changes: 4 additions & 151 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.3
context: ./vendor/laravel/sail/runtimes/8.2
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.3/app
image: sail-8.2/app
extra_hosts:
- 'host.docker.internal:host-gateway'
environment:
Expand Down
16 changes: 14 additions & 2 deletions src/Concerns/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use Psl\Type;
use RuntimeException;

trait Filterable
{
Expand All @@ -24,7 +24,19 @@ protected function scopeFilter(Builder $query, array $params = [], array $additi
fn (array $filters): array => array_merge($filters, $additional),
fn (array $filters): array => array_intersect_key($filters, $params),
fn (array $filters): array => array_map(
fn (string $filter): Filter => Type\instance_of(Filter::class)->assert(App::make($filter)),
function (string $filter): Filter {
$filter = App::make($filter);

if (!$filter instanceof Filter) {
throw new RuntimeException(sprintf(
'Filters must implement %s, but %s does not.',
Filter::class,
is_object($filter) ? get_class($filter) : gettype($filter),
));
}

return $filter;
},
$filters,
),
fn (array $filters): true => array_walk(
Expand Down
23 changes: 23 additions & 0 deletions tests/BadFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Tests\Aldemeery\Sieve;

use Illuminate\Database\Eloquent\Builder;

class BadFilter
{
public function map(mixed $value): mixed
{
return match ($value) {
default => $value,
};
}

/** @param Builder<ModelWithoutFilters> $query */
public function apply(Builder $query, mixed $value): void
{
$query->where('test', $value);
}
}
14 changes: 14 additions & 0 deletions tests/Concerns/FilterableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use PDO;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Tests\Aldemeery\Sieve\BadFilter;
use Tests\Aldemeery\Sieve\ModelWithFilters;
use Tests\Aldemeery\Sieve\ModelWithoutFilters;
use Tests\Aldemeery\Sieve\TestFilter;
Expand Down Expand Up @@ -73,4 +75,16 @@ public function test_additional_filters_are_applied(): void
static::assertSame('select * from "tests" where "test" = ? and "test" = ?', $query->toSql());
static::assertSame(['one', 'three'], $query->getBindings());
}

public function test_filters_not_implementing_the_filter_interface_throw_an_exception(): void
{
App::shouldReceive('make')->with(BadFilter::class)->andReturn(new BadFilter());

$filterable = new ModelWithoutFilters();

static::expectException(RuntimeException::class);
static::expectExceptionMessage('Filters must implement Aldemeery\Sieve\Contracts\Filter, but Tests\Aldemeery\Sieve\BadFilter does not.');

$filterable->filter(['test-1' => 'one'], ['test-1' => BadFilter::class]);
}
}
2 changes: 1 addition & 1 deletion tests/ModelWithoutFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Aldemeery\Sieve\Concerns\Filterable;
use Illuminate\Database\Eloquent\Model;

/** @method \Illuminate\Database\Eloquent\Builder filter(array $params) */
/** @method \Illuminate\Database\Eloquent\Builder filter(array $params = [], array $additional = []) */
class ModelWithoutFilters extends Model
{
use Filterable;
Expand Down

0 comments on commit e5e844a

Please sign in to comment.