adrema/tests/Feature/Form/FormStoreActionTest.php

80 lines
3.1 KiB
PHP
Raw Normal View History

2023-12-27 22:54:58 +01:00
<?php
namespace Tests\Feature\Form;
use App\Form\Models\Form;
use App\Lib\Events\Succeeded;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
use Generator;
class FormStoreActionTest extends TestCase
{
use DatabaseTransactions;
public function testItStoresForm(): void
{
Event::fake([Succeeded::class]);
$this->login()->loginNami()->withoutExceptionHandling();
FormRequest::new()
->name('formname')
->description('lala ggg')
->excerpt('avff')
2023-12-31 22:35:13 +01:00
->registrationFrom('2023-05-04 01:00:00')->registrationUntil('2023-07-07 01:00:00')->from('2023-07-07')->to('2023-07-08')
2023-12-31 21:46:52 +01:00
->mailTop('Guten Tag')
->mailBottom('Viele Grüße')
2023-12-27 22:54:58 +01:00
->sections([FormtemplateSectionRequest::new()->name('sname')->fields([FormtemplateFieldRequest::new()])])
->fake();
$this->postJson(route('form.store'))->assertOk();
$form = Form::latest()->first();
$this->assertEquals('sname', $form->config['sections'][0]['name']);
$this->assertEquals('formname', $form->name);
$this->assertEquals('avff', $form->excerpt);
$this->assertEquals('lala ggg', $form->description);
2023-12-31 21:46:52 +01:00
$this->assertEquals('Guten Tag', $form->mail_top);
$this->assertEquals('Viele Grüße', $form->mail_bottom);
$this->assertEquals('2023-05-04 01:00', $form->registration_from->format('Y-m-d H:i'));
$this->assertEquals('2023-07-07 01:00', $form->registration_until->format('Y-m-d H:i'));
2023-12-31 22:35:13 +01:00
$this->assertEquals('2023-07-07', $form->from->format('Y-m-d'));
$this->assertEquals('2023-07-08', $form->to->format('Y-m-d'));
2023-12-27 22:54:58 +01:00
Event::assertDispatched(Succeeded::class, fn (Succeeded $event) => $event->message === 'Formular gespeichert.');
}
2023-12-31 21:46:52 +01:00
public function testRegistrationDatesCanBeNull(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$this->postJson(route('form.store'), FormRequest::new()->registrationFrom(null)->registrationUntil(null)->create())->assertOk();
$this->assertDatabaseHas('forms', [
'registration_until' => null,
'registration_from' => null,
]);
}
2023-12-27 22:54:58 +01:00
public function validationDataProvider(): Generator
{
yield [FormRequest::new()->name(''), ['name' => 'Name ist erforderlich.']];
yield [FormRequest::new()->excerpt(''), ['excerpt' => 'Auszug ist erforderlich.']];
yield [FormRequest::new()->description(''), ['description' => 'Beschreibung ist erforderlich.']];
2023-12-31 21:46:52 +01:00
yield [FormRequest::new()->state(['from' => null]), ['from' => 'Start ist erforderlich']];
yield [FormRequest::new()->state(['to' => null]), ['to' => 'Ende ist erforderlich']];
2023-12-27 22:54:58 +01:00
}
/**
* @dataProvider validationDataProvider
* @param array<string, string> $messages
*/
public function testItValidatesRequests(FormRequest $request, array $messages): void
{
$this->login()->loginNami();
$request->fake();
$this->postJson(route('form.store'))->assertJsonValidationErrors($messages);
}
}