Skip to content

Commit 33ec0d4

Browse files
committed
Tests: add more tests for symfony/validator [#205]
1 parent 816aa4f commit 33ec0d4

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

tests/Cases/Core/Mapping/Validator/SymfonyValidator.phpt

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,77 @@ use Tests\Fixtures\Mapping\Validator\SimpleEntity;
1313
Toolkit::test(function (): void {
1414
$validator = new SymfonyValidator(new AnnotationReader());
1515

16-
$entity = (new SimpleEntity())->factory(['id' => 1, 'typedId' => 1]);
17-
$validator->validate($entity);
16+
$entity = (new SimpleEntity())->factory([
17+
'id' => 1,
18+
'typedId1' => 1,
19+
'typedId2' => 1,
20+
'typedId3' => 1,
21+
'typedId4' => 1,
22+
]);
23+
24+
try {
25+
$validator->validate($entity);
26+
Assert::true(true);
27+
} catch (ValidationException $e) {
28+
Assert::fail('Validation should pass', null, null, $e);
29+
}
1830
});
1931

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

24-
$entity = (new SimpleEntity())->factory(['id' => 1, 'typedId' => 'foo']);
36+
$entity = (new SimpleEntity())->factory([
37+
'id' => 1,
38+
'typedId1' => 'foo',
39+
'typedId2' => 'foo',
40+
'typedId3' => 1,
41+
'typedId4' => 1,
42+
]);
2543

26-
Assert::exception(static function () use ($entity, $validator): void {
44+
try {
2745
$validator->validate($entity);
28-
}, ValidationException::class);
46+
Assert::fail('Validation should fail');
47+
} catch (ValidationException $e) {
48+
Assert::equal([
49+
'validation' => [
50+
'typedId1' => ['This value should be of type integer.'],
51+
'typedId2' => ['This value should not be null.'],
52+
],
53+
], $e->getContext());
54+
}
2955
});
3056

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

35-
$entity = (new SimpleEntity())->factory(['id' => null, 'typedId' => 'foo']);
61+
$entity = (new SimpleEntity())->factory([
62+
'id' => null,
63+
'typedId1' => 1,
64+
'typedId2' => 'foo',
65+
'typedId3' => 1,
66+
'typedId4' => 1,
67+
]);
3668

37-
Assert::exception(static function () use ($entity, $validator): void {
69+
try {
3870
$validator->validate($entity);
39-
}, ValidationException::class);
71+
Assert::fail('Validation should fail');
72+
} catch (ValidationException $e) {
73+
Assert::equal([
74+
'validation' => [
75+
'typedId2' => ['This value should not be null.'],
76+
],
77+
], $e->getContext());
78+
}
4079

41-
$entity = (new SimpleEntity())->factory(['id' => null, 'typedId' => 1]);
80+
$entity = (new SimpleEntity())->factory([
81+
'id' => null,
82+
'typedId1' => 1,
83+
'typedId2' => 1,
84+
'typedId3' => 1,
85+
'typedId4' => 1,
86+
]);
4287

4388
Assert::noError(static function () use ($entity, $validator): void {
4489
$validator->validate($entity);

tests/Fixtures/Mapping/Validator/SimpleEntity.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ final class SimpleEntity extends BasicEntity
1919

2020
#[Assert\NotNull]
2121
#[Assert\Type(type: 'integer')]
22-
public int $typedId;
22+
public $typedId1;
23+
24+
#[Assert\NotNull]
25+
#[Assert\Type(type: 'integer')]
26+
public int $typedId2;
27+
28+
#[Assert\Type(type: 'integer')]
29+
public ?int $typedId3;
30+
31+
#[Assert\Type(type: 'integer')]
32+
public ?int $typedId4 = null;
2333

2434
}

0 commit comments

Comments
 (0)