1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
4+
5+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \AllException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \All ;
7+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \GreaterThan ;
8+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \NotBlank ;
9+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleFailureConditionTrait ;
10+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleMessageOptionTrait ;
11+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleSuccessConditionTrait ;
12+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleUnexpectedValueTrait ;
13+ use ProgrammatorDev \YetAnotherPhpValidator \Validator ;
14+
15+ class AllTest extends AbstractTest
16+ {
17+ use TestRuleUnexpectedValueTrait;
18+ use TestRuleFailureConditionTrait;
19+ use TestRuleSuccessConditionTrait;
20+ use TestRuleMessageOptionTrait;
21+
22+ public static function provideRuleUnexpectedValueData (): \Generator
23+ {
24+ yield 'invalid constraint ' => [
25+ new All ([new NotBlank (), 'invalid ' ]),
26+ [1 , 2 , 3 ],
27+ '/Expected constraint of type "RuleInterface", "(.*)" given./ '
28+ ];
29+ yield 'invalid value type ' => [
30+ new All ([new NotBlank ()]),
31+ 'invalid ' ,
32+ '/Expected value of type "array", "(.*)" given./ '
33+ ];
34+ yield 'unexpected value propagation ' => [
35+ new All ([new GreaterThan (10 )]),
36+ ['a ' ],
37+ '/Cannot compare a type "(.*)" with a type "(.*)"./ '
38+ ];
39+ }
40+
41+ public static function provideRuleFailureConditionData (): \Generator
42+ {
43+ $ exception = AllException::class;
44+ $ message = '/At "(.*)": The "(.*)" value should not be blank, "(.*)" given./ ' ;
45+
46+ yield 'constraint ' => [
47+ new All ([new NotBlank ()]),
48+ [1 , 2 , '' ],
49+ $ exception ,
50+ $ message
51+ ];
52+ yield 'validator ' => [
53+ new All ([(new Validator (new NotBlank ()))]),
54+ [1 , 2 , '' ],
55+ $ exception ,
56+ $ message
57+ ];
58+ }
59+
60+ public static function provideRuleSuccessConditionData (): \Generator
61+ {
62+ yield 'constraints ' => [
63+ new All ([new NotBlank (), new GreaterThan (1 )]),
64+ [2 , 3 , 4 ]
65+ ];
66+ yield 'validators ' => [
67+ new All ([
68+ (new Validator (new NotBlank ())),
69+ (new Validator (new GreaterThan (1 )))
70+ ]),
71+ [2 , 3 , 4 ]
72+ ];
73+ yield 'constraints and validators ' => [
74+ new All ([
75+ new NotBlank (),
76+ (new Validator (new GreaterThan (1 )))
77+ ]),
78+ [2 , 3 , 4 ]
79+ ];
80+ }
81+
82+ public static function provideRuleMessageOptionData (): \Generator
83+ {
84+ yield 'constraint ' => [
85+ new All (
86+ constraints: [new NotBlank ()],
87+ options: [
88+ 'message ' => 'The "{{ name }}" value "{{ value }}" failed at key "{{ key }}". '
89+ ]
90+ ), [1 , 2 , '' ], 'The "test" value "[1, 2, ]" failed at key "2". '
91+ ];
92+ }
93+ }
0 commit comments