From b9b14c2c490e4d8ffc88ce72b0e5838ee2fcf24a Mon Sep 17 00:00:00 2001 From: v1p3r75 Date: Thu, 16 Nov 2023 20:55:45 +0100 Subject: [PATCH] Added Email Rule --- phpunit.xml | 18 ++++++++++ src/LangManager.php | 2 ++ src/Rules/EmailRule.php | 64 +++++++++++++++++++++++++++++++++++ tests/Feature/ExampleTest.php | 8 +++++ tests/Pest.php | 45 ++++++++++++++++++++++++ tests/TestCase.php | 10 ++++++ tests/Unit/ExampleTest.php | 14 ++++++++ 7 files changed, 161 insertions(+) create mode 100644 phpunit.xml create mode 100644 src/Rules/EmailRule.php create mode 100644 tests/Feature/ExampleTest.php create mode 100644 tests/Pest.php create mode 100644 tests/TestCase.php create mode 100644 tests/Unit/ExampleTest.php diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..7d0904f --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests + + + + + ./app + ./src + + + diff --git a/src/LangManager.php b/src/LangManager.php index f7892dc..6131160 100644 --- a/src/LangManager.php +++ b/src/LangManager.php @@ -27,6 +27,7 @@ class LangManager 'validation.string_rule' => "Le champ :attribute doit être une chaîne de caractères.", 'validation.required_rule' => "Le champ :attribute est requis.", 'validation.max_length_rule' => "Le champ :attribute ne doit pas dépasser :max caractères.", + 'validation.email_rule' => "Le champ :attribute doit être un e-mail." ], 'en' => [ // English translations @@ -36,6 +37,7 @@ class LangManager 'validation.string_rule' => "The :attribute field must be a string.", 'validation.required_rule' => "The :attribute field is required.", 'validation.max_length_rule' => "The :attribute field must not exceed :max characters.", + 'validation.email_rule' => "The :attribute field must be a email.", ], ]; diff --git a/src/Rules/EmailRule.php b/src/Rules/EmailRule.php new file mode 100644 index 0000000..3ebb6f1 --- /dev/null +++ b/src/Rules/EmailRule.php @@ -0,0 +1,64 @@ +field = $field; + + // Check if the field is set in the data and not empty. + return (bool)filter_var($value, FILTER_VALIDATE_EMAIL); + } + + /** + * Get the validation error message for the required rule. + * + * @return string Validation error message. + */ + public function message(): string + { + + // Use LangManager to get a translated validation error message. + return LangManager::getTranslation('validation.email_rule', [ + 'attribute' => $this->field, + ]); + } +} \ No newline at end of file diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php new file mode 100644 index 0000000..711d83e --- /dev/null +++ b/tests/Feature/ExampleTest.php @@ -0,0 +1,8 @@ +does(fn () => (new EmailRule([]))->passes('name', 'John', [])) + ->assertTrue(); \ No newline at end of file diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..5949c61 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,45 @@ +in('Feature'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ + +function something() +{ + // .. +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..cfb05b6 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,10 @@ +