From e3a0ca7673de91774bbae0f300ea66f7bdbd3c5e Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 30 Jan 2024 00:49:22 +0100 Subject: [PATCH] Fix searching via api --- app/Form/Actions/FormApiListAction.php | 14 +++++--- .../Form/FormApiListActionTest.php | 34 ++++++++++++++++--- tests/EndToEndTestCase.php | 16 +++++++++ tests/Lib/MakesHttpCalls.php | 10 +++++- tests/TestCase.php | 12 ------- 5 files changed, 64 insertions(+), 22 deletions(-) rename tests/{Feature => EndToEnd}/Form/FormApiListActionTest.php (61%) diff --git a/app/Form/Actions/FormApiListAction.php b/app/Form/Actions/FormApiListAction.php index 9cfb13ce..0476f05d 100644 --- a/app/Form/Actions/FormApiListAction.php +++ b/app/Form/Actions/FormApiListAction.php @@ -5,8 +5,8 @@ namespace App\Form\Actions; use App\Form\FilterScope; use App\Form\Models\Form; use App\Form\Resources\FormApiResource; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; +use Illuminate\Pagination\LengthAwarePaginator; use Lorisleiva\Actions\ActionRequest; use Lorisleiva\Actions\Concerns\AsAction; @@ -15,15 +15,19 @@ class FormApiListAction use AsAction; /** - * @return Collection + * @param array $filter + * @return LengthAwarePaginator
*/ - public function handle(FilterScope $filter): Collection + public function handle(string $filter, int $perPage): LengthAwarePaginator { - return Form::withFilter($filter)->get(); + return FilterScope::fromRequest($filter)->getQuery()->paginate($perPage); } public function asController(ActionRequest $request): AnonymousResourceCollection { - return FormApiResource::collection($this->handle(FilterScope::fromRequest($request->input('filter', '')))); + return FormApiResource::collection($this->handle( + $request->input('filter', ''), + $request->input('perPage', 10) + )); } } diff --git a/tests/Feature/Form/FormApiListActionTest.php b/tests/EndToEnd/Form/FormApiListActionTest.php similarity index 61% rename from tests/Feature/Form/FormApiListActionTest.php rename to tests/EndToEnd/Form/FormApiListActionTest.php index 138eb89f..46f16ac2 100644 --- a/tests/Feature/Form/FormApiListActionTest.php +++ b/tests/EndToEnd/Form/FormApiListActionTest.php @@ -1,20 +1,25 @@ loginNami()->withoutExceptionHandling(); Formtemplate::factory()->name('tname')->sections([FormtemplateSectionRequest::new()->name('sname')])->create(); @@ -28,7 +33,8 @@ class FormApiListActionTest extends TestCase ->sections([FormtemplateSectionRequest::new()->name('sname')->fields([FormtemplateFieldRequest::new()])]) ->create(); - $this->get('/api/form') + sleep(1); + $this->get('/api/form?perPage=15') ->assertOk() ->assertJsonPath('data.0.name', 'lala 2') ->assertJsonPath('data.0.config.sections.0.name', 'sname') @@ -39,18 +45,38 @@ class FormApiListActionTest extends TestCase ->assertJsonPath('data.0.image', $form->getMedia('headerImage')->first()->getFullUrl('square')) ->assertJsonPath('data.0.dates', '05.05.2023 - 07.06.2023') ->assertJsonPath('data.0.from_human', '05.05.2023') - ->assertJsonPath('data.0.to_human', '07.06.2023'); + ->assertJsonPath('data.0.to_human', '07.06.2023') + ->assertJsonPath('meta.per_page', 15) + ->assertJsonPath('meta.total', 1); } public function testItDisplaysDailyForms(): void { + Carbon::setTestNow(Carbon::parse('2023-03-02')); $this->loginNami()->withoutExceptionHandling(); Form::factory() + ->withImage('headerImage', 'lala-2.jpg') ->from('2023-05-05') ->to('2023-05-05') ->create(); + sleep(1); $this->get('/api/form') ->assertJsonPath('data.0.dates', '05.05.2023'); } + + public function testItDisplaysPastEvents(): void + { + Carbon::setTestNow(Carbon::parse('2023-05-10')); + $this->loginNami()->withoutExceptionHandling(); + Form::factory() + ->withImage('headerImage', 'lala-2.jpg') + ->from('2023-05-05') + ->to('2023-05-05') + ->create(); + + sleep(1); + $this->get('/api/form?filter=' . $this->filterString(['past' => true])) + ->assertJsonCount(1, 'data'); + } } diff --git a/tests/EndToEndTestCase.php b/tests/EndToEndTestCase.php index b2729185..cba5c944 100644 --- a/tests/EndToEndTestCase.php +++ b/tests/EndToEndTestCase.php @@ -2,7 +2,12 @@ namespace Tests; +use App\Form\Models\Form; +use App\Member\Member; +use Laravel\Scout\Console\FlushCommand; +use Laravel\Scout\Console\SyncIndexSettingsCommand; use Illuminate\Foundation\Testing\DatabaseMigrations; +use Illuminate\Support\Facades\Artisan; abstract class EndToEndTestCase extends TestCase { @@ -14,4 +19,15 @@ abstract class EndToEndTestCase extends TestCase $this->useMeilisearch(); } + + + public function useMeilisearch(): self + { + config()->set('scout.driver', 'meilisearch'); + Artisan::call(FlushCommand::class, ['model' => Member::class]); + Artisan::call(FlushCommand::class, ['model' => Form::class]); + Artisan::call(SyncIndexSettingsCommand::class); + + return $this; + } } diff --git a/tests/Lib/MakesHttpCalls.php b/tests/Lib/MakesHttpCalls.php index 4376928c..ede4c73f 100644 --- a/tests/Lib/MakesHttpCalls.php +++ b/tests/Lib/MakesHttpCalls.php @@ -20,9 +20,17 @@ trait MakesHttpCalls public function filterUrl(string $routeName, array $filter): string { $params = [ - 'filter' => base64_encode(rawurlencode(json_encode($filter))), + 'filter' => $this->filterString($filter), ]; return route($routeName, $params); } + + /** + * @param array $filter + */ + public function filterString(array $filter): string + { + return base64_encode(rawurlencode(json_encode($filter))); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 8a315791..7b2923b5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -16,9 +16,6 @@ use PHPUnit\Framework\Assert; use Tests\Lib\MakesHttpCalls; use Tests\Lib\TestsInertia; use Zoomyboy\LaravelNami\Authentication\Auth; -use Illuminate\Support\Facades\Artisan; -use Laravel\Scout\Console\FlushCommand; -use Laravel\Scout\Console\SyncIndexSettingsCommand; abstract class TestCase extends BaseTestCase { @@ -94,15 +91,6 @@ abstract class TestCase extends BaseTestCase return $this; } - public function useMeilisearch(): self - { - config()->set('scout.driver', 'meilisearch'); - Artisan::call(FlushCommand::class, ['model' => Member::class]); - Artisan::call(SyncIndexSettingsCommand::class); - - return $this; - } - /** * @param $class */