respect-validation/tests/integration/lib/helpers.php
Henrique Moody 66faefd695
Remove previous validation engine
After many refactorings, no rules use the previous validation engine.
That means we can remove the unused code from the repository and switch
from the previous to the new validation engine everywhere.

This commit will also soft deprecate the methods "validate()", and
"check()" in all the rules and the "assert()" in all rules but the
Validator itself. That means using those methods will still be allowed,
but static analysis tools might complain.

This is a big step toward releasing the next major version, as the code
is pretty much the way it should be when I release the next version.
There's some documentation to be updated, and I would like to change the
behavior of a couple of rules.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-25 12:28:25 +01:00

70 lines
2.1 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;
if (is_string($template)) {
$rule->setTemplate($template);
}
if (is_array($template)) {
$rule->setTemplates($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), $fallbackMessage);
echo PHP_EOL;
}
}