Skip to content

Commit

Permalink
Merge pull request #11 from BlakvGhost/fix-custom-rule-namespace-issu…
Browse files Browse the repository at this point in the history
…e-in-validator-class

Fix custom rule interface namespace issues in Validator
  • Loading branch information
BlakvGhost authored Nov 21, 2023
2 parents 697e4cc + 94157a6 commit 79a7978
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use BlakvGhost\PHPValidator\Lang\LangManager;
use BlakvGhost\PHPValidator\Mapping\RulesMaped;
use BlakvGhost\PHPValidator\Rules\RequiredRule;
use BlakvGhost\PHPValidator\Validator;
use BlakvGhost\PHPValidator\ValidatorException;

Expand All @@ -19,6 +21,30 @@
->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";
Expand Down

0 comments on commit 79a7978

Please sign in to comment.