Skip to content

Commit d802691

Browse files
authored
Added seeFormErrorMessages function (#67)
1 parent 5a8af5f commit d802691

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,60 @@ public function seeCurrentActionIs(string $action): void
13461346
$this->fail("Action '$action' does not exist");
13471347
}
13481348

1349+
/**
1350+
* Verifies that multiple fields on a form have errors.
1351+
*
1352+
* If you only specify the name of the fields, this method will
1353+
* verify that the field contains at least one error of any type:
1354+
*
1355+
* ``` php
1356+
* <?php
1357+
* $I->seeFormErrorMessages(['telephone', 'address']);
1358+
* ```
1359+
*
1360+
* If you want to specify the error messages, you can do so
1361+
* by sending an associative array instead, with the key being
1362+
* the name of the field and the error message the value.
1363+
*
1364+
* This method will validate that the expected error message
1365+
* is contained in the actual error message, that is,
1366+
* you can specify either the entire error message or just a part of it:
1367+
*
1368+
* ``` php
1369+
* <?php
1370+
* $I->seeFormErrorMessages([
1371+
* 'address' => 'The address is too long'
1372+
* 'telephone' => 'too short', // the full error message is 'The telephone is too short'
1373+
* ]);
1374+
* ```
1375+
*
1376+
* If you don't want to specify the error message for some fields,
1377+
* you can pass `null` as value instead of the message string,
1378+
* or you can directly omit the value of that field. If that is the case,
1379+
* it will be validated that that field has at least one error of any type:
1380+
*
1381+
* ``` php
1382+
* <?php
1383+
* $I->seeFormErrorMessages([
1384+
* 'telephone' => 'too short',
1385+
* 'address' => null,
1386+
* 'postal code',
1387+
* ]);
1388+
* ```
1389+
*
1390+
* @param string[] $expectedErrors
1391+
*/
1392+
public function seeFormErrorMessages(array $expectedErrors): void
1393+
{
1394+
foreach ($expectedErrors as $field => $message) {
1395+
if (is_int($field)) {
1396+
$this->seeFormErrorMessage($message);
1397+
} else {
1398+
$this->seeFormErrorMessage($field, $message);
1399+
}
1400+
}
1401+
}
1402+
13491403
/**
13501404
* Verifies that a form field has an error.
13511405
* You can specify the expected error message as second parameter.

0 commit comments

Comments
 (0)