Skip to content

Commit

Permalink
Fixed test suite and added a search case-insensitive test
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur LORENT committed Feb 6, 2023
1 parent 55b8c7f commit 9cd555c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Okipa\LaravelTable\Abstracts\AbstractFilter;
use Okipa\LaravelTable\Abstracts\AbstractHeadAction;
Expand Down Expand Up @@ -53,11 +52,6 @@ public function __construct()
$this->results = collect();
}

public static function make(): self
{
return new self();
}

public function model(string $modelClass): self
{
$this->model = app($modelClass);
Expand Down Expand Up @@ -89,6 +83,11 @@ public function reorderable(string $attribute, string $title = null, string $sor
return $this;
}

public static function make(): self
{
return new self();
}

/** @throws \Okipa\LaravelTable\Exceptions\NoColumnsDeclared */
public function prependReorderColumn(): void
{
Expand Down Expand Up @@ -140,6 +139,13 @@ public function getReorderConfig(string|null $sortDir): array
];
}

public function query(Closure $queryClosure): self
{
$this->queryClosure = $queryClosure;

return $this;
}

/** @throws \Okipa\LaravelTable\Exceptions\NoColumnsDeclared */
public function prepareQuery(
array $filterClosures,
Expand All @@ -150,19 +156,19 @@ public function prepareQuery(
$query = $this->model->query();
// Query
if ($this->queryClosure) {
$query->where(fn ($subQueryQuery) => ($this->queryClosure)($query));
$query->where(fn (Builder $subQueryQuery) => ($this->queryClosure)($query));
}
// Filters
if ($filterClosures) {
$query->where(function ($subFiltersQuery) use ($filterClosures) {
$query->where(function (Builder $subFiltersQuery) use ($filterClosures) {
foreach ($filterClosures as $filterClosure) {
$filterClosure($subFiltersQuery);
}
});
}
// Search
if ($searchBy) {
$query->where(function ($subSearchQuery) use ($searchBy) {
$query->where(function (Builder $subSearchQuery) use ($searchBy) {
$this->getSearchableColumns()
->each(function (Column $searchableColumn) use ($subSearchQuery, $searchBy) {
$searchableClosure = $searchableColumn->getSearchableClosure();
Expand All @@ -171,10 +177,10 @@ public function prepareQuery(
$orWhereQuery,
$searchBy
))
: $subSearchQuery->orWhere(
DB::raw('LOWER(' . $searchableColumn->getAttribute() . ')'),
$this->getCaseInsensitiveSearchingLikeOperator(),
'%' . mb_strtolower($searchBy) . '%'
: $subSearchQuery->orWhereRaw(
'LOWER(' . $searchableColumn->getAttribute() . ') '
. $this->getCaseInsensitiveSearchingLikeOperator() . ' ?',
['%' . Str::of($searchBy)->trim()->lower() . '%']
);
});
});
Expand Down Expand Up @@ -308,13 +314,6 @@ public function getColumn(string $attribute): Column
return $this->getColumns()->filter(fn (Column $column) => $column->getAttribute() === $attribute)->first();
}

public function query(Closure $queryClosure): self
{
$this->queryClosure = $queryClosure;

return $this;
}

public function paginateRows(Builder $query, int $numberOfRowsPerPage): void
{
$this->rows = $query->paginate($numberOfRowsPerPage);
Expand Down
52 changes: 52 additions & 0 deletions tests/Unit/Bootstrap5/ColumnSearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Livewire\Livewire;
use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration;
use Okipa\LaravelTable\Column;
Expand Down Expand Up @@ -265,6 +266,57 @@ protected function columns(): array
]);
}

/** @test */
public function it_can_search_case_insensitively(): void
{
$users = User::factory()->count(2)->create();
$config = new class extends AbstractTableConfiguration
{
protected function table(): Table
{
return Table::make()->model(User::class);
}

protected function columns(): array
{
return [
Column::make('id'),
Column::make('name')->searchable(),
Column::make('email')->searchable(),
];
}
};
Livewire::test(\Okipa\LaravelTable\Livewire\Table::class, ['config' => $config::class])
->call('init')
->assertSet('searchBy', '')
->assertSeeHtmlInOrder([
'<tbody>',
$users->first()->name,
$users->last()->name,
'</tbody>',
])
->set('searchBy', Str::lower($users->first()->name))
->call('$refresh')
->assertSeeHtmlInOrder([
'<tbody>',
$users->first()->name,
'</tbody>',
])
->assertDontSeeHtml([
$users->last()->name,
])
->set('searchBy', Str::upper($users->last()->email))
->call('$refresh')
->assertSeeHtmlInOrder([
'<tbody>',
$users->last()->name,
'</tbody>',
])
->assertDontSeeHtml([
$users->first()->name,
]);
}

/** @test */
public function it_can_reset_search(): void
{
Expand Down

0 comments on commit 9cd555c

Please sign in to comment.