From 162371ca1297da1062c287fe9f92dfc9c037453c Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Fri, 17 Nov 2023 23:10:48 +0100 Subject: [PATCH 01/31] Edit required rule test format to bind from Validator --- tests/Feature/RuleTest.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 11a5d13..21ccb40 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -4,7 +4,6 @@ use BlakvGhost\PHPValidator\Rules\AcceptedIfRule; use BlakvGhost\PHPValidator\Rules\AcceptedRule; use BlakvGhost\PHPValidator\Rules\ActiveURLRule; -use BlakvGhost\PHPValidator\Rules\AlphaNumeric; use BlakvGhost\PHPValidator\Rules\AlphaNumericRule; use BlakvGhost\PHPValidator\Rules\AlphaRule; use BlakvGhost\PHPValidator\Rules\BooleanRule; @@ -28,14 +27,18 @@ use BlakvGhost\PHPValidator\Rules\UpperRule; use BlakvGhost\PHPValidator\Rules\UrlRule; use BlakvGhost\PHPValidator\Rules\ValidIpRule; +use BlakvGhost\PHPValidator\Validator; + it('validates required rule successfully', function () { - $validator = new RequiredRule([]); - expect($validator->passes('field', 'value', ['field' => 'value']))->toBeTrue(); - expect($validator->passes('field', '', []))->toBeFalse(); + $validator = new Validator(['field' => 'value'], ['field' => 'required']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => ''], ['field' => 'required']); + expect($validator->isValid())->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.required_rule', ['attribute' => 'field']) ); }); From cbf60b149ce9a1f6b89cdb3d059cbf734cb87958 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Fri, 17 Nov 2023 23:22:57 +0100 Subject: [PATCH 02/31] Edit max length rule test format to bind from Validator --- tests/Feature/RuleTest.php | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 21ccb40..223d04d 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -34,7 +34,7 @@ $validator = new Validator(['field' => 'value'], ['field' => 'required']); expect($validator->isValid())->toBeTrue(); - + $validator = new Validator(['field' => ''], ['field' => 'required']); expect($validator->isValid())->toBeFalse(); @@ -44,13 +44,18 @@ }); it('validates max length rule successfully', function () { - $validator = new MaxLengthRule([5]); - expect($validator->passes('field', 'value', []))->toBeTrue(); - expect($validator->passes('field', 'toolongvalue', []))->toBeFalse(); + $validator = new Validator(['field' => 'value'], ['field' => 'max_length:5']); + expect($validator->isValid())->toBeTrue(); - expect($validator->message())->toBe( - LangManager::getTranslation('validation.max_length_rule', ['attribute' => 'field', 'max' => 5]) + $validator = new Validator(['field' => 'value'], ['field' => 'max_length:6']); + expect($validator->isValid())->toBeFalse(); + + expect($validator->getErrors()['field'][0])->toBe( + LangManager::getTranslation('validation.max_length_rule', [ + 'attribute' => 'field', + 'max' => 5, + ]) ); }); @@ -59,7 +64,7 @@ expect($validator->passes('email', 'test@example.com', []))->toBeTrue(); expect($validator->passes('email', 'invalid-email', []))->toBeFalse(); - + expect($validator->message())->toBe( LangManager::getTranslation('validation.email_rule', ['attribute' => 'email']) ); @@ -67,10 +72,10 @@ it('validates string rule successfully', function () { $validator = new StringRule([]); - + expect($validator->passes('field', 'value', []))->toBeTrue(); expect($validator->passes('field', 5, []))->toBeFalse(); - + expect($validator->message())->toBe( LangManager::getTranslation('validation.string_rule', ['attribute' => 'field']) ); @@ -438,4 +443,4 @@ 'value' => 512, ]) ); -}); \ No newline at end of file +}); From dd27d4427b327adfbd3505596ab56d7b6c54a99b Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 00:45:08 +0100 Subject: [PATCH 03/31] Fix singleton pattern persistance issues for testing --- src/Validator.php | 56 +++++++++++++++++++++++++++----------- tests/Feature/RuleTest.php | 10 +++---- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/Validator.php b/src/Validator.php index 49e12ff..ffe1cff 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -16,7 +16,11 @@ class Validator { - protected static $errors = []; + // protected static $instance; + + // protected static $langManagerInstance; + + private $errors = []; /** * Constructor of the Validator class. @@ -24,12 +28,31 @@ class Validator * @param array $data Data to be validated. * @param array $rules Validation rules to apply. */ - public function __construct(private array $data, protected array $rules) + public function __construct(private array $data, private array $rules) { $this->validateConstructorInputs(); $this->validate(); } + // public static function getInstance(): Validator + // { + // if (!self::$instance) { + // self::$instance = new Validator(self::$data, self::$rules); + // } + + // return self::$instance; + // } + + // public static function getLangManagerInstance(): LangManager + // { + // if (!self::$langManagerInstance) { + // self::$langManagerInstance = new LangManager(); + // } + + // return self::$langManagerInstance; + // } + + /** * Parse a rule to extract the name and any parameters. * @@ -63,7 +86,7 @@ protected function resolveRuleClass($ruleName): string $className = implode('', array_map('ucfirst', $ruleParts)) . 'Rule'; $fullClassName = "BlakvGhost\\PHPValidator\\Rules\\$className"; - + if (!class_exists($fullClassName)) { $translatedMessage = LangManager::getTranslation('validation.rule_not_found', [ @@ -84,7 +107,7 @@ protected function resolveRuleClass($ruleName): string */ protected function addError($field, $message) { - self::$errors[$field][] = $message; + $this->errors[$field][] = $message; } /** @@ -103,15 +126,15 @@ protected function validate() foreach ($rulesArray as $rule) { if (is_a($rule, RuleInterface::class, true)) { - return $this->checkPasses($rule, $field); - } + $this->checkPasses($rule, $field); + } else { + list($ruleName, $parameters) = $this->parseRule($rule); + $ruleClass = $this->resolveRuleClass($ruleName); - list($ruleName, $parameters) = $this->parseRule($rule); - $ruleClass = $this->resolveRuleClass($ruleName); + $validator = new $ruleClass($parameters); - $validator = new $ruleClass($parameters); - - $this->checkPasses($validator, $field); + $this->checkPasses($validator, $field); + } } } } @@ -124,11 +147,12 @@ protected function validate() */ protected function checkPasses(mixed $validator, string $field) { - if (!$validator->passes($field, $this->data[$field] ?? null, $this->data)) { + if (isset($this->data[$field]) && !$validator->passes($field, $this->data[$field], $this->data)) { $this->addError($field, $validator->message()); } } + /** * Validate constructor inputs to ensure required data and rules are provided. * @@ -150,9 +174,9 @@ protected function validateConstructorInputs() * * @return array List of errors. */ - public static function getErrors(): array + public function getErrors(): array { - return self::$errors; + return $this->errors; } /** @@ -160,8 +184,8 @@ public static function getErrors(): array * * @return bool True if validation is successful, otherwise false. */ - public static function isValid(): bool + public function isValid(): bool { - return count(self::$errors) < 1; + return count($this->errors) < 1; } } diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 223d04d..f3b042d 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -45,16 +45,16 @@ it('validates max length rule successfully', function () { - $validator = new Validator(['field' => 'value'], ['field' => 'max_length:5']); + $validator = new Validator(['username' => 'value'], ['username' => 'max_length:5']); expect($validator->isValid())->toBeTrue(); - $validator = new Validator(['field' => 'value'], ['field' => 'max_length:6']); + $validator = new Validator(['username' => 'value'], ['username' => 'max_length:6']); expect($validator->isValid())->toBeFalse(); - expect($validator->getErrors()['field'][0])->toBe( + expect($validator->getErrors()['username'][0])->toBe( LangManager::getTranslation('validation.max_length_rule', [ - 'attribute' => 'field', - 'max' => 5, + 'attribute' => 'username', + 'max' => 6, ]) ); }); From 286a43f103c7b32b0fcc3f66965e4e6e9bfc79aa Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:08:25 +0100 Subject: [PATCH 04/31] Edit max length rule test format to bind from Validator --- tests/Feature/RuleTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index f3b042d..c55459c 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -48,13 +48,13 @@ $validator = new Validator(['username' => 'value'], ['username' => 'max_length:5']); expect($validator->isValid())->toBeTrue(); - $validator = new Validator(['username' => 'value'], ['username' => 'max_length:6']); + $validator = new Validator(['username' => 'value_long'], ['username' => 'max_length:5']); expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['username'][0])->toBe( LangManager::getTranslation('validation.max_length_rule', [ 'attribute' => 'username', - 'max' => 6, + 'max' => 5, ]) ); }); From 5084d35f750f03372cf55273ee0824ec2f92912f Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:11:48 +0100 Subject: [PATCH 05/31] Edit email rule test format to bind from Validator --- tests/Feature/RuleTest.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index c55459c..015fc5d 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -8,7 +8,6 @@ use BlakvGhost\PHPValidator\Rules\AlphaRule; use BlakvGhost\PHPValidator\Rules\BooleanRule; use BlakvGhost\PHPValidator\Rules\ConfirmedRule; -use BlakvGhost\PHPValidator\Rules\EmailRule; use BlakvGhost\PHPValidator\Rules\FileRule; use BlakvGhost\PHPValidator\Rules\InRule; use BlakvGhost\PHPValidator\Rules\JsonRule; @@ -16,8 +15,6 @@ use BlakvGhost\PHPValidator\Rules\RequiredWithRule; use BlakvGhost\PHPValidator\Rules\SizeRule; use BlakvGhost\PHPValidator\Rules\StringRule; -use BlakvGhost\PHPValidator\Rules\RequiredRule; -use BlakvGhost\PHPValidator\Rules\MaxLengthRule; use BlakvGhost\PHPValidator\Rules\MinLengthRule; use BlakvGhost\PHPValidator\Rules\NotInRule; use BlakvGhost\PHPValidator\Rules\NullableRule; @@ -60,12 +57,14 @@ }); it('validates email rule successfully', function () { - $validator = new EmailRule([]); - expect($validator->passes('email', 'test@example.com', []))->toBeTrue(); - expect($validator->passes('email', 'invalid-email', []))->toBeFalse(); + $validator = new Validator(['email' => 'test@example.com'], ['email' => 'email']); + expect($validator->isValid())->toBeTrue(); - expect($validator->message())->toBe( + $validator = new Validator(['email' => 'invalid-email'], ['email' => 'email']); + expect($validator->isValid())->toBeFalse(); + + expect($validator->getErrors()['email'][0])->toBe( LangManager::getTranslation('validation.email_rule', ['attribute' => 'email']) ); }); From e3ae859a43a7cf0205eece136be4a340be4475cf Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:14:03 +0100 Subject: [PATCH 06/31] Edit string rule test format to bind from Validator --- tests/Feature/RuleTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 015fc5d..400f8ad 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -70,12 +70,14 @@ }); it('validates string rule successfully', function () { - $validator = new StringRule([]); - expect($validator->passes('field', 'value', []))->toBeTrue(); - expect($validator->passes('field', 5, []))->toBeFalse(); + $validator = new Validator(['field' => 'value'], ['field' => 'string']); + expect($validator->isValid())->toBeTrue(); - expect($validator->message())->toBe( + $validator = new Validator(['field' => 5], ['field' => 'string']); + expect($validator->isValid())->toBeFalse(); + + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.string_rule', ['attribute' => 'field']) ); }); From 1030ed68ce5b70e9ef569405a00e2d5b348163c6 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:16:29 +0100 Subject: [PATCH 07/31] Edit min length rule test format to bind from Validator --- tests/Feature/RuleTest.php | 58 ++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 400f8ad..cf87f55 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -83,12 +83,14 @@ }); it('validates min length rule successfully', function () { - $validator = new MinLengthRule([10]); - expect($validator->passes('field', 'toolongvalue', []))->toBeTrue(); - expect($validator->passes('field', 'less', []))->toBeFalse(); + $validator = new Validator(['field' => 'toolongvalue'], ['field' => 'min_length:10']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'less'], ['field' => 'min_length:10']); + expect($validator->isValid())->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.min_length_rule', ['attribute' => 'field', 'min' => 10]) ); }); @@ -99,7 +101,7 @@ expect($validator->passes('field', 'Alphabetic', []))->toBeTrue(); expect($validator->passes('field', 'Alpha123', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.alpha_rule', ['attribute' => 'field']) ); }); @@ -110,7 +112,7 @@ expect($validator->passes('field', 'some_value', ['other_field' => true]))->toBeTrue(); expect($validator->passes('field', 'some_value', ['other_field' => false]))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.accepted_if', ['attribute' => 'field', 'other' => 'other_field']) ); }); @@ -124,7 +126,7 @@ expect($validator->passes('field', 'yes', []))->toBeTrue(); expect($validator->passes('field', 'invalid_value', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.accepted', ['attribute' => 'field']) ); }); @@ -136,7 +138,7 @@ expect($validator->passes('field', 'value', ['other_field' => 'different_value']))->toBeFalse(); expect($validator->passes('field', 'value', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.same_rule', [ 'attribute' => 'field', 'otherAttribute' => 'other_field', @@ -153,7 +155,7 @@ expect($validator->passes('password', 'UPPERCASE1', []))->toBeFalse(); expect($validator->passes('password', 'NoDigit', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.password_rule', [ 'attribute' => 'password', ]) @@ -168,7 +170,7 @@ expect($validator->passes('numericField', 'NotNumeric', []))->toBeFalse(); expect($validator->passes('numericField', null, []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.numeric_rule', [ 'attribute' => 'numericField', ]) @@ -181,7 +183,7 @@ expect($validator->passes('nullableField', null, []))->toBeTrue(); expect($validator->passes('nullableField', 'NotNull', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.nullable_rule', [ 'attribute' => 'nullableField', ]) @@ -195,7 +197,7 @@ expect($validator->passes('field', 'value2', []))->toBeTrue(); expect($validator->passes('field', 'invalidValue', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.in_rule', [ 'attribute' => 'field', 'values' => implode(', ', $validValues), @@ -210,7 +212,7 @@ expect($validator->passes('field', 'other_value', []))->toBeTrue(); expect($validator->passes('field', 'value1', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.not_in_rule', [ 'attribute' => 'field', 'values' => implode(', ', $values), @@ -242,7 +244,7 @@ ]; expect($validator->passes('field', 'value', $data))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.confirmed_rule', [ 'attribute' => 'field', 'confirmedAttribute' => $confirmationFieldName, @@ -264,7 +266,7 @@ // When the URL is not valid, the validation should fail. expect($validator->passes('field', 'invalid-url', $data))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.active_url', [ 'attribute' => 'field', ]) @@ -277,7 +279,7 @@ expect($validator->passes('field', 'lowercase', []))->toBeTrue(); expect($validator->passes('field', 'UPPERCASE', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.lowercase_rule', [ 'attribute' => 'field', ]) @@ -290,7 +292,7 @@ expect($validator->passes('field', 'lowercase', []))->toBeFalse(); expect($validator->passes('field', 'UPPERCASE', []))->toBeTrue(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.uppercase_rule', [ 'attribute' => 'field', ]) @@ -303,7 +305,7 @@ expect($validator->passes('field', __FILE__, []))->toBeTrue(); expect($validator->passes('field', 'nonexistentfile.txt', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.file_rule', [ 'attribute' => 'field', ]) @@ -316,7 +318,7 @@ expect($validator->passes('field', 'alpha2324', []))->toBeTrue(); expect($validator->passes('field', 's$sdfde$*', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.alpha_numeric', [ 'attribute' => 'field', ]) @@ -329,7 +331,7 @@ expect($validator->passes('field', 'value', ['other_field' => 'value2']))->toBeTrue(); expect($validator->passes('field', 'value', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.required_with', [ 'attribute' => 'field', 'value' => 'other_field', @@ -343,7 +345,7 @@ expect($validator->passes('field', false, []))->toBeTrue(); expect($validator->passes('field', 'string', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.boolean', [ 'attribute' => 'field', ]) @@ -357,7 +359,7 @@ expect($validator->passes('field', '{"name":"vishal", "email": "abc@gmail.com"}', []))->toBeTrue(); expect($validator->passes('field', '{name:vishal, email: abc@gmail.com}', []))->toBeFalse(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.json', [ 'attribute' => 'field', ]) @@ -370,7 +372,7 @@ expect($validator->passes('field', "invalid_url", []))->toBeFalse(); expect($validator->passes('field', 'http://google.com', []))->toBeTrue(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.url', [ 'attribute' => 'field', ]) @@ -383,7 +385,7 @@ expect($validator->passes('field', "3853598", []))->toBeFalse(); expect($validator->passes('field', '127.0.0.1', []))->toBeTrue(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.valid_ip', [ 'attribute' => 'field', ]) @@ -396,7 +398,7 @@ expect($validator->passes('field', "azerty", []))->toBeFalse(); expect($validator->passes('field', 'azer', []))->toBeTrue(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ 'attribute' => 'field', 'value' => 4, @@ -410,7 +412,7 @@ expect($validator->passes('field', 6, []))->toBeFalse(); expect($validator->passes('field', 3, []))->toBeTrue(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ 'attribute' => 'field', 'value' => 3, @@ -424,7 +426,7 @@ expect($validator->passes('field', ['key1', 'key2', 'key3'], []))->toBeFalse(); expect($validator->passes('field', ['key1', 'key2'], []))->toBeTrue(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ 'attribute' => 'field', 'value' => 2, @@ -438,7 +440,7 @@ expect($validator->passes('field', __FILE__, []))->toBeFalse(); // expect($validator->passes('field', __FILE__, []))->toBeTrue(); - expect($validator->message())->toBe( + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ 'attribute' => 'field', 'value' => 512, From cf814d1d08d5b9a517c140d2b0f2dc230840c9b7 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:19:23 +0100 Subject: [PATCH 08/31] Edit alpha length rule test format to bind from Validator --- tests/Feature/RuleTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index cf87f55..9ff82c2 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -96,10 +96,12 @@ }); it('validates alpha rule successfully', function () { - $validator = new AlphaRule([]); - expect($validator->passes('field', 'Alphabetic', []))->toBeTrue(); - expect($validator->passes('field', 'Alpha123', []))->toBeFalse(); + $validator = new Validator(['field' => 'Alphabetic'], ['field' => 'alpha']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'Alpha123'], ['field' => 'alpha']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.alpha_rule', ['attribute' => 'field']) From fb04daf5ca39f6691e0a2b9a1e9f026c04f0889b Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:29:02 +0100 Subject: [PATCH 09/31] Edit accepted if rule test format to bind from Validator --- tests/Feature/RuleTest.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 9ff82c2..75cde06 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -109,10 +109,12 @@ }); it('validates accepted if rule successfully', function () { - $validator = new AcceptedIfRule(['other_field']); + + $validator = new Validator(['field' => 'some_field'], ['other_field' => 'accepted_if:true']); + expect($validator->isValid())->toBeTrue(); - expect($validator->passes('field', 'some_value', ['other_field' => true]))->toBeTrue(); - expect($validator->passes('field', 'some_value', ['other_field' => false]))->toBeFalse(); + $validator = new Validator(['field' => 'some_field'], ['other_field' => 'accepted_if:false']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.accepted_if', ['attribute' => 'field', 'other' => 'other_field']) @@ -120,13 +122,21 @@ }); it('validates accepted rule successfully', function () { - $validator = new AcceptedRule([]); - expect($validator->passes('field', '1', []))->toBeTrue(); - expect($validator->passes('field', 'true', []))->toBeTrue(); - expect($validator->passes('field', 'on', []))->toBeTrue(); - expect($validator->passes('field', 'yes', []))->toBeTrue(); - expect($validator->passes('field', 'invalid_value', []))->toBeFalse(); + $validator = new Validator(['field' => '1'], ['field' => 'accepted']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'true'], ['field' => 'accepted']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'on'], ['field' => 'accepted']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'yes'], ['field' => 'accepted']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'invalid_value'], ['field' => 'accepted']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.accepted', ['attribute' => 'field']) From 0cc4ff67bf9911accd0728ffe9e36a1139419cba Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:34:50 +0100 Subject: [PATCH 10/31] Edit same rule test format to bind from Validator --- tests/Feature/RuleTest.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 75cde06..a0816b3 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -109,7 +109,7 @@ }); it('validates accepted if rule successfully', function () { - + $validator = new Validator(['field' => 'some_field'], ['other_field' => 'accepted_if:true']); expect($validator->isValid())->toBeTrue(); @@ -125,16 +125,16 @@ $validator = new Validator(['field' => '1'], ['field' => 'accepted']); expect($validator->isValid())->toBeTrue(); - + $validator = new Validator(['field' => 'true'], ['field' => 'accepted']); expect($validator->isValid())->toBeTrue(); - + $validator = new Validator(['field' => 'on'], ['field' => 'accepted']); expect($validator->isValid())->toBeTrue(); - + $validator = new Validator(['field' => 'yes'], ['field' => 'accepted']); - expect($validator->isValid())->toBeTrue(); - + expect($validator->isValid())->toBeTrue(); + $validator = new Validator(['field' => 'invalid_value'], ['field' => 'accepted']); expect($validator->isValid())->toBeFalse(); @@ -144,11 +144,15 @@ }); it('validates same rule successfully', function () { - $validator = new SameRule(['other_field']); - expect($validator->passes('field', 'value', ['other_field' => 'value']))->toBeTrue(); - expect($validator->passes('field', 'value', ['other_field' => 'different_value']))->toBeFalse(); - expect($validator->passes('field', 'value', []))->toBeFalse(); + $validator = new Validator(['field' => 'value', 'other_field' => 'value'], ['field' => 'same:other_field']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'value', 'other_field' => 'different_value'], ['field' => 'same:other_field']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['field' => 'value'], ['field' => 'same:other_field']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.same_rule', [ From 234a041836a225ab64064932a5ecb8415ce66994 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:37:37 +0100 Subject: [PATCH 11/31] Fix accepted if rule test format to bind from Validator --- tests/Feature/RuleTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index a0816b3..ffde7bb 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -110,10 +110,10 @@ it('validates accepted if rule successfully', function () { - $validator = new Validator(['field' => 'some_field'], ['other_field' => 'accepted_if:true']); + $validator = new Validator(['field' => 'some_field', 'other_field' => true], ['field' => 'accepted_if:other_field']); expect($validator->isValid())->toBeTrue(); - $validator = new Validator(['field' => 'some_field'], ['other_field' => 'accepted_if:false']); + $validator = new Validator(['field' => 'some_field', 'other_field' => false], ['other_field' => 'accepted_if:other_field']); expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( From c9eb13d3c82ccc78b18eb929e79700093d15a699 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:44:34 +0100 Subject: [PATCH 12/31] Edit password rule test format to bind from Validator --- tests/Feature/RuleTest.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index ffde7bb..6108eab 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -163,13 +163,24 @@ }); it('validates password rule successfully', function () { - $validator = new PasswordRule([8]); - expect($validator->passes('password', 'StrongPwd1', []))->toBeTrue(); - expect($validator->passes('password', 'Short1', []))->toBeFalse(); - expect($validator->passes('password', 'lowercase1', []))->toBeFalse(); - expect($validator->passes('password', 'UPPERCASE1', []))->toBeFalse(); - expect($validator->passes('password', 'NoDigit', []))->toBeFalse(); + $validator = new Validator(['password' => 'StrongPwd1'], ['password' => 'password:8']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['password' => 'StrongPwd1'], ['password' => 'password']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['password' => 'Short1'], ['password' => 'password']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['password' => 'lowercase1'], ['password' => 'password']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['password' => 'UPPERCASE1'], ['password' => 'password']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['password' => 'NoDigit'], ['password' => 'password']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.password_rule', [ @@ -179,6 +190,13 @@ }); it('validates numeric rule successfully', function () { + + $validator = new Validator(['password' => 'StrongPwd1'], ['password' => 'password']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['password' => 'Short1'], ['password' => 'password']); + expect($validator->isValid())->toBeFalse(); + $validator = new NumericRule([]); expect($validator->passes('numericField', 123, []))->toBeTrue(); From 573061ca31b3ccff68cb24e228bd0edeef9ff78d Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:47:18 +0100 Subject: [PATCH 13/31] Edit numeric rule test format to bind from Validator --- tests/Feature/RuleTest.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 6108eab..73781af 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -191,18 +191,17 @@ it('validates numeric rule successfully', function () { - $validator = new Validator(['password' => 'StrongPwd1'], ['password' => 'password']); + $validator = new Validator(['numericField' => 123], ['numericField' => 'numeric']); expect($validator->isValid())->toBeTrue(); - $validator = new Validator(['password' => 'Short1'], ['password' => 'password']); + $validator = new Validator(['numericField' => '456'], ['numericField' => 'numeric']); expect($validator->isValid())->toBeFalse(); - $validator = new NumericRule([]); - - expect($validator->passes('numericField', 123, []))->toBeTrue(); - expect($validator->passes('numericField', '456', []))->toBeTrue(); - expect($validator->passes('numericField', 'NotNumeric', []))->toBeFalse(); - expect($validator->passes('numericField', null, []))->toBeFalse(); + $validator = new Validator(['numericField' => 'NotNumeric'], ['numericField' => 'numeric']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['numericField' => null], ['numericField' => 'numeric']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.numeric_rule', [ From d96e0eda3df4ee838c27e280b143c82ad29666dc Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:48:55 +0100 Subject: [PATCH 14/31] Edit nullable rule test format to bind from Validator --- tests/Feature/RuleTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 73781af..cc41aca 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -211,10 +211,12 @@ }); it('validates nullable rule successfully', function () { - $validator = new NullableRule([]); - expect($validator->passes('nullableField', null, []))->toBeTrue(); - expect($validator->passes('nullableField', 'NotNull', []))->toBeFalse(); + $validator = new Validator(['nullableField' => null], ['nullableField' => 'nullable']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['nullableField' => 'NotNull'], ['nullableField' => 'nullable']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.nullable_rule', [ From 8593bd00fb577efc1ea480e6e2e63d29492c1af8 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:54:29 +0100 Subject: [PATCH 15/31] Edit In rule test format to bind from Validator --- tests/Feature/RuleTest.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index cc41aca..3078501 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -226,16 +226,18 @@ }); it('validates in rule successfully', function () { - $validValues = ['value1', 'value2', 'value3']; - $validator = new InRule($validValues); + $validValues = 'value1,value2,value3'; - expect($validator->passes('field', 'value2', []))->toBeTrue(); - expect($validator->passes('field', 'invalidValue', []))->toBeFalse(); + $validator = new Validator(['field' => 'value2'], ['field' => 'in:' . $validValues]); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'invalidValue'], ['field' => 'in:' . $validValues]); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.in_rule', [ 'attribute' => 'field', - 'values' => implode(', ', $validValues), + 'values' => $validValues, ]) ); }); From faf22d516fbc0dff41e66c2e1c113b54e70ac75f Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 01:56:08 +0100 Subject: [PATCH 16/31] Edit NotIn rule test format to bind from Validator --- tests/Feature/RuleTest.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 3078501..9e6e48b 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -243,16 +243,19 @@ }); it('validates not in rule successfully', function () { - $values = ['value1', 'value2', 'value3']; - $validator = new NotInRule($values); - expect($validator->passes('field', 'other_value', []))->toBeTrue(); - expect($validator->passes('field', 'value1', []))->toBeFalse(); + $values = 'value1,value2,value3'; + + $validator = new Validator(['field' => 'other_value'], ['field' => 'not_in:' . $values]); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'value1'], ['field' => 'not_in:' . $values]); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.not_in_rule', [ 'attribute' => 'field', - 'values' => implode(', ', $values), + 'values' => $values, ]) ); }); From 1d09b1b6e5f789f49408249a94e2651737532f90 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 02:02:07 +0100 Subject: [PATCH 17/31] Edit Confirmed rule test format to bind from Validator --- tests/Feature/RuleTest.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 9e6e48b..5f95cf4 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -262,27 +262,26 @@ it('validates confirmed rule successfully', function () { $confirmationFieldName = 'confirmation_field'; - $validator = new ConfirmedRule([$confirmationFieldName]); - // When the confirmation field is present and its value matches the field's value, the validation should pass. $data = [ 'field' => 'value', $confirmationFieldName => 'value', ]; - expect($validator->passes('field', 'value', $data))->toBeTrue(); + $validator = new Validator($data, ['field' => 'confirmed:' . $confirmationFieldName]); + expect($validator->isValid())->toBeTrue(); - // When the confirmation field is present but its value doesn't match the field's value, the validation should fail. $data = [ 'field' => 'value1', $confirmationFieldName => 'value2', ]; - expect($validator->passes('field', 'value1', $data))->toBeFalse(); + $validator = new Validator($data, ['field' => 'confirmed:' . $confirmationFieldName]); + expect($validator->isValid())->toBeFalse(); - // When the confirmation field is not present, the validation should fail. $data = [ 'field' => 'value', ]; - expect($validator->passes('field', 'value', $data))->toBeFalse(); + $validator = new Validator($data, ['field' => 'confirmed:' . $confirmationFieldName]); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.confirmed_rule', [ From aa82de873fc1a9fc7b9f0e4f79d4a5a8fef94c79 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 02:05:24 +0100 Subject: [PATCH 18/31] Edit ActiveUrl rule test format to bind from Validator --- tests/Feature/RuleTest.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 5f95cf4..8047287 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -292,18 +292,15 @@ }); it('validates active URL rule successfully', function () { - $validator = new ActiveURLRule([]); - $data = []; - - // When the URL is valid and has an active DNS record, the validation should pass. - expect($validator->passes('field', 'https://example.com', $data))->toBeTrue(); + $validator = new Validator(['field' => 'https://example.com'], ['field' => 'active_url']); + expect($validator->isValid())->toBeTrue(); - // When the URL is valid but doesn't have an active DNS record, the validation should fail. - expect($validator->passes('field', 'https://nonexistent.example.com', $data))->toBeFalse(); + $validator = new Validator(['field' => 'https://nonexistent.example.com'], ['field' => 'active_url']); + expect($validator->isValid())->toBeFalse(); - // When the URL is not valid, the validation should fail. - expect($validator->passes('field', 'invalid-url', $data))->toBeFalse(); + $validator = new Validator(['field' => 'invalid-url'], ['field' => 'active_url']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.active_url', [ From afce8b92a6c3e89ef5afc52f648412a01e6e68ae Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 02:06:33 +0100 Subject: [PATCH 19/31] Edit LowerCase rule test format to bind from Validator --- tests/Feature/RuleTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 8047287..7d6359c 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -310,10 +310,12 @@ }); it('validates lowercase rule successfully', function () { - $validator = new LowerRule([]); - expect($validator->passes('field', 'lowercase', []))->toBeTrue(); - expect($validator->passes('field', 'UPPERCASE', []))->toBeFalse(); + $validator = new Validator(['field' => 'lowercase'], ['field' => 'lower']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'UPPERCASE'], ['field' => 'lower']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.lowercase_rule', [ From 25c1140d433417ac151a7dafb18a01234ed9419b Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 02:11:03 +0100 Subject: [PATCH 20/31] Edit File and alpha_numeric rule test format to bind from Validator --- tests/Feature/RuleTest.php | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 7d6359c..d8e2df4 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -325,10 +325,12 @@ }); it('validates uppercase rule successfully', function () { - $validator = new UpperRule([]); - expect($validator->passes('field', 'lowercase', []))->toBeFalse(); - expect($validator->passes('field', 'UPPERCASE', []))->toBeTrue(); + $validator = new Validator(['field' => 'lowercase'], ['field' => 'upper']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['field' => 'UPPERCASE'], ['field' => 'upper']); + expect($validator->isValid())->toBeTrue(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.uppercase_rule', [ @@ -338,10 +340,12 @@ }); it('validates file rule successfully', function () { - $validator = new FileRule([]); - expect($validator->passes('field', __FILE__, []))->toBeTrue(); - expect($validator->passes('field', 'nonexistentfile.txt', []))->toBeFalse(); + $validator = new Validator(['field' => __FILE__], ['field' => 'file']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'nonexistentfile.txt'], ['field' => 'file']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.file_rule', [ @@ -351,10 +355,12 @@ }); it('validates alpha_numeric rule successfully', function () { - $validator = new AlphaNumericRule([]); - expect($validator->passes('field', 'alpha2324', []))->toBeTrue(); - expect($validator->passes('field', 's$sdfde$*', []))->toBeFalse(); + $validator = new Validator(['field' => 'alpha2324'], ['field' => 'alpha_numeric']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 's$sdfde$*'], ['field' => 'alpha_numeric']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.alpha_numeric', [ @@ -364,6 +370,7 @@ }); it('validates required_with rule successfully', function () { + $validator = new RequiredWithRule(['other_field']); expect($validator->passes('field', 'value', ['other_field' => 'value2']))->toBeTrue(); From d0e70ed45968d68a03c049cecc09436b4330d8c8 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 02:18:27 +0100 Subject: [PATCH 21/31] Edit json rule test format to bind from Validator --- tests/Feature/RuleTest.php | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index d8e2df4..49bdfd7 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -370,11 +370,12 @@ }); it('validates required_with rule successfully', function () { - - $validator = new RequiredWithRule(['other_field']); - expect($validator->passes('field', 'value', ['other_field' => 'value2']))->toBeTrue(); - expect($validator->passes('field', 'value', []))->toBeFalse(); + $validator = new Validator(['field' => 'value', 'other_field' => 'value2'], ['field' => 'required_with:other_field']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'value'], ['field' => 'required_with:other_field']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.required_with', [ @@ -385,10 +386,12 @@ }); it('validates boolean rule successfully', function () { - $validator = new BooleanRule([]); - expect($validator->passes('field', false, []))->toBeTrue(); - expect($validator->passes('field', 'string', []))->toBeFalse(); + $validator = new Validator(['field' => false], ['field' => 'boolean']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'string'], ['field' => 'boolean']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.boolean', [ @@ -398,11 +401,15 @@ }); it('validates json rule successfully', function () { - $validator = new JsonRule([]); - expect($validator->passes('field', "", []))->toBeFalse(); - expect($validator->passes('field', '{"name":"vishal", "email": "abc@gmail.com"}', []))->toBeTrue(); - expect($validator->passes('field', '{name:vishal, email: abc@gmail.com}', []))->toBeFalse(); + $validator = new Validator(['field' => ""], ['field' => 'json']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['field' => '{"name":"vishal", "email": "abc@gmail.com"}'], ['field' => 'json']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => '{name:vishal, email: abc@gmail.com}'], ['field' => 'json']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.json', [ From 956dbd07aa3245bae75f7bacafcf9269a7387392 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 02:19:59 +0100 Subject: [PATCH 22/31] Edit url rule test format to bind from Validator --- tests/Feature/RuleTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 49bdfd7..b924422 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -419,10 +419,12 @@ }); it('validates url rule successfully', function () { - $validator = new UrlRule([]); - expect($validator->passes('field', "invalid_url", []))->toBeFalse(); - expect($validator->passes('field', 'http://google.com', []))->toBeTrue(); + $validator = new Validator(['field' => 'invalid_url'], ['field' => 'url']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['field' => 'http://google.com'], ['field' => 'url']); + expect($validator->isValid())->toBeTrue(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.url', [ From cdef811ade060f48a8efde95675dfe64d29f6de2 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 02:30:26 +0100 Subject: [PATCH 23/31] Edit size rule test format to bind from Validator --- tests/Feature/RuleTest.php | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index b924422..7cd6b93 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -434,10 +434,12 @@ }); it('validates ip rule successfully', function () { - $validator = new ValidIpRule([]); - expect($validator->passes('field', "3853598", []))->toBeFalse(); - expect($validator->passes('field', '127.0.0.1', []))->toBeTrue(); + $validator = new Validator(['field' => '3853598'], ['field' => 'valid_ip']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['field' => '127.0.0.1'], ['field' => 'valid_ip']); + expect($validator->isValid())->toBeTrue(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.valid_ip', [ @@ -447,10 +449,12 @@ }); it('validates size rule (string) successfully', function () { - $validator = new SizeRule([4]); - expect($validator->passes('field', "azerty", []))->toBeFalse(); - expect($validator->passes('field', 'azer', []))->toBeTrue(); + $validator = new Validator(['field' => 'azerty'], ['field' => 'size:4']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['field' => 'azer'], ['field' => 'size:4']); + expect($validator->isValid())->toBeTrue(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ @@ -461,10 +465,12 @@ }); it('validates size rule (integer) successfully', function () { - $validator = new SizeRule([3]); - expect($validator->passes('field', 6, []))->toBeFalse(); - expect($validator->passes('field', 3, []))->toBeTrue(); + $validator = new Validator(['field' => 6], ['field' => 'size:3']); + expect($validator->isValid())->toBeFalse(); + + $validator = new Validator(['field' => 3], ['field' => 'size:3']); + expect($validator->isValid())->toBeTrue(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ @@ -475,10 +481,12 @@ }); it('validates size rule (array) successfully', function () { - $validator = new SizeRule([2]); - expect($validator->passes('field', ['key1', 'key2', 'key3'], []))->toBeFalse(); - expect($validator->passes('field', ['key1', 'key2'], []))->toBeTrue(); + $validator = new Validator(['field' => ['key1', 'key2']], ['field' => 'size:2']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => ['key1', 'key2', 'key3']], ['field' => 'size:2']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ @@ -489,10 +497,9 @@ }); it('validates size rule (file) successfully', function () { - $validator = new SizeRule([512]); - expect($validator->passes('field', __FILE__, []))->toBeFalse(); - // expect($validator->passes('field', __FILE__, []))->toBeTrue(); + $validator = new Validator(['field' => __FILE__], ['field' => 'size:512']); + expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ From fef53438c7145e2add2704ea834412855eb293a1 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 02:46:59 +0100 Subject: [PATCH 24/31] Fix "asserting that two strings are identical" bug on tests --- src/LangManager.php | 2 ++ tests/Feature/RuleTest.php | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/LangManager.php b/src/LangManager.php index 3cd2207..837de99 100644 --- a/src/LangManager.php +++ b/src/LangManager.php @@ -31,6 +31,7 @@ class LangManager 'validation.min_length_rule' => "Le champ :attribute doit dépasser :min caractères.", 'validation.email_rule' => "Le champ :attribute doit être un e-mail.", 'validation.accepted' => "Le champ :attribute doit être accepté.", + 'validation.accepted_if' => "Le champ :attribute doit être accepté uniquement si :other existe et vrai.", 'validation.same_rule' => "Le champ :attribute doit être identique au champ :otherAttribute.", 'validation.password_rule' => "Le champ :attribute doit répondre aux critères de mot de passe.", 'validation.numeric_rule' => "Le champ :attribute doit être numérique.", @@ -61,6 +62,7 @@ class LangManager 'validation.min_length_rule' => "The :attribute field must exceed :min characters.", 'validation.email_rule' => "The :attribute field must be an email.", 'validation.accepted' => "The :attribute field must be accepted.", + 'validation.accepted_if' => "The :attribute field must be accepted if :other.", 'validation.same_rule' => "The :attribute field must be identical to the :otherAttribute field.", 'validation.password_rule' => "The :attribute field must meet password requirements.", 'validation.numeric_rule' => "The :attribute field must be numeric.", diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 7cd6b93..45ad8e7 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -113,7 +113,7 @@ $validator = new Validator(['field' => 'some_field', 'other_field' => true], ['field' => 'accepted_if:other_field']); expect($validator->isValid())->toBeTrue(); - $validator = new Validator(['field' => 'some_field', 'other_field' => false], ['other_field' => 'accepted_if:other_field']); + $validator = new Validator(['field' => 'some_field', 'other_field' => false], ['field' => 'accepted_if:other_field']); expect($validator->isValid())->toBeFalse(); expect($validator->getErrors()['field'][0])->toBe( @@ -244,7 +244,7 @@ it('validates not in rule successfully', function () { - $values = 'value1,value2,value3'; + $values = "value1,value2,value3"; $validator = new Validator(['field' => 'other_value'], ['field' => 'not_in:' . $values]); expect($validator->isValid())->toBeTrue(); @@ -326,12 +326,12 @@ it('validates uppercase rule successfully', function () { - $validator = new Validator(['field' => 'lowercase'], ['field' => 'upper']); - expect($validator->isValid())->toBeFalse(); - $validator = new Validator(['field' => 'UPPERCASE'], ['field' => 'upper']); expect($validator->isValid())->toBeTrue(); + $validator = new Validator(['field' => 'lowercase'], ['field' => 'upper']); + expect($validator->isValid())->toBeFalse(); + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.uppercase_rule', [ 'attribute' => 'field', @@ -419,13 +419,13 @@ }); it('validates url rule successfully', function () { - - $validator = new Validator(['field' => 'invalid_url'], ['field' => 'url']); - expect($validator->isValid())->toBeFalse(); $validator = new Validator(['field' => 'http://google.com'], ['field' => 'url']); expect($validator->isValid())->toBeTrue(); + $validator = new Validator(['field' => 'invalid_url'], ['field' => 'url']); + expect($validator->isValid())->toBeFalse(); + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.url', [ 'attribute' => 'field', @@ -435,12 +435,12 @@ it('validates ip rule successfully', function () { - $validator = new Validator(['field' => '3853598'], ['field' => 'valid_ip']); - expect($validator->isValid())->toBeFalse(); - $validator = new Validator(['field' => '127.0.0.1'], ['field' => 'valid_ip']); expect($validator->isValid())->toBeTrue(); + $validator = new Validator(['field' => '3853598'], ['field' => 'valid_ip']); + expect($validator->isValid())->toBeFalse(); + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.valid_ip', [ 'attribute' => 'field', @@ -450,12 +450,12 @@ it('validates size rule (string) successfully', function () { - $validator = new Validator(['field' => 'azerty'], ['field' => 'size:4']); - expect($validator->isValid())->toBeFalse(); - $validator = new Validator(['field' => 'azer'], ['field' => 'size:4']); expect($validator->isValid())->toBeTrue(); + $validator = new Validator(['field' => 'azerty'], ['field' => 'size:4']); + expect($validator->isValid())->toBeFalse(); + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ 'attribute' => 'field', @@ -466,12 +466,12 @@ it('validates size rule (integer) successfully', function () { - $validator = new Validator(['field' => 6], ['field' => 'size:3']); - expect($validator->isValid())->toBeFalse(); - $validator = new Validator(['field' => 3], ['field' => 'size:3']); expect($validator->isValid())->toBeTrue(); + $validator = new Validator(['field' => 6], ['field' => 'size:3']); + expect($validator->isValid())->toBeFalse(); + expect($validator->getErrors()['field'][0])->toBe( LangManager::getTranslation('validation.size', [ 'attribute' => 'field', From 20aa771c785972bb350d7c71c877c41a805b0924 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 02:56:13 +0100 Subject: [PATCH 25/31] Fix singleton pattern persistance issues for testing --- src/Validator.php | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/src/Validator.php b/src/Validator.php index ffe1cff..9fe41f7 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -16,9 +16,6 @@ class Validator { - // protected static $instance; - - // protected static $langManagerInstance; private $errors = []; @@ -34,25 +31,6 @@ public function __construct(private array $data, private array $rules) $this->validate(); } - // public static function getInstance(): Validator - // { - // if (!self::$instance) { - // self::$instance = new Validator(self::$data, self::$rules); - // } - - // return self::$instance; - // } - - // public static function getLangManagerInstance(): LangManager - // { - // if (!self::$langManagerInstance) { - // self::$langManagerInstance = new LangManager(); - // } - - // return self::$langManagerInstance; - // } - - /** * Parse a rule to extract the name and any parameters. * From 4ad6812cfbf2f8bf6114187528a82ac148aa7754 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 03:00:43 +0100 Subject: [PATCH 26/31] Fix "asserting that two strings are identical" bug on tests --- src/Rules/InRule.php | 2 +- src/Rules/NotInRule.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Rules/InRule.php b/src/Rules/InRule.php index 2f3616a..2ca120b 100644 --- a/src/Rules/InRule.php +++ b/src/Rules/InRule.php @@ -59,7 +59,7 @@ public function message(): string // Use LangManager to get a translated validation error message. return LangManager::getTranslation('validation.in_rule', [ 'attribute' => $this->field, - 'values' => implode(', ', $this->parameters), + 'values' => implode(',', $this->parameters), ]); } } diff --git a/src/Rules/NotInRule.php b/src/Rules/NotInRule.php index a4ebe25..460c403 100644 --- a/src/Rules/NotInRule.php +++ b/src/Rules/NotInRule.php @@ -57,7 +57,7 @@ public function message(): string // Use LangManager to get a translated validation error message. return LangManager::getTranslation('validation.not_in_rule', [ 'attribute' => $this->field, - 'values' => implode(', ', $this->parameters), + 'values' => implode(',', $this->parameters), ]); } } From 75675cf04e24c64981ab6aef2ca26928436d3c6f Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 03:15:38 +0100 Subject: [PATCH 27/31] Fix numeric rule tests failing issues --- src/Rules/NumericRule.php | 2 +- tests/Feature/RuleTest.php | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Rules/NumericRule.php b/src/Rules/NumericRule.php index 4bed622..b6e0c56 100644 --- a/src/Rules/NumericRule.php +++ b/src/Rules/NumericRule.php @@ -46,7 +46,7 @@ public function passes(string $field, $value, array $data): bool $this->field = $field; // Check if the value is numeric. - return is_numeric($value); + return ($value !== null) & is_numeric($value); } /** diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 45ad8e7..5c0088c 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -182,7 +182,7 @@ $validator = new Validator(['password' => 'NoDigit'], ['password' => 'password']); expect($validator->isValid())->toBeFalse(); - expect($validator->getErrors()['field'][0])->toBe( + expect($validator->getErrors()['password'][0])->toBe( LangManager::getTranslation('validation.password_rule', [ 'attribute' => 'password', ]) @@ -195,15 +195,12 @@ expect($validator->isValid())->toBeTrue(); $validator = new Validator(['numericField' => '456'], ['numericField' => 'numeric']); - expect($validator->isValid())->toBeFalse(); + expect($validator->isValid())->toBeTrue(); $validator = new Validator(['numericField' => 'NotNumeric'], ['numericField' => 'numeric']); expect($validator->isValid())->toBeFalse(); - - $validator = new Validator(['numericField' => null], ['numericField' => 'numeric']); - expect($validator->isValid())->toBeFalse(); - expect($validator->getErrors()['field'][0])->toBe( + expect($validator->getErrors()['numericField'][0])->toBe( LangManager::getTranslation('validation.numeric_rule', [ 'attribute' => 'numericField', ]) @@ -218,7 +215,7 @@ $validator = new Validator(['nullableField' => 'NotNull'], ['nullableField' => 'nullable']); expect($validator->isValid())->toBeFalse(); - expect($validator->getErrors()['field'][0])->toBe( + expect($validator->getErrors()['nullableField'][0])->toBe( LangManager::getTranslation('validation.nullable_rule', [ 'attribute' => 'nullableField', ]) From 76e0ed00abb589fb36bbcb8781ef3c5e54572a63 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 03:24:24 +0100 Subject: [PATCH 28/31] Fix Validation rule 'active_url' not found issues. --- src/Rules/ActiveURLRule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/ActiveURLRule.php b/src/Rules/ActiveURLRule.php index d96f475..ba2c262 100644 --- a/src/Rules/ActiveURLRule.php +++ b/src/Rules/ActiveURLRule.php @@ -13,7 +13,7 @@ use BlakvGhost\PHPValidator\LangManager; -class ActiveURLRule implements RuleInterface +class ActiveUrlRule implements RuleInterface { /** * The name of the field being validated. From d24421a29007b9c2a9a5e1f0bd1b655a4cdb574f Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Sat, 18 Nov 2023 03:28:01 +0100 Subject: [PATCH 29/31] Fix Validation rule 'active_url' not found issues. --- tests/Feature/RuleTest.php | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index 5c0088c..5703eeb 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -1,29 +1,6 @@ Date: Sat, 18 Nov 2023 03:32:31 +0100 Subject: [PATCH 30/31] Fix Validation rule 'active_url' not found issues. --- src/Rules/ActiveURLRule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/ActiveURLRule.php b/src/Rules/ActiveURLRule.php index ba2c262..aaa6fba 100644 --- a/src/Rules/ActiveURLRule.php +++ b/src/Rules/ActiveURLRule.php @@ -1,7 +1,7 @@ Date: Sat, 18 Nov 2023 03:32:54 +0100 Subject: [PATCH 31/31] Fix Validation rule 'active_url' not found issue. --- src/Rules/ActiveURLRule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/ActiveURLRule.php b/src/Rules/ActiveURLRule.php index aaa6fba..d9268eb 100644 --- a/src/Rules/ActiveURLRule.php +++ b/src/Rules/ActiveURLRule.php @@ -23,7 +23,7 @@ class ActiveUrlRule implements RuleInterface protected $field; /** - * Constructor of the ActiveURLRule class. + * Constructor of the ActiveUrlRule class. * * @param array $parameters Parameters for the rule (none needed for this rule). */