From 94157a64c5edc7f7356d0dba4da0d964efd511e4 Mon Sep 17 00:00:00 2001 From: Kabirou ALASSANE Date: Tue, 21 Nov 2023 03:07:21 +0100 Subject: [PATCH] Fix custom rule interface namespace issues in Validator --- src/Validator.php | 2 +- tests/Feature/ValidatorTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Validator.php b/src/Validator.php index c315c46..7984a8b 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -13,7 +13,7 @@ use BlakvGhost\PHPValidator\Lang\LangManager; use BlakvGhost\PHPValidator\Mapping\RulesMaped; -use BlakvGhost\PHPValidator\Rules\RuleInterface; +use BlakvGhost\PHPValidator\Contracts\Rule as RuleInterface; use BlakvGhost\PHPValidator\ValidatorException; class Validator extends RulesMaped diff --git a/tests/Feature/ValidatorTest.php b/tests/Feature/ValidatorTest.php index 1ae3d98..1ddf73e 100644 --- a/tests/Feature/ValidatorTest.php +++ b/tests/Feature/ValidatorTest.php @@ -1,6 +1,8 @@ toThrow(ValidatorException::class, LangManager::getTranslation('validation.rule_not_found', ['ruleName' => 'nonexistent'])); }); +it('validates custom rule (required) rule successfully', function () { + + $validator = new Validator(['field' => 'value'], ['field' => new RequiredRule([])]); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'value'], ['field' => [new RequiredRule([])]]); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => 'value'], ['field' => ['string', new RequiredRule([])]]); + expect($validator->isValid())->toBeTrue(); + + RulesMaped::addRule('custom_required', RequiredRule::class); + + $validator = new Validator(['field' => 'value'], ['field' => 'string|custom_required']); + expect($validator->isValid())->toBeTrue(); + + $validator = new Validator(['field' => ''], ['field' => new RequiredRule([])]); + expect($validator->isValid())->toBeFalse(); + + expect($validator->getErrors()['field'][0])->toBe( + LangManager::getTranslation('validation.required_rule', ['attribute' => 'field']) + ); +}); + it('validates rule with custom error message', function () { $errorMessage = "Je teste une règle custom";