Skip to content

Commit

Permalink
Standardize integration tests
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
henriquemoody committed Feb 11, 2024
1 parent c6677fd commit d3b6b74
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 "__"
1 change: 0 additions & 1 deletion tests/integration/get_messages_with_replacements.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
--FILE--
--FILE--
<?php

declare(strict_types=1);
Expand Down
74 changes: 6 additions & 68 deletions tests/integration/issue-1376.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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" }`
Expand All @@ -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"
}
6 changes: 2 additions & 4 deletions tests/integration/issue-619.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 11 additions & 13 deletions tests/integration/readme/custom_messages.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions tests/integration/readme/example_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 1 addition & 7 deletions tests/integration/readme/example_4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions tests/integration/readme/getting_messages_as_an_array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/rules/beetwen.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 0 additions & 13 deletions tests/integration/rules/beetwen_5.phpt

This file was deleted.

13 changes: 0 additions & 13 deletions tests/integration/rules/beetwen_6.phpt

This file was deleted.

4 changes: 2 additions & 2 deletions tests/integration/rules/call.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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));
exceptionFullMessage(static fn() => v::call('array_shift', v::alwaysValid())->assert(INF));
?>
--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`
Expand Down
10 changes: 3 additions & 7 deletions tests/integration/rules/callableType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/translator-check.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
);

Expand Down

0 comments on commit d3b6b74

Please sign in to comment.