Improve output of PHPT tests

The `print_r()` is noisy and doesn't show values as they are in PHP, and
using `VarExporter::export()` improved the output a lot.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-02-17 16:11:59 +01:00
parent 7199c5e440
commit db11faf129
No known key found for this signature in database
GPG key ID: 221E9281655813A6
12 changed files with 78 additions and 90 deletions

View file

@ -35,7 +35,8 @@
"respect/coding-standard": "^4.0",
"sokil/php-isocodes": "^4.2",
"sokil/php-isocodes-db-only": "^4.0",
"squizlabs/php_codesniffer": "^3.8"
"squizlabs/php_codesniffer": "^3.8",
"symfony/var-exporter": "^6.4 || ^7.0"
},
"suggest": {
"ext-bcmath": "Arbitrary Precision Mathematics",

View file

@ -40,22 +40,17 @@ exceptionMessages(static function (): void {
});
?>
--EXPECT--
Array
(
[mysql] => Array
(
[host] => host must be of type string
[user] => user must be present
[password] => password must be present
[schema] => schema must be of type string
)
[postgresql] => Array
(
[host] => host must be present
[user] => user must be of type string
[password] => password must be of type string
[schema] => schema must be present
)
)
[
'mysql' => [
'host' => 'host must be of type string',
'user' => 'user must be present',
'password' => 'password must be present',
'schema' => 'schema must be of type string',
],
'postgresql' => [
'host' => 'host must be present',
'user' => 'user must be of type string',
'password' => 'password must be of type string',
'schema' => 'schema must be present',
],
]

View file

@ -27,10 +27,9 @@ exceptionMessages(static function (): void {
});
?>
--EXPECT--
Array
(
[username] => username must have a length between 2 and 32
[birthdate] => birthdate must be a valid date/time
[password] => password must not be empty
[email] => email must be present
)
[
'username' => 'username must have a length between 2 and 32',
'birthdate' => 'birthdate must be a valid date/time',
'password' => 'password must not be empty',
'email' => 'email must be present',
]

View file

@ -51,22 +51,17 @@ exceptionMessages(
);
?>
--EXPECT--
Array
(
[mysql] => Array
(
[host] => 42 should be a MySQL host
[user] => Value should be a MySQL username
[password] => password must be present
[schema] => schema must be of type string
)
[postgresql] => Array
(
[host] => host must be present
[user] => user must be of type string
[password] => password must be of type string
[schema] => You must provide a valid PostgreSQL schema
)
)
[
'mysql' => [
'host' => '42 should be a MySQL host',
'user' => 'Value should be a MySQL username',
'password' => 'password must be present',
'schema' => 'schema must be of type string',
],
'postgresql' => [
'host' => 'host must be present',
'user' => 'user must be of type string',
'password' => 'password must be of type string',
'schema' => 'You must provide a valid PostgreSQL schema',
],
]

View file

@ -10,7 +10,6 @@ use Respect\Validation\Validator as v;
exceptionMessages(static fn () => v::key('firstname', v::notBlank()->setName('First Name'))->assert([]));
?>
--EXPECTF--
Array
(
[First Name] => First Name must be present
)
[
'First Name' => 'First Name must be present',
]

View file

@ -28,12 +28,9 @@ exceptionMessages(static function () use ($cars): void {
});
?>
--EXPECT--
Array
(
[each] => Array
(
[validator.0] => All of the required rules must pass for `["manufacturer": "Ford", "model": "not real"]`
[validator.1] => All of the required rules must pass for `["manufacturer": "Honda", "model": "not valid"]`
)
)
[
'each' => [
'validator.0' => 'All of the required rules must pass for `["manufacturer": "Ford", "model": "not real"]`',
'validator.1' => 'All of the required rules must pass for `["manufacturer": "Honda", "model": "not valid"]`',
],
]

View file

@ -20,7 +20,6 @@ exceptionMessages(static function () use ($arr): void {
});
?>
--EXPECT--
Array
(
[name] => name must have a length between 2 and 32
)
[
'name' => 'name must have a length between 2 and 32',
]

View file

@ -12,7 +12,6 @@ exceptionMessages(static function (): void {
});
?>
--EXPECT--
Array
(
[email] => WRONG EMAIL!!!!!!
)
[
'email' => 'WRONG EMAIL!!!!!!',
]

View file

@ -9,11 +9,13 @@ declare(strict_types=1);
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\ValidationException;
use Symfony\Component\VarExporter\VarExporter;
function exceptionMessage(callable $callable): void
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;
}
@ -22,19 +24,24 @@ function exceptionMessage(callable $callable): void
/**
* @param array<string, array<string, string>> $templates
*/
function exceptionMessages(callable $callable, array $templates = []): void
{
function exceptionMessages(
callable $callable,
array $templates = [],
string $fallbackMessage = 'No exception was thrown'
): void {
try {
$callable();
echo $fallbackMessage . PHP_EOL;
} catch (NestedValidationException $exception) {
print_r($exception->getMessages($templates));
echo VarExporter::export($exception->getMessages($templates)) . PHP_EOL;
}
}
function exceptionFullMessage(callable $callable): void
function exceptionFullMessage(callable $callable, string $fallbackMessage = 'No exception was thrown'): void
{
try {
$callable();
echo $fallbackMessage . PHP_EOL;
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage() . PHP_EOL;
}

View file

@ -20,9 +20,8 @@ exceptionMessages(
);
?>
--EXPECT--
Array
(
[alnum] => "really messed up screen#name" must contain only letters and digits
[noWhitespace] => "really messed up screen#name" cannot contain spaces
[length] => "really messed up screen#name" must not have more than 15 chars
)
[
'alnum' => '"really messed up screen#name" must contain only letters and digits',
'noWhitespace' => '"really messed up screen#name" cannot contain spaces',
'length' => '"really messed up screen#name" must not have more than 15 chars',
]

View file

@ -10,9 +10,8 @@ use Respect\Validation\Validator as v;
exceptionMessages(static fn () => v::alnum()->noWhitespace()->length(1, 15)->assert('really messed up screen#name'));
?>
--EXPECT--
Array
(
[alnum] => "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
[noWhitespace] => "really messed up screen#name" must not contain whitespace
[length] => "really messed up screen#name" must have a length between 1 and 15
)
[
'alnum' => '"really messed up screen#name" must contain only letters (a-z) and digits (0-9)',
'noWhitespace' => '"really messed up screen#name" must not contain whitespace',
'length' => '"really messed up screen#name" must have a length between 1 and 15',
]

View file

@ -10,9 +10,8 @@ use Respect\Validation\Validator as v;
exceptionMessages(static fn() => v::alnum()->noWhitespace()->length(1, 15)->assert('really messed up screen#name'));
?>
--EXPECT--
Array
(
[alnum] => "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
[noWhitespace] => "really messed up screen#name" must not contain whitespace
[length] => "really messed up screen#name" must have a length between 1 and 15
)
[
'alnum' => '"really messed up screen#name" must contain only letters (a-z) and digits (0-9)',
'noWhitespace' => '"really messed up screen#name" must not contain whitespace',
'length' => '"really messed up screen#name" must have a length between 1 and 15',
]