From 4021431a0f37c91a30006d0f1db3fd391e86de88 Mon Sep 17 00:00:00 2001 From: Tavo Nieves J Date: Sun, 6 Dec 2020 19:14:52 -0500 Subject: [PATCH 1/2] Added seeFormErrorMessages function --- src/Codeception/Module/Symfony.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 103263b1..7ce4bd3d 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -1346,6 +1346,32 @@ public function seeCurrentActionIs(string $action): void $this->fail("Action '$action' does not exist"); } + /** + * Verifies that a form has fields with errors. + * + * This method calls `seeFormErrorMessage` for each entry in the `$bindings` array. + * + * ``` php + * seeFormErrorMessages(['telephone', 'address']); + * $I->seeFormErrorMessages([ + * 'telephone' => 'Phone number too short', + * 'address' => null + * ]); + * ``` + * @param string[] $bindings + */ + public function seeFormErrorMessages(array $bindings) + { + foreach ($bindings as $key => $value) { + if (is_int($key)) { + $this->seeFormErrorMessage($value); + } else { + $this->seeFormErrorMessage($key, $value); + } + } + } + /** * Verifies that a form field has an error. * You can specify the expected error message as second parameter. From 6f3b18375d5fd5b18b9b7805f5ae1e95622c1214 Mon Sep 17 00:00:00 2001 From: Tavo Nieves J Date: Thu, 17 Dec 2020 20:11:50 -0500 Subject: [PATCH 2/2] Update seeFormErrorMessages function --- src/Codeception/Module/Symfony.php | 48 +++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 7ce4bd3d..2f342598 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -1347,27 +1347,55 @@ public function seeCurrentActionIs(string $action): void } /** - * Verifies that a form has fields with errors. + * Verifies that multiple fields on a form have errors. * - * This method calls `seeFormErrorMessage` for each entry in the `$bindings` array. + * 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 * 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 + * 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 + * seeFormErrorMessages([ - * 'telephone' => 'Phone number too short', - * 'address' => null + * 'telephone' => 'too short', + * 'address' => null, + * 'postal code', * ]); * ``` - * @param string[] $bindings + * + * @param string[] $expectedErrors */ - public function seeFormErrorMessages(array $bindings) + public function seeFormErrorMessages(array $expectedErrors): void { - foreach ($bindings as $key => $value) { - if (is_int($key)) { - $this->seeFormErrorMessage($value); + foreach ($expectedErrors as $field => $message) { + if (is_int($field)) { + $this->seeFormErrorMessage($message); } else { - $this->seeFormErrorMessage($key, $value); + $this->seeFormErrorMessage($field, $message); } } }