mirror of
https://github.com/Respect/Validation.git
synced 2026-03-14 22:35:45 +01:00
This commit introduces REUSE compliance by annotating all files with SPDX information and placing the reused licences in the LICENSES folder. We additionally removed the docheader tool which is made obsolete by this change. The main LICENSE and copyright text of the project is now not under my personal name anymore, and it belongs to "The Respect Project Contributors" instead. This change restores author names to several files, giving the appropriate attribution for contributions.
116 lines
3.2 KiB
PHP
116 lines
3.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
|
|
function catchAll(Closure $try, Closure $catch): Closure
|
|
{
|
|
return function () use ($try, $catch): void {
|
|
try {
|
|
$try->call($this);
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $e) {
|
|
$catch->call($this, $e->getMessage(), $e->getFullMessage(), $e->getMessages());
|
|
}
|
|
};
|
|
}
|
|
|
|
function catchMessage(Closure $try, Closure $catch): Closure
|
|
{
|
|
return function () use ($try, $catch): void {
|
|
try {
|
|
$try->call($this);
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $e) {
|
|
$catch->call($this, $e->getMessage());
|
|
}
|
|
};
|
|
}
|
|
|
|
function catchFullMessage(Closure $try, Closure $catch): Closure
|
|
{
|
|
return function () use ($try, $catch): void {
|
|
try {
|
|
$try->call($this);
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $e) {
|
|
$catch->call($this, $e->getFullMessage());
|
|
}
|
|
};
|
|
}
|
|
|
|
function catchMessages(Closure $try, Closure $catch): Closure
|
|
{
|
|
return function () use ($try, $catch): void {
|
|
try {
|
|
$try->call($this);
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $e) {
|
|
$catch->call($this, $e->getMessages());
|
|
}
|
|
};
|
|
}
|
|
|
|
function expectDeprecation(Closure $callback, string $error): Closure
|
|
{
|
|
return function () use ($callback, $error): void {
|
|
$lastError = null;
|
|
set_error_handler(static function (int $errno, string $errstr) use (&$lastError): bool {
|
|
if ($errno !== E_USER_DEPRECATED) {
|
|
return false;
|
|
}
|
|
|
|
$lastError = $errstr;
|
|
|
|
return true;
|
|
});
|
|
|
|
try {
|
|
$callback->call($this);
|
|
} catch (Throwable $throwable) {
|
|
restore_error_handler();
|
|
|
|
throw $throwable;
|
|
}
|
|
|
|
restore_error_handler();
|
|
expect($lastError)->toBe($error);
|
|
};
|
|
}
|
|
|
|
function expectMessageAndDeprecation(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 {
|
|
if ($errno !== E_USER_DEPRECATED) {
|
|
return false;
|
|
}
|
|
|
|
$lastError = $errstr;
|
|
|
|
return true;
|
|
});
|
|
try {
|
|
$callback->call($this);
|
|
test()->expectException(ValidationException::class);
|
|
} catch (ValidationException $e) {
|
|
expect($e->getMessage())->toBe($message, 'Validation message does not match');
|
|
} catch (Throwable $throwable) {
|
|
restore_error_handler();
|
|
|
|
throw $throwable;
|
|
}
|
|
|
|
restore_error_handler();
|
|
|
|
expect($lastError)->toBe($error);
|
|
};
|
|
}
|