Skip to content

Commit

Permalink
Unit Test: fix bug on unit test using pest issues
Browse files Browse the repository at this point in the history
  • Loading branch information
BlakvGhost committed Nov 16, 2023
1 parent 367e326 commit b0c7ea8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
52 changes: 52 additions & 0 deletions tests/Feature/RuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use BlakvGhost\PHPValidator\LangManager;
use BlakvGhost\PHPValidator\Rules\EmailRule;
use BlakvGhost\PHPValidator\Rules\MaxLengthRule;
use BlakvGhost\PHPValidator\Rules\RequiredRule;
use BlakvGhost\PHPValidator\Rules\StringRule;


it('validates required rule successfully', function () {
$validator = new RequiredRule([]);

expect($validator->passes('field', 'value', ['field' => 'value']))->toBeTrue();
expect($validator->passes('field', '', []))->toBeFalse();

expect($validator->message())->toBe(
LangManager::getTranslation('validation.required_rule', ['attribute' => 'field'])
);
});

it('validates max length rule successfully', function () {
$validator = new MaxLengthRule([5]);

expect($validator->passes('field', 'value', []))->toBeTrue();
expect($validator->passes('field', 'toolongvalue', []))->toBeFalse();

expect($validator->message())->toBe(
LangManager::getTranslation('validation.max_length_rule', ['attribute' => 'field', 'max' => 5])
);
});

it('validates email rule successfully', function () {
$validator = new EmailRule([]);

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'])
);
});

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'])
);
});
Empty file removed tests/Feature/RulesTest.php
Empty file.
10 changes: 0 additions & 10 deletions tests/Feature/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,3 @@
expect(fn () => new Validator(['name' => 'John'], ['name' => 'nonexistent']))
->toThrow(ValidatorException::class, LangManager::getTranslation('validation.rule_not_found', ['ruleName' => 'nonexistent']));
});

// it('validates string rule successfully', function () {
// $validator = new Validator(['name' => 'John'], ['name' => 'string']);
// expect($validator->isValid())->toBeTrue();
// });

// it('throws exception for invalid string rule', function () {
// expect(fn () => new Validator(['name' => 123], ['name' => 'string']))
// ->toThrow(ValidatorException::class, LangManager::getTranslation('validation.string_rule', ['attribute' => 'name']));
// });

0 comments on commit b0c7ea8

Please sign in to comment.