Skip to content

Commit e465810

Browse files
dmjohnsson23henriquemoody
authored andcommitted
Add more tests for Each rule
1 parent 6d3b518 commit e465810

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/feature/Rules/EachTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,55 @@
239239
'intType.3' => 'Wrapped must be an integer',
240240
]
241241
));
242+
243+
test('Chained wrapped rule', expectAll(
244+
fn() => v::each(v::between(5, 7)->odd())->assert([2, 4]),
245+
'2 must be between 5 and 7',
246+
<<<'FULL_MESSAGE'
247+
- Each item in `[2, 4]` must be valid
248+
- All of the required rules must pass for 2
249+
- 2 must be between 5 and 7
250+
- 2 must be an odd number
251+
- All of the required rules must pass for 4
252+
- 4 must be between 5 and 7
253+
- 4 must be an odd number
254+
FULL_MESSAGE,
255+
[
256+
'__root__' => 'Each item in `[2, 4]` must be valid',
257+
'allOf.1' => [
258+
'__root__' => 'All of the required rules must pass for 2',
259+
'between' => '2 must be between 5 and 7',
260+
'odd' => '2 must be an odd number',
261+
],
262+
'allOf.2' => [
263+
'__root__' => 'All of the required rules must pass for 4',
264+
'between' => '4 must be between 5 and 7',
265+
'odd' => '4 must be an odd number',
266+
],
267+
]
268+
));
269+
270+
test('Multiple nested rules', expectAll(
271+
fn() => v::each(v::arrayType()->key('my_int', v::intType()->odd()))->assert([['not_int' => 'wrong'], ['my_int' => 2], 'not an array']),
272+
'my_int must be present',
273+
<<<'FULL_MESSAGE'
274+
- Each item in `[["not_int": "wrong"], ["my_int": 2], "not an array"]` must be valid
275+
- These rules must pass for `["not_int": "wrong"]`
276+
- my_int must be present
277+
- These rules must pass for `["my_int": 2]`
278+
- my_int must be an odd number
279+
- All of the required rules must pass for "not an array"
280+
- "not an array" must be an array
281+
- my_int must be present
282+
FULL_MESSAGE,
283+
[
284+
'__root__' => 'Each item in `[["not_int": "wrong"], ["my_int": 2], "not an array"]` must be valid',
285+
'allOf.1' => 'my_int must be present',
286+
'allOf.2' => 'my_int must be an odd number',
287+
'allOf.3' => [
288+
'__root__' => 'All of the required rules must pass for "not an array"',
289+
'arrayType' => '"not an array" must be an array',
290+
'my_int' => 'my_int must be present',
291+
],
292+
]
293+
));

0 commit comments

Comments
 (0)