1+ <?php
2+
3+ namespace ProgrammatorDev \Validator \Test ;
4+
5+ use ProgrammatorDev \Validator \Exception \CollectionException ;
6+ use ProgrammatorDev \Validator \Rule \Collection ;
7+ use ProgrammatorDev \Validator \Test \Util \TestRuleFailureConditionTrait ;
8+ use ProgrammatorDev \Validator \Test \Util \TestRuleMessageOptionTrait ;
9+ use ProgrammatorDev \Validator \Test \Util \TestRuleSuccessConditionTrait ;
10+ use ProgrammatorDev \Validator \Test \Util \TestRuleUnexpectedValueTrait ;
11+ use ProgrammatorDev \Validator \Validator ;
12+
13+ class CollectionTest extends AbstractTest
14+ {
15+ use TestRuleUnexpectedValueTrait;
16+ use TestRuleFailureConditionTrait;
17+ use TestRuleSuccessConditionTrait;
18+ use TestRuleMessageOptionTrait;
19+
20+ public static function provideRuleUnexpectedValueData (): \Generator
21+ {
22+ $ unexpectedFieldValueMessage = '/At field (.*)\: (.*)\./ ' ;
23+ $ unexpectedTypeMessage = '/Expected value of type "array", "(.*)" given\./ ' ;
24+
25+ yield 'invalid field value ' => [
26+ new Collection (fields: ['field ' => 'invalid ' ]),
27+ ['field ' => 'value ' ],
28+ $ unexpectedFieldValueMessage
29+ ];
30+ yield 'invalid value type ' => [
31+ new Collection (fields: ['field ' => Validator::notBlank ()]),
32+ 'invalid ' ,
33+ $ unexpectedTypeMessage
34+ ];
35+ }
36+
37+ public static function provideRuleFailureConditionData (): \Generator
38+ {
39+ $ exception = CollectionException::class;
40+ $ extraFieldsMessage = '/The (.*) field is not allowed\./ ' ;
41+ $ missingFieldsMessage = '/The (.*) field is missing\./ ' ;
42+
43+ yield 'invalid field ' => [
44+ new Collection (fields: ['field ' => Validator::notBlank ()]),
45+ ['field ' => '' ],
46+ $ exception ,
47+ '/The "(.*)" value should not be blank, "" given\./ '
48+ ];
49+ yield 'extra fields ' => [
50+ new Collection (fields: ['field ' => Validator::notBlank ()]),
51+ ['field ' => 'value ' , 'extrafield ' => 'extravalue ' ],
52+ $ exception ,
53+ $ extraFieldsMessage
54+ ];
55+ yield 'missing fields ' => [
56+ new Collection (
57+ fields: [
58+ 'field1 ' => Validator::notBlank (),
59+ 'field2 ' => Validator::notBlank ()
60+ ]
61+ ),
62+ ['field1 ' => 'value1 ' ],
63+ $ exception ,
64+ $ missingFieldsMessage
65+ ];
66+ }
67+
68+ public static function provideRuleSuccessConditionData (): \Generator
69+ {
70+ yield 'field ' => [
71+ new Collection (fields: ['field ' => Validator::notBlank ()]),
72+ ['field ' => 'value ' ],
73+ ];
74+ yield 'extra fields ' => [
75+ new Collection (
76+ fields: ['field ' => Validator::notBlank ()],
77+ allowExtraFields: true
78+ ),
79+ ['field ' => 'value ' , 'extrafield ' => 'extravalue ' ]
80+ ];
81+ }
82+
83+ public static function provideRuleMessageOptionData (): \Generator
84+ {
85+ yield 'message ' => [
86+ new Collection (
87+ fields: ['field ' => Validator::notBlank ()],
88+ message: 'There was an error: {{ message }} '
89+ ),
90+ ['field ' => '' ],
91+ 'There was an error: The "field" value should not be blank, "" given. '
92+ ];
93+ yield 'extra fields message ' => [
94+ new Collection (
95+ fields: ['field ' => Validator::notBlank ()],
96+ extraFieldsMessage: 'The {{ field }} was not expected. '
97+ ),
98+ ['field ' => 'value ' , 'extrafield ' => 'extravalue ' ],
99+ 'The "extrafield" was not expected. '
100+ ];
101+ yield 'missing fields message ' => [
102+ new Collection (
103+ fields: ['field ' => Validator::notBlank ()],
104+ missingFieldsMessage: 'Missing field: {{ field }}. '
105+ ),
106+ [],
107+ 'Missing field: "field". '
108+ ];
109+ }
110+ }
0 commit comments