Fix searching via api

This commit is contained in:
philipp lang 2024-01-30 00:49:22 +01:00
parent 28c821eeaf
commit e3a0ca7673
5 changed files with 64 additions and 22 deletions

View File

@ -5,8 +5,8 @@ namespace App\Form\Actions;
use App\Form\FilterScope; use App\Form\FilterScope;
use App\Form\Models\Form; use App\Form\Models\Form;
use App\Form\Resources\FormApiResource; use App\Form\Resources\FormApiResource;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection; use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Pagination\LengthAwarePaginator;
use Lorisleiva\Actions\ActionRequest; use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction; use Lorisleiva\Actions\Concerns\AsAction;
@ -15,15 +15,19 @@ class FormApiListAction
use AsAction; use AsAction;
/** /**
* @return Collection<int, Form> * @param array<string, mixed> $filter
* @return LengthAwarePaginator<Form>
*/ */
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 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)
));
} }
} }

View File

@ -1,20 +1,25 @@
<?php <?php
namespace Tests\Feature\Form; namespace Tests\Feature\EndToEnd;
use App\Form\Models\Form; use App\Form\Models\Form;
use App\Form\Models\Formtemplate; use App\Form\Models\Formtemplate;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Tests\EndToEndTestCase;
use Tests\Feature\Form\FormtemplateFieldRequest;
use Tests\Feature\Form\FormtemplateSectionRequest;
use Tests\TestCase; use Tests\TestCase;
class FormApiListActionTest extends TestCase class FormApiListActionTest extends EndToEndTestCase
{ {
use DatabaseTransactions; use DatabaseTransactions;
public function testItDisplaysForms(): void public function testItDisplaysForms(): void
{ {
Carbon::setTestNow(Carbon::parse('2023-03-02'));
Storage::fake('temp'); Storage::fake('temp');
$this->loginNami()->withoutExceptionHandling(); $this->loginNami()->withoutExceptionHandling();
Formtemplate::factory()->name('tname')->sections([FormtemplateSectionRequest::new()->name('sname')])->create(); 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()])]) ->sections([FormtemplateSectionRequest::new()->name('sname')->fields([FormtemplateFieldRequest::new()])])
->create(); ->create();
$this->get('/api/form') sleep(1);
$this->get('/api/form?perPage=15')
->assertOk() ->assertOk()
->assertJsonPath('data.0.name', 'lala 2') ->assertJsonPath('data.0.name', 'lala 2')
->assertJsonPath('data.0.config.sections.0.name', 'sname') ->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.image', $form->getMedia('headerImage')->first()->getFullUrl('square'))
->assertJsonPath('data.0.dates', '05.05.2023 - 07.06.2023') ->assertJsonPath('data.0.dates', '05.05.2023 - 07.06.2023')
->assertJsonPath('data.0.from_human', '05.05.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 public function testItDisplaysDailyForms(): void
{ {
Carbon::setTestNow(Carbon::parse('2023-03-02'));
$this->loginNami()->withoutExceptionHandling(); $this->loginNami()->withoutExceptionHandling();
Form::factory() Form::factory()
->withImage('headerImage', 'lala-2.jpg')
->from('2023-05-05') ->from('2023-05-05')
->to('2023-05-05') ->to('2023-05-05')
->create(); ->create();
sleep(1);
$this->get('/api/form') $this->get('/api/form')
->assertJsonPath('data.0.dates', '05.05.2023'); ->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');
}
} }

View File

@ -2,7 +2,12 @@
namespace Tests; 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\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Artisan;
abstract class EndToEndTestCase extends TestCase abstract class EndToEndTestCase extends TestCase
{ {
@ -14,4 +19,15 @@ abstract class EndToEndTestCase extends TestCase
$this->useMeilisearch(); $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;
}
} }

View File

@ -20,9 +20,17 @@ trait MakesHttpCalls
public function filterUrl(string $routeName, array $filter): string public function filterUrl(string $routeName, array $filter): string
{ {
$params = [ $params = [
'filter' => base64_encode(rawurlencode(json_encode($filter))), 'filter' => $this->filterString($filter),
]; ];
return route($routeName, $params); return route($routeName, $params);
} }
/**
* @param array<string, mixed> $filter
*/
public function filterString(array $filter): string
{
return base64_encode(rawurlencode(json_encode($filter)));
}
} }

View File

@ -16,9 +16,6 @@ use PHPUnit\Framework\Assert;
use Tests\Lib\MakesHttpCalls; use Tests\Lib\MakesHttpCalls;
use Tests\Lib\TestsInertia; use Tests\Lib\TestsInertia;
use Zoomyboy\LaravelNami\Authentication\Auth; 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 abstract class TestCase extends BaseTestCase
{ {
@ -94,15 +91,6 @@ abstract class TestCase extends BaseTestCase
return $this; 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-string> $class * @param <class-string> $class
*/ */