Skip to content

Commit

Permalink
feat: adding filter placeholder (#531)
Browse files Browse the repository at this point in the history
* feat: adding filter placeholder

* Fix styling

* fix: tests

* fix: wip

* Fix styling

* fix: psalm

* fix: adding test

* Fix styling

* fix: psalm

Co-authored-by: binaryk <[email protected]>
  • Loading branch information
binaryk and binaryk authored Jan 17, 2023
1 parent d76fb58 commit f62d60f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
22 changes: 22 additions & 0 deletions src/Filters/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ abstract class Filter implements JsonSerializable

public string $description = '';

public string $placeholder = '';

public ?string $column = null;

public ?Closure $canSeeCallback = null;
Expand Down Expand Up @@ -227,6 +229,7 @@ public function jsonSerialize()
'advanced' => $this->advanced,
'title' => $this->title(),
'description' => $this->description(),
'placeholder' => $this->placeholder(),
'column' => $this->column(),
'key' => static::uriKey(),
], function (array $initial) {
Expand Down Expand Up @@ -282,4 +285,23 @@ protected function description(): string
{
return $this->description;
}

protected function placeholder(): string
{
return $this->placeholder;
}

public function setPlaceholder(string $placeholder): self
{
$this->placeholder = $placeholder;

return $this;
}

public function setDescription(string $description): self
{
$this->description = $description;

return $this;
}
}
39 changes: 31 additions & 8 deletions tests/Controllers/RepositoryFilterControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Binaryk\LaravelRestify\Tests\Controllers;

use Binaryk\LaravelRestify\Filters\MatchFilter;
use Binaryk\LaravelRestify\Filters\SortableFilter;
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
use Binaryk\LaravelRestify\Tests\IntegrationTest;
Expand Down Expand Up @@ -34,14 +35,14 @@ public function test_available_filters_contains_matches_sortables_searches(): vo
// 2 searchable
->assertJson(
fn (AssertableJson $json) => $json
->where('data.0.rules.is_active', 'bool')
->where('data.4.type', 'text')
->where('data.4.column', 'title')
->where('data.5.type', 'value')
->where('data.5.column', 'title')
->where('data.6.type', 'value')
->where('data.6.column', 'id')
->etc()
->where('data.0.rules.is_active', 'bool')
->where('data.4.type', 'text')
->where('data.4.column', 'title')
->where('data.5.type', 'value')
->where('data.5.column', 'title')
->where('data.6.type', 'value')
->where('data.6.column', 'id')
->etc()
)
->assertJsonCount(8, 'data');
}
Expand Down Expand Up @@ -73,4 +74,26 @@ public function test_available_filters_returns_only_matches_sortables_searches()
$this->getJson(PostRepository::route('filters', query: ['only' => 'searchables']))
->assertJsonCount(2, 'data');
}

public function test_filters_will_render_placeholder(): void
{
PostRepository::$match = [
'title' => MatchFilter::make()
->setDescription('Sort by title')
->setPlaceholder('-title')
->setType('string'),
];

$this->getJson(PostRepository::route('filters', query: [
'only' => 'matches',
]))
->assertJson(function (AssertableJson $json) {
$json
->where('data.0.placeholder', '-title')
->where('data.0.description', 'Sort by title')
->where('data.0.type', 'string')
->where('data.0.column', 'title')
->etc();
});
}
}
3 changes: 3 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Gate;
use JetBrains\PhpStorm\Pure;
use Mockery;
Expand All @@ -45,6 +46,8 @@ protected function setUp(): void
{
parent::setUp();

$this->withoutMiddleware(ThrottleRequests::class);

$this
->repositories()
->policies()
Expand Down

0 comments on commit f62d60f

Please sign in to comment.