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