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:
Henrique Moody 2024-02-11 18:34:50 +01:00
parent c6677fd7ce
commit d3b6b74f8d
No known key found for this signature in database
GPG key ID: 221E9281655813A6
14 changed files with 35 additions and 153 deletions

View file

@ -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 "__"

View file

@ -1,5 +1,4 @@
--FILE--
--FILE--
<?php
declare(strict_types=1);

View file

@ -5,35 +5,16 @@ declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\RecursiveExceptionIterator;
use Respect\Validation\Validator as v;
$input = (object) [
'author' => '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"
}

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -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`

View file

@ -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

View file

@ -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;
})
);