Make it simpler to test multiple scenarios in PHPT

With this change, we can test showing the main, full, and array messages
with a single definition. It also makes the scenario we're trying to
test clear, because we describe it with text.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-02-17 16:35:19 +01:00
parent db11faf129
commit 2d8962c6e9
No known key found for this signature in database
GPG key ID: 221E9281655813A6
2 changed files with 87 additions and 12 deletions

View file

@ -9,8 +9,11 @@ declare(strict_types=1);
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validator;
use Symfony\Component\VarExporter\VarExporter;
use function Respect\Stringifier\stringify;
function exceptionMessage(callable $callable, string $fallbackMessage = 'No exception was thrown'): void
{
try {
@ -46,3 +49,24 @@ function exceptionFullMessage(callable $callable, string $fallbackMessage = 'No
echo $exception->getFullMessage() . PHP_EOL;
}
}
/** @param array<string, array{Validator, mixed, null|string|array<string, mixed>}> $scenarios */
function run(array $scenarios): void
{
foreach ($scenarios as $description => $data) {
[$rule, $input, $template] = array_pad($data, 3, null);
echo $description . PHP_EOL;
echo str_repeat('⎺', strlen($description)) . PHP_EOL;
if (is_string($template)) {
$rule->setTemplate($template);
}
$fallbackMessage = 'No exception was thrown with: ' . stringify($input);
exceptionMessage(static fn() => $rule->check($input), $fallbackMessage);
exceptionFullMessage(static fn() => $rule->assert($input), $fallbackMessage);
exceptionMessages(static fn() => $rule->assert($input), is_array($template) ? $template : [], $fallbackMessage);
echo PHP_EOL;
}
}

View file

@ -7,17 +7,68 @@ require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
exceptionMessage(static fn() => v::not(v::allOf(v::intType(), v::positive()))->check(42));
exceptionMessage(static fn() => v::allOf(v::stringType(), v::consonant())->check('Luke i\'m your father'));
exceptionFullMessage(static fn() => v::allOf(v::stringType(), v::consonant())->assert(42));
exceptionFullMessage(static function (): void {
v::not(v::allOf(v::stringType(), v::length(10)))->assert('Frank Zappa is fantastic');
});
run([
'Single rule' => [v::allOf(v::stringType()), 1],
'Two rules' => [v::allOf(v::intType(), v::negative()), '2'],
'Wrapped by "not"' => [v::not(v::allOf(v::intType(), v::positive())), 3],
'Wrapping "not"' => [v::allOf(v::not(v::intType(), v::positive())), 4],
'With a single template' => [v::allOf(v::stringType()), 5, 'This is a single template'],
'With multiple templates' => [
v::allOf(v::stringType(), v::uppercase()),
5,
['allOf' => 'Unfortunately, we cannot template this'],
],
]);
?>
--EXPECT--
42 must not be of type integer
"Luke i'm your father" must contain only consonants
- All of the required rules must pass for 42
- 42 must be of type string
- 42 must contain only consonants
- "Frank Zappa is fantastic" must not be of type string
Single rule
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
1 must be of type string
- 1 must be of type string
[
'allOf' => '1 must be of type string',
]
Two rules
⎺⎺⎺⎺⎺⎺⎺⎺⎺
"2" must be of type integer
- All of the required rules must pass for "2"
- "2" must be of type integer
- "2" must be negative
[
'allOf' => '"2" must be negative',
]
Wrapped by "not"
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
3 must not be of type integer
- 3 must not be of type integer
[
'intType' => '3 must not be of type integer',
]
Wrapping "not"
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
4 must not be of type integer
- 4 must not be of type integer
[
'allOf' => '4 must not be of type integer',
]
With a single template
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
This is a single template
- This is a single template
[
'allOf' => 'This is a single template',
]
With multiple templates
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
5 must be of type string
- All of the required rules must pass for 5
- 5 must be of type string
- 5 must be uppercase
[
'allOf' => '5 must be uppercase',
]