Skip to content

Commit

Permalink
Tests: add more tests for symfony/validator [#205]
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Feb 23, 2024
1 parent 816aa4f commit 33ec0d4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
63 changes: 54 additions & 9 deletions tests/Cases/Core/Mapping/Validator/SymfonyValidator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,77 @@ use Tests\Fixtures\Mapping\Validator\SimpleEntity;
Toolkit::test(function (): void {
$validator = new SymfonyValidator(new AnnotationReader());

$entity = (new SimpleEntity())->factory(['id' => 1, 'typedId' => 1]);
$validator->validate($entity);
$entity = (new SimpleEntity())->factory([
'id' => 1,
'typedId1' => 1,
'typedId2' => 1,
'typedId3' => 1,
'typedId4' => 1,
]);

try {
$validator->validate($entity);
Assert::true(true);
} catch (ValidationException $e) {
Assert::fail('Validation should pass', null, null, $e);
}
});

// Invalid value
Toolkit::test(function (): void {
$validator = new SymfonyValidator(new AnnotationReader());

$entity = (new SimpleEntity())->factory(['id' => 1, 'typedId' => 'foo']);
$entity = (new SimpleEntity())->factory([
'id' => 1,
'typedId1' => 'foo',
'typedId2' => 'foo',
'typedId3' => 1,
'typedId4' => 1,
]);

Assert::exception(static function () use ($entity, $validator): void {
try {
$validator->validate($entity);
}, ValidationException::class);
Assert::fail('Validation should fail');
} catch (ValidationException $e) {
Assert::equal([
'validation' => [
'typedId1' => ['This value should be of type integer.'],
'typedId2' => ['This value should not be null.'],
],
], $e->getContext());
}
});

// Without annotation reader
Toolkit::test(function (): void {
$validator = new SymfonyValidator();

$entity = (new SimpleEntity())->factory(['id' => null, 'typedId' => 'foo']);
$entity = (new SimpleEntity())->factory([
'id' => null,
'typedId1' => 1,
'typedId2' => 'foo',
'typedId3' => 1,
'typedId4' => 1,
]);

Assert::exception(static function () use ($entity, $validator): void {
try {
$validator->validate($entity);
}, ValidationException::class);
Assert::fail('Validation should fail');
} catch (ValidationException $e) {
Assert::equal([
'validation' => [
'typedId2' => ['This value should not be null.'],
],
], $e->getContext());
}

$entity = (new SimpleEntity())->factory(['id' => null, 'typedId' => 1]);
$entity = (new SimpleEntity())->factory([
'id' => null,
'typedId1' => 1,
'typedId2' => 1,
'typedId3' => 1,
'typedId4' => 1,
]);

Assert::noError(static function () use ($entity, $validator): void {
$validator->validate($entity);
Expand Down
12 changes: 11 additions & 1 deletion tests/Fixtures/Mapping/Validator/SimpleEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ final class SimpleEntity extends BasicEntity

#[Assert\NotNull]
#[Assert\Type(type: 'integer')]
public int $typedId;
public $typedId1;

#[Assert\NotNull]
#[Assert\Type(type: 'integer')]
public int $typedId2;

#[Assert\Type(type: 'integer')]
public ?int $typedId3;

#[Assert\Type(type: 'integer')]
public ?int $typedId4 = null;

}

0 comments on commit 33ec0d4

Please sign in to comment.