-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathControls.BaseControl.phpt
More file actions
175 lines (122 loc) · 4.77 KB
/
Controls.BaseControl.phpt
File metadata and controls
175 lines (122 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Test: Nette\Forms\Controls\BaseControl
*/
declare(strict_types=1);
use Nette\Forms\Form;
use Nette\Forms\ScalarValue;
use Nette\Forms\Validator;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
test(function () { // error handling
$form = new Form;
$input = $form->addText('text')
->setRequired('error');
Assert::same([], $input->getErrors());
Assert::null($input->getError());
Assert::false($input->hasErrors());
$input->validate();
Assert::same(['error'], $input->getErrors());
Assert::same('error', $input->getError());
Assert::true($input->hasErrors());
$input->cleanErrors();
Assert::false($input->hasErrors());
});
test(function () { // validators
$form = new Form;
$input = $form->addText('text');
$input->setValue(123);
Assert::true(Validator::validateEqual($input, 123));
Assert::true(Validator::validateEqual($input, '123'));
Assert::true(Validator::validateEqual($input, [123, 3])); // "is in"
Assert::false(Validator::validateEqual($input, ['x']));
Assert::true(Validator::validateFilled($input));
Assert::true(Validator::validateValid($input));
Assert::false(Validator::validateLength($input, null));
Assert::false(Validator::validateLength($input, 2));
Assert::true(Validator::validateLength($input, 3));
Assert::true(Validator::validateMinLength($input, 3));
Assert::false(Validator::validateMinLength($input, 4));
Assert::true(Validator::validateMaxLength($input, 3));
Assert::false(Validator::validateMaxLength($input, 2));
Assert::false(Validator::validateRange($input, [null, null]));
Assert::true(Validator::validateRange($input, [100, 1000]));
Assert::false(Validator::validateRange($input, [1000, null]));
Assert::true(Validator::validateMin($input, 122));
Assert::true(Validator::validateMin($input, 123));
Assert::false(Validator::validateMin($input, 124));
Assert::false(Validator::validateMax($input, 122));
Assert::true(Validator::validateMax($input, 123));
Assert::true(Validator::validateMax($input, 124));
});
test(function () { // validators for array
$form = new Form;
$input = $form->addMultiSelect('select', null, ['a', 'b', 'c', 'd']);
$input->setValue([1, 2, 3]);
Assert::true(Validator::validateEqual($input, [1, 2, 3, 4]));
Assert::true(Validator::validateEqual($input, ['1', '2', '3']));
Assert::false(Validator::validateEqual($input, ['x']));
Assert::true(Validator::validateFilled($input));
Assert::true(Validator::validateValid($input));
Assert::false(Validator::validateLength($input, null));
Assert::false(Validator::validateLength($input, 2));
Assert::true(Validator::validateLength($input, 3));
Assert::true(Validator::validateMinLength($input, 3));
Assert::false(Validator::validateMinLength($input, 4));
Assert::true(Validator::validateMaxLength($input, 3));
Assert::false(Validator::validateMaxLength($input, 2));
});
test(function () { // setHtmlId
$form = new Form;
$input = $form->addText('text')->setHtmlId('myId');
Assert::same('<input type="text" name="text" id="myId">', (string) $input->getControl());
});
test(function () { // special name
$form = new Form;
$input = $form->addText('submit');
Assert::same('<input type="text" name="_submit" id="frm-submit">', (string) $input->getControl());
});
test(function () { // disabled
$form = new Form;
$form->addText('disabled')
->setDisabled()
->setDefaultValue('default');
Assert::false($form->isSubmitted());
Assert::true($form['disabled']->isDisabled());
Assert::same('default', $form['disabled']->getValue());
});
test(function () { // disabled & submitted
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = ['disabled' => 'submitted value'];
$form = new Form;
$form->addText('disabled')
->setDisabled()
->setDefaultValue('default');
Assert::true($form->isSubmitted());
Assert::same('default', $form['disabled']->getValue());
unset($form['disabled']);
$input = new Nette\Forms\Controls\TextInput;
$input->setDisabled()
->setDefaultValue('default');
$form['disabled'] = $input;
Assert::same('default', $input->getValue());
});
test(function () { // scalarValue
$form = new Form;
$input = $form->addText('text');
$input->setValue(new ScalarValue('scalarValue', null));
Assert::null($input->getValue());
Assert::same('<input type="text" name="text" id="frm-text" value="scalarValue">', (string) $input->getControl());
});
test(function () { // scalarValue from filter
$form = new Form;
$input = $form->addText('text');
$input->setValue('scalarValue');
$input->addFilter(function (string $scalarValue): ScalarValue {
return new ScalarValue($scalarValue, 'mixedValue');
});
$form->validate();
Assert::same('mixedValue', $input->getValue());
Assert::same('<input type="text" name="text" id="frm-text" value="scalarValue">', (string) $input->getControl());
Assert::true(Validator::validateFilled($input));
});