2023-12-22 20:40:24 +01:00
< ? php
namespace Tests\Feature\Form ;
2023-12-26 20:06:57 +01:00
use App\Form\Fields\RadioField ;
use App\Form\Fields\TextareaField ;
use App\Form\Fields\TextField ;
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 ;
2023-12-22 20:40:24 +01:00
use Tests\TestCase ;
2023-12-26 20:06:57 +01:00
use Generator ;
2023-12-22 20:40:24 +01:00
class FormtemplateStoreActionTest extends TestCase
{
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 ([
2023-12-27 00:42:17 +01:00
FormtemplateFieldRequest :: new () -> type ( TextField :: class ) -> name ( 'lala1' ) -> columns ([ 'mobile' => 2 , 'tablet' => 2 , 'desktop' => 1 ]) -> required ( false ) -> default ( 'zuzu' ),
2023-12-26 20:06:57 +01:00
FormtemplateFieldRequest :: new () -> type ( TextareaField :: class ) -> name ( 'lala2' ) -> required ( false ) -> rows ( 10 ),
]),
]) -> 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 ();
$this -> assertEquals ( 'Persönliches' , $formtemplate -> config [ 'sections' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'lala1' , $formtemplate -> config [ 'sections' ][ 0 ][ 'fields' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'TextField' , $formtemplate -> config [ 'sections' ][ 0 ][ 'fields' ][ 0 ][ 'type' ]);
2023-12-27 00:42:17 +01:00
$this -> assertEquals ( 'zuzu' , $formtemplate -> config [ 'sections' ][ 0 ][ 'fields' ][ 0 ][ 'default' ]);
2023-12-26 20:06:57 +01:00
$this -> assertEquals ( 'TextareaField' , $formtemplate -> config [ 'sections' ][ 0 ][ 'fields' ][ 1 ][ 'type' ]);
$this -> assertEquals ( false , $formtemplate -> config [ 'sections' ][ 0 ][ 'fields' ][ 1 ][ 'required' ]);
$this -> assertEquals ([ 'mobile' => 2 , 'tablet' => 2 , 'desktop' => 1 ], $formtemplate -> config [ 'sections' ][ 0 ][ 'fields' ][ 0 ][ 'columns' ]);
$this -> assertEquals ( 10 , $formtemplate -> config [ 'sections' ][ 0 ][ 'fields' ][ 1 ][ 'rows' ]);
$this -> assertFalse ( $formtemplate -> config [ 'sections' ][ 0 ][ 'fields' ][ 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 ([
FormtemplateFieldRequest :: new () -> name ( '' ),
])]), [ 'config.sections.0.fields.0.name' => 'Feldname ist erforderlich.' ]];
yield [ FormtemplateRequest :: new () -> sections ([ FormtemplateSectionRequest :: new () -> fields ([
FormtemplateFieldRequest :: new () -> type ( '' ),
])]), [ 'config.sections.0.fields.0.type' => 'Feldtyp ist erforderlich.' ]];
yield [ FormtemplateRequest :: new () -> sections ([ FormtemplateSectionRequest :: new () -> fields ([
FormtemplateFieldRequest :: new () -> type ( 'aaaaa' ),
])]), [ 'config.sections.0.fields.0.type' => 'Feldtyp ist ungültig.' ]];
yield [ FormtemplateRequest :: new () -> sections ([ FormtemplateSectionRequest :: new () -> fields ([
FormtemplateFieldRequest :: new () -> key ( '' ),
])]), [ 'config.sections.0.fields.0.key' => 'Feldkey ist erforderlich.' ]];
yield [ FormtemplateRequest :: new () -> sections ([ FormtemplateSectionRequest :: new () -> fields ([
FormtemplateFieldRequest :: new () -> key ( 'a b' ),
])]), [ 'config.sections.0.fields.0.key' => 'Feldkey Format ist ungültig.' ]];
yield [ FormtemplateRequest :: new () -> sections ([ FormtemplateSectionRequest :: new () -> fields ([
FormtemplateFieldRequest :: new () -> type ( TextField :: class ) -> required ( 'la' )
])]), [ '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
}
}