Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 6052deb

Browse files
committed
feat: add Validator tests
1 parent ebc49a3 commit 6052deb

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/Validator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ public function validate(mixed $value): bool
6868
/**
6969
* @return RuleInterface[]
7070
*/
71-
private function getRules(): array
71+
public function getRules(): array
7272
{
7373
return $this->rules;
7474
}
7575

76-
private function addRule(RuleInterface $rule): self
76+
public function addRule(RuleInterface $rule): self
7777
{
7878
$this->rules[] = $rule;
7979

tests/ValidatorTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\GreaterThan;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\LessThan;
9+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\NotBlank;
10+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
11+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
12+
13+
class ValidatorTest extends AbstractTest
14+
{
15+
#[DataProvider('provideValidatorUsageApproachData')]
16+
public function testValidatorGetRules(Validator $validator)
17+
{
18+
$this->assertCount(3, $validator->getRules());
19+
$this->assertContainsOnlyInstancesOf(RuleInterface::class, $validator->getRules());
20+
}
21+
22+
#[DataProvider('provideValidatorUsageApproachData')]
23+
public function testValidatorFailureCondition(Validator $validator)
24+
{
25+
$this->assertFalse($validator->validate(false));
26+
27+
$this->expectException(ValidationException::class);
28+
$validator->assert(false, 'test');
29+
}
30+
31+
#[DataProvider('provideValidatorUsageApproachData')]
32+
public function testValidatorSuccessCondition(Validator $validator)
33+
{
34+
$this->assertTrue($validator->validate(15));
35+
36+
$validator->assert(15, 'test');
37+
}
38+
39+
public static function provideValidatorUsageApproachData(): \Generator
40+
{
41+
yield 'fluent approach' => [
42+
Validator
43+
::notBlank()
44+
->greaterThan(10)
45+
->lessThan(20)
46+
];
47+
yield 'dependency injection approach' => [
48+
new Validator(
49+
new NotBlank(),
50+
new GreaterThan(10),
51+
new LessThan(20)
52+
)
53+
];
54+
yield 'method approach' => [
55+
(new Validator())
56+
->addRule(new NotBlank())
57+
->addRule(new GreaterThan(10))
58+
->addRule(new LessThan(20))
59+
];
60+
}
61+
}

0 commit comments

Comments
 (0)