forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormAssertionsTrait.php
More file actions
225 lines (201 loc) · 6.91 KB
/
FormAssertionsTrait.php
File metadata and controls
225 lines (201 loc) · 6.91 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use PHPUnit\Framework\Assert;
use Symfony\Component\Form\Extension\DataCollector\FormDataCollector;
use Symfony\Component\VarDumper\Cloner\Data;
use function count;
use function implode;
use function is_array;
use function is_int;
use function is_numeric;
use function is_string;
use function sprintf;
trait FormAssertionsTrait
{
/**
* Asserts that value of the field of the first form matching the given selector does equal the expected value.
*
* ```php
* <?php
* $I->assertFormValue('#loginForm', 'username', 'john_doe');
* ```
*/
public function assertFormValue(string $formSelector, string $fieldName, string $value, string $message = ''): void
{
$node = $this->getClient()->getCrawler()->filter($formSelector);
$this->assertGreaterThan(0, count($node), sprintf('Form "%s" not found.', $formSelector));
$values = $node->form()->getValues();
$this->assertArrayHasKey(
$fieldName,
$values,
$message ?: sprintf('Field "%s" not found in form "%s".', $fieldName, $formSelector)
);
$this->assertSame($value, $values[$fieldName]);
}
/**
* Asserts that the field of the first form matching the given selector does not have a value.
*
* ```php
* <?php
* $I->assertNoFormValue('#registrationForm', 'middle_name');
* ```
*/
public function assertNoFormValue(string $formSelector, string $fieldName, string $message = ''): void
{
$node = $this->getClient()->getCrawler()->filter($formSelector);
$this->assertGreaterThan(0, count($node), sprintf('Form "%s" not found.', $formSelector));
$values = $node->form()->getValues();
$this->assertArrayNotHasKey(
$fieldName,
$values,
$message ?: sprintf('Field "%s" has a value in form "%s".', $fieldName, $formSelector)
);
}
/**
* Verifies that there are no errors bound to the submitted form.
*
* ```php
* <?php
* $I->dontSeeFormErrors();
* ```
*/
public function dontSeeFormErrors(): void
{
$this->assertSame(0, $this->getFormErrorsCount(__FUNCTION__), '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');
* ```
*/
public function seeFormErrorMessage(string $field, ?string $message = null): void
{
$errors = $this->getErrorsForField($field);
if ($errors === []) {
Assert::fail("No form error message for field '{$field}'.");
}
if ($message !== null) {
$this->assertStringContainsString(
$message,
implode("\n", $errors),
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.
*
* Use a list of field names when you only need to assert that each field
* has at least one validation error:
*
* ```php
* <?php
* $I->seeFormErrorMessages(['telephone', 'address']);
* ```
*
* Use an associative array to also verify the error text for one or more
* fields. The expected message is matched as a substring, so partial
* fragments are allowed:
*
* ```php
* <?php
* $I->seeFormErrorMessages([
* 'address' => 'The address is too long',
* 'telephone' => 'too short',
* ]);
* ```
*
* You can mix both styles in the same call. If a field maps to `null`,
* only the existence of an error is checked for that field:
*
* ```php
* <?php
* $I->seeFormErrorMessages([
* 'telephone' => 'too short',
* 'address' => null,
* 'postal code',
* ]);
* ```
*
* @param array<int|string, string|null> $expectedErrors
*/
public function seeFormErrorMessages(array $expectedErrors): void
{
foreach ($expectedErrors as $field => $msg) {
is_int($field) ? $this->seeFormErrorMessage((string) $msg) : $this->seeFormErrorMessage($field, $msg);
}
}
/**
* Verifies that there are one or more errors bound to the submitted form.
*
* ```php
* <?php
* $I->seeFormHasErrors();
* ```
*/
public function seeFormHasErrors(): void
{
$this->assertGreaterThan(0, $this->getFormErrorsCount(__FUNCTION__), 'Expecting that the form has errors, but there were none!');
}
protected function grabFormCollector(string $function): FormDataCollector
{
return $this->grabCollector(DataCollectorName::FORM, $function);
}
private function getFormErrorsCount(string $function): int
{
$collector = $this->grabFormCollector($function);
$rawData = $this->getRawCollectorData($collector);
return isset($rawData['nb_errors']) && is_numeric($rawData['nb_errors']) ? (int) $rawData['nb_errors'] : 0;
}
/**
* @return list<string>
*/
private function getErrorsForField(string $field): array
{
$collector = $this->grabFormCollector('seeFormErrorMessage');
$formsData = $this->getRawCollectorData($collector)['forms'] ?? [];
if (!is_array($formsData)) {
return [];
}
$errorsForField = [];
$fieldFound = false;
foreach ($formsData as $form) {
if (!is_array($form) || !isset($form['children']) || !is_array($form['children'])) {
continue;
}
foreach ($form['children'] as $child) {
if (!is_array($child) || ($child['name'] ?? null) !== $field) {
continue;
}
$fieldFound = true;
if (isset($child['errors']) && is_array($child['errors'])) {
foreach ($child['errors'] as $error) {
if (is_array($error) && isset($error['message']) && is_string($error['message'])) {
$errorsForField[] = $error['message'];
}
}
}
}
}
if (!$fieldFound) {
Assert::fail("The field '{$field}' does not exist in the form.");
}
return $errorsForField;
}
/** @return array<string, mixed> */
private function getRawCollectorData(FormDataCollector $collector): array
{
$data = $collector->getData();
if ($data instanceof Data) {
$data = $data->getValue(true);
}
/** @var array<string, mixed> */
return is_array($data) ? $data : [];
}
}