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