1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
4+
5+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \EachKeyException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \EachKey ;
7+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \GreaterThan ;
8+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \NotBlank ;
9+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \Type ;
10+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleFailureConditionTrait ;
11+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleMessageOptionTrait ;
12+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleSuccessConditionTrait ;
13+ use ProgrammatorDev \YetAnotherPhpValidator \Test \Util \TestRuleUnexpectedValueTrait ;
14+ use ProgrammatorDev \YetAnotherPhpValidator \Validator ;
15+
16+ class EachKeyTest extends AbstractTest
17+ {
18+ use TestRuleUnexpectedValueTrait;
19+ use TestRuleFailureConditionTrait;
20+ use TestRuleSuccessConditionTrait;
21+ use TestRuleMessageOptionTrait;
22+
23+ public static function provideRuleUnexpectedValueData (): \Generator
24+ {
25+ yield 'invalid value type ' => [
26+ new EachKey (new Validator (new Type ('string ' ))),
27+ 'invalid ' ,
28+ '/Expected value of type "(.*)", "(.*)" given./ '
29+ ];
30+ yield 'unexpected value propagation ' => [
31+ new EachKey (new Validator (new GreaterThan (10 ))),
32+ ['key1 ' => 1 ],
33+ '/Cannot compare a type "(.*)" with a type "(.*)"./ '
34+ ];
35+ }
36+
37+ public static function provideRuleFailureConditionData (): \Generator
38+ {
39+ $ exception = EachKeyException::class;
40+ $ message = '/Invalid key: The "(.*)" key should be of type "(.*)", "(.*)" given./ ' ;
41+
42+ yield 'invalid array element ' => [
43+ new EachKey (new Validator (new Type ('string ' ))),
44+ ['key1 ' => 1 , 'key2 ' => 2 , 1 => 3 ],
45+ $ exception ,
46+ $ message
47+ ];
48+ yield 'invalid traversable element ' => [
49+ new EachKey (new Validator (new Type ('string ' ))),
50+ new \ArrayIterator (['key1 ' => 1 , 'key2 ' => 2 , 1 => 3 ]),
51+ $ exception ,
52+ $ message
53+ ];
54+ }
55+
56+ public static function provideRuleSuccessConditionData (): \Generator
57+ {
58+ yield 'array element ' => [
59+ new EachKey (new Validator (new Type ('string ' ))),
60+ ['key1 ' => 1 , 'key2 ' => 2 , 'key3 ' => 3 ]
61+ ];
62+ yield 'traversable element ' => [
63+ new EachKey (new Validator (new Type ('string ' ))),
64+ new \ArrayIterator (['key1 ' => 1 , 'key2 ' => 2 , 'key3 ' => 3 ])
65+ ];
66+ }
67+
68+ public static function provideRuleMessageOptionData (): \Generator
69+ {
70+ yield 'message ' => [
71+ new EachKey (
72+ validator: new Validator (new Type ('string ' )),
73+ message: 'The "{{ name }}" key "{{ key }}" is invalid. '
74+ ),
75+ ['key1 ' => 1 , 'key2 ' => 2 , 1 => 3 ],
76+ 'The "test" key "1" is invalid. '
77+ ];
78+ }
79+ }
0 commit comments