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:
@@ -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()
|
||||
|
||||
@@ -1,126 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Helpers;
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use Faker;
|
||||
|
||||
class FormSubmissionDataFactory
|
||||
{
|
||||
private ?Faker\Generator $faker;
|
||||
|
||||
/**
|
||||
* If true, then format expected by answer endpoint
|
||||
* otherwise, format of answer as we store it in the FormSubmission's data
|
||||
*/
|
||||
private bool $answerFormat = true;
|
||||
|
||||
public function __construct(private Form $form)
|
||||
{
|
||||
$this->faker = Faker\Factory::create();
|
||||
}
|
||||
|
||||
public static function generateSubmissionData(Form $form, array $data = [])
|
||||
{
|
||||
return (new self($form))->createSubmissionData($data);
|
||||
}
|
||||
|
||||
public function asFormSubmissionData()
|
||||
{
|
||||
$this->answerFormat = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function createSubmissionData($mergeData = [])
|
||||
{
|
||||
$data = [];
|
||||
|
||||
// for all non-hidden fields in form, create some fake data
|
||||
collect($this->form->properties)->each(function ($property) use (&$data) {
|
||||
$value = null;
|
||||
switch ($property['type']) {
|
||||
case 'text':
|
||||
$value = $this->faker->name();
|
||||
break;
|
||||
case 'email':
|
||||
$value = $this->faker->unique()->email();
|
||||
break;
|
||||
case 'checkbox':
|
||||
$value = $this->faker->randomElement([true, false]);
|
||||
break;
|
||||
case 'number':
|
||||
$value = $this->faker->numberBetween();
|
||||
break;
|
||||
case 'rating':
|
||||
case 'scale':
|
||||
$value = $this->faker->numberBetween(1, 5);
|
||||
break;
|
||||
case 'slider':
|
||||
$value = $this->faker->numberBetween(0, 50);
|
||||
break;
|
||||
case 'url':
|
||||
$value = $this->faker->url();
|
||||
break;
|
||||
case 'phone_number':
|
||||
$value = 'FR+33749119783';
|
||||
break;
|
||||
case 'date':
|
||||
$value = $this->faker->date();
|
||||
break;
|
||||
case 'select':
|
||||
$value = $this->generateSelectValue($property);
|
||||
break;
|
||||
case 'multi_select':
|
||||
$value = $this->generateMultiSelectValues($property);
|
||||
break;
|
||||
case 'files':
|
||||
$value = null; // TODO: Will do this in future
|
||||
break;
|
||||
}
|
||||
$data[$property['id']] = $value;
|
||||
});
|
||||
|
||||
if (!$this->answerFormat) {
|
||||
$data = $this->formatAsSubmissionData($data);
|
||||
}
|
||||
|
||||
return array_merge($data, $mergeData);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
return ($values) ? $this->faker->randomElement($values) : null;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
return ($values) ? $this->faker->randomElements(
|
||||
$values,
|
||||
$this->faker->numberBetween(1, count($values))
|
||||
) : null;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user