From d3b6b74f8dd44ed856b72d6d5a0d6d0540a26019 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Sun, 11 Feb 2024 18:34:50 +0100 Subject: [PATCH] Standardize integration tests Some integration tests did not use the standard functions for testing exceptions in PHPT files. This change will make all integration tests more uniform, which will help me change the internal validation engine later. This change will also unify the integration tests for "Between" into a single file and a few other improvements. Signed-off-by: Henrique Moody --- ...idation_exception_interface_for_check.phpt | 13 +--- .../get_messages_with_replacements.phpt | 1 - tests/integration/issue-1376.phpt | 74 ++----------------- tests/integration/issue-619.phpt | 6 +- tests/integration/readme/custom_messages.phpt | 24 +++--- tests/integration/readme/example_2.phpt | 8 +- tests/integration/readme/example_4.phpt | 8 +- .../readme/getting_messages_as_an_array.phpt | 8 +- tests/integration/rules/beetwen.phpt | 4 + tests/integration/rules/beetwen_5.phpt | 13 ---- tests/integration/rules/beetwen_6.phpt | 13 ---- tests/integration/rules/call.phpt | 4 +- tests/integration/rules/callableType.phpt | 10 +-- tests/integration/translator-check.phpt | 2 +- 14 files changed, 35 insertions(+), 153 deletions(-) delete mode 100644 tests/integration/rules/beetwen_5.phpt delete mode 100644 tests/integration/rules/beetwen_6.phpt diff --git a/tests/integration/do_not_rely_on_nested_validation_exception_interface_for_check.phpt b/tests/integration/do_not_rely_on_nested_validation_exception_interface_for_check.phpt index c605a019..3b8ff372 100644 --- a/tests/integration/do_not_rely_on_nested_validation_exception_interface_for_check.phpt +++ b/tests/integration/do_not_rely_on_nested_validation_exception_interface_for_check.phpt @@ -7,18 +7,11 @@ declare(strict_types=1); require 'vendor/autoload.php'; -use Respect\Validation\Exceptions\NestedValidationException; -use Respect\Validation\Exceptions\ValidationException; use Respect\Validation\Validator; -$usernameValidator = Validator::alnum('__')->length(1, 15)->noWhitespace(); -try { - $usernameValidator->check('really messed up screen#name'); -} catch (NestedValidationException $e) { - echo 'Check used NestedValidationException'; -} catch (ValidationException $e) { - echo $e->getMessage(); -} +exceptionMessage( + static fn () => Validator::alnum('__')->length(1, 15)->noWhitespace()->check('really messed up screen#name') +); ?> --EXPECT-- "really messed up screen#name" must contain only letters (a-z), digits (0-9) and "__" diff --git a/tests/integration/get_messages_with_replacements.phpt b/tests/integration/get_messages_with_replacements.phpt index f32f71ce..4b6ebd7f 100644 --- a/tests/integration/get_messages_with_replacements.phpt +++ b/tests/integration/get_messages_with_replacements.phpt @@ -1,5 +1,4 @@ --FILE-- ---FILE-- 'foo', -]; - -$postValidator = v::property('title', v::length(2, 3)->stringType()) +exceptionFullMessage(static function (): void { + v::property('title', v::length(2, 3)->stringType()) ->property('description', v::stringType()) ->property('author', v::intType()->length(1, 2)) - ->property('user', v::intVal()->length(1, 2)); -try { - $postValidator->assert($input); -} catch (NestedValidationException $e) { - echo $e->getFullMessage(); - echo PHP_EOL; - $explorer = new RecursiveIteratorIterator( - new RecursiveExceptionIterator($e), - RecursiveIteratorIterator::SELF_FIRST - ); - echo PHP_EOL; - foreach ($explorer as $innerException) { - var_dump([ - 'level' => $explorer->getDepth(), - 'message' => $innerException->getMessage(), - ]); - } -} + ->property('user', v::intVal()->length(1, 2)) + ->assert((object) ['author' => 'foo']); +}); + ?> --EXPECT-- - All of the required rules must pass for `stdClass { +$author="foo" }` @@ -43,46 +24,3 @@ try { - author must be of type integer - author must have a length between 1 and 2 - Property user must be present - -array(2) { - ["level"]=> - int(0) - ["message"]=> - string(30) "Property title must be present" -} -array(2) { - ["level"]=> - int(0) - ["message"]=> - string(36) "Property description must be present" -} -array(2) { - ["level"]=> - int(0) - ["message"]=> - string(29) "Property author must be valid" -} -array(2) { - ["level"]=> - int(1) - ["message"]=> - string(46) "All of the required rules must pass for author" -} -array(2) { - ["level"]=> - int(2) - ["message"]=> - string(30) "author must be of type integer" -} -array(2) { - ["level"]=> - int(2) - ["message"]=> - string(41) "author must have a length between 1 and 2" -} -array(2) { - ["level"]=> - int(0) - ["message"]=> - string(29) "Property user must be present" -} diff --git a/tests/integration/issue-619.phpt b/tests/integration/issue-619.phpt index d29379fe..9a87bec6 100644 --- a/tests/integration/issue-619.phpt +++ b/tests/integration/issue-619.phpt @@ -5,11 +5,9 @@ declare(strict_types=1); require 'vendor/autoload.php'; -use Respect\Validation\Rules\Instance; +use Respect\Validation\Validator as v; -exceptionMessage(static function (): void { - (new Instance('stdClass'))->setTemplate('invalid object')->assert('test'); -}); +exceptionMessage(static fn() => v::instance(stdClass::class)->setTemplate('invalid object')->assert('test')); ?> --EXPECT-- invalid object diff --git a/tests/integration/readme/custom_messages.phpt b/tests/integration/readme/custom_messages.phpt index 14023dbc..c1279dc1 100644 --- a/tests/integration/readme/custom_messages.phpt +++ b/tests/integration/readme/custom_messages.phpt @@ -5,21 +5,19 @@ declare(strict_types=1); require 'vendor/autoload.php'; -use Respect\Validation\Exceptions\NestedValidationException; use Respect\Validation\Validator as v; -$usernameValidator = v::alnum()->noWhitespace()->length(1, 15); -try { - $usernameValidator->assert('really messed up screen#name'); -} catch (NestedValidationException $exception) { - print_r( - $exception->getMessages([ - 'alnum' => '{{name}} must contain only letters and digits', - 'noWhitespace' => '{{name}} cannot contain spaces', - 'length' => '{{name}} must not have more than 15 chars', - ]) - ); -} +exceptionMessages( + static fn() => v::alnum() + ->noWhitespace() + ->length(1, 15) + ->assert('really messed up screen#name'), + [ + 'alnum' => '{{name}} must contain only letters and digits', + 'noWhitespace' => '{{name}} cannot contain spaces', + 'length' => '{{name}} must not have more than 15 chars', + ] +); ?> --EXPECT-- Array diff --git a/tests/integration/readme/example_2.phpt b/tests/integration/readme/example_2.phpt index 875192f4..317c7c93 100644 --- a/tests/integration/readme/example_2.phpt +++ b/tests/integration/readme/example_2.phpt @@ -5,15 +5,9 @@ declare(strict_types=1); require 'vendor/autoload.php'; -use Respect\Validation\Exceptions\NestedValidationException; use Respect\Validation\Validator as v; -$usernameValidator = v::alnum()->noWhitespace()->length(1, 15); -try { - $usernameValidator->assert('really messed up screen#name'); -} catch (NestedValidationException $exception) { - echo $exception->getFullMessage(); -} +exceptionFullMessage(static fn() => v::alnum()->noWhitespace()->length(1, 15)->assert('really messed up screen#name')); ?> --EXPECT-- - All of the required rules must pass for "really messed up screen#name" diff --git a/tests/integration/readme/example_4.phpt b/tests/integration/readme/example_4.phpt index d1a743b2..d2386422 100644 --- a/tests/integration/readme/example_4.phpt +++ b/tests/integration/readme/example_4.phpt @@ -5,15 +5,9 @@ declare(strict_types=1); require 'vendor/autoload.php'; -use Respect\Validation\Exceptions\NestedValidationException; use Respect\Validation\Validator as v; -$usernameValidator = v::alnum()->noWhitespace()->length(1, 15); -try { - $usernameValidator->assert('really messed up screen#name'); -} catch (NestedValidationException $exception) { - print_r($exception->getMessages()); -} +exceptionMessages(static fn () => v::alnum()->noWhitespace()->length(1, 15)->assert('really messed up screen#name')); ?> --EXPECT-- Array diff --git a/tests/integration/readme/getting_messages_as_an_array.phpt b/tests/integration/readme/getting_messages_as_an_array.phpt index d1a743b2..2b273dcd 100644 --- a/tests/integration/readme/getting_messages_as_an_array.phpt +++ b/tests/integration/readme/getting_messages_as_an_array.phpt @@ -5,15 +5,9 @@ declare(strict_types=1); require 'vendor/autoload.php'; -use Respect\Validation\Exceptions\NestedValidationException; use Respect\Validation\Validator as v; -$usernameValidator = v::alnum()->noWhitespace()->length(1, 15); -try { - $usernameValidator->assert('really messed up screen#name'); -} catch (NestedValidationException $exception) { - print_r($exception->getMessages()); -} +exceptionMessages(static fn() => v::alnum()->noWhitespace()->length(1, 15)->assert('really messed up screen#name')); ?> --EXPECT-- Array diff --git a/tests/integration/rules/beetwen.phpt b/tests/integration/rules/beetwen.phpt index 4c764c13..995b1381 100644 --- a/tests/integration/rules/beetwen.phpt +++ b/tests/integration/rules/beetwen.phpt @@ -11,9 +11,13 @@ exceptionMessage(static fn() => v::between(1, 2)->check(0)); exceptionMessage(static fn() => v::not(v::between('yesterday', 'tomorrow'))->check('today')); exceptionFullMessage(static fn() => v::between('a', 'c')->assert('d')); exceptionFullMessage(static fn() => v::not(v::between(-INF, INF))->assert(0)); +exceptionFullMessage(static fn() => v::not(v::between('a', 'b'))->assert('a')); +exceptionFullMessage(static fn() => v::not(v::between(1, 42))->assert(41)); ?> --EXPECT-- 0 must be between 1 and 2 "today" must not be between "yesterday" and "tomorrow" - "d" must be between "a" and "c" - 0 must not be between `-INF` and `INF` +- "a" must not be between "a" and "b" +- 41 must not be between 1 and 42 diff --git a/tests/integration/rules/beetwen_5.phpt b/tests/integration/rules/beetwen_5.phpt deleted file mode 100644 index f1e580b6..00000000 --- a/tests/integration/rules/beetwen_5.phpt +++ /dev/null @@ -1,13 +0,0 @@ ---FILE-- - v::not(v::between('a', 'b'))->assert('a')); -?> ---EXPECT-- -- "a" must not be between "a" and "b" diff --git a/tests/integration/rules/beetwen_6.phpt b/tests/integration/rules/beetwen_6.phpt deleted file mode 100644 index e88bd086..00000000 --- a/tests/integration/rules/beetwen_6.phpt +++ /dev/null @@ -1,13 +0,0 @@ ---FILE-- - v::not(v::intType()->between(1, 42))->assert(41)); -?> ---EXPECT-- -- 41 must not be between 1 and 42 diff --git a/tests/integration/rules/call.phpt b/tests/integration/rules/call.phpt index 35707aed..42301b32 100644 --- a/tests/integration/rules/call.phpt +++ b/tests/integration/rules/call.phpt @@ -8,7 +8,7 @@ require 'vendor/autoload.php'; use Respect\Validation\Validator as v; exceptionMessage(static fn() => v::call('trim', v::noWhitespace())->check(' two words ')); -exceptionMessage(static fn() => v::not(v::call('stripslashes', v::stringType()))->check(' something ')); +exceptionMessage(static fn() => v::not(v::call('stripslashes', v::stringType()))->check(' some\\thing ')); exceptionMessage(static fn() => v::call('stripslashes', v::alwaysValid())->check([])); exceptionFullMessage(static fn() => v::call('strval', v::intType())->assert(1234)); exceptionFullMessage(static fn() => v::not(v::call('is_float', v::boolType()))->assert(1.2)); @@ -16,7 +16,7 @@ exceptionFullMessage(static fn() => v::call('array_shift', v::alwaysValid())->as ?> --EXPECT-- "two words" must not contain whitespace -" something " must not be valid when executed with `stripslashes(string $string): string` +" some\\thing " must not be valid when executed with `stripslashes(string $string): string` `[]` must be valid when executed with `stripslashes(string $string): string` - "1234" must be of type integer - 1.2 must not be valid when executed with `is_float(?mixed $value): bool` diff --git a/tests/integration/rules/callableType.phpt b/tests/integration/rules/callableType.phpt index f6c5a07b..b5829c50 100644 --- a/tests/integration/rules/callableType.phpt +++ b/tests/integration/rules/callableType.phpt @@ -5,18 +5,14 @@ declare(strict_types=1); require 'vendor/autoload.php'; -use Respect\Validation\Exceptions\NestedValidationException; use Respect\Validation\Validator as v; exceptionMessage(static fn() => v::callableType()->check([])); exceptionMessage(static fn() => v::not(v::callableType())->check('trim')); exceptionFullMessage(static fn() => v::callableType()->assert(true)); -try { - v::not(v::callableType())->assert(static function (): void { - }); -} catch (NestedValidationException $exception) { - echo $exception->getFullMessage() . PHP_EOL; -} +exceptionFullMessage(static fn() => v::not(v::callableType())->assert(static function (): void { + // Do nothing +})); ?> --EXPECT-- `[]` must be callable diff --git a/tests/integration/translator-check.phpt b/tests/integration/translator-check.phpt index 7416fd85..9686237c 100644 --- a/tests/integration/translator-check.phpt +++ b/tests/integration/translator-check.phpt @@ -13,7 +13,7 @@ Factory::setDefaultInstance( ->withTranslator(static function (string $message): string { return [ '{{name}} must be of type string' => '{{name}} deve ser do tipo string', - ][$message]; + ][$message] ?? $message; }) );