tests
This commit is contained in:
parent
6b1a385292
commit
bba10b455f
10 changed files with 296 additions and 514 deletions
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Tests;
|
||||
|
||||
use Icefox\DTO\Attributes\Flat;
|
||||
use Icefox\DTO\Attributes\Overwrite;
|
||||
use Icefox\DTO\RuleFactory;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
|
|
@ -157,3 +158,54 @@ test('annotated collection', function () {
|
|||
'group.*.value' => ['required', 'numeric'],
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
readonly class MergedRules
|
||||
{
|
||||
public function __construct(public int $value, public string $text) {}
|
||||
|
||||
/**
|
||||
* @return array<string,array<int,string>>
|
||||
*/
|
||||
public static function rules(): array
|
||||
{
|
||||
// only customized fields need to be provided
|
||||
return [
|
||||
'value' => ['min:20'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
test('merging rules', function () {
|
||||
expect(RuleFactory::instance()->make(MergedRules::class))->toBe([
|
||||
'value' => ['required', 'numeric', 'min:20'],
|
||||
'text' => ['required'],
|
||||
]);
|
||||
});
|
||||
|
||||
readonly class OverwriteRules
|
||||
{
|
||||
// union types are not supported, generated rules are undefined
|
||||
public function __construct(public int|bool $value, public string $text) {}
|
||||
|
||||
/**
|
||||
* @return array<string,array<int,mixed>>
|
||||
*/
|
||||
#[Overwrite]
|
||||
public static function rules(): array
|
||||
{
|
||||
// when overwriting, all fields must be provided with all rules, disables rules inference.
|
||||
return [
|
||||
'value' => ['required', function ($attribute, $value, $fail) {
|
||||
if (!is_int($value) && !is_bool($value)) {
|
||||
$fail("$attribute must be an integer or an array.");
|
||||
}
|
||||
}],
|
||||
'text' => ['required'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
test('overwriting rules', function () {
|
||||
expect(RuleFactory::instance()->make(OverwriteRules::class))->toHaveKeys(['value', 'text']);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue