Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom rule interface namespace issues in Validator #11

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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