mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
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 <henriquemoody@gmail.com>
This commit is contained in:
parent
c6677fd7ce
commit
d3b6b74f8d
|
@ -7,18 +7,11 @@ declare(strict_types=1);
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Exceptions\ValidationException;
|
|
||||||
use Respect\Validation\Validator;
|
use Respect\Validation\Validator;
|
||||||
|
|
||||||
$usernameValidator = Validator::alnum('__')->length(1, 15)->noWhitespace();
|
exceptionMessage(
|
||||||
try {
|
static fn () => Validator::alnum('__')->length(1, 15)->noWhitespace()->check('really messed up screen#name')
|
||||||
$usernameValidator->check('really messed up screen#name');
|
);
|
||||||
} catch (NestedValidationException $e) {
|
|
||||||
echo 'Check used NestedValidationException';
|
|
||||||
} catch (ValidationException $e) {
|
|
||||||
echo $e->getMessage();
|
|
||||||
}
|
|
||||||
?>
|
?>
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
"really messed up screen#name" must contain only letters (a-z), digits (0-9) and "__"
|
"really messed up screen#name" must contain only letters (a-z), digits (0-9) and "__"
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
--FILE--
|
--FILE--
|
||||||
--FILE--
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
|
@ -5,35 +5,16 @@ declare(strict_types=1);
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Exceptions\RecursiveExceptionIterator;
|
|
||||||
use Respect\Validation\Validator as v;
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
$input = (object) [
|
exceptionFullMessage(static function (): void {
|
||||||
'author' => 'foo',
|
v::property('title', v::length(2, 3)->stringType())
|
||||||
];
|
|
||||||
|
|
||||||
$postValidator = v::property('title', v::length(2, 3)->stringType())
|
|
||||||
->property('description', v::stringType())
|
->property('description', v::stringType())
|
||||||
->property('author', v::intType()->length(1, 2))
|
->property('author', v::intType()->length(1, 2))
|
||||||
->property('user', v::intVal()->length(1, 2));
|
->property('user', v::intVal()->length(1, 2))
|
||||||
try {
|
->assert((object) ['author' => 'foo']);
|
||||||
$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(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
?>
|
?>
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
- All of the required rules must pass for `stdClass { +$author="foo" }`
|
- All of the required rules must pass for `stdClass { +$author="foo" }`
|
||||||
|
@ -43,46 +24,3 @@ try {
|
||||||
- author must be of type integer
|
- author must be of type integer
|
||||||
- author must have a length between 1 and 2
|
- author must have a length between 1 and 2
|
||||||
- Property user must be present
|
- 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"
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,11 +5,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use Respect\Validation\Rules\Instance;
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
exceptionMessage(static function (): void {
|
exceptionMessage(static fn() => v::instance(stdClass::class)->setTemplate('invalid object')->assert('test'));
|
||||||
(new Instance('stdClass'))->setTemplate('invalid object')->assert('test');
|
|
||||||
});
|
|
||||||
?>
|
?>
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
invalid object
|
invalid object
|
||||||
|
|
|
@ -5,21 +5,19 @@ declare(strict_types=1);
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
|
exceptionMessages(
|
||||||
try {
|
static fn() => v::alnum()
|
||||||
$usernameValidator->assert('really messed up screen#name');
|
->noWhitespace()
|
||||||
} catch (NestedValidationException $exception) {
|
->length(1, 15)
|
||||||
print_r(
|
->assert('really messed up screen#name'),
|
||||||
$exception->getMessages([
|
[
|
||||||
'alnum' => '{{name}} must contain only letters and digits',
|
'alnum' => '{{name}} must contain only letters and digits',
|
||||||
'noWhitespace' => '{{name}} cannot contain spaces',
|
'noWhitespace' => '{{name}} cannot contain spaces',
|
||||||
'length' => '{{name}} must not have more than 15 chars',
|
'length' => '{{name}} must not have more than 15 chars',
|
||||||
])
|
]
|
||||||
);
|
);
|
||||||
}
|
|
||||||
?>
|
?>
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
Array
|
Array
|
||||||
|
|
|
@ -5,15 +5,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
|
exceptionFullMessage(static fn() => v::alnum()->noWhitespace()->length(1, 15)->assert('really messed up screen#name'));
|
||||||
try {
|
|
||||||
$usernameValidator->assert('really messed up screen#name');
|
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
echo $exception->getFullMessage();
|
|
||||||
}
|
|
||||||
?>
|
?>
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
- All of the required rules must pass for "really messed up screen#name"
|
- All of the required rules must pass for "really messed up screen#name"
|
||||||
|
|
|
@ -5,15 +5,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
|
exceptionMessages(static fn () => v::alnum()->noWhitespace()->length(1, 15)->assert('really messed up screen#name'));
|
||||||
try {
|
|
||||||
$usernameValidator->assert('really messed up screen#name');
|
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
print_r($exception->getMessages());
|
|
||||||
}
|
|
||||||
?>
|
?>
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
Array
|
Array
|
||||||
|
|
|
@ -5,15 +5,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
|
exceptionMessages(static fn() => v::alnum()->noWhitespace()->length(1, 15)->assert('really messed up screen#name'));
|
||||||
try {
|
|
||||||
$usernameValidator->assert('really messed up screen#name');
|
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
print_r($exception->getMessages());
|
|
||||||
}
|
|
||||||
?>
|
?>
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
Array
|
Array
|
||||||
|
|
|
@ -11,9 +11,13 @@ exceptionMessage(static fn() => v::between(1, 2)->check(0));
|
||||||
exceptionMessage(static fn() => v::not(v::between('yesterday', 'tomorrow'))->check('today'));
|
exceptionMessage(static fn() => v::not(v::between('yesterday', 'tomorrow'))->check('today'));
|
||||||
exceptionFullMessage(static fn() => v::between('a', 'c')->assert('d'));
|
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(-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--
|
--EXPECT--
|
||||||
0 must be between 1 and 2
|
0 must be between 1 and 2
|
||||||
"today" must not be between "yesterday" and "tomorrow"
|
"today" must not be between "yesterday" and "tomorrow"
|
||||||
- "d" must be between "a" and "c"
|
- "d" must be between "a" and "c"
|
||||||
- 0 must not be between `-INF` and `INF`
|
- 0 must not be between `-INF` and `INF`
|
||||||
|
- "a" must not be between "a" and "b"
|
||||||
|
- 41 must not be between 1 and 42
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Validator as v;
|
|
||||||
|
|
||||||
exceptionFullMessage(static fn() => v::not(v::between('a', 'b'))->assert('a'));
|
|
||||||
?>
|
|
||||||
--EXPECT--
|
|
||||||
- "a" must not be between "a" and "b"
|
|
|
@ -1,13 +0,0 @@
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Validator as v;
|
|
||||||
|
|
||||||
exceptionFullMessage(static fn() => v::not(v::intType()->between(1, 42))->assert(41));
|
|
||||||
?>
|
|
||||||
--EXPECT--
|
|
||||||
- 41 must not be between 1 and 42
|
|
|
@ -8,7 +8,7 @@ require 'vendor/autoload.php';
|
||||||
use Respect\Validation\Validator as v;
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
exceptionMessage(static fn() => v::call('trim', v::noWhitespace())->check(' two words '));
|
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([]));
|
exceptionMessage(static fn() => v::call('stripslashes', v::alwaysValid())->check([]));
|
||||||
exceptionFullMessage(static fn() => v::call('strval', v::intType())->assert(1234));
|
exceptionFullMessage(static fn() => v::call('strval', v::intType())->assert(1234));
|
||||||
exceptionFullMessage(static fn() => v::not(v::call('is_float', v::boolType()))->assert(1.2));
|
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--
|
--EXPECT--
|
||||||
"two words" must not contain whitespace
|
"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`
|
`[]` must be valid when executed with `stripslashes(string $string): string`
|
||||||
- "1234" must be of type integer
|
- "1234" must be of type integer
|
||||||
- 1.2 must not be valid when executed with `is_float(?mixed $value): bool`
|
- 1.2 must not be valid when executed with `is_float(?mixed $value): bool`
|
||||||
|
|
|
@ -5,18 +5,14 @@ declare(strict_types=1);
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
use Respect\Validation\Validator as v;
|
||||||
|
|
||||||
exceptionMessage(static fn() => v::callableType()->check([]));
|
exceptionMessage(static fn() => v::callableType()->check([]));
|
||||||
exceptionMessage(static fn() => v::not(v::callableType())->check('trim'));
|
exceptionMessage(static fn() => v::not(v::callableType())->check('trim'));
|
||||||
exceptionFullMessage(static fn() => v::callableType()->assert(true));
|
exceptionFullMessage(static fn() => v::callableType()->assert(true));
|
||||||
try {
|
exceptionFullMessage(static fn() => v::not(v::callableType())->assert(static function (): void {
|
||||||
v::not(v::callableType())->assert(static function (): void {
|
// Do nothing
|
||||||
});
|
}));
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
echo $exception->getFullMessage() . PHP_EOL;
|
|
||||||
}
|
|
||||||
?>
|
?>
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
`[]` must be callable
|
`[]` must be callable
|
||||||
|
|
|
@ -13,7 +13,7 @@ Factory::setDefaultInstance(
|
||||||
->withTranslator(static function (string $message): string {
|
->withTranslator(static function (string $message): string {
|
||||||
return [
|
return [
|
||||||
'{{name}} must be of type string' => '{{name}} deve ser do tipo string',
|
'{{name}} must be of type string' => '{{name}} deve ser do tipo string',
|
||||||
][$message];
|
][$message] ?? $message;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue