2023-12-22 20:40:24 +01:00
< ? php
namespace Tests\Feature\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-02-19 01:07:54 +01:00
$this -> textField ( 'a' ) -> name ( 'lala1' ) -> columns ([ 'mobile' => 2 , 'tablet' => 2 , 'desktop' => 1 ]) -> required ( false ) -> default ( 'zuzu' ),
$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 ();
$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 ([
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
}
}