respect-validation/tests/integration/lib/helpers.php
Henrique Moody e6af762fe4
Rename "Validatable" to "Rule"
Besides the interface's name, everything already calls this type "Rule",
not "Validatable." This commit puts a stone on it and renames the
interface for better naming.
2024-12-05 19:32:14 +01:00

61 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\Rule;
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: Rule, 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;
}
}