From b34328c6996946b07429f455348f3f53b61b33e8 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 8 Jul 2025 23:22:24 +0200 Subject: [PATCH] Fix tests --- app/Form/Requests/FormCompileRequest.php | 3 ++ .../Feature/Form/GenerateContributionTest.php | 46 ++++++------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/app/Form/Requests/FormCompileRequest.php b/app/Form/Requests/FormCompileRequest.php index e5b20d7b..41fc1f86 100644 --- a/app/Form/Requests/FormCompileRequest.php +++ b/app/Form/Requests/FormCompileRequest.php @@ -64,6 +64,9 @@ class FormCompileRequest extends Data implements HasContributionData { $member = []; foreach ($fields as [$type, $name]) { $f = $this->form->getFields()->findBySpecialType($type); + if (!$f) { + continue; + } $member[$name] = $participant->getFields()->find($f)->value; } diff --git a/tests/Feature/Form/GenerateContributionTest.php b/tests/Feature/Form/GenerateContributionTest.php index 48030cfe..12099d41 100644 --- a/tests/Feature/Form/GenerateContributionTest.php +++ b/tests/Feature/Form/GenerateContributionTest.php @@ -33,11 +33,7 @@ it('doesnt create document when no special fields given', function (array $field ->has(Participant::factory()) ->create(); - $this->json('GET', route('form.contribution', [ - 'type' => $type, - 'form' => $form, - 'validate' => '1', - ]))->assertJsonValidationErrors([$field => $message]); + generate($type, $form, true)->assertJsonValidationErrors([$field => $message]); }) ->with([ [fn() => [], 'FIRSTNAME', 'Kein Feld für Vorname vorhanden.'], @@ -61,11 +57,7 @@ it('validates special types of each document', function (string $type, array $fi ->has(Participant::factory()) ->create(); - $this->json('GET', route('form.contribution', [ - 'type' => $type, - 'form' => $form, - 'validate' => '1', - ]))->assertJsonValidationErrors([$field => $message]); + generate($type, $form, true)->assertJsonValidationErrors([$field => $message]); }) ->with([ [CitySolingenDocument::class, [], 'ADDRESS', 'Kein Feld für Adresse vorhanden.'], @@ -79,10 +71,7 @@ it('throws error when not validating but fields are not present', function () { ->has(Participant::factory()) ->create(); - $this->json('GET', route('form.contribution', [ - 'type' => CitySolingenDocument::class, - 'form' => $form, - ]))->assertStatus(422); + generate(CitySolingenDocument::class, $form, false)->assertStatus(422); }); it('throws error when form doesnt have meta', function () { @@ -94,10 +83,7 @@ it('throws error when form doesnt have meta', function () { ->location('') ->create(); - $this->json('GET', route('form.contribution', [ - 'type' => CitySolingenDocument::class, - 'form' => $form, - ]))->assertStatus(422)->assertJsonValidationErrors([ + generate(CitySolingenDocument::class, $form, false)->assertStatus(422)->assertJsonValidationErrors([ 'zip' => 'PLZ ist erforderlich.', 'location' => 'Ort ist erforderlich.' ]); @@ -108,11 +94,7 @@ it('throws error when form doesnt have participants', function () { $form = Form::factory()->fields([])->create(); - $this->json('GET', route('form.contribution', [ - 'type' => CitySolingenDocument::class, - 'form' => $form, - 'validate' => '1', - ]))->assertJsonValidationErrors(['participants' => 'Veranstaltung besitzt noch keine Teilnehmer*innen.']); + generate(CitySolingenDocument::class, $form, true)->assertJsonValidationErrors(['participants' => 'Veranstaltung besitzt noch keine Teilnehmer*innen.']); }); it('creates document when fields are present', function () { @@ -130,10 +112,7 @@ it('creates document when fields are present', function () { ->has(Participant::factory()->data(['fn' => 'Baum', 'ln' => 'Muster', 'bd' => '1991-05-06', 'zip' => '33333', 'loc' => 'Musterstadt', 'add' => 'Laastr 4'])) ->create(); - $this->json('GET', route('form.contribution', [ - 'type' => CitySolingenDocument::class, - 'form' => $form, - ]))->assertOk(); + generate(CitySolingenDocument::class, $form, false)->assertOk(); Tex::assertCompiled(CitySolingenDocument::class, fn($document) => $document->hasAllContent(['Baum', 'Muster', '1991', 'Musterstadt', 'Laastr 4', '33333'])); }); @@ -159,9 +138,14 @@ it('creates document with form meta', function () { ->location('Frankfurt') ->create(); - $this->json('GET', route('form.contribution', [ - 'type' => RdpNrwDocument::class, - 'form' => $form, - ]))->assertOk(); + generate(RdpNrwDocument::class, $form, false)->assertOk(); Tex::assertCompiled(RdpNrwDocument::class, fn($document) => $document->hasAllContent(['20.06.2008', '22.06.2008', '12345 Frankfurt'])); }); + +function generate(string $document, Form $form, bool $validate) { + return test()->json('GET', route('form.contribution', [ + 'payload' => test()->filterString(['type' => $document]), + 'form' => $form, + 'validate' => $validate ? '1' : '0' + ])); +}