Refactor FormSubmissionDataFactory to use TestHelpers trait
- Move FormSubmissionDataFactory methods into TestHelpers trait - Update import statements across multiple test files - Remove standalone FormSubmissionDataFactory class - Simplify form submission data generation in tests - Add documentation for new generateFormSubmissionData method
This commit is contained in:
parent
d2f77aaa3f
commit
b633f97ce1
|
|
@ -7,7 +7,8 @@ use App\Http\Requests\Zapier\CreateIntegrationRequest;
|
|||
use App\Http\Requests\Zapier\DeleteIntegrationRequest;
|
||||
use App\Integrations\Handlers\ZapierIntegration;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
use App\Service\Forms\FormSubmissionDataFactory;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class IntegrationController
|
||||
{
|
||||
|
|
@ -58,9 +59,8 @@ class IntegrationController
|
|||
$submissionData = (new FormSubmissionDataFactory($form))->asFormSubmissionData()->createSubmissionData();
|
||||
}
|
||||
$cacheKey = "zapier-poll-submissions-{$form->id}";
|
||||
return (array) \Cache::remember($cacheKey, 60 * 5, function () use ($form, $submissionData, $lastSubmission) {
|
||||
return (array) Cache::remember($cacheKey, 60 * 5, function () use ($form, $submissionData, $lastSubmission) {
|
||||
return [ZapierIntegration::formatWebhookData($form, $submissionData ?? $lastSubmission->data)];
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Helpers;
|
||||
namespace App\Service\Forms;
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use Faker;
|
||||
|
|
@ -89,38 +89,33 @@ class FormSubmissionDataFactory
|
|||
|
||||
private function formatAsSubmissionData($data)
|
||||
{
|
||||
collect($this->form->properties)->each(function ($property) use (&$data) {
|
||||
if ($property['type'] === 'phone_number') {
|
||||
$data[$property['id']] = '+33749119783';
|
||||
}
|
||||
});
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function generateSelectValue($property)
|
||||
{
|
||||
$values = [];
|
||||
if (isset($property['select']['options']) && count($property['select']['options']) > 0) {
|
||||
$values = collect($property['select']['options'])->map(function ($option) {
|
||||
return $option['name'];
|
||||
})->toArray();
|
||||
if (empty($property['options'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ($values) ? $this->faker->randomElement($values) : null;
|
||||
$option = $this->faker->randomElement($property['options']);
|
||||
return $option['id'];
|
||||
}
|
||||
|
||||
private function generateMultiSelectValues($property)
|
||||
{
|
||||
$values = [];
|
||||
if (isset($property['multi_select']['options']) && count($property['multi_select']['options']) > 0) {
|
||||
$values = collect($property['multi_select']['options'])->map(function ($option) {
|
||||
return $option['name'];
|
||||
})->toArray();
|
||||
if (empty($property['options'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return ($values) ? $this->faker->randomElements(
|
||||
$values,
|
||||
$this->faker->numberBetween(1, count($values))
|
||||
) : null;
|
||||
$numOptions = count($property['options']);
|
||||
$numToSelect = $this->faker->numberBetween(1, min(3, $numOptions));
|
||||
|
||||
$selectedOptions = $this->faker->randomElements(
|
||||
array_column($property['options'], 'id'),
|
||||
$numToSelect
|
||||
);
|
||||
|
||||
return $selectedOptions;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
it('can answer a form', function () {
|
||||
$user = $this->actingAsUser();
|
||||
|
|
@ -17,7 +16,7 @@ it('can submit form if close date is in future', function () {
|
|||
$form = $this->createForm($user, $workspace, [
|
||||
'closes_at' => \Carbon\Carbon::now()->addDays(1)->toDateTimeString(),
|
||||
]);
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertSuccessful()
|
||||
|
|
@ -33,7 +32,7 @@ it('can not submit closed form', function () {
|
|||
$form = $this->createForm($user, $workspace, [
|
||||
'closes_at' => \Carbon\Carbon::now()->subDays(1)->toDateTimeString(),
|
||||
]);
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(403);
|
||||
|
|
@ -45,7 +44,7 @@ it('can submit form till max submissions count is not reached at limit', functio
|
|||
$form = $this->createForm($user, $workspace, [
|
||||
'max_submissions_count' => 3,
|
||||
]);
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
// Can submit form
|
||||
for ($i = 1; $i <= 3; $i++) {
|
||||
|
|
@ -79,7 +78,7 @@ it('can not submit draft form', function () {
|
|||
$form = $this->createForm($user, $workspace, [
|
||||
'visibility' => 'draft',
|
||||
]);
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(403);
|
||||
|
|
@ -91,7 +90,7 @@ it('can not submit visibility closed form', function () {
|
|||
$form = $this->createForm($user, $workspace, [
|
||||
'visibility' => 'closed',
|
||||
]);
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(403);
|
||||
|
|
@ -113,7 +112,7 @@ it('can not submit form with past dates', function () {
|
|||
})->toArray();
|
||||
$form->update();
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(422)
|
||||
|
|
@ -138,7 +137,7 @@ it('can not submit form with future dates', function () {
|
|||
})->toArray();
|
||||
$form->update();
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(422)
|
||||
|
|
@ -183,7 +182,7 @@ it('can submit form with passed custom validation condition', function () {
|
|||
})->toArray();
|
||||
|
||||
$form->update();
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$response = $this->postJson(route('forms.answer', $form->slug), $formData);
|
||||
$response->assertSuccessful()
|
||||
|
|
@ -229,7 +228,7 @@ it('can not submit form with failed custom validation condition', function () {
|
|||
|
||||
$form->update();
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(422)
|
||||
|
|
@ -343,7 +342,7 @@ it('executes custom validation before required field validation', function () {
|
|||
})->toArray();
|
||||
$form->update();
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(422)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
|
||||
it('can submit form with dyanamic select option', function () {
|
||||
$user = $this->actingAsUser();
|
||||
|
|
@ -11,14 +11,14 @@ it('can submit form with dyanamic select option', function () {
|
|||
$form->properties = collect($form->properties)->map(function ($property) use (&$selectionsPreData) {
|
||||
if (in_array($property['type'], ['select', 'multi_select'])) {
|
||||
$property['allow_creation'] = true;
|
||||
$selectionsPreData[$property['id']] = ($property['type'] == 'select') ? 'New single select - '.time() : ['New multi select - '.time()];
|
||||
$selectionsPreData[$property['id']] = ($property['type'] == 'select') ? 'New single select - ' . time() : ['New multi select - ' . time()];
|
||||
}
|
||||
|
||||
return $property;
|
||||
})->toArray();
|
||||
$form->update();
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $selectionsPreData);
|
||||
$formData = $this->generateFormSubmissionData($form, $selectionsPreData);
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertSuccessful()
|
||||
->assertJson([
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
use App\Notifications\Forms\FormEmailNotification;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
use Illuminate\Notifications\AnonymousNotifiable;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
|
|
@ -40,7 +39,7 @@ it('send email with custom SMTP settings', function () {
|
|||
'reply_to' => 'reply@example.com',
|
||||
]);
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
Notification::fake();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
use App\Notifications\Forms\FormEmailNotification;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
use Illuminate\Notifications\AnonymousNotifiable;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
|
|
@ -19,7 +18,7 @@ it('send email with the submitted data', function () {
|
|||
'reply_to' => 'reply@example.com',
|
||||
]);
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
|
||||
$mailable = new FormEmailNotification($event, $integrationData, 'mail');
|
||||
|
|
@ -119,7 +118,7 @@ it('uses custom sender email in self-hosted mode', function () {
|
|||
'reply_to' => 'reply@example.com',
|
||||
]);
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
|
||||
$mailable = new FormEmailNotification($event, $integrationData, 'mail');
|
||||
|
|
@ -152,7 +151,7 @@ it('does not use custom sender email in non-self-hosted mode', function () {
|
|||
'reply_to' => 'reply@example.com',
|
||||
]);
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
|
||||
$mailable = new FormEmailNotification($event, $integrationData, 'mail');
|
||||
|
|
@ -243,7 +242,7 @@ it('uses exact email address without timestamp in self-hosted mode', function ()
|
|||
'include_submission_data' => true,
|
||||
]);
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
|
||||
$mailable = new FormEmailNotification($event, $integrationData, 'mail');
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Testing\Fluent\AssertableJson;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
it('create form with logic', function () {
|
||||
$user = $this->actingAsUser();
|
||||
|
|
@ -390,7 +389,7 @@ it('can submit form with passed regex validation condition', function () {
|
|||
})->toArray();
|
||||
|
||||
$form->update();
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$response = $this->postJson(route('forms.answer', $form->slug), $formData);
|
||||
$response->assertSuccessful()
|
||||
|
|
@ -439,7 +438,7 @@ it('can not submit form with failed regex validation condition', function () {
|
|||
})->toArray();
|
||||
|
||||
$form->update();
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(422)
|
||||
|
|
@ -487,7 +486,7 @@ it('can submit form with does not match regex validation condition', function ()
|
|||
})->toArray();
|
||||
|
||||
$form->update();
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$response = $this->postJson(route('forms.answer', $form->slug), $formData);
|
||||
$response->assertSuccessful()
|
||||
|
|
@ -536,7 +535,7 @@ it('handles invalid regex patterns gracefully', function () {
|
|||
})->toArray();
|
||||
|
||||
$form->update();
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(422)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Testing\Fluent\AssertableJson;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->password = '12345';
|
||||
|
|
@ -10,7 +9,7 @@ beforeEach(function () {
|
|||
$this->form = $this->createForm($user, $workspace, [
|
||||
'password' => $this->password,
|
||||
]);
|
||||
$this->formData = FormSubmissionDataFactory::generateSubmissionData($this->form);
|
||||
$this->formData = $this->generateFormSubmissionData($this->form);
|
||||
});
|
||||
|
||||
it('can allow form owner to access and submit form without password', function () {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
use App\Models\User;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
it('can export form submissions with selected columns', function () {
|
||||
$user = $this->actingAsProUser();
|
||||
|
|
@ -31,7 +30,7 @@ it('can export form submissions with selected columns', function () {
|
|||
];
|
||||
|
||||
foreach ($submissions as $submission) {
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submission);
|
||||
$formData = $this->generateFormSubmissionData($form, $submission);
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertSuccessful();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
|
||||
it('can update form with existing record', function () {
|
||||
$user = $this->actingAsProUser();
|
||||
|
|
@ -23,7 +23,7 @@ it('can update form with existing record', function () {
|
|||
expect($submissionId)->toBeString();
|
||||
|
||||
if ($submissionId) {
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, ['submission_id' => $submissionId, $nameProperty['id'] => 'Testing Updated']);
|
||||
$formData = $this->generateFormSubmissionData($form, ['submission_id' => $submissionId, $nameProperty['id'] => 'Testing Updated']);
|
||||
$response = $this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertSuccessful()
|
||||
->assertJson([
|
||||
|
|
@ -36,6 +36,6 @@ it('can update form with existing record', function () {
|
|||
|
||||
$response = $this->getJson(route('forms.fetchSubmission', [$form->slug, $submissionId]))
|
||||
->assertSuccessful();
|
||||
expect($response->json('data.'.$nameProperty['id']))->toBe('Testing Updated');
|
||||
expect($response->json('data.' . $nameProperty['id']))->toBe('Testing Updated');
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
|
||||
it('can submit form with valid matrix input', function () {
|
||||
$user = $this->actingAsUser();
|
||||
|
|
@ -27,7 +27,7 @@ it('can submit form with valid matrix input', function () {
|
|||
]
|
||||
];
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertSuccessful()
|
||||
|
|
@ -62,7 +62,7 @@ it('cannot submit form with invalid matrix input', function () {
|
|||
]
|
||||
];
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(422)
|
||||
|
|
@ -97,7 +97,7 @@ it('can submit form with optional matrix input left empty', function () {
|
|||
'matrix_field' => []
|
||||
];
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertSuccessful()
|
||||
|
|
@ -128,7 +128,7 @@ it('cannot submit form with required matrix input left empty', function () {
|
|||
'matrix_field' => []
|
||||
];
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertStatus(422)
|
||||
|
|
@ -167,7 +167,7 @@ it('can validate matrix input with precognition', function () {
|
|||
]
|
||||
];
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, $submissionData);
|
||||
$formData = $this->generateFormSubmissionData($form, $submissionData);
|
||||
|
||||
$response = $this->withPrecognition()->withHeaders([
|
||||
'Precognition-Validate-Only' => 'matrix_field'
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
it('can validate Update Workspace Select Option Job', function () {
|
||||
$user = $this->actingAsUser();
|
||||
$workspace = $this->createUserWorkspace($user);
|
||||
$form = $this->createForm($user, $workspace);
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertSuccessful()
|
||||
|
|
@ -16,7 +15,7 @@ it('can validate Update Workspace Select Option Job', function () {
|
|||
'message' => 'Form submission saved.',
|
||||
]);
|
||||
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertSuccessful()
|
||||
->assertJson([
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
|
||||
it('can update form submission', function () {
|
||||
$user = $this->actingAsUser();
|
||||
|
|
@ -9,7 +9,7 @@ it('can update form submission', function () {
|
|||
$form = $this->createForm($user, $workspace, [
|
||||
'closes_at' => \Carbon\Carbon::now()->addDays(1)->toDateTimeString(),
|
||||
]);
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, ['text' => 'John']);
|
||||
$formData = $this->generateFormSubmissionData($form, ['text' => 'John']);
|
||||
$textFieldId = array_keys($formData)[0];
|
||||
$updatedFormData = $formData;
|
||||
$updatedFormTextValue = 'Updated text';
|
||||
|
|
@ -41,7 +41,7 @@ it('cannot update form submission as non admin', function () {
|
|||
$form = $this->createForm($user, $workspace, [
|
||||
'closes_at' => \Carbon\Carbon::now()->addDays(1)->toDateTimeString(),
|
||||
]);
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form, ['text' => 'John']);
|
||||
$formData = $this->generateFormSubmissionData($form, ['text' => 'John']);
|
||||
$textFieldId = array_keys($formData)[0];
|
||||
$updatedFormData = $formData;
|
||||
$updatedFormTextValue = 'Updated text';
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
use App\Models\Integration\FormIntegration;
|
||||
use App\Models\User;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\Helpers\FormSubmissionDataFactory;
|
||||
|
||||
use function Pest\Laravel\assertDatabaseCount;
|
||||
use function Pest\Laravel\delete;
|
||||
|
|
@ -157,7 +156,7 @@ test('poll for the latest submission', function () {
|
|||
]);
|
||||
|
||||
// Create a submission for the form
|
||||
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
|
||||
$formData = $this->generateFormSubmissionData($form);
|
||||
|
||||
$this->postJson(route('forms.answer', $form->slug), $formData)
|
||||
->assertSuccessful()
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace Tests;
|
|||
use App\Models\Forms\Form;
|
||||
use App\Models\User;
|
||||
use App\Models\Workspace;
|
||||
use App\Service\Forms\FormSubmissionDataFactory;
|
||||
use Database\Factories\FormFactory;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
|
|
@ -35,9 +36,12 @@ trait TestHelpers
|
|||
}
|
||||
|
||||
/**
|
||||
* Generates a Form instance (not saved)
|
||||
* Creates a form model instance without saving it to the database
|
||||
*
|
||||
* @return array
|
||||
* @param User $user The user who owns the form
|
||||
* @param Workspace $workspace The workspace the form belongs to
|
||||
* @param array $data Additional data for the form
|
||||
* @return Form The form model instance
|
||||
*/
|
||||
public function makeForm(User $user, Workspace $workspace, array $data = [])
|
||||
{
|
||||
|
|
@ -151,6 +155,14 @@ trait TestHelpers
|
|||
->make($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form with the given data
|
||||
*
|
||||
* @param User $user The user who owns the form
|
||||
* @param Workspace $workspace The workspace the form belongs to
|
||||
* @param array $data Additional data for the form
|
||||
* @return Form The created form
|
||||
*/
|
||||
public function createForm(User $user, Workspace $workspace, array $data = [])
|
||||
{
|
||||
$form = $this->makeForm($user, $workspace, $data);
|
||||
|
|
@ -257,4 +269,23 @@ trait TestHelpers
|
|||
|
||||
return (object) $response->json('form_integration.data');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates fake form submission data for testing
|
||||
*
|
||||
* @param Form $form The form to generate data for
|
||||
* @param array $data Additional data to merge with the generated data
|
||||
* @param bool $asFormSubmissionData If true, formats data as stored in FormSubmission
|
||||
* @return array The generated submission data
|
||||
*/
|
||||
public function generateFormSubmissionData(Form $form, array $data = [], bool $asFormSubmissionData = false)
|
||||
{
|
||||
$factory = new FormSubmissionDataFactory($form);
|
||||
|
||||
if ($asFormSubmissionData) {
|
||||
$factory->asFormSubmissionData();
|
||||
}
|
||||
|
||||
return $factory->createSubmissionData($data);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue