Skip to content

Commit

Permalink
Make it simpler to test multiple scenarios in PHPT
Browse files Browse the repository at this point in the history
With this change, we can test showing the main, full, and array messages
with a single definition. It also makes the scenario we're trying to
test clear, because we describe it with text.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 17, 2024
1 parent db11faf commit 2d8962c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
24 changes: 24 additions & 0 deletions tests/integration/lib/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validator;
use Symfony\Component\VarExporter\VarExporter;

use function Respect\Stringifier\stringify;

function exceptionMessage(callable $callable, string $fallbackMessage = 'No exception was thrown'): void
{
try {
Expand Down Expand Up @@ -46,3 +49,24 @@ function exceptionFullMessage(callable $callable, string $fallbackMessage = 'No
echo $exception->getFullMessage() . PHP_EOL;
}
}

/** @param array<string, array{Validator, mixed, null|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);
}

$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), is_array($template) ? $template : [], $fallbackMessage);
echo PHP_EOL;
}
}
75 changes: 63 additions & 12 deletions tests/integration/rules/allOf.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,68 @@ require 'vendor/autoload.php';

use Respect\Validation\Validator as v;

exceptionMessage(static fn() => v::not(v::allOf(v::intType(), v::positive()))->check(42));
exceptionMessage(static fn() => v::allOf(v::stringType(), v::consonant())->check('Luke i\'m your father'));
exceptionFullMessage(static fn() => v::allOf(v::stringType(), v::consonant())->assert(42));
exceptionFullMessage(static function (): void {
v::not(v::allOf(v::stringType(), v::length(10)))->assert('Frank Zappa is fantastic');
});
run([
'Single rule' => [v::allOf(v::stringType()), 1],
'Two rules' => [v::allOf(v::intType(), v::negative()), '2'],
'Wrapped by "not"' => [v::not(v::allOf(v::intType(), v::positive())), 3],
'Wrapping "not"' => [v::allOf(v::not(v::intType(), v::positive())), 4],
'With a single template' => [v::allOf(v::stringType()), 5, 'This is a single template'],
'With multiple templates' => [
v::allOf(v::stringType(), v::uppercase()),
5,
['allOf' => 'Unfortunately, we cannot template this'],
],
]);
?>
--EXPECT--
42 must not be of type integer
"Luke i'm your father" must contain only consonants
- All of the required rules must pass for 42
- 42 must be of type string
- 42 must contain only consonants
- "Frank Zappa is fantastic" must not be of type string
Single rule
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
1 must be of type string
- 1 must be of type string
[
'allOf' => '1 must be of type string',
]

Two rules
⎺⎺⎺⎺⎺⎺⎺⎺⎺
"2" must be of type integer
- All of the required rules must pass for "2"
- "2" must be of type integer
- "2" must be negative
[
'allOf' => '"2" must be negative',
]

Wrapped by "not"
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
3 must not be of type integer
- 3 must not be of type integer
[
'intType' => '3 must not be of type integer',
]

Wrapping "not"
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
4 must not be of type integer
- 4 must not be of type integer
[
'allOf' => '4 must not be of type integer',
]

With a single template
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
This is a single template
- This is a single template
[
'allOf' => 'This is a single template',
]

With multiple templates
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
5 must be of type string
- All of the required rules must pass for 5
- 5 must be of type string
- 5 must be uppercase
[
'allOf' => '5 must be uppercase',
]

0 comments on commit 2d8962c

Please sign in to comment.