From 710fd216e956166c97cafd577df1cbc96e24df8d Mon Sep 17 00:00:00 2001 From: l3aro Date: Sun, 12 Jun 2022 12:44:58 +0700 Subject: [PATCH 1/3] define sqlite test flow --- .github/workflows/run-tests.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 24cd0fe..80c0744 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -60,10 +60,18 @@ jobs: run: | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update composer update --${{ matrix.stability }} --prefer-dist --no-interaction - - name: Execute tests + - name: Execute tests with MySQL run: vendor/bin/pest env: + DB_CONNECTION: mysql DB_USERNAME: user DB_PASSWORD: secret DB_PORT: ${{ job.services.mysql.ports[3306] }} REDIS_PORT: 6379 + - name: Execute tests with SQLite + run: vendor/bin/pest + env: + DB_CONNECTION: sqlite + DB_DATABASE: :memory: + REDIS_PORT: 6379 + \ No newline at end of file From 40d046d986d904dd12bb3f098dfeec616cbc5f05 Mon Sep 17 00:00:00 2001 From: l3aro Date: Sun, 12 Jun 2022 12:46:59 +0700 Subject: [PATCH 2/3] fix define db --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 80c0744..26f4891 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -72,6 +72,6 @@ jobs: run: vendor/bin/pest env: DB_CONNECTION: sqlite - DB_DATABASE: :memory: + DB_DATABASE: ":memory:" REDIS_PORT: 6379 \ No newline at end of file From 6c4438fd7f831ca105d963dc0c76e16cc8946c74 Mon Sep 17 00:00:00 2001 From: l3aro Date: Sun, 12 Jun 2022 13:32:52 +0700 Subject: [PATCH 3/3] abstract base pipe --- src/BaseFilter.php | 15 +++++---------- src/BasePipe.php | 28 ++++++++++++++++++++++++++++ src/BaseSort.php | 17 ++++++++++++----- src/BitwiseFilter.php | 8 +++----- src/BooleanFilter.php | 8 +++----- src/DateFromFilter.php | 16 ++++++++++++---- src/DateToFilter.php | 16 ++++++++++++---- src/ExactFilter.php | 8 +++----- src/RelationFilter.php | 7 +++---- src/RelativeFilter.php | 7 +++---- src/ScopeFilter.php | 8 +++----- src/Sort.php | 12 ++++-------- src/SortAscending.php | 12 ++++-------- src/SortDescending.php | 12 ++++-------- src/TrashFilter.php | 11 +++++------ tests/FilterTest.php | 1 + 16 files changed, 105 insertions(+), 81 deletions(-) create mode 100644 src/BasePipe.php diff --git a/src/BaseFilter.php b/src/BaseFilter.php index 73ea65f..8111bb3 100644 --- a/src/BaseFilter.php +++ b/src/BaseFilter.php @@ -2,32 +2,27 @@ namespace Baro\PipelineQueryCollection; -use Illuminate\Database\Eloquent\Builder; -use Illuminate\Http\Request; - -abstract class BaseFilter +abstract class BaseFilter extends BasePipe { protected string $ignore; protected ?string $field = null; protected string $detector; protected ?string $searchColumn = null; - protected Request $request; public function __construct() { - $this->request = app(Request::class); + parent::__construct(); $this->detector = config('pipeline-query-collection.detect_key'); } - abstract protected function apply(Builder $query): Builder; - public function handle($query, \Closure $next) { + $this->query = $query; if (!$this->shouldFilter($this->getFilterName())) { return $next($query); } - - return $next($this->apply($query)); + $this->apply(); + return $next($this->query); } protected function getFilterName(): string diff --git a/src/BasePipe.php b/src/BasePipe.php new file mode 100644 index 0000000..d7b7240 --- /dev/null +++ b/src/BasePipe.php @@ -0,0 +1,28 @@ +request = app(Request::class); + } + + abstract protected function apply(): static; + + abstract public function handle($query, \Closure $next); + + protected function getDriverName(): string + { + /** @var \Illuminate\Database\Connection */ + $connection = $this->query->getConnection(); + return $connection->getDriverName(); + } +} diff --git a/src/BaseSort.php b/src/BaseSort.php index 6cf1ee2..c2ff7b9 100644 --- a/src/BaseSort.php +++ b/src/BaseSort.php @@ -2,14 +2,21 @@ namespace Baro\PipelineQueryCollection; -use Illuminate\Http\Request; - -abstract class BaseSort +abstract class BaseSort extends BasePipe { - protected Request $request; + protected array $sort; public function __construct() { - $this->request = app(Request::class); + parent::__construct(); + } + + public function handle($query, \Closure $next) + { + $this->query = $query; + $sort = $this->request->input('sort', []); + $this->sort = !is_array($sort) ? [$sort] : $sort; + $this->apply(); + return $next($this->query); } } diff --git a/src/BitwiseFilter.php b/src/BitwiseFilter.php index d523024..dea7e9e 100644 --- a/src/BitwiseFilter.php +++ b/src/BitwiseFilter.php @@ -2,8 +2,6 @@ namespace Baro\PipelineQueryCollection; -use Illuminate\Database\Eloquent\Builder; - class BitwiseFilter extends BaseFilter { public function __construct($field) @@ -12,14 +10,14 @@ public function __construct($field) $this->field = $field; } - protected function apply(Builder $query): Builder + protected function apply(): static { $flag = null; foreach ($this->getSearchValue() as $value) { $flag ??= intval($value); $flag = intval($flag) | intval($value); } - $query->whereRaw("{$this->getSearchColumn()} & ? = ?", [$flag, $flag]); - return $query; + $this->query->whereRaw("{$this->getSearchColumn()} & ? = ?", [$flag, $flag]); + return $this; } } diff --git a/src/BooleanFilter.php b/src/BooleanFilter.php index 2315a73..8e6f1a9 100644 --- a/src/BooleanFilter.php +++ b/src/BooleanFilter.php @@ -2,8 +2,6 @@ namespace Baro\PipelineQueryCollection; -use Illuminate\Database\Eloquent\Builder; - class BooleanFilter extends BaseFilter { public function __construct($field) @@ -12,12 +10,12 @@ public function __construct($field) $this->field = $field; } - protected function apply(Builder $query): Builder + protected function apply(): static { foreach ($this->getSearchValue() as $value) { - $query->where($this->getSearchColumn(), $value ? true : false); + $this->query->where($this->getSearchColumn(), $value ? true : false); } - return $query; + return $this; } } diff --git a/src/DateFromFilter.php b/src/DateFromFilter.php index e668011..5afe804 100644 --- a/src/DateFromFilter.php +++ b/src/DateFromFilter.php @@ -3,7 +3,6 @@ namespace Baro\PipelineQueryCollection; use Baro\PipelineQueryCollection\Enums\MotionEnum; -use Illuminate\Database\Eloquent\Builder; class DateFromFilter extends BaseFilter { @@ -23,13 +22,22 @@ public function __construct($field = 'created_at', MotionEnum|string $motion = n $this->motion = $motion; } - protected function apply(Builder $query): Builder + protected function apply(): static { $operator = $this->motion === MotionEnum::FIND ? '>=' : '>'; + $action = $this->getAction(); foreach ($this->getSearchValue() as $value) { - $query->whereDate($this->getSearchColumn(), $operator, $value); + $this->query->$action($this->getSearchColumn(), $operator, $value); } - return $query; + return $this; + } + + private function getAction(): string + { + return match ($this->getDriverName()) { + 'sqlite' => 'whereDate', + default => 'where', + }; } protected function getFilterName(): string diff --git a/src/DateToFilter.php b/src/DateToFilter.php index 07abe0b..0098d3f 100644 --- a/src/DateToFilter.php +++ b/src/DateToFilter.php @@ -3,7 +3,6 @@ namespace Baro\PipelineQueryCollection; use Baro\PipelineQueryCollection\Enums\MotionEnum; -use Illuminate\Database\Eloquent\Builder; class DateToFilter extends BaseFilter { @@ -23,13 +22,22 @@ public function __construct($field = 'created_at', MotionEnum|string $motion = n $this->motion = $motion; } - protected function apply(Builder $query): Builder + protected function apply(): static { $operator = $this->motion === MotionEnum::FIND ? '<=' : '<'; + $action = $this->getAction(); foreach ($this->getSearchValue() as $value) { - $query->whereDate($this->getSearchColumn(), $operator, $value); + $this->query->$action($this->getSearchColumn(), $operator, $value); } - return $query; + return $this; + } + + private function getAction(): string + { + return match ($this->getDriverName()) { + 'sqlite' => 'whereDate', + default => 'where', + }; } protected function getFilterName(): string diff --git a/src/ExactFilter.php b/src/ExactFilter.php index 8cd7c12..ccc1d9e 100644 --- a/src/ExactFilter.php +++ b/src/ExactFilter.php @@ -2,8 +2,6 @@ namespace Baro\PipelineQueryCollection; -use Illuminate\Database\Eloquent\Builder; - class ExactFilter extends BaseFilter { public function __construct($field) @@ -12,11 +10,11 @@ public function __construct($field) $this->field = $field; } - protected function apply(Builder $query): Builder + protected function apply(): static { foreach ($this->getSearchValue() as $value) { - $query->where($this->getSearchColumn(), $value); + $this->query->where($this->getSearchColumn(), $value); } - return $query; + return $this; } } diff --git a/src/RelationFilter.php b/src/RelationFilter.php index 594774a..89827e5 100644 --- a/src/RelationFilter.php +++ b/src/RelationFilter.php @@ -2,7 +2,6 @@ namespace Baro\PipelineQueryCollection; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Str; class RelationFilter extends BaseFilter @@ -21,14 +20,14 @@ protected function getFilterName(): string return "{$this->detector}{$this->relation}_{$this->field}"; } - protected function apply(Builder $query): Builder + protected function apply(): static { $searchValue = $this->getSearchValue(); $this->relation = Str::camel($this->relation); - $query->whereHas($this->relation, function ($query) use ($searchValue) { + $this->query->whereHas($this->relation, function ($query) use ($searchValue) { $query->whereIn($this->getSearchColumn(), $searchValue); }); - return $query; + return $this; } } diff --git a/src/RelativeFilter.php b/src/RelativeFilter.php index bea98f8..dafe8ac 100644 --- a/src/RelativeFilter.php +++ b/src/RelativeFilter.php @@ -3,7 +3,6 @@ namespace Baro\PipelineQueryCollection; use Baro\PipelineQueryCollection\Enums\WildcardPositionEnum; -use Illuminate\Database\Eloquent\Builder; class RelativeFilter extends BaseFilter { @@ -22,12 +21,12 @@ public function __construct($field, WildcardPositionEnum|string $wildcardPositio $this->wildcardPosition = $wildcardPosition; } - protected function apply(Builder $query): Builder + protected function apply(): static { foreach ($this->getSearchValue() as $value) { - $query->where($this->getSearchColumn(), 'like', $this->computeSearchValue($value)); + $this->query->where($this->getSearchColumn(), 'like', $this->computeSearchValue($value)); } - return $query; + return $this; } private function computeSearchValue($value) diff --git a/src/ScopeFilter.php b/src/ScopeFilter.php index 5a97406..0e6b1d7 100644 --- a/src/ScopeFilter.php +++ b/src/ScopeFilter.php @@ -2,8 +2,6 @@ namespace Baro\PipelineQueryCollection; -use Illuminate\Database\Eloquent\Builder; - class ScopeFilter extends BaseFilter { public function __construct($scopeName) @@ -12,12 +10,12 @@ public function __construct($scopeName) $this->field = $scopeName; } - protected function apply(Builder $query): Builder + protected function apply(): static { foreach ($this->getSearchValue() as $value) { - $query->{$this->field}($value); + $this->query->{$this->field}($value); } - return $query; + return $this; } } diff --git a/src/Sort.php b/src/Sort.php index 2722619..6369221 100644 --- a/src/Sort.php +++ b/src/Sort.php @@ -9,16 +9,12 @@ public function __construct() parent::__construct(); } - public function handle($query, \Closure $next) + protected function apply(): static { - $sort = $this->request->input('sort', []); - if (!is_array($sort)) { - $sort = [$sort]; - } - foreach ($sort as $field => $direction) { - $query->orderBy($field, $direction); + foreach ($this->sort as $field => $direction) { + $this->query->orderBy($field, $direction); } - return $next($query); + return $this; } } diff --git a/src/SortAscending.php b/src/SortAscending.php index db3b5a5..ccf9ff7 100644 --- a/src/SortAscending.php +++ b/src/SortAscending.php @@ -9,16 +9,12 @@ public function __construct() parent::__construct(); } - public function handle($query, \Closure $next) + protected function apply(): static { - $sort = $this->request->input('sort', []); - if (!is_array($sort)) { - $sort = [$sort]; - } - foreach ($sort as $field) { - $query->orderBy($field, 'asc'); + foreach ($this->sort as $field) { + $this->query->orderBy($field, 'asc'); } - return $next($query); + return $this; } } diff --git a/src/SortDescending.php b/src/SortDescending.php index aa86617..adaba18 100644 --- a/src/SortDescending.php +++ b/src/SortDescending.php @@ -9,16 +9,12 @@ public function __construct() parent::__construct(); } - public function handle($query, \Closure $next) + protected function apply(): static { - $sort = $this->request->input('sort', []); - if (!is_array($sort)) { - $sort = [$sort]; - } - foreach ($sort as $field) { - $query->orderBy($field, 'desc'); + foreach ($this->sort as $field) { + $this->query->orderBy($field, 'desc'); } - return $next($query); + return $this; } } diff --git a/src/TrashFilter.php b/src/TrashFilter.php index aa8ec96..ad6b20f 100644 --- a/src/TrashFilter.php +++ b/src/TrashFilter.php @@ -3,7 +3,6 @@ namespace Baro\PipelineQueryCollection; use Baro\PipelineQueryCollection\Enums\TrashOptionEnum; -use Illuminate\Database\Eloquent\Builder; class TrashFilter extends BaseFilter { @@ -13,14 +12,14 @@ public function __construct($field = 'trashed') $this->field = $field; } - protected function apply(Builder $query): Builder + protected function apply(): static { $option = TrashOptionEnum::tryFrom($this->getSearchValue()[0]); match ($option) { - TrashOptionEnum::ONLY => $query->onlyTrashed(), // @phpstan-ignore-line - TrashOptionEnum::WITH => $query->withTrashed(), // @phpstan-ignore-line - default => $query, + TrashOptionEnum::ONLY => $this->query->onlyTrashed(), // @phpstan-ignore-line + TrashOptionEnum::WITH => $this->query->withTrashed(), // @phpstan-ignore-line + default => $this->query, }; - return $query; + return $this; } } diff --git a/tests/FilterTest.php b/tests/FilterTest.php index 322d3af..e3ab9e4 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -40,6 +40,7 @@ expect(TestModel::filter()->count())->toBe(0); injectRequest(['created_at_from' => '2020-01-02']); + // dump(TestModel::getConnectionResolver()->getDriverName()); expect(TestModel::filter()->count())->toBe(4); expect(TestModel::filter([ new DateFromFilter('created_at', MotionEnum::TILL),