Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests with supported DBs + Input filter null/blank/empty #147

Merged
merged 2 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ on:
jobs:
build:
runs-on: ubuntu-latest

services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: powergridtest
ports:
- 3308:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

postgres:
image: postgres:10.8
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: powergridtest
ports:
- 5433:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

strategy:
matrix:
php: [ 7.4, 8.0 ]
Expand Down Expand Up @@ -49,10 +70,19 @@ jobs:
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Cs Fixer
run: vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no --stop-on-violation
run: ./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no --stop-on-violation

- name: Testing with SQLite
run: |
./vendor/bin/pest --configuration phpunit.sqlite.xml

- name: Testing with MySQL
run: |
./vendor/bin/pest --configuration phpunit.mysql.xml

- name: Larastan
run: vendor/bin/phpstan analyse --ansi --memory-limit=-1
- name: Testing with PostgreSQL
run: |
./vendor/bin/pest --configuration phpunit.pgsql.xml

- name: Pest tests
run: vendor/bin/pest
- name: Larastan
run: ./vendor/bin/phpstan analyse --ansi --memory-limit=-1
22 changes: 13 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,22 @@
"friendsofphp/php-cs-fixer": "^3.2"
},
"scripts": {
"stan": "phpstan analyse --ansi --memory-limit=-1",
"pest": "php ./vendor/bin/pest --colors=always",
"cs-check": "./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no --stop-on-violation",
"test": "@test:sqlite",
"test:sqlite": "./vendor/bin/pest --configuration phpunit.sqlite.xml",
"test:mysql": "./vendor/bin/pest --configuration phpunit.mysql.xml",
"test:pgsql": "./vendor/bin/pest --configuration phpunit.pgsql.xml",
"test:types": "./vendor/bin/phpstan analyse --ansi --memory-limit=-1",
"cs-fixer": "./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no --stop-on-violation",
"fix": "./vendor/bin/php-cs-fixer fix",
"test": [
"@pest",
"@stan",
"@cs-check"
],
"dbs": [
"@test:sqlite",
"@test:mysql",
"@test:pgsql"
],
"check": [
"@cs-fixer",
"@test"
"@test",
"@test:types"
]
},
"require-dev": {
Expand Down
22 changes: 22 additions & 0 deletions phpunit.mysql.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<server name="DB_DRIVER" value="mysql"/>
<server name="DB_HOST" value="127.0.0.1"/>
<server name="DB_PORT" value="3308"/>
<server name="DB_USERNAME" value="root"/>
<server name="DB_PASSWORD" value="password"/>
<server name="DB_DATABASE" value="powergridtest"/>
</php>
</phpunit>
26 changes: 26 additions & 0 deletions phpunit.pgsql.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<server name="DB_DRIVER" value="pgsql"/>
<server name="DB_HOST" value="127.0.0.1"/>
<server name="DB_PORT" value="5433"/>
<server name="DB_USERNAME" value="postgres"/>
<server name="DB_PASSWORD" value="postgres"/>
<server name="DB_DATABASE" value="powergridtest"/>
</php>
</phpunit>
26 changes: 26 additions & 0 deletions phpunit.sqlite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<server name="DB_DRIVER" value="sqlite"/>
<server name="DB_HOST" value="127.0.0.1"/>
<server name="DB_PORT" value="0"/>
<server name="DB_USERNAME" value=""/>
<server name="DB_PASSWORD" value=""/>
<server name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
22 changes: 22 additions & 0 deletions phpunit.sqlsrv.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<server name="DB_DRIVER" value="sqlsrv"/>
<server name="DB_HOST" value="127.0.0.1"/>
<server name="DB_PORT" value="3308"/>
<server name="DB_USERNAME" value="root"/>
<server name="DB_PASSWORD" value="password"/>
<server name="DB_DATABASE" value="powergridtest"/>
</php>
</phpunit>
12 changes: 9 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
Expand All @@ -18,4 +15,13 @@
<directory suffix=".php">./src</directory>
</include>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<server name="DB_DRIVER" value="sqlite"/>
<server name="DB_HOST" value=""/>
<server name="DB_PORT" value="0"/>
<server name="DB_USERNAME" value=""/>
<server name="DB_PASSWORD" value=""/>
<server name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
7 changes: 7 additions & 0 deletions resources/lang/en/datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
'contains_not' => 'Does not contain',
'starts_with' => 'Starts with',
'ends_with' => 'Ends with',
'is_empty' => 'Is empty',
'is_not_empty' => 'Is not empty',
'is_null' => 'Is null',
'is_not_null' => 'Is not null',
'is_blank' => 'Is blank',
'is_not_blank' => 'Is not blank',

],
'export' => [
'exporting' => 'Please wait!',
Expand Down
6 changes: 6 additions & 0 deletions resources/views/components/filters/input-text.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class="power_grid {{ $theme->selectClass }} {{ data_get($column, 'headerClass')
<option value="is_not">{{ trans('livewire-powergrid::datatable.input_text_options.is_not') }}</option>
<option value="starts_with">{{ trans('livewire-powergrid::datatable.input_text_options.starts_with') }}</option>
<option value="ends_with">{{ trans('livewire-powergrid::datatable.input_text_options.ends_with') }}</option>
<option value="is_null">{{ trans('livewire-powergrid::datatable.input_text_options.is_null') }}</option>
<option value="is_not_null">{{ trans('livewire-powergrid::datatable.input_text_options.is_not_null') }}</option>
<option value="is_blank">{{ trans('livewire-powergrid::datatable.input_text_options.is_blank') }}</option>
<option value="is_not_blank">{{ trans('livewire-powergrid::datatable.input_text_options.is_not_blank') }}</option>
<option value="is_empty">{{ trans('livewire-powergrid::datatable.input_text_options.is_empty') }}</option>
<option value="is_not_empty">{{ trans('livewire-powergrid::datatable.input_text_options.is_not_empty') }}</option>
</select>
<div class="{{ $theme->relativeDivClass }}">
<x-livewire-powergrid::icons.down class="w-4 h-4"/>
Expand Down
47 changes: 43 additions & 4 deletions src/Helpers/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ public function filter(): BaseCollection

foreach ($this->filters as $key => $type) {
foreach ($type as $field => $value) {
if (!filled($value)) {
continue;
}
switch ($key) {
case 'date_picker':
$this->filterDatePicker($field, $value);
Expand Down Expand Up @@ -195,7 +192,7 @@ private function validateInputTextOptions(string $field): bool
{
return isset($this->filters['input_text_options'][$field]) && in_array(
strtolower($this->filters['input_text_options'][$field]),
['is', 'is_not', 'contains', 'contains_not', 'starts_with', 'ends_with']
['is', 'is_not', 'contains', 'contains_not', 'starts_with', 'ends_with', 'is_empty', 'is_not_empty', 'is_null', 'is_not_null', 'is_blank', 'is_not_blank']
);
}

Expand Down Expand Up @@ -250,6 +247,48 @@ public function filterInputText(string $field, string $value): void
});

break;

case 'is_blank':
$this->query = $this->query->whereNotNull($field)->where($field, '=', '');

break;

case 'is_not_blank':
$this->query = $this->query->filter(function ($row) use ($field) {
$row = (object) $row;

return $row->{$field} != '' || is_null($row->{$field});
});

break;

case 'is_null':
$this->query = $this->query->whereNull($field);

break;

case 'is_not_null':
$this->query = $this->query->whereNotNull($field);

break;

case 'is_empty':
$this->query = $this->query->filter(function ($row) use ($field) {
$row = (object) $row;

return $row->{$field} == '' || is_null($row->{$field});
});

break;

case 'is_not_empty':
$this->query = $this->query->filter(function ($row) use ($field) {
$row = (object) $row;

return $row->{$field} !== '' && !is_null($row->{$field});
});

break;
}
}

Expand Down
43 changes: 33 additions & 10 deletions src/Helpers/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ public function filterInputText(Builder $query, string $field, $value): void
}

$textFieldOperator = ($this->validateInputTextOptions($field) ? strtolower($this->filters['input_text_options'][$field]) : 'contains');

switch ($textFieldOperator) {
case 'is':
$query->where($field, '=', $value);
Expand All @@ -205,22 +204,46 @@ public function filterInputText(Builder $query, string $field, $value): void

break;
case 'starts_with':
$query->where($field, 'like', $value . '%');
$query->where($field, SqlSupport::like(), $value . '%');

break;
case 'ends_with':
$query->where($field, 'like', '%' . $value);
$query->where($field, SqlSupport::like(), '%' . $value);

break;
case 'contains':
$query->where($field, 'like', '%' . $value . '%');
$query->where($field, SqlSupport::like(), '%' . $value . '%');

break;
case 'contains_not':
$query->where($field, 'not like', '%' . $value . '%');
$query->where($field, 'NOT ' . SqlSupport::like(), '%' . $value . '%');

break;
}
case 'is_empty':
$query->where($field, '=', '')->orWhereNull($field);

break;
case 'is_not_empty':
$query->where($field, '!=', '')->whereNotNull($field);

break;
case 'is_null':
$query->whereNull($field);

break;
case 'is_not_null':
$query->whereNotNull($field);

break;
case 'is_blank':
$query->where($field, '=', '');

break;
case 'is_not_blank':
$query->where($field, '!=', '')->orWhereNull($field);

break;
}
}

/**
Expand All @@ -233,7 +256,7 @@ private function validateInputTextOptions(string $field): bool
{
return isset($this->filters['input_text_options'][$field]) && in_array(
strtolower($this->filters['input_text_options'][$field]),
['is', 'is_not', 'contains', 'contains_not', 'starts_with', 'ends_with']
['is', 'is_not', 'contains', 'contains_not', 'starts_with', 'ends_with', 'is_empty', 'is_not_empty', 'is_null', 'is_not_null', 'is_blank', 'is_not_blank']
);
}

Expand Down Expand Up @@ -285,7 +308,7 @@ public function filterContains(): Model
$hasColumn = Schema::hasColumn($table, $field);

if ($hasColumn) {
$query->orWhere($table . '.' . $field, 'like', '%' . $this->search . '%');
$query->orWhere($table . '.' . $field, SqlSupport::like(), '%' . $this->search . '%');
}
}
}
Expand Down Expand Up @@ -316,13 +339,13 @@ private function filterRelation(): void
if ($query->getRelation($nestedTable) != '') {
foreach ($column as $nestedColumn) {
$this->query = $this->query->orWhereHas($table . '.' . $nestedTable, function (Builder $query) use ($nestedColumn) {
$query->where($nestedColumn, 'like', '%' . $this->search . '%');
$query->where($nestedColumn, SqlSupport::like(), '%' . $this->search . '%');
});
}
}
} else {
$this->query = $this->query->orWhereHas($table, function (Builder $query) use ($column) {
$query->where($column, 'like', '%' . $this->search . '%');
$query->where($column, SqlSupport::like(), '%' . $this->search . '%');
});
}
}
Expand Down
Loading