2022-09-20 19:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Helpers;
|
|
|
|
|
|
|
|
use App\Models\Forms\Form;
|
|
|
|
use Faker;
|
|
|
|
|
|
|
|
class FormSubmissionDataFactory
|
|
|
|
{
|
2024-02-23 10:54:12 +00:00
|
|
|
private ?Faker\Generator $faker;
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
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 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 'url':
|
|
|
|
$value = $this->faker->url();
|
|
|
|
break;
|
|
|
|
case 'phone_number':
|
2023-10-03 15:50:46 +00:00
|
|
|
$value = 'FR+33749119783';
|
2022-09-20 19:59:52 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
return array_merge($data, $mergeData);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateSelectValue($property)
|
|
|
|
{
|
|
|
|
$values = [];
|
2024-02-23 10:54:12 +00:00
|
|
|
if (isset($property['select']['options']) && count($property['select']['options']) > 0) {
|
|
|
|
$values = collect($property['select']['options'])->map(function ($option) {
|
2022-09-20 19:59:52 +00:00
|
|
|
return $option['name'];
|
|
|
|
})->toArray();
|
|
|
|
}
|
2024-02-23 10:54:12 +00:00
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
return ($values) ? $this->faker->randomElement($values) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateMultiSelectValues($property)
|
|
|
|
{
|
|
|
|
$values = [];
|
2024-02-23 10:54:12 +00:00
|
|
|
if (isset($property['multi_select']['options']) && count($property['multi_select']['options']) > 0) {
|
|
|
|
$values = collect($property['multi_select']['options'])->map(function ($option) {
|
2022-09-20 19:59:52 +00:00
|
|
|
return $option['name'];
|
|
|
|
})->toArray();
|
|
|
|
}
|
2024-02-23 10:54:12 +00:00
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
return ($values) ? $this->faker->randomElements(
|
|
|
|
$values,
|
2024-02-23 10:54:12 +00:00
|
|
|
$this->faker->numberBetween(1, count($values))
|
|
|
|
) : null;
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
}
|