Skip to content

Commit f57f76a

Browse files
committed
✨ scout with and only trashed
1 parent acf8050 commit f57f76a

File tree

4 files changed

+144
-9
lines changed

4 files changed

+144
-9
lines changed

src/Query/ScoutBuilder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function search(array $parameters = [])
4141
$this->applyFilters($parameters['filters']);
4242
});
4343

44+
$this->when(isset($parameters['text']['trashed']), function () use ($parameters) {
45+
$this->applyTrashed($parameters['text']['trashed']);
46+
});
47+
4448
$this->when(isset($parameters['sorts']), function () use ($parameters) {
4549
$this->applySorts($parameters['sorts']);
4650
});
@@ -125,6 +129,15 @@ public function applySorts($sorts)
125129
}
126130
}
127131

132+
public function applyTrashed(string $trashed): void
133+
{
134+
if ($trashed === 'only') {
135+
$this->queryBuilder->onlyTrashed();
136+
} elseif ($trashed === 'with') {
137+
$this->queryBuilder->withTrashed();
138+
}
139+
}
140+
128141
/**
129142
* Apply an instruction to the query builder.
130143
*

src/Rules/Search/SearchText.php

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

33
namespace Lomkit\Rest\Rules\Search;
44

5+
use Illuminate\Validation\Rule;
56
use Lomkit\Rest\Rules\RestRule;
67

78
class SearchText extends RestRule
@@ -17,6 +18,9 @@ public function buildValidationRules(string $attribute, mixed $value): array
1718
return [
1819
$attribute => ['sometimes', 'array'],
1920
$attribute.'.value' => ['nullable', 'string'],
21+
$attribute.'.trashed' => [
22+
Rule::in('with', 'only'),
23+
],
2024
];
2125
}
2226
}

tests/Feature/Controllers/SearchScoutOperationsTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Illuminate\Support\Facades\Config;
66
use Illuminate\Support\Facades\Gate;
7+
use Lomkit\Rest\Query\ScoutBuilder;
78
use Lomkit\Rest\Tests\Feature\TestCase;
89
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
910
use Lomkit\Rest\Tests\Support\Models\Model;
1011
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
1112
use Lomkit\Rest\Tests\Support\Rest\Resources\ModelResource;
13+
use Mockery\MockInterface;
1214

1315
class SearchScoutOperationsTest extends TestCase
1416
{
@@ -379,4 +381,78 @@ public function test_getting_a_list_of_resources_with_null_scout(): void
379381
new ModelResource()
380382
);
381383
}
384+
385+
public function test_getting_a_list_of_resources_with_unknown_trashed(): void
386+
{
387+
ModelFactory::new()->count(2)->create();
388+
389+
Gate::policy(Model::class, GreenPolicy::class);
390+
391+
$response = $this->post(
392+
'/api/searchable-models/search',
393+
[
394+
'search' => [
395+
'text' => [
396+
'trashed' => 'unknown',
397+
],
398+
],
399+
],
400+
['Accept' => 'application/json']
401+
);
402+
403+
$response->assertStatus(422);
404+
$response->assertExactJsonStructure(['message', 'errors' => ['search.text.trashed']]);
405+
}
406+
407+
public function test_getting_a_list_of_resources_with_trashed(): void
408+
{
409+
ModelFactory::new()->count(2)->create();
410+
411+
Gate::policy(Model::class, GreenPolicy::class);
412+
413+
$response = $this->post(
414+
'/api/searchable-models/search',
415+
[
416+
'search' => [
417+
'text' => [
418+
'trashed' => 'with',
419+
],
420+
],
421+
],
422+
['Accept' => 'application/json']
423+
);
424+
425+
$this->assertResourcePaginated(
426+
$response,
427+
[],
428+
new ModelResource()
429+
);
430+
}
431+
432+
public function test_getting_a_list_of_resources_only_trashed(): void
433+
{
434+
ModelFactory::new()->count(2)->create();
435+
436+
Gate::policy(Model::class, GreenPolicy::class);
437+
438+
$response = $this->post(
439+
'/api/searchable-models/search',
440+
[
441+
'search' => [
442+
'text' => [
443+
'trashed' => 'only',
444+
],
445+
],
446+
],
447+
['Accept' => 'application/json']
448+
);
449+
450+
// @TODO: test only correctly applyed
451+
452+
$this->assertResourcePaginated(
453+
$response,
454+
[],
455+
new ModelResource()
456+
);
457+
}
382458
}

tests/Unit/LaravelScoutTest.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public function test_building_scout_with_filters()
4747

4848
$scoutQueryBuilderMock
4949
->search([
50-
'text' => [
51-
['value' => 'test'],
52-
],
50+
'text' => ['value' => 'test'],
5351
'filters' => [
5452
['field' => 'test', 'value' => 1],
5553
],
@@ -69,9 +67,7 @@ public function test_building_scout_with_sorts()
6967

7068
$scoutQueryBuilderMock
7169
->search([
72-
'text' => [
73-
['value' => 'test'],
74-
],
70+
'text' => ['value' => 'test'],
7571
'sorts' => [
7672
['field' => 'id'],
7773
],
@@ -91,14 +87,60 @@ public function test_building_scout_with_instructions()
9187

9288
$scoutQueryBuilderMock
9389
->search([
94-
'text' => [
95-
['value' => 'test'],
96-
],
90+
'text' => ['value' => 'test'],
9791
'instructions' => [
9892
['name' => 'my_instruction'],
9993
],
10094
]);
10195

10296
($scoutQueryBuilderMock->toBase()->queryCallback)(Model::query());
10397
}
98+
99+
public function test_building_scout_with_trashed()
100+
{
101+
Auth::setUser(Mockery::mock(User::class));
102+
Gate::policy(Model::class, GreenPolicy::class);
103+
104+
$scoutQueryBuilderMock = Mockery::mock(ScoutBuilder::class, [new SearchableModelResource()])->makePartial();
105+
106+
$scoutQueryBuilderMock->shouldReceive('applyFilters')->never();
107+
$scoutQueryBuilderMock->shouldReceive('applySorts')->never();
108+
$scoutQueryBuilderMock->shouldReceive('applyInstructions')->never();
109+
$scoutQueryBuilderMock->shouldReceive('applyTrashed')->with('with')->once();
110+
111+
$scoutQueryBuilderMock
112+
->search([
113+
'text' =>
114+
[
115+
'value' => 'test',
116+
'trashed' => 'with'
117+
],
118+
]);
119+
120+
($scoutQueryBuilderMock->toBase()->queryCallback)(Model::query());
121+
}
122+
123+
public function test_building_scout_only_trashed()
124+
{
125+
Auth::setUser(Mockery::mock(User::class));
126+
Gate::policy(Model::class, GreenPolicy::class);
127+
128+
$scoutQueryBuilderMock = Mockery::mock(ScoutBuilder::class, [new SearchableModelResource()])->makePartial();
129+
130+
$scoutQueryBuilderMock->shouldReceive('applyFilters')->never();
131+
$scoutQueryBuilderMock->shouldReceive('applySorts')->never();
132+
$scoutQueryBuilderMock->shouldReceive('applyInstructions')->never();
133+
$scoutQueryBuilderMock->shouldReceive('applyTrashed')->with('only')->once();
134+
135+
$scoutQueryBuilderMock
136+
->search([
137+
'text' =>
138+
[
139+
'value' => 'test',
140+
'trashed' => 'only'
141+
],
142+
]);
143+
144+
($scoutQueryBuilderMock->toBase()->queryCallback)(Model::query());
145+
}
104146
}

0 commit comments

Comments
 (0)