From f62d60f7a4674ecd4d4a3f3d1257ea35e733439f Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Tue, 17 Jan 2023 12:10:06 +0200 Subject: [PATCH] feat: adding filter placeholder (#531) * 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 --- src/Filters/Filter.php | 22 +++++++++++ .../RepositoryFilterControllerTest.php | 39 +++++++++++++++---- tests/IntegrationTest.php | 3 ++ 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/Filters/Filter.php b/src/Filters/Filter.php index 3aa15570..c9b2016a 100644 --- a/src/Filters/Filter.php +++ b/src/Filters/Filter.php @@ -27,6 +27,8 @@ abstract class Filter implements JsonSerializable public string $description = ''; + public string $placeholder = ''; + public ?string $column = null; public ?Closure $canSeeCallback = null; @@ -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) { @@ -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; + } } diff --git a/tests/Controllers/RepositoryFilterControllerTest.php b/tests/Controllers/RepositoryFilterControllerTest.php index 84bef8ff..82b915e1 100644 --- a/tests/Controllers/RepositoryFilterControllerTest.php +++ b/tests/Controllers/RepositoryFilterControllerTest.php @@ -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; @@ -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'); } @@ -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(); + }); + } } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 7800e431..734544ae 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -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; @@ -45,6 +46,8 @@ protected function setUp(): void { parent::setUp(); + $this->withoutMiddleware(ThrottleRequests::class); + $this ->repositories() ->policies()