Skip to content

Commit

Permalink
Merge pull request #6 from l3aro/cover-sqlite
Browse files Browse the repository at this point in the history
define sqlite test flow
  • Loading branch information
l3aro authored Jun 12, 2022
2 parents 6ea4bf7 + 6c4438f commit f5638b2
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 82 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

15 changes: 5 additions & 10 deletions src/BaseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/BasePipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Baro\PipelineQueryCollection;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

abstract class BasePipe
{
protected Request $request;
protected Builder $query;

public function __construct()
{
$this->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();
}
}
17 changes: 12 additions & 5 deletions src/BaseSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 3 additions & 5 deletions src/BitwiseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Baro\PipelineQueryCollection;

use Illuminate\Database\Eloquent\Builder;

class BitwiseFilter extends BaseFilter
{
public function __construct($field)
Expand All @@ -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;
}
}
8 changes: 3 additions & 5 deletions src/BooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Baro\PipelineQueryCollection;

use Illuminate\Database\Eloquent\Builder;

class BooleanFilter extends BaseFilter
{
public function __construct($field)
Expand All @@ -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;
}
}
16 changes: 12 additions & 4 deletions src/DateFromFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Baro\PipelineQueryCollection;

use Baro\PipelineQueryCollection\Enums\MotionEnum;
use Illuminate\Database\Eloquent\Builder;

class DateFromFilter extends BaseFilter
{
Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/DateToFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Baro\PipelineQueryCollection;

use Baro\PipelineQueryCollection\Enums\MotionEnum;
use Illuminate\Database\Eloquent\Builder;

class DateToFilter extends BaseFilter
{
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/ExactFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Baro\PipelineQueryCollection;

use Illuminate\Database\Eloquent\Builder;

class ExactFilter extends BaseFilter
{
public function __construct($field)
Expand All @@ -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;
}
}
7 changes: 3 additions & 4 deletions src/RelationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Baro\PipelineQueryCollection;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;

class RelationFilter extends BaseFilter
Expand All @@ -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;
}
}
7 changes: 3 additions & 4 deletions src/RelativeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Baro\PipelineQueryCollection;

use Baro\PipelineQueryCollection\Enums\WildcardPositionEnum;
use Illuminate\Database\Eloquent\Builder;

class RelativeFilter extends BaseFilter
{
Expand All @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions src/ScopeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Baro\PipelineQueryCollection;

use Illuminate\Database\Eloquent\Builder;

class ScopeFilter extends BaseFilter
{
public function __construct($scopeName)
Expand All @@ -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;
}
}
12 changes: 4 additions & 8 deletions src/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 4 additions & 8 deletions src/SortAscending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 4 additions & 8 deletions src/SortDescending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading

0 comments on commit f5638b2

Please sign in to comment.