adrema/tests/Feature/Form/FormtemplateStoreActionTest...

81 lines
4.0 KiB
PHP
Raw Normal View History

2023-12-22 20:40:24 +01:00
<?php
namespace Tests\Feature\Form;
2024-03-07 00:58:14 +01:00
use App\Form\Fields\TextareaField;
use App\Form\Fields\TextField;
use App\Form\Models\Form;
2023-12-26 20:06:57 +01:00
use App\Form\Models\Formtemplate;
use App\Lib\Events\Succeeded;
2023-12-22 20:40:24 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2023-12-26 20:06:57 +01:00
use Illuminate\Support\Facades\Event;
use Generator;
2023-12-22 20:40:24 +01:00
2024-02-19 01:07:54 +01:00
class FormtemplateStoreActionTest extends FormTestCase
2023-12-22 20:40:24 +01:00
{
use DatabaseTransactions;
public function testItStoresTemplates(): void
{
2023-12-26 20:06:57 +01:00
Event::fake([Succeeded::class]);
2023-12-22 20:40:24 +01:00
$this->login()->loginNami()->withoutExceptionHandling();
2023-12-26 20:06:57 +01:00
FormtemplateRequest::new()->name('testname')->sections([
FormtemplateSectionRequest::new()->name('Persönliches')->fields([
2024-03-07 00:58:14 +01:00
$this->textField('a')->name('lala1')->columns(['mobile' => 2, 'tablet' => 2, 'desktop' => 1])->required(false),
2024-02-19 01:07:54 +01:00
$this->textareaField('b')->name('lala2')->required(false)->rows(10),
2023-12-26 20:06:57 +01:00
]),
])->fake();
2023-12-22 20:40:24 +01:00
2023-12-26 20:06:57 +01:00
$this->postJson(route('formtemplate.store'))->assertOk();
$formtemplate = Formtemplate::latest()->first();
2024-03-07 00:58:14 +01:00
$this->assertEquals('Persönliches', $formtemplate->config->sections->get(0)->name);
$this->assertEquals('lala1', $formtemplate->config->sections->get(0)->fields->get(0)->name);
$this->assertInstanceOf(TextField::class, $formtemplate->config->sections->get(0)->fields->get(0));
$this->assertInstanceOf(TextareaField::class, $formtemplate->config->sections->get(0)->fields->get(1));
$this->assertEquals(false, $formtemplate->config->sections->get(0)->fields->get(1)->required);
$this->assertEquals(['mobile' => 2, 'tablet' => 2, 'desktop' => 1], $formtemplate->config->sections->get(0)->fields->get(0)->columns->toArray());
$this->assertEquals(10, $formtemplate->config->sections->get(0)->fields->get(1)->rows);
$this->assertFalse($formtemplate->config->sections->get(0)->fields->get(0)->required);
2023-12-27 22:41:07 +01:00
Event::assertDispatched(Succeeded::class, fn (Succeeded $event) => $event->message === 'Vorlage gespeichert.');
2023-12-26 20:06:57 +01:00
}
public function validationDataProvider(): Generator
{
yield [FormtemplateRequest::new()->name(''), ['name' => 'Name ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->name('')]), ['config.sections.0.name' => 'Sektionsname ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-02-19 01:07:54 +01:00
$this->textField()->name(''),
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.name' => 'Feldname ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-02-04 03:19:28 +01:00
FormtemplateFieldRequest::type('')
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-02-04 03:19:28 +01:00
FormtemplateFieldRequest::type('aaaaa'),
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.type' => 'Feldtyp ist ungültig.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-02-19 01:07:54 +01:00
$this->textField(''),
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.key' => 'Feldkey ist erforderlich.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-02-19 01:07:54 +01:00
$this->textField('a b'),
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.key' => 'Feldkey Format ist ungültig.']];
yield [FormtemplateRequest::new()->sections([FormtemplateSectionRequest::new()->fields([
2024-02-19 01:07:54 +01:00
$this->textField()->required('la')
2023-12-26 20:06:57 +01:00
])]), ['config.sections.0.fields.0.required' => 'Erforderlich muss ein Wahrheitswert sein.']];
}
/**
* @dataProvider validationDataProvider
* @param array<string, string> $messages
*/
public function testItValidatesRequests(FormtemplateRequest $request, array $messages): void
{
$this->login()->loginNami();
$request->fake();
2023-12-22 20:40:24 +01:00
2023-12-26 20:06:57 +01:00
$this->postJson(route('formtemplate.store'))
->assertJsonValidationErrors($messages);
2023-12-22 20:40:24 +01:00
}
}