7
7
use karmabunny \kb \AttributeValidatorTrait ;
8
8
use karmabunny \kb \Collection ;
9
9
use karmabunny \kb \Rule ;
10
+ use karmabunny \kb \Scenario ;
10
11
use karmabunny \kb \Validates ;
11
12
use karmabunny \kb \ValidationException ;
12
13
use PHPUnit \Framework \TestCase ;
13
14
14
15
16
+ /**
17
+ * Test the attribute rules validator.
18
+ */
15
19
class AttributeValidatorTest extends TestCase
16
20
{
17
21
public function testEmpty ()
@@ -30,6 +34,7 @@ public function testEmpty()
30
34
31
35
if (PHP_VERSION_ID >= 80000 ) {
32
36
$ expected [] = 'php8 ' ;
37
+ $ expected [] = 'php8_scenario ' ;
33
38
}
34
39
35
40
$ actual = array_keys ($ exception ->errors );
@@ -53,14 +58,14 @@ public function testValid()
53
58
// range - 10-20
54
59
'ten_twenty ' => 13 ,
55
60
56
- // range - 20-30
57
- 'twenty_thirty ' => 26 ,
58
-
59
61
// email + end in karmabunny.com.au
60
62
61
63
62
64
// Not tested in PHP7
63
65
66
+
67
+ // Not tested in PHP7
68
+ 'php8_scenario ' => 'OK ' ,
64
69
]);
65
70
66
71
$ thing ->validate ();
@@ -72,6 +77,74 @@ public function testValid()
72
77
}
73
78
74
79
80
+ public function testScenarios ()
81
+ {
82
+ // Test required.
83
+ // missing twenty_thirty + (php8_scenario, when applicable)
84
+ try {
85
+ $ thing = new AttrThing ([
86
+ 'id ' => 100 ,
87
+ 'amount ' => 400 ,
88
+
89
+
90
+ ]);
91
+
92
+ $ thing ->validate (AttrThing::SCENARIO_TEST );
93
+ $ this ->fail ('Expected ValidationException. ' );
94
+ }
95
+ catch (ValidationException $ exception ) {
96
+ $ expected = [
97
+ 'twenty_thirty ' ,
98
+ ];
99
+
100
+ if (PHP_VERSION_ID >= 80000 ) {
101
+ $ expected [] = 'php8_scenario ' ;
102
+ }
103
+
104
+ $ actual = array_keys ($ exception ->errors );
105
+ $ this ->assertEquals ($ expected , $ actual );
106
+ }
107
+
108
+ // Just bad validations.
109
+ try {
110
+ $ thing = new AttrThing ([
111
+ 'ten_twenty ' => -13 ,
112
+ 'twenty_thirty ' => 10 ,
113
+ 'php8_scenario ' => 'OK ' ,
114
+ ]);
115
+
116
+ $ thing ->validate (AttrThing::SCENARIO_TEST );
117
+ $ this ->fail ('Expected ValidationException. ' );
118
+ }
119
+ catch (ValidationException $ exception ) {
120
+ $ expected = [
121
+ 'ten_twenty ' ,
122
+ 'twenty_thirty ' ,
123
+ ];
124
+
125
+ $ actual = array_keys ($ exception ->errors );
126
+ $ this ->assertEquals ($ expected , $ actual );
127
+ }
128
+
129
+ try {
130
+ $ thing = new AttrThing ([
131
+ 'ten_twenty ' => 15 ,
132
+
133
+ 'twenty_thirty ' => 25 ,
134
+
135
+ // Not tested in PHP7
136
+ 'php8_scenario ' => 'OK ' ,
137
+ ]);
138
+
139
+ $ thing ->validate (AttrThing::SCENARIO_TEST );
140
+ $ this ->assertTrue (true );
141
+ }
142
+ catch (ValidationException $ exception ) {
143
+ $ this ->fail ($ exception ->getMessage ());
144
+ }
145
+ }
146
+
147
+
75
148
public function testFailure ()
76
149
{
77
150
try {
@@ -84,9 +157,6 @@ public function testFailure()
84
157
// range - 10-20
85
158
'ten_twenty ' => -13 ,
86
159
87
- // range - 20-30
88
- 'twenty_thirty ' => 31 ,
89
-
90
160
// email, matchDomain
91
161
'address ' => 'eeeeee ' ,
92
162
]);
@@ -100,12 +170,12 @@ public function testFailure()
100
170
'id ' ,
101
171
'amount ' ,
102
172
'ten_twenty ' ,
103
- 'twenty_thirty ' ,
104
173
'address ' ,
105
174
];
106
175
107
176
if (PHP_VERSION_ID >= 80000 ) {
108
177
$ expected [] = 'php8 ' ;
178
+ $ expected [] = 'php8_scenario ' ;
109
179
}
110
180
111
181
$ actual = array_keys ($ exception ->errors );
@@ -114,7 +184,6 @@ public function testFailure()
114
184
$ this ->assertTrue (isset ($ exception ->errors ['id ' ]['required ' ]));
115
185
$ this ->assertCount (1 , $ exception ->errors ['amount ' ] ?? []);
116
186
$ this ->assertCount (1 , $ exception ->errors ['ten_twenty ' ] ?? []);
117
- $ this ->assertCount (1 , $ exception ->errors ['twenty_thirty ' ] ?? []);
118
187
$ this ->assertCount (2 , $ exception ->errors ['address ' ] ?? []);
119
188
}
120
189
}
@@ -137,6 +206,7 @@ public function testAttributes()
137
206
// test valid.
138
207
try {
139
208
$ thing->
php8 =
'[email protected] ' ;
209
+ $ thing ->php8_scenario = 'OK ' ;
140
210
141
211
$ thing ->validate ();
142
212
$ this ->assertTrue (true );
@@ -148,24 +218,51 @@ public function testAttributes()
148
218
// test required.
149
219
try {
150
220
$ thing ->php8 = null ;
221
+ $ thing ->php8_scenario = 'OK ' ;
151
222
152
223
$ thing ->validate ();
153
224
$ this ->fail ('Expected ValidationException. ' );
154
225
}
155
226
catch (ValidationException $ exception ) {
156
227
$ this ->assertTrue (isset ($ exception ->errors ['php8 ' ]['required ' ]));
228
+ $ this ->assertFalse (isset ($ exception ->errors ['php8_scenario ' ]['required ' ]));
157
229
}
158
230
159
231
// test invalid.
160
232
try {
161
233
$ thing ->php8 = 'eee ' ;
234
+ $ thing ->php8_scenario = 'OK ' ;
162
235
163
236
$ thing ->validate ();
164
237
$ this ->fail ('Expected ValidationException. ' );
165
238
}
166
239
catch (ValidationException $ exception ) {
167
240
$ this ->assertCount (3 , $ exception ->errors ['php8 ' ] ?? []);
168
241
}
242
+
243
+ // test bad scenario.
244
+ try {
245
+ $ thing ->php8 = null ;
246
+ $ thing ->php8_scenario = null ;
247
+
248
+ $ thing ->validate (AttrThing::SCENARIO_TEST );
249
+ $ this ->fail ('Expected ValidationException. ' );
250
+ }
251
+ catch (ValidationException $ exception ) {
252
+ $ this ->assertTrue (isset ($ exception ->errors ['php8_scenario ' ]['required ' ]));
253
+ }
254
+
255
+ // test good scenario.
256
+ try {
257
+ $ thing ->php8 = null ;
258
+ $ thing ->php8_scenario = 'OK ' ;
259
+
260
+ $ thing ->validate (AttrThing::SCENARIO_TEST );
261
+ $ this ->assertTrue (true );
262
+ }
263
+ catch (ValidationException $ exception ) {
264
+ $ this ->fail ($ exception ->getMessage ());
265
+ }
169
266
}
170
267
}
171
268
@@ -174,6 +271,8 @@ class AttrThing extends Collection implements Validates
174
271
{
175
272
use AttributeValidatorTrait;
176
273
274
+ const SCENARIO_TEST = 'TEST ' ;
275
+
177
276
/**
178
277
* @var int
179
278
* @rule required
@@ -190,18 +289,23 @@ class AttrThing extends Collection implements Validates
190
289
/**
191
290
* @var int
192
291
* @rule required
292
+ * @scenario
193
293
*/
194
294
public $ default = 333 ;
195
295
196
296
/**
197
297
* @var int
198
298
* @rule range 10, 20
299
+ * @scenario
300
+ * @scenario TEST
199
301
*/
200
302
public $ ten_twenty ;
201
303
202
304
/**
203
305
* @var int
306
+ * @rule required
204
307
* @rule range 20, 30
308
+ * @scenario TEST
205
309
*/
206
310
public $ twenty_thirty ;
207
311
@@ -219,6 +323,14 @@ class AttrThing extends Collection implements Validates
219
323
#[Rule('matchDomain ' , 'example.com ' )]
220
324
public $ php8 ;
221
325
326
+ /**
327
+ * @var string
328
+ */
329
+ #[Rule('required ' )]
330
+ #[Scenario()]
331
+ #[Scenario(self ::SCENARIO_TEST )]
332
+ public $ php8_scenario ;
333
+
222
334
223
335
public static function matchDomain ($ value , string $ domain )
224
336
{
0 commit comments