Skip to content

Commit 70e14e2

Browse files
committed
Standardize validator tests using the catchAll() function
Legacy test patterns like using `catchMessage()` and generic "Scenario #X" naming make it difficult to verify that all message templates behave correctly, especially in inverted states. This change adopts the `catchAll()` function to ensure a predictable testing matrix where every template is validated in both default and inverted scenarios. By enforcing this comprehensive template coverage and fixing naming typos, we establish a robust foundation for the remaining Pest suite.
1 parent 8dfa487 commit 70e14e2

14 files changed

Lines changed: 357 additions & 213 deletions

tests/feature/Validators/AlnumTest.php

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,34 @@
77

88
declare(strict_types=1);
99

10-
test('Scenario #1', catchMessage(
10+
test('Standard alnum template validation', catchAll(
1111
fn() => v::alnum()->assert('abc%1'),
12-
fn(string $message) => expect($message)->toBe('"abc%1" must contain only letters (a-z) and digits (0-9)'),
12+
fn(string $message, string $fullMessage, array $messages) => expect()
13+
->and($message)->toBe('"abc%1" must contain only letters (a-z) and digits (0-9)')
14+
->and($fullMessage)->toBe('- "abc%1" must contain only letters (a-z) and digits (0-9)')
15+
->and($messages)->toBe(['alnum' => '"abc%1" must contain only letters (a-z) and digits (0-9)']),
1316
));
1417

15-
test('Scenario #2', catchMessage(
16-
fn() => v::alnum(' ')->assert('abc%2'),
17-
fn(string $message) => expect($message)->toBe('"abc%2" must contain only letters (a-z), digits (0-9), and " "'),
18-
));
19-
20-
test('Scenario #3', catchMessage(
18+
test('Standard alnum template validation (inverted)', catchAll(
2119
fn() => v::not(v::alnum())->assert('abcd3'),
22-
fn(string $message) => expect($message)->toBe('"abcd3" must not contain letters (a-z) or digits (0-9)'),
23-
));
24-
25-
test('Scenario #4', catchMessage(
26-
fn() => v::not(v::alnum('% '))->assert('abc%4'),
27-
fn(string $message) => expect($message)->toBe('"abc%4" must not contain letters (a-z), digits (0-9), or "% "'),
28-
));
29-
30-
test('Scenario #5', catchFullMessage(
31-
fn() => v::alnum()->assert('abc^1'),
32-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "abc^1" must contain only letters (a-z) and digits (0-9)'),
33-
));
34-
35-
test('Scenario #6', catchFullMessage(
36-
fn() => v::not(v::alnum())->assert('abcd2'),
37-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "abcd2" must not contain letters (a-z) or digits (0-9)'),
20+
fn(string $message, string $fullMessage, array $messages) => expect()
21+
->and($message)->toBe('"abcd3" must not contain letters (a-z) or digits (0-9)')
22+
->and($fullMessage)->toBe('- "abcd3" must not contain letters (a-z) or digits (0-9)')
23+
->and($messages)->toBe(['notAlnum' => '"abcd3" must not contain letters (a-z) or digits (0-9)']),
3824
));
3925

40-
test('Scenario #7', catchFullMessage(
41-
fn() => v::alnum('* &%')->assert('abc^3'),
42-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "abc^3" must contain only letters (a-z), digits (0-9), and "* &%"'),
43-
));
44-
45-
test('Scenario #8', catchFullMessage(
46-
fn() => v::not(v::alnum('^'))->assert('abc^4'),
47-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "abc^4" must not contain letters (a-z), digits (0-9), or "^"'),
26+
test('Extra alnum template validation with additional characters', catchAll(
27+
fn() => v::alnum(' ')->assert('abc%2'),
28+
fn(string $message, string $fullMessage, array $messages) => expect()
29+
->and($message)->toBe('"abc%2" must contain only letters (a-z), digits (0-9), and " "')
30+
->and($fullMessage)->toBe('- "abc%2" must contain only letters (a-z), digits (0-9), and " "')
31+
->and($messages)->toBe(['alnum' => '"abc%2" must contain only letters (a-z), digits (0-9), and " "']),
32+
));
33+
34+
test('Extra alnum template validation with additional characters (inverted)', catchAll(
35+
fn() => v::not(v::alnum(' '))->assert('ab cd12'),
36+
fn(string $message, string $fullMessage, array $messages) => expect()
37+
->and($message)->toBe('"ab cd12" must not contain letters (a-z), digits (0-9), or " "')
38+
->and($fullMessage)->toBe('- "ab cd12" must not contain letters (a-z), digits (0-9), or " "')
39+
->and($messages)->toBe(['notAlnum' => '"ab cd12" must not contain letters (a-z), digits (0-9), or " "']),
4840
));

tests/feature/Validators/AlphaTest.php

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,34 @@
77

88
declare(strict_types=1);
99

10-
test('Scenario #1', catchMessage(
10+
test('Standard alpha template validation', catchAll(
1111
fn() => v::alpha()->assert('aaa%a'),
12-
fn(string $message) => expect($message)->toBe('"aaa%a" must contain only letters (a-z)'),
12+
fn(string $message, string $fullMessage, array $messages) => expect()
13+
->and($message)->toBe('"aaa%a" must contain only letters (a-z)')
14+
->and($fullMessage)->toBe('- "aaa%a" must contain only letters (a-z)')
15+
->and($messages)->toBe(['alpha' => '"aaa%a" must contain only letters (a-z)']),
1316
));
1417

15-
test('Scenario #2', catchMessage(
16-
fn() => v::alpha(' ')->assert('bbb%b'),
17-
fn(string $message) => expect($message)->toBe('"bbb%b" must contain only letters (a-z) and " "'),
18-
));
19-
20-
test('Scenario #3', catchMessage(
18+
test('Standard alpha template validation (inverted)', catchAll(
2119
fn() => v::not(v::alpha())->assert('ccccc'),
22-
fn(string $message) => expect($message)->toBe('"ccccc" must not contain letters (a-z)'),
23-
));
24-
25-
test('Scenario #4', catchMessage(
26-
fn() => v::not(v::alpha('% '))->assert('ddd%d'),
27-
fn(string $message) => expect($message)->toBe('"ddd%d" must not contain letters (a-z) or "% "'),
28-
));
29-
30-
test('Scenario #5', catchFullMessage(
31-
fn() => v::alpha()->assert('eee^e'),
32-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "eee^e" must contain only letters (a-z)'),
33-
));
34-
35-
test('Scenario #6', catchFullMessage(
36-
fn() => v::not(v::alpha())->assert('fffff'),
37-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "fffff" must not contain letters (a-z)'),
20+
fn(string $message, string $fullMessage, array $messages) => expect()
21+
->and($message)->toBe('"ccccc" must not contain letters (a-z)')
22+
->and($fullMessage)->toBe('- "ccccc" must not contain letters (a-z)')
23+
->and($messages)->toBe(['notAlpha' => '"ccccc" must not contain letters (a-z)']),
3824
));
3925

40-
test('Scenario #7', catchFullMessage(
41-
fn() => v::alpha('* &%')->assert('ggg^g'),
42-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "ggg^g" must contain only letters (a-z) and "* &%"'),
43-
));
44-
45-
test('Scenario #8', catchFullMessage(
46-
fn() => v::not(v::alpha('^'))->assert('hhh^h'),
47-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "hhh^h" must not contain letters (a-z) or "^"'),
26+
test('Extra alpha template validation with additional characters', catchAll(
27+
fn() => v::alpha(' ')->assert('bbb%b'),
28+
fn(string $message, string $fullMessage, array $messages) => expect()
29+
->and($message)->toBe('"bbb%b" must contain only letters (a-z) and " "')
30+
->and($fullMessage)->toBe('- "bbb%b" must contain only letters (a-z) and " "')
31+
->and($messages)->toBe(['alpha' => '"bbb%b" must contain only letters (a-z) and " "']),
32+
));
33+
34+
test('Extra alpha template validation with additional characters (inverted)', catchAll(
35+
fn() => v::not(v::alpha(' '))->assert('dddd'),
36+
fn(string $message, string $fullMessage, array $messages) => expect()
37+
->and($message)->toBe('"dddd" must not contain letters (a-z) or " "')
38+
->and($fullMessage)->toBe('- "dddd" must not contain letters (a-z) or " "')
39+
->and($messages)->toBe(['notAlpha' => '"dddd" must not contain letters (a-z) or " "']),
4840
));

tests/feature/Validators/AlwaysInvalidTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77

88
declare(strict_types=1);
99

10-
test('Scenario #1', catchMessage(
10+
use Respect\Validation\Validators\AlwaysInvalid;
11+
12+
test('Standard alwaysInvalid template validation', catchAll(
1113
fn() => v::alwaysInvalid()->assert('whatever'),
12-
fn(string $message) => expect($message)->toBe('"whatever" must be valid'),
14+
fn(string $message, string $fullMessage, array $messages) => expect()
15+
->and($message)->toBe('"whatever" must be valid')
16+
->and($fullMessage)->toBe('- "whatever" must be valid')
17+
->and($messages)->toBe(['alwaysInvalid' => '"whatever" must be valid']),
1318
));
1419

15-
test('Scenario #2', catchFullMessage(
16-
fn() => v::alwaysInvalid()->assert(''),
17-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "" must be valid'),
20+
test('Simple alwaysInvalid template validation', catchAll(
21+
fn() => v::templated(AlwaysInvalid::TEMPLATE_SIMPLE, v::alwaysInvalid())->assert('whatever'),
22+
fn(string $message, string $fullMessage, array $messages) => expect()
23+
->and($message)->toBe('"whatever" is invalid')
24+
->and($fullMessage)->toBe('- "whatever" is invalid')
25+
->and($messages)->toBe(['alwaysInvalid' => '"whatever" is invalid']),
1826
));

tests/feature/Validators/BeetwenTest.php

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
test('Standard between template validation', catchAll(
11+
fn() => v::between(1, 2)->assert(0),
12+
fn(string $message, string $fullMessage, array $messages) => expect()
13+
->and($message)->toBe('0 must be between 1 and 2')
14+
->and($fullMessage)->toBe('- 0 must be between 1 and 2')
15+
->and($messages)->toBe(['between' => '0 must be between 1 and 2']),
16+
));
17+
18+
test('Standard between template validation (inverted)', catchAll(
19+
fn() => v::not(v::between('yesterday', 'tomorrow'))->assert('today'),
20+
fn(string $message, string $fullMessage, array $messages) => expect()
21+
->and($message)->toBe('"today" must not be between "yesterday" and "tomorrow"')
22+
->and($fullMessage)->toBe('- "today" must not be between "yesterday" and "tomorrow"')
23+
->and($messages)->toBe(['notBetween' => '"today" must not be between "yesterday" and "tomorrow"']),
24+
));
25+
26+
test('Between template validation with characters', catchAll(
27+
fn() => v::between('a', 'c')->assert('d'),
28+
fn(string $message, string $fullMessage, array $messages) => expect()
29+
->and($message)->toBe('"d" must be between "a" and "c"')
30+
->and($fullMessage)->toBe('- "d" must be between "a" and "c"')
31+
->and($messages)->toBe(['between' => '"d" must be between "a" and "c"']),
32+
));
33+
34+
test('Between template validation with characters (inverted)', catchAll(
35+
fn() => v::not(v::between(-INF, INF))->assert(0),
36+
fn(string $message, string $fullMessage, array $messages) => expect()
37+
->and($message)->toBe('0 must not be between `-INF` and `INF`')
38+
->and($fullMessage)->toBe('- 0 must not be between `-INF` and `INF`')
39+
->and($messages)->toBe(['notBetween' => '0 must not be between `-INF` and `INF`']),
40+
));

tests/feature/Validators/CntrlTest.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

tests/feature/Validators/ConsonantTest.php

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,34 @@
77

88
declare(strict_types=1);
99

10-
test('Scenario #1', catchMessage(
10+
test('Standard consonant template validation', catchAll(
1111
fn() => v::consonant()->assert('aeiou'),
12-
fn(string $message) => expect($message)->toBe('"aeiou" must only contain consonants'),
12+
fn(string $message, string $fullMessage, array $messages) => expect()
13+
->and($message)->toBe('"aeiou" must only contain consonants')
14+
->and($fullMessage)->toBe('- "aeiou" must only contain consonants')
15+
->and($messages)->toBe(['consonant' => '"aeiou" must only contain consonants']),
1316
));
1417

15-
test('Scenario #2', catchMessage(
16-
fn() => v::consonant('d')->assert('daeiou'),
17-
fn(string $message) => expect($message)->toBe('"daeiou" must only contain consonants and "d"'),
18-
));
19-
20-
test('Scenario #3', catchMessage(
18+
test('Standard consonant template validation (inverted)', catchAll(
2119
fn() => v::not(v::consonant())->assert('bcd'),
22-
fn(string $message) => expect($message)->toBe('"bcd" must not contain consonants'),
23-
));
24-
25-
test('Scenario #4', catchMessage(
26-
fn() => v::not(v::consonant('a'))->assert('abcd'),
27-
fn(string $message) => expect($message)->toBe('"abcd" must not contain consonants or "a"'),
20+
fn(string $message, string $fullMessage, array $messages) => expect()
21+
->and($message)->toBe('"bcd" must not contain consonants')
22+
->and($fullMessage)->toBe('- "bcd" must not contain consonants')
23+
->and($messages)->toBe(['notConsonant' => '"bcd" must not contain consonants']),
2824
));
2925

30-
test('Scenario #5', catchFullMessage(
31-
fn() => v::consonant()->assert('aeiou'),
32-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "aeiou" must only contain consonants'),
33-
));
34-
35-
test('Scenario #6', catchFullMessage(
26+
test('Extra consonant template validation with additional characters', catchAll(
3627
fn() => v::consonant('d')->assert('daeiou'),
37-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "daeiou" must only contain consonants and "d"'),
38-
));
39-
40-
test('Scenario #7', catchFullMessage(
41-
fn() => v::not(v::consonant())->assert('bcd'),
42-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "bcd" must not contain consonants'),
28+
fn(string $message, string $fullMessage, array $messages) => expect()
29+
->and($message)->toBe('"daeiou" must only contain consonants and "d"')
30+
->and($fullMessage)->toBe('- "daeiou" must only contain consonants and "d"')
31+
->and($messages)->toBe(['consonant' => '"daeiou" must only contain consonants and "d"']),
4332
));
4433

45-
test('Scenario #8', catchFullMessage(
34+
test('Extra consonant template validation with additional characters (inverted)', catchAll(
4635
fn() => v::not(v::consonant('a'))->assert('abcd'),
47-
fn(string $fullMessage) => expect($fullMessage)->toBe('- "abcd" must not contain consonants or "a"'),
36+
fn(string $message, string $fullMessage, array $messages) => expect()
37+
->and($message)->toBe('"abcd" must not contain consonants or "a"')
38+
->and($fullMessage)->toBe('- "abcd" must not contain consonants or "a"')
39+
->and($messages)->toBe(['notConsonant' => '"abcd" must not contain consonants or "a"']),
4840
));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
test('Standard control template validation', catchAll(
11+
fn() => v::control()->assert('16-50'),
12+
fn(string $message, string $fullMessage, array $messages) => expect()
13+
->and($message)->toBe('"16-50" must only contain control characters')
14+
->and($fullMessage)->toBe('- "16-50" must only contain control characters')
15+
->and($messages)->toBe(['control' => '"16-50" must only contain control characters']),
16+
));
17+
18+
test('Standard control template validation (inverted)', catchAll(
19+
fn() => v::not(v::control())->assert("\n"),
20+
fn(string $message, string $fullMessage, array $messages) => expect()
21+
->and($message)->toBe('"\\n" must not contain control characters')
22+
->and($fullMessage)->toBe('- "\\n" must not contain control characters')
23+
->and($messages)->toBe(['notControl' => '"\\n" must not contain control characters']),
24+
));
25+
26+
test('Extra control template validation with additional characters', catchAll(
27+
fn() => v::control('16')->assert('16-50'),
28+
fn(string $message, string $fullMessage, array $messages) => expect()
29+
->and($message)->toBe('"16-50" must only contain control characters and "16"')
30+
->and($fullMessage)->toBe('- "16-50" must only contain control characters and "16"')
31+
->and($messages)->toBe(['control' => '"16-50" must only contain control characters and "16"']),
32+
));
33+
34+
test('Extra control template validation with additional characters (inverted)', catchAll(
35+
fn() => v::not(v::control('16'))->assert("16\n"),
36+
fn(string $message, string $fullMessage, array $messages) => expect()
37+
->and($message)->toBe('"16\\n" must not contain control characters or "16"')
38+
->and($fullMessage)->toBe('- "16\\n" must not contain control characters or "16"')
39+
->and($messages)->toBe(['notControl' => '"16\\n" must not contain control characters or "16"']),
40+
));

0 commit comments

Comments
 (0)