respect-validation/tests/integration/lib/helpers.php
Henrique Moody 2ae1df177a
Allow to customise messages while asserting
Because we now have a single "assert()" method, we have more freedom to
add more customizations to it. This specific one is handy if someone
wants to use the library to validate but wants to use their own
exceptions.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-26 15:04:04 +01:00

62 lines
1.9 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validatable;
use Symfony\Component\VarExporter\VarExporter;
use function Respect\Stringifier\stringify;
function exceptionMessage(callable $callable, string $fallbackMessage = 'No exception was thrown'): void
{
try {
$callable();
echo $fallbackMessage . PHP_EOL;
} catch (ValidationException $exception) {
echo $exception->getMessage() . PHP_EOL;
}
}
function exceptionMessages(callable $callable, string $fallbackMessage = 'No exception was thrown'): void
{
try {
$callable();
echo $fallbackMessage . PHP_EOL;
} catch (ValidationException $exception) {
echo VarExporter::export($exception->getMessages()) . PHP_EOL;
}
}
function exceptionFullMessage(callable $callable, string $fallbackMessage = 'No exception was thrown'): void
{
try {
$callable();
echo $fallbackMessage . PHP_EOL;
} catch (ValidationException $exception) {
echo $exception->getFullMessage() . PHP_EOL;
}
}
/** @param array<string, array{0: Validatable, 1: mixed, 2?: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;
$fallbackMessage = 'No exception was thrown with: ' . stringify($input);
exceptionMessage(static fn() => $rule->assert($input, $template), $fallbackMessage);
exceptionFullMessage(static fn() => $rule->assert($input, $template), $fallbackMessage);
exceptionMessages(static fn() => $rule->assert($input, $template), $fallbackMessage);
echo PHP_EOL;
}
}