Skip to content

Commit 7a6ea00

Browse files
committed
fix(Matches): Correctly match Booleans: GET converts them to 0 and 1
1 parent 37f8098 commit 7a6ea00

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/Filters/MatchFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function filter(RestifyRequest $request, Builder|Relation $query, $value)
4646
break;
4747
case RestifySearchable::MATCH_BOOL:
4848
case 'boolean':
49-
if ($value === 'false') {
49+
if ($value === 'false' || $value === '0') {
5050
$query->where(function ($query) use ($field) {
5151
if ($this->negation) {
5252
return $query->where($field, true);

tests/Feature/Filters/MatchFilterTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
66
use Binaryk\LaravelRestify\Filters\MatchFilter;
7+
use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post;
78
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
89
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
910
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
@@ -16,8 +17,7 @@ class MatchFilterTest extends IntegrationTestCase
1617
{
1718
public function test_matchable_filter_has_key(): void
1819
{
19-
$filter = new class extends MatchFilter
20-
{
20+
$filter = new class () extends MatchFilter {
2121
public ?string $column = 'approved_at';
2222
};
2323

@@ -175,6 +175,26 @@ public function test_can_match_date(): void
175175
$this->getJson(UserRepository::route(query: ['created_at' => '2020-12-01']))->assertJsonCount(3, 'data');
176176
}
177177

178+
public function test_can_match_boolean(): void
179+
{
180+
PostRepository::$match = [
181+
'is_active' => 'boolean',
182+
];
183+
184+
Post::factory(2)->sequence(
185+
['is_active' => true, 'title' => 'Active post 1'],
186+
['is_active' => false, 'title' => 'Inactive post 2'],
187+
)->create();
188+
189+
$this->getJson(PostRepository::route())->assertJsonCount(2, 'data');
190+
191+
$this->getJson(PostRepository::route(query: ['is_active' => true]))
192+
->assertJsonCount(1, 'data');
193+
194+
$this->getJson(PostRepository::route(query: ['is_active' => false]))
195+
->assertJsonCount(1, 'data');
196+
}
197+
178198
public function test_can_match_datetime_interval(): void
179199
{
180200
User::factory()->state([

tests/Fixtures/Post/PostRepository.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class PostRepository extends Repository
2727

2828
public static array $match = [
2929
'title' => RestifySearchable::MATCH_TEXT,
30+
'is_active' => RestifySearchable::MATCH_BOOL,
3031
];
3132

3233
public static array $middleware = [];
@@ -50,6 +51,8 @@ public function fields(RestifyRequest $request): array
5051
field('description')->storingRules('required')->messages([
5152
'required' => 'Description field is required',
5253
]),
54+
55+
field('is_active'),
5356
];
5457
}
5558

@@ -121,7 +124,7 @@ public function actions(RestifyRequest $request): array
121124
->canSee(function (ActionRequest $request) {
122125
return $_SERVER['actions.posts.invalidate'] ?? true;
123126
}),
124-
new PublishInvokablePostAction,
127+
new PublishInvokablePostAction(),
125128
];
126129
}
127130

@@ -131,8 +134,8 @@ public function getters(RestifyRequest $request): array
131134
PostsIndexGetter::make(),
132135
PostsShowGetter::make()->onlyOnShow(),
133136
UnauthenticatedActionGetter::make()->withoutMiddleware(AuthorizeRestify::class),
134-
new PostsShowInvokableGetter,
135-
new PostsIndexInvokableGetter,
137+
new PostsShowInvokableGetter(),
138+
new PostsIndexInvokableGetter(),
136139
];
137140
}
138141
}

0 commit comments

Comments
 (0)