12
12
namespace Symfony \Component \Validator \Tests \Mapping ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Validator \Constraints \Collection ;
16
+ use Symfony \Component \Validator \Constraints \Composite ;
17
+ use Symfony \Component \Validator \Constraints \Required ;
15
18
use Symfony \Component \Validator \Constraints \Valid ;
19
+ use Symfony \Component \Validator \Exception \ConstraintDefinitionException ;
16
20
use Symfony \Component \Validator \Mapping \MemberMetadata ;
17
21
use Symfony \Component \Validator \Tests \Fixtures \ClassConstraint ;
18
22
use Symfony \Component \Validator \Tests \Fixtures \ConstraintA ;
19
23
use Symfony \Component \Validator \Tests \Fixtures \ConstraintB ;
24
+ use Symfony \Component \Validator \Tests \Fixtures \PropertyConstraint ;
20
25
21
26
class MemberMetadataTest extends TestCase
22
27
{
@@ -43,6 +48,34 @@ public function testAddConstraintRequiresClassConstraints()
43
48
$ this ->metadata ->addConstraint (new ClassConstraint ());
44
49
}
45
50
51
+ public function testAddCompositeConstraintRejectsNestedClassConstraints ()
52
+ {
53
+ $ this ->expectException (ConstraintDefinitionException::class);
54
+ $ this ->expectExceptionMessage ('The constraint "Symfony\Component\Validator\Tests\Fixtures\ClassConstraint" cannot be put on properties or getters. ' );
55
+
56
+ $ this ->metadata ->addConstraint (new PropertyCompositeConstraint ([new ClassConstraint ()]));
57
+ }
58
+
59
+ public function testAddCompositeConstraintRejectsDeepNestedClassConstraints ()
60
+ {
61
+ $ this ->expectException (ConstraintDefinitionException::class);
62
+ $ this ->expectExceptionMessage ('The constraint "Symfony\Component\Validator\Tests\Fixtures\ClassConstraint" cannot be put on properties or getters. ' );
63
+
64
+ $ this ->metadata ->addConstraint (new Collection (['field1 ' => new Required ([new ClassConstraint ()])]));
65
+ }
66
+
67
+ public function testAddCompositeConstraintAcceptsNestedPropertyConstraints ()
68
+ {
69
+ $ this ->metadata ->addConstraint ($ constraint = new PropertyCompositeConstraint ([new PropertyConstraint ()]));
70
+ $ this ->assertSame ($ this ->metadata ->getConstraints (), [$ constraint ]);
71
+ }
72
+
73
+ public function testAddCompositeConstraintAcceptsDeepNestedPropertyConstraints ()
74
+ {
75
+ $ this ->metadata ->addConstraint ($ constraint = new Collection (['field1 ' => new Required ([new PropertyConstraint ()])]));
76
+ $ this ->assertSame ($ this ->metadata ->getConstraints (), [$ constraint ]);
77
+ }
78
+
46
79
public function testSerialize ()
47
80
{
48
81
$ this ->metadata ->addConstraint (new ConstraintA (['property1 ' => 'A ' ]));
@@ -82,3 +115,18 @@ protected function newReflectionMember($object)
82
115
{
83
116
}
84
117
}
118
+
119
+ class PropertyCompositeConstraint extends Composite
120
+ {
121
+ public $ nested ;
122
+
123
+ public function getDefaultOption ()
124
+ {
125
+ return $ this ->getCompositeOption ();
126
+ }
127
+
128
+ protected function getCompositeOption ()
129
+ {
130
+ return 'nested ' ;
131
+ }
132
+ }
0 commit comments