Skip to content

Commit

Permalink
[FIX] Filtering with Filter::number and methods: thousands() / decima…
Browse files Browse the repository at this point in the history
…l() not working (#1538)

* Enhance tests for Filter::Number dec/thousand separators

* [FIX] set dec/thousand separators in Filter::Number

* remove errors from ignore

* Split and refactor Multiple Filters Test

* improve feedback message on skip
  • Loading branch information
dansysanalyst authored May 12, 2024
1 parent 5897535 commit 615da76
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 171 deletions.
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ parameters:
ignoreErrors:
- '#Cannot call method [a-zA-Z0-9\\_]#'
- '#Variable \$title might not be defined.#'
- '#Variable \$thousands might not be defined.#'
- '#Variable \$decimal might not be defined.#'
- '~^Parameter #1 \$value of function strval expects bool\|float\|int\|resource\|string\|null, mixed given\.$~'
- '~^Parameter #1 \$value of function intval expects array\|bool\|float\|int\|resource\|string\|null, mixed given\.$~'
- '#^Method .*::fromLivewire\(\) has no return type specified\.#'
Expand Down
34 changes: 16 additions & 18 deletions src/Components/Filters/Builders/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Collection;

use function Livewire\store;

class Number extends BuilderBase
{
public function builder(Builder|QueryBuilder $builder, string $field, int|array|string|null $values): void
{
$thousands = store($this->component)->get('filters.number.' . $field . '.thousands');
$decimal = store($this->component)->get('filters.number.' . $field . '.decimal');
$thousands = data_get($this->filterBase, 'thousands');
$decimal = data_get($this->filterBase, 'decimal');

if (data_get($this->filterBase, 'builder')) {
/** @var \Closure $closure */
Expand All @@ -28,11 +26,11 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array|
if (isset($values['start']) && !isset($values['end'])) {
$start = $values['start'];

if (isset($thousands)) {
if (is_string($thousands)) {
$start = str_replace($thousands, '', $start);
}

if (isset($decimal)) {
if (is_string($decimal)) {
$start = str_replace($decimal, '.', $start);
}

Expand All @@ -42,11 +40,11 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array|
if (!isset($values['start']) && isset($values['end'])) {
$end = $values['end'];

if (isset($decimal)) {
if (is_string($thousands)) {
$end = str_replace($thousands, '', $values['end']);
}

if (isset($decimal)) {
if (is_string($decimal)) {
$end = (float) str_replace($decimal, '.', $end);
}

Expand All @@ -57,12 +55,12 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array|
$start = $values['start'];
$end = $values['end'];

if (isset($thousands)) {
if (is_string($thousands)) {
$start = str_replace($thousands, '', $values['start']);
$end = str_replace($thousands, '', $values['end']);
}

if (isset($decimal)) {
if (is_string($decimal)) {
$start = str_replace($decimal, '.', $start);
$end = str_replace($decimal, '.', $end);
}
Expand All @@ -73,8 +71,8 @@ public function builder(Builder|QueryBuilder $builder, string $field, int|array|

public function collection(Collection $collection, string $field, int|array|string|null $values): Collection
{
$thousands = store($this->component)->get('filters.number.' . $field . '.thousands');
$decimal = store($this->component)->get('filters.number.' . $field . '.decimal');
$thousands = data_get($this->filterBase, 'thousands');
$decimal = data_get($this->filterBase, 'decimal');

if (data_get($this->filterBase, 'collection')) {
/** @var \Closure $closure */
Expand All @@ -87,11 +85,11 @@ public function collection(Collection $collection, string $field, int|array|stri
if (isset($values['start']) && !isset($values['end'])) {
$start = $values['start'];

if (isset($thousands)) {
if (is_string($thousands)) {
$start = str_replace($thousands, '', $values['start']);
}

if (isset($decimal)) {
if (is_string($decimal)) {
$start = (float) str_replace($decimal, '.', $start);
}

Expand All @@ -101,11 +99,11 @@ public function collection(Collection $collection, string $field, int|array|stri
if (!isset($values['start']) && isset($values['end'])) {
$end = $values['end'];

if (isset($thousands)) {
if (is_string($thousands)) {
$end = str_replace($thousands, '', $values['end']);
}

if (isset($decimal)) {
if (is_string($decimal)) {
$end = (float) str_replace($decimal, '.', $end);
}

Expand All @@ -116,12 +114,12 @@ public function collection(Collection $collection, string $field, int|array|stri
$start = $values['start'];
$end = $values['end'];

if (isset($thousands)) {
if (is_string($thousands)) {
$start = str_replace($thousands, '', $values['start']);
$end = str_replace($thousands, '', $values['end']);
}

if (isset($decimal)) {
if (is_string($decimal)) {
$start = str_replace($decimal, '.', $start);
$end = str_replace($decimal, '.', $end);
}
Expand Down
8 changes: 0 additions & 8 deletions src/Concerns/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use Illuminate\Support\{Arr, Carbon};
use Livewire\Attributes\On;

use function Livewire\store;

trait Filter
{
public array $filters = [];
Expand Down Expand Up @@ -169,9 +167,6 @@ public function filterNumberStart(string $field, array $params, string $value):

$this->resetPage();

store($this)->set('filters.number.' . $field . '.thousands', $thousands);
store($this)->set('filters.number.' . $field . '.decimal', $decimal);

$this->addEnabledFilters($field, $title);

if (blank($value)) {
Expand All @@ -189,9 +184,6 @@ public function filterNumberEnd(string $field, array $params, string $value): vo

$this->resetPage();

store($this)->set('filters.number.' . $field . '.thousands', $thousands);
store($this)->set('filters.number.' . $field . '.decimal', $decimal);

$this->addEnabledFilters($field, $title);

if (blank($value)) {
Expand Down
45 changes: 45 additions & 0 deletions tests/Concerns/Components/ComponentsForFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use PowerComponents\LivewirePowerGrid\Facades\Filter;

use PowerComponents\LivewirePowerGrid\Tests\{Concerns\Components\DishesQueryBuilderTable,
Concerns\Components\DishesTable,
Concerns\Components\DishesTableWithJoin};

$component = new class () extends DishesTable {
public function filters(): array
{
return [
Filter::number('price_BRL')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::number('price') ->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::inputText('name')->placeholder('dish_name_xyz_placeholder')->operators(),
Filter::number('price')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::boolean('in_stock'),
];
}
};

$componentQueryBuilder = new class () extends DishesQueryBuilderTable {
public function filters(): array
{
return [
Filter::number('price_BRL')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::number('price') ->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::inputText('name')->placeholder('dish_name_xyz_placeholder')->operators(),
Filter::number('price')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::boolean('in_stock'),
];
}
};

$componentJoin = new class () extends DishesTableWithJoin {
public function filters(): array
{
return [
Filter::number('price_BRL') ->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::inputText('dish_name')->placeholder('dish_name_xyz_placeholder')->operators(),
Filter::number('price')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::boolean('in_stock'),
];
}
};
8 changes: 4 additions & 4 deletions tests/Concerns/TestDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public static function generate(): array
'name' => 'Barco-Sushi da Sueli',
'category_id' => 1,
'chef_id' => 1,
'price' => 70.00,
'price' => 5000.00,
'in_stock' => false,
'produced_at' => '2021-07-07 19:59:59',
'additional' => json_encode([
Expand All @@ -224,7 +224,7 @@ public static function generate(): array
'name' => 'Barco-Sushi Simples',
'category_id' => 1,
'chef_id' => 1,
'price' => 80.40,
'price' => 1500.40,
'in_stock' => false,
'produced_at' => '2021-08-08 00:00:00',
'additional' => json_encode([
Expand All @@ -239,15 +239,15 @@ public static function generate(): array
'name' => 'Polpetone Filé Mignon',
'category_id' => 1,
'chef_id' => 1,
'price' => 90.10,
'price' => 5000.00,
'in_stock' => false,
'produced_at' => '2021-09-09 00:00:00',
],
[
'name' => 'борщ',
'category_id' => 7,
'chef_id' => 1,
'price' => 100.90,
'price' => 5000.00,
'in_stock' => false,
'produced_at' => '2021-10-10 00:00:00',
],
Expand Down
10 changes: 10 additions & 0 deletions tests/Datasets/FilterComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

require(__DIR__ . '/../Concerns/Components/ComponentsForFilterTest.php');

dataset('filterComponent', [
'tailwind -> id' => [$component::class, (object) ['theme' => 'tailwind', 'field' => 'name']],
'bootstrap -> id' => [$component::class, (object) ['theme' => 'bootstrap', 'field' => 'name']],
'tailwind -> dishes.id' => [$componentJoin::class, (object) ['theme' => 'tailwind', 'field' => 'dishes.name']],
'bootstrap -> dishes.id' => [$componentJoin::class, (object) ['theme' => 'bootstrap', 'field' => 'dishes.name']],
]);
65 changes: 65 additions & 0 deletions tests/Feature/Filters/FilterInputTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use Illuminate\Support\Str;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

require(__DIR__ . '/../../Concerns/Components/ComponentsForFilterTest.php');

it('properly filters by inputText', function (string $component, object $params) {
$component = livewire($component)
->call($params->theme);

/** @var PowerGridComponent $component */
expect($component->filters)
->toMatchArray([]);

$component->set('filters', filterInputText('ba', 'contains', $params->field));

if (str_contains($params->field, '.')) {
$data = Str::of($params->field)->explode('.');
$table = $data->get(0);
$field = $data->get(1);

expect($component->filters)
->toMatchArray([
'input_text' => [
$table => [
$field => 'ba',
],
],
'input_text_options' => [
$table => [
$field => 'contains',
],
],
]);
} else {
expect($component->filters)
->toMatchArray([
'input_text' => [
$params->field => 'ba',
],
'input_text_options' => [
$params->field => 'contains',
],
]);
}

$component->assertSee('Barco-Sushi da Sueli')
->assertSeeHtml('dish_name_xyz_placeholder');

$filters = array_merge($component->filters, filterNumber('price', min: '1\'500.20', max: '3\'000.00'));

$component->set('filters', $filters)
->assertSeeHtml('placeholder="min_xyz_placeholder"')
->assertSeeHtml('placeholder="max_xyz_placeholder"')
->assertSee('Barco-Sushi Simples')
->assertDontSee('Barco-Sushi da Sueli')
->assertDontSee('Polpetone Filé Mignon')
->assertDontSee('борщ');

expect($component->filters)->toBe($filters);
})->group('filters')
->with('filterComponent');
Loading

0 comments on commit 615da76

Please sign in to comment.