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