1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of FlexPHP. |
4
|
|
|
* |
5
|
|
|
* (c) Freddie Gar <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace FlexPHP\Inputs\Tests\Unit\Builder; |
11
|
|
|
|
12
|
|
|
use FlexPHP\Inputs\Builder\FormBuilder; |
13
|
|
|
use FlexPHP\Inputs\Tests\TestCase; |
14
|
|
|
|
15
|
|
|
class FormBuilderTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testItDefault(): void |
18
|
|
|
{ |
19
|
|
|
$render = (new FormBuilder([]))->render(); |
20
|
|
|
|
21
|
|
|
$this->assertEquals(<<<T |
22
|
|
|
<form name="form" method="post"> |
23
|
|
|
</form> |
24
|
|
|
T |
25
|
|
|
, $render); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testItWithInputRender(): void |
29
|
|
|
{ |
30
|
|
|
$render = (new FormBuilder([ |
31
|
|
|
'foo' => '<div class="form-group"><label for="form_foo">Foo</label><input type="text" id="form_foo" name="form[foo]" class="form-control" /></div>', |
32
|
|
|
]))->render(); |
33
|
|
|
|
34
|
|
|
$this->assertEquals(<<<T |
35
|
|
|
<form name="form" method="post"> |
36
|
|
|
<div class="form-group"><label for="form_foo">Foo</label><input type="text" id="form_foo" name="form[foo]" class="form-control" /></div> |
37
|
|
|
</form> |
38
|
|
|
T |
39
|
|
|
, $render); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testItWithInputOptions(): void |
43
|
|
|
{ |
44
|
|
|
$render = (new FormBuilder([ |
45
|
|
|
'foo' => [ |
46
|
|
|
'type' => 'email', |
47
|
|
|
], |
48
|
|
|
]))->render(); |
49
|
|
|
|
50
|
|
|
$this->assertEquals(<<<T |
51
|
|
|
<form name="form" method="post"> |
52
|
|
|
<div class="form-group"><label for="form_foo">Foo</label><input type="email" id="form_foo" name="form[foo]" class="form-control" /></div> |
53
|
|
|
</form> |
54
|
|
|
T |
55
|
|
|
, $render); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testItWithInputs(): void |
59
|
|
|
{ |
60
|
|
|
$render = (new FormBuilder([ |
61
|
|
|
'foo' => [ |
62
|
|
|
'type' => 'email', |
63
|
|
|
], |
64
|
|
|
'bar' => [ |
65
|
|
|
'type' => 'textarea', |
66
|
|
|
], |
67
|
|
|
]))->render(); |
68
|
|
|
|
69
|
|
|
$this->assertEquals(<<<T |
70
|
|
|
<form name="form" method="post"> |
71
|
|
|
<div class="form-group"><label for="form_foo">Foo</label><input type="email" id="form_foo" name="form[foo]" class="form-control" /></div> |
72
|
|
|
<div class="form-group"><label for="form_bar">Bar</label><textarea id="form_bar" name="form[bar]" class="form-control"></textarea></div> |
73
|
|
|
</form> |
74
|
|
|
T |
75
|
|
|
, $render); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testItWithStringTemplate(): void |
79
|
|
|
{ |
80
|
|
|
$render = (new FormBuilder([], [], [], '{{ form(form) }}'))->render(); |
81
|
|
|
|
82
|
|
|
$this->assertEquals(<<<T |
83
|
|
|
<form name="form" method="post"><div id="form"></div></form> |
84
|
|
|
T |
85
|
|
|
, $render); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testItWithFileTemplate(): void |
89
|
|
|
{ |
90
|
|
|
$file = \sprintf('%1$s/../../Resources/Template.html.twig', __DIR__); |
91
|
|
|
|
92
|
|
|
$render = (new FormBuilder([], [], [], $file))->render(); |
93
|
|
|
|
94
|
|
|
$this->assertEquals(<<<T |
95
|
|
|
File: <form name="form" method="post"><div id="form"></div></form> |
96
|
|
|
T |
97
|
|
|
, $render); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|