1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Fiv\Form; |
4
|
|
|
|
5
|
|
|
use Fiv\Form\Element\Input; |
6
|
|
|
use Fiv\Form\Form; |
7
|
|
|
use Fiv\Form\Validator\CallBackValidator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @package Tests\Form\Form |
11
|
|
|
*/ |
12
|
|
|
class FormTest extends \PHPUnit_Framework_TestCase { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
public function testIsValidFlush() { |
18
|
|
|
$form = new Form(); |
19
|
|
|
$form->input('email'); |
20
|
|
|
|
21
|
|
|
$form->setData([ |
22
|
|
|
$form->getUid() => 1, |
23
|
|
|
'email' => 'test@test', |
24
|
|
|
]); |
25
|
|
|
|
26
|
|
|
$this->assertTrue($form->isValid()); |
27
|
|
|
|
28
|
|
|
$form->setData([]); |
29
|
|
|
|
30
|
|
|
$this->assertFalse($form->isValid()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
public function testUid() { |
38
|
|
|
$form = new Form(); |
39
|
|
|
|
40
|
|
|
$this->assertEquals(32, strlen($form->getUid())); |
41
|
|
|
|
42
|
|
|
$form->setName('test'); |
43
|
|
|
$this->assertEquals('test', $form->getUid()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
public function testData() { |
51
|
|
|
$form = new Form(); |
52
|
|
|
$form->input('email'); |
53
|
|
|
|
54
|
|
|
$form->setData([ |
55
|
|
|
$form->getUid() => 1, |
56
|
|
|
'other_custom_data' => 123, |
57
|
|
|
'email' => 'test@test', |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$this->assertEquals([ |
61
|
|
|
'email' => 'test@test', |
62
|
|
|
], $form->getData()); |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
|
public function testGetElements() { |
71
|
|
|
$form = new Form(); |
72
|
|
|
$this->assertEmpty($form->getElements()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
|
|
public function testFormMethods() { |
80
|
|
|
$form = new Form(); |
81
|
|
|
$this->assertEquals('post', $form->getMethod()); |
82
|
|
|
|
83
|
|
|
$form->setMethod('POST'); |
84
|
|
|
$this->assertEquals('post', $form->getMethod()); |
85
|
|
|
|
86
|
|
|
$form->setMethod('get'); |
87
|
|
|
$this->assertEquals('get', $form->getMethod()); |
88
|
|
|
|
89
|
|
|
$form->setMethod('put'); |
90
|
|
|
$this->assertEquals('put', $form->getMethod()); |
91
|
|
|
|
92
|
|
|
$form->setMethod(false); |
|
|
|
|
93
|
|
|
$this->assertEquals(null, $form->getMethod()); |
94
|
|
|
|
95
|
|
|
$form->setAttribute('method', 'test'); |
96
|
|
|
$this->assertEquals('test', $form->getMethod()); |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* |
103
|
|
|
*/ |
104
|
|
|
public function testFormRender() { |
105
|
|
|
$form = new Form(); |
106
|
|
|
$this->assertContains('method="post"', $form->render()); |
107
|
|
|
|
108
|
|
|
$form = new Form(); |
109
|
|
|
$form->setMethod('get'); |
110
|
|
|
$this->assertContains('method="get"', $form->render()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* |
116
|
|
|
*/ |
117
|
|
|
public function testIsSubmittedFalse() { |
118
|
|
|
$form = new Form(); |
119
|
|
|
$form->setData([]); |
120
|
|
|
$this->assertEquals(false, $form->isSubmitted()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* |
126
|
|
|
*/ |
127
|
|
|
public function testIsSubmittedTrue() { |
128
|
|
|
$form = new Form(); |
129
|
|
|
$form->setName('test-form'); |
130
|
|
|
$form->setData([ |
131
|
|
|
'test-form' => 1, |
132
|
|
|
]); |
133
|
|
|
$this->assertEquals(true, $form->isSubmitted()); |
134
|
|
|
|
135
|
|
|
$form = new Form(); |
136
|
|
|
$form->submit('test-submit', 'test-value'); |
137
|
|
|
$form->setData([ |
138
|
|
|
$form->getUid() => 1, |
139
|
|
|
]); |
140
|
|
|
$this->assertEquals(true, $form->isSubmitted()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* |
146
|
|
|
*/ |
147
|
|
|
public function testValidateWithoutSubmit() { |
148
|
|
|
$form = new Form(); |
149
|
|
|
$form->setName('test-form'); |
150
|
|
|
$this->assertFalse($form->isValid()); |
151
|
|
|
$this->assertFalse($form->isSubmitted()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
*/ |
158
|
|
|
public function testFormSetData() { |
159
|
|
|
$exception = null; |
160
|
|
|
|
161
|
|
|
try { |
162
|
|
|
$form = new Form(); |
163
|
|
|
$form->setName('test-form'); |
164
|
|
|
$form->setData(null); |
|
|
|
|
165
|
|
|
} catch (\Exception $e) { |
166
|
|
|
$exception = $e; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->assertNotEmpty($exception, 'Should throw exception if data not array or Iterator!'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @expectedException \Exception |
175
|
|
|
*/ |
176
|
|
|
public function testAddElementsWithSameNames() { |
177
|
|
|
$form = new Form(); |
178
|
|
|
$form->addElement((new Input())->setName('test')); |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
$this->assertCount(1, $form->getElements()); |
182
|
|
|
|
183
|
|
|
$form->addElement((new Input())->setName('test')); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* |
189
|
|
|
*/ |
190
|
|
|
public function testSetElementsWithSameNames() { |
191
|
|
|
$form = new Form(); |
192
|
|
|
$form->setElement((new Input())->setName('test')->setValue('first')); |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
$elements = $form->getElements(); |
196
|
|
|
$this->assertCount(1, $elements); |
197
|
|
|
$this->assertEquals('first', $elements['test']->getValue()); |
198
|
|
|
|
199
|
|
|
$form->setElement((new Input())->setName('test')->setValue('second')); |
200
|
|
|
|
201
|
|
|
$elements = $form->getElements(); |
202
|
|
|
$this->assertCount(1, $elements); |
203
|
|
|
$this->assertEquals('second', $elements['test']->getValue()); |
204
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
public function testFormValidationCache() { |
209
|
|
|
$form = new Form(); |
210
|
|
|
$form->setName('user_registration'); |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
$element = (new Input())->setName('test')->setValue('first'); |
214
|
|
|
|
215
|
|
|
$checkedItemsNum = 0; |
216
|
|
|
|
217
|
|
|
$element->addValidator(new CallBackValidator(function ($value) use (&$checkedItemsNum) { |
218
|
|
|
$checkedItemsNum++; |
219
|
|
|
|
220
|
|
|
return !empty($value); |
221
|
|
|
})); |
222
|
|
|
|
223
|
|
|
$form->setElement($element); |
224
|
|
|
# emulate form submit |
225
|
|
|
$form->setData([ |
226
|
|
|
$form->getUid() => '1', |
227
|
|
|
'test' => '123', |
228
|
|
|
]); |
229
|
|
|
|
230
|
|
|
$this->assertTrue($form->isValid()); |
231
|
|
|
$this->assertEquals(1, $checkedItemsNum); |
232
|
|
|
$this->assertTrue($form->isValid()); |
233
|
|
|
$this->assertTrue($form->isValid()); |
234
|
|
|
|
235
|
|
|
$this->assertEquals(1, $checkedItemsNum); |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
public function testRenderStartEnd() { |
241
|
|
|
$form = new Form(); |
242
|
|
|
$form->hidden('test', '123'); |
243
|
|
|
$start = $form->renderStart(); |
244
|
|
|
$this->assertContains('<input type="hidden" name="test" value="123"', $start); |
245
|
|
|
|
246
|
|
|
$this->assertEquals('</form>', $form->renderEnd()); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
public function testRenderElements() { |
251
|
|
|
$form = new Form(); |
252
|
|
|
$form->textarea('text', '123'); |
253
|
|
|
$this->assertContains('<dl><dt>123</dt><dd><textarea name="text" ></textarea></dd></dl>', $form->render()); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: