Skip to content

Commit

Permalink
Add more tests for Each rule
Browse files Browse the repository at this point in the history
  • Loading branch information
dmjohnsson23 authored and henriquemoody committed Dec 20, 2024
1 parent 6d3b518 commit e465810
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/feature/Rules/EachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,55 @@
'intType.3' => 'Wrapped must be an integer',
]
));

test('Chained wrapped rule', expectAll(
fn() => v::each(v::between(5, 7)->odd())->assert([2, 4]),
'2 must be between 5 and 7',
<<<'FULL_MESSAGE'
- Each item in `[2, 4]` must be valid
- All of the required rules must pass for 2
- 2 must be between 5 and 7
- 2 must be an odd number
- All of the required rules must pass for 4
- 4 must be between 5 and 7
- 4 must be an odd number
FULL_MESSAGE,
[
'__root__' => 'Each item in `[2, 4]` must be valid',
'allOf.1' => [
'__root__' => 'All of the required rules must pass for 2',
'between' => '2 must be between 5 and 7',
'odd' => '2 must be an odd number',
],
'allOf.2' => [
'__root__' => 'All of the required rules must pass for 4',
'between' => '4 must be between 5 and 7',
'odd' => '4 must be an odd number',
],
]
));

test('Multiple nested rules', expectAll(
fn() => v::each(v::arrayType()->key('my_int', v::intType()->odd()))->assert([['not_int' => 'wrong'], ['my_int' => 2], 'not an array']),
'my_int must be present',
<<<'FULL_MESSAGE'
- Each item in `[["not_int": "wrong"], ["my_int": 2], "not an array"]` must be valid
- These rules must pass for `["not_int": "wrong"]`
- my_int must be present
- These rules must pass for `["my_int": 2]`
- my_int must be an odd number
- All of the required rules must pass for "not an array"
- "not an array" must be an array
- my_int must be present
FULL_MESSAGE,
[
'__root__' => 'Each item in `[["not_int": "wrong"], ["my_int": 2], "not an array"]` must be valid',
'allOf.1' => 'my_int must be present',
'allOf.2' => 'my_int must be an odd number',
'allOf.3' => [
'__root__' => 'All of the required rules must pass for "not an array"',
'arrayType' => '"not an array" must be an array',
'my_int' => 'my_int must be present',
],
]
));

0 comments on commit e465810

Please sign in to comment.