-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathHelpers.exportRules.phpt
More file actions
104 lines (86 loc) · 2.32 KB
/
Helpers.exportRules.phpt
File metadata and controls
104 lines (86 loc) · 2.32 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
<?php
/**
* Test: Nette\Forms\Helpers::exportRules()
*/
declare(strict_types=1);
use Nette\Forms\Controls\TextInput;
use Nette\Forms\Form;
use Nette\Forms\Helpers;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
test(function () {
$form = new Form;
$input = $form->addText('text');
$input->addRule(Form::FILLED, null, []);
Assert::same([
[
'op' => ':filled',
'msg' => 'This field is required.',
'arg' => [],
],
], Helpers::exportRules($input->getRules()));
});
test(function () {
$form = new Form;
$input = $form->addText('text');
$input->addRule(Form::EMAIL);
Assert::same([
['op' => ':email', 'msg' => 'Please enter a valid email address.'],
], Helpers::exportRules($input->getRules()));
});
test(function () {
$form = new Form;
$input = $form->addText('text');
$input->setRequired(true);
$input->addRule(Form::EMAIL);
Assert::same([
['op' => ':filled', 'msg' => 'This field is required.'],
['op' => ':email', 'msg' => 'Please enter a valid email address.'],
], Helpers::exportRules($input->getRules()));
});
test(function () {
$form = new Form;
$input = $form->addText('text');
$input->setRequired(false);
$input->addRule(Form::EMAIL);
Assert::same([
['op' => ':email', 'msg' => 'Please enter a valid email address.'],
], Helpers::exportRules($input->getRules()));
});
test(function () {
$form = new Form;
$input1 = $form->addText('text1');
$input2 = $form->addText('text2');
$input2->setRequired(false);
$input2->addConditionOn($input1, Form::EMAIL)
->setRequired(true)
->addRule($form::EMAIL);
$input2->addConditionOn($input1, Form::INTEGER)
->setRequired(false)
->addRule($form::EMAIL);
Assert::same([
[
'op' => ':email',
'rules' => [
['op' => ':filled', 'msg' => 'This field is required.'],
['op' => ':email', 'msg' => 'Please enter a valid email address.'],
],
'control' => 'text1',
],
[
'op' => ':integer',
'rules' => [
['op' => ':email', 'msg' => 'Please enter a valid email address.'],
],
'control' => 'text1',
],
], Helpers::exportRules($input2->getRules()));
});
test(function () {
$form = new Form;
$input = $form->addText('text');
$input->addRule(Form::EMAIL, function (TextInput $input, $arg) {
return $input->getValue() . ' is not valid e-mail address.';
});
Assert::same([], Helpers::exportRules($input->getRules()));
});