test(unit): Add Neuron and Validator regression tests
This commit is contained in:
166
tests/Unit/ValidatorTest.php
Normal file
166
tests/Unit/ValidatorTest.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Libs\Neuron;
|
||||
use Libs\Validator;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* ValidatorTest - DuckBrain test harness
|
||||
*
|
||||
* Regression net de las reglas escalares, el batch validateList()
|
||||
* (paro en el primer fallo + $lastFailed) y message(). Las reglas de
|
||||
* archivo (file/image/mimes) requieren fixtures de $_FILES y se dejan
|
||||
* para la suite multi-motor de integración.
|
||||
*/
|
||||
final class ValidatorTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
Validator::$lastFailed = '';
|
||||
}
|
||||
|
||||
public static function scalarRuleProvider(): array
|
||||
{
|
||||
return [
|
||||
'valid email' => ['email', 'kj@duckbrain.dev', true],
|
||||
'invalid email' => ['email', 'not-an-email', false],
|
||||
'valid url' => ['url', 'https://kj2.me', true],
|
||||
'invalid url' => ['url', 'kj2.me', false],
|
||||
'int from string' => ['int', '42', true],
|
||||
'int with decimals' => ['int', '42.5', false],
|
||||
'float' => ['float', '3.14', true],
|
||||
'number e-notation' => ['number', '1e3', true],
|
||||
'bool yes' => ['bool', 'yes', true],
|
||||
'bool garbage' => ['bool', 'quizas', false],
|
||||
'strict string' => ['string', '42', true],
|
||||
'string rejects int' => ['string', 42, false],
|
||||
'array' => ['array', [1], true],
|
||||
'array rejects string' => ['array', 'x', false],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('scalarRuleProvider')]
|
||||
public function testScalarRules(string $rule, mixed $value, bool $expected): void
|
||||
{
|
||||
$this->assertSame($expected, Validator::checkRule($value, $rule), "rule {$rule} with " . var_export($value, true));
|
||||
}
|
||||
|
||||
public function testExistsAndRequiredDisagreeOnEmptyString(): void
|
||||
{
|
||||
$this->assertTrue(Validator::checkRule('', 'exists'));
|
||||
$this->assertFalse(Validator::checkRule('', 'required'));
|
||||
$this->assertFalse(Validator::checkRule(null, 'exists'));
|
||||
}
|
||||
|
||||
public function testSizeRulesMeasureStringNumberAndArray(): void
|
||||
{
|
||||
$this->assertTrue(Validator::checkRule('abc', 'min:3'));
|
||||
$this->assertFalse(Validator::checkRule('abc', 'min:4'));
|
||||
$this->assertTrue(Validator::checkRule(10, 'max:10'));
|
||||
$this->assertTrue(Validator::checkRule([1, 2, 3], 'between:2,3'));
|
||||
$this->assertTrue(Validator::checkRule('hey', 'size:3'));
|
||||
$this->assertFalse(Validator::checkRule('hey', 'size:4'));
|
||||
}
|
||||
|
||||
public function testRegexKeepsColonsAndCommasInsidePattern(): void
|
||||
{
|
||||
$this->assertTrue(Validator::checkRule('a,b', 'regex:/^a,b$/'));
|
||||
$this->assertFalse(Validator::checkRule('a;b', 'regex:/^a,b$/'));
|
||||
}
|
||||
|
||||
public function testEnumIsLooseComparison(): void
|
||||
{
|
||||
$this->assertTrue(Validator::checkRule('1', 'enum:1,2,3'));
|
||||
$this->assertFalse(Validator::checkRule('9', 'enum:1,2,3'));
|
||||
}
|
||||
|
||||
public function testNotNegatesNextRule(): void
|
||||
{
|
||||
$this->assertTrue(Validator::checkRule('3.5', 'not:int'));
|
||||
$this->assertFalse(Validator::checkRule('42', 'not:int'));
|
||||
}
|
||||
|
||||
public function testParseRule(): void
|
||||
{
|
||||
$this->assertSame(['required', ''], Validator::parseRule('required'));
|
||||
$this->assertSame(['enum', 'a,b'], Validator::parseRule('enum:a,b'));
|
||||
$this->assertSame(['regex', '/^a,b$/'], Validator::parseRule('regex:/^a,b$/'));
|
||||
}
|
||||
|
||||
public function testValidateListPassesFullyValidBatch(): void
|
||||
{
|
||||
$data = new Neuron(['username' => 'kj', 'email' => 'kj@duckbrain.dev']);
|
||||
|
||||
$this->assertTrue(Validator::validateList(
|
||||
['username' => 'required|string|min:2', 'email' => 'required|email'],
|
||||
$data
|
||||
));
|
||||
$this->assertSame('', Validator::$lastFailed);
|
||||
}
|
||||
|
||||
public function testValidateListStopsAtFirstFailure(): void
|
||||
{
|
||||
$data = new Neuron(['email' => 'not-an-email', 'name' => null]);
|
||||
|
||||
$this->assertFalse(Validator::validateList(
|
||||
['email' => 'required|email', 'name' => 'required'],
|
||||
$data
|
||||
));
|
||||
$this->assertSame('email.email', Validator::$lastFailed);
|
||||
}
|
||||
|
||||
public function testValidateListNullableSkipsRestWhenEmpty(): void
|
||||
{
|
||||
$rules = ['bio' => 'nullable|min:10'];
|
||||
|
||||
$this->assertTrue(Validator::validateList($rules, new Neuron(['bio' => null])));
|
||||
$this->assertFalse(Validator::validateList($rules, new Neuron(['bio' => 'too-short'])));
|
||||
}
|
||||
|
||||
public function testValidateListConfirmedUsesSiblingField(): void
|
||||
{
|
||||
$rules = ['password' => 'required|confirmed'];
|
||||
|
||||
$ok = new Neuron(['password' => 'secret', 'password_confirmation' => 'secret']);
|
||||
$bad = new Neuron(['password' => 'secret', 'password_confirmation' => 'different']);
|
||||
|
||||
$this->assertTrue(Validator::validateList($rules, $ok));
|
||||
$this->assertFalse(Validator::validateList($rules, $bad));
|
||||
$this->assertSame('password.confirmed', Validator::$lastFailed);
|
||||
}
|
||||
|
||||
public function testMessageBuildsHumanTextFromLastFailed(): void
|
||||
{
|
||||
$this->assertSame(
|
||||
'The email must be a valid email address.',
|
||||
Validator::message('email.email')
|
||||
);
|
||||
$this->assertSame(
|
||||
'The age must be between 5 and 10.',
|
||||
Validator::message('age.between:5,10')
|
||||
);
|
||||
$this->assertSame(
|
||||
'The selected status is invalid. Allowed: a, b, c.',
|
||||
Validator::message('status.enum:a,b,c')
|
||||
);
|
||||
$this->assertSame(
|
||||
'The reason field is required when mode is other.',
|
||||
Validator::message('reason.required_if:mode,other')
|
||||
);
|
||||
}
|
||||
|
||||
public function testMessageRespectsAttributesAndOverrides(): void
|
||||
{
|
||||
$this->assertSame(
|
||||
'The user name field is required.',
|
||||
Validator::message('username.required', [], ['username' => 'user name'])
|
||||
);
|
||||
$this->assertSame(
|
||||
'custom',
|
||||
Validator::message('username.min:3', ['username.min:3' => 'custom'])
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user