forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormAssertionsTrait.php
173 lines (153 loc) · 4.87 KB
/
FormAssertionsTrait.php
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
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use Symfony\Component\Form\Extension\DataCollector\FormDataCollector;
use function array_key_exists;
use function in_array;
use function is_int;
use function sprintf;
trait FormAssertionsTrait
{
/**
* Verifies that there are no errors bound to the submitted form.
*
* ```php
* <?php
* $I->dontSeeFormErrors();
* ```
*/
public function dontSeeFormErrors(): void
{
$formCollector = $this->grabFormCollector(__FUNCTION__);
$errors = (int)$formCollector->getData()->offsetGet('nb_errors');
$this->assertSame(
0,
$errors,
'Expecting that the form does not have errors, but there were!'
);
}
/**
* Verifies that a form field has an error.
* You can specify the expected error message as second parameter.
*
* ```php
* <?php
* $I->seeFormErrorMessage('username');
* $I->seeFormErrorMessage('username', 'Username is empty');
* ```
*
* @param string|null $message
*/
public function seeFormErrorMessage(string $field, ?string $message = null): void
{
$formCollector = $this->grabFormCollector(__FUNCTION__);
if (!$forms = $formCollector->getData()->getValue(true)['forms']) {
$this->fail('No forms found on the current page.');
}
$fields = [];
$errors = [];
foreach ($forms as $form) {
foreach ($form['children'] as $child) {
$fieldName = $child['name'];
$fields[] = $fieldName;
if (!array_key_exists('errors', $child)) {
continue;
}
foreach ($child['errors'] as $error) {
$errors[$fieldName] = $error['message'];
}
}
}
if (!in_array($field, $fields)) {
$this->fail("The field '{$field}' does not exist in the form.");
}
if (!array_key_exists($field, $errors)) {
$this->fail("No form error message for field '{$field}'.");
}
if (!$message) {
return;
}
$this->assertStringContainsString(
$message,
$errors[$field],
sprintf(
"There is an error message for the field '%s', but it does not match the expected message.",
$field
)
);
}
/**
* Verifies that multiple fields on a form have errors.
*
* If you only specify the name of the fields, this method will
* verify that the field contains at least one error of any type:
*
* ```php
* <?php
* $I->seeFormErrorMessages(['telephone', 'address']);
* ```
*
* If you want to specify the error messages, you can do so
* by sending an associative array instead, with the key being
* the name of the field and the error message the value.
*
* This method will validate that the expected error message
* is contained in the actual error message, that is,
* you can specify either the entire error message or just a part of it:
*
* ```php
* <?php
* $I->seeFormErrorMessages([
* 'address' => 'The address is too long'
* 'telephone' => 'too short', // the full error message is 'The telephone is too short'
* ]);
* ```
*
* If you don't want to specify the error message for some fields,
* you can pass `null` as value instead of the message string,
* or you can directly omit the value of that field. If that is the case,
* it will be validated that that field has at least one error of any type:
*
* ```php
* <?php
* $I->seeFormErrorMessages([
* 'telephone' => 'too short',
* 'address' => null,
* 'postal code',
* ]);
* ```
*
* @param string[] $expectedErrors
*/
public function seeFormErrorMessages(array $expectedErrors): void
{
foreach ($expectedErrors as $field => $message) {
if (is_int($field)) {
$this->seeFormErrorMessage($message);
} else {
$this->seeFormErrorMessage($field, $message);
}
}
}
/**
* Verifies that there are one or more errors bound to the submitted form.
*
* ```php
* <?php
* $I->seeFormHasErrors();
* ```
*/
public function seeFormHasErrors(): void
{
$formCollector = $this->grabFormCollector(__FUNCTION__);
$this->assertGreaterThan(
0,
$formCollector->getData()->offsetGet('nb_errors'),
'Expecting that the form has errors, but there were none!'
);
}
protected function grabFormCollector(string $function): FormDataCollector
{
return $this->grabCollector('form', $function);
}
}