mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 23:35:45 +01:00
Although I love PHPT files, and I've done my fair share of making it easier to write them in this library, they're very slow, and running them has become a hindrance. I've been fidgeting with the idea of using Pest for a while, and I think it's the right tool for the job. I had to create a couple of functions to make it easier to run those tests, and now they're working really alright. I migrated all the PHPT files into Pest files -- I automated most of the work with a little script using "nikic/php-parser"; this commit should contain all the previous PHPT tests as Pest tests. The previous integration tests would take sixteen seconds, and the Pest tests take less than a second.
104 lines
3.6 KiB
PHP
104 lines
3.6 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 Symfony\Component\VarExporter\VarExporter;
|
|
|
|
use function PHPUnit\Framework\assertStringMatchesFormat;
|
|
|
|
/** @param array<string, mixed> $messages */
|
|
function expectAll(callable $callback, string $message, string $fullMessage, array $messages): Closure
|
|
{
|
|
return function () use ($callback, $message, $fullMessage, $messages): void {
|
|
try {
|
|
$callback();
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $e) {
|
|
expect($e->getMessage())->toBe($message)
|
|
->and($e->getFullMessage())->toBe($fullMessage)
|
|
->and($e->getMessages())->toBe($messages);
|
|
}
|
|
};
|
|
}
|
|
|
|
/** @param array<string, mixed> $messages */
|
|
function expectAllToMatch(callable $callback, string $message, string $fullMessage, array $messages): Closure
|
|
{
|
|
return function () use ($callback, $message, $fullMessage, $messages): void {
|
|
try {
|
|
$callback();
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $e) {
|
|
assertStringMatchesFormat($message, $e->getMessage(), 'Validation message does not match');
|
|
assertStringMatchesFormat($fullMessage, $e->getFullMessage(), 'Validation full message does not match');
|
|
assertStringMatchesFormat(
|
|
VarExporter::export($messages),
|
|
VarExporter::export($e->getMessages()),
|
|
'Validation messages do not match'
|
|
);
|
|
}
|
|
};
|
|
}
|
|
|
|
function expectMessage(callable $callback, string $message): Closure
|
|
{
|
|
return function () use ($callback, $message): void {
|
|
try {
|
|
$callback();
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $e) {
|
|
expect($e->getMessage())->toBe($message, 'Validation message does not match');
|
|
}
|
|
};
|
|
}
|
|
|
|
function expectFullMessage(callable $callback, string $fullMessage): Closure
|
|
{
|
|
return function () use ($callback, $fullMessage): void {
|
|
try {
|
|
$callback();
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $exception) {
|
|
expect($exception->getFullMessage())->toBe($fullMessage, 'Validation full message does not match');
|
|
}
|
|
};
|
|
}
|
|
|
|
/** @param array<string, mixed> $messages */
|
|
function expectMessages(callable $callback, array $messages): Closure
|
|
{
|
|
return function () use ($callback, $messages): void {
|
|
try {
|
|
$callback();
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $exception) {
|
|
expect($exception->getMessages())->toBe($messages, 'Validation messages do not match');
|
|
}
|
|
};
|
|
}
|
|
|
|
function expectMessageAndError(Closure $callback, string $message, string $error): Closure
|
|
{
|
|
return function () use ($callback, $message, $error): void {
|
|
$lastError = null;
|
|
set_error_handler(static function (int $errno, string $errstr) use (&$lastError): bool {
|
|
$lastError = $errstr;
|
|
|
|
return true;
|
|
});
|
|
try {
|
|
$callback();
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $e) {
|
|
expect($e->getMessage())->toBe($message, 'Validation message does not match');
|
|
}
|
|
restore_error_handler();
|
|
expect($lastError)->toBe($error);
|
|
};
|
|
}
|