12
12
namespace Symfony \Component \Validator \Tests \Constraints ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Validator \Constraints \Choice ;
16
+ use Symfony \Component \Validator \Mapping \ClassMetadata ;
17
+ use Symfony \Component \Validator \Mapping \Loader \AnnotationLoader ;
15
18
use Symfony \Component \Validator \Tests \Fixtures \ConstraintChoiceWithPreset ;
16
19
17
20
class ChoiceTest extends TestCase
@@ -22,4 +25,50 @@ public function testSetDefaultPropertyChoice()
22
25
23
26
self ::assertEquals (['A ' , 'B ' , 'C ' ], $ constraint ->choices );
24
27
}
28
+
29
+ /**
30
+ * @requires PHP 8
31
+ */
32
+ public function testAttributes ()
33
+ {
34
+ $ metadata = new ClassMetadata (ChoiceDummy::class);
35
+ $ loader = new AnnotationLoader ();
36
+ self ::assertTrue ($ loader ->loadClassMetadata ($ metadata ));
37
+
38
+ /** @var Choice $aConstraint */
39
+ [$ aConstraint ] = $ metadata ->properties ['a ' ]->getConstraints ();
40
+ self ::assertSame ([1 , 2 ], $ aConstraint ->choices );
41
+ self ::assertSame (['Default ' , 'ChoiceDummy ' ], $ aConstraint ->groups );
42
+
43
+ /** @var Choice $bConstraint */
44
+ [$ bConstraint ] = $ metadata ->properties ['b ' ]->getConstraints ();
45
+ self ::assertSame (['foo ' , 'bar ' ], $ bConstraint ->choices );
46
+ self ::assertSame ('myMessage ' , $ bConstraint ->message );
47
+ self ::assertSame (['Default ' , 'ChoiceDummy ' ], $ bConstraint ->groups );
48
+
49
+ /** @var Choice $cConstraint */
50
+ [$ cConstraint ] = $ metadata ->properties ['c ' ]->getConstraints ();
51
+ self ::assertSame ([1 , 2 ], $ aConstraint ->choices );
52
+ self ::assertSame (['my_group ' ], $ cConstraint ->groups );
53
+ self ::assertSame ('some attached data ' , $ cConstraint ->payload );
54
+
55
+ /** @var Choice $stringIndexedConstraint */
56
+ [$ stringIndexedConstraint ] = $ metadata ->properties ['stringIndexed ' ]->getConstraints ();
57
+ self ::assertSame (['one ' => 1 , 'two ' => 2 ], $ stringIndexedConstraint ->choices );
58
+ }
59
+ }
60
+
61
+ class ChoiceDummy
62
+ {
63
+ #[Choice(choices: [1 , 2 ])]
64
+ private $ a ;
65
+
66
+ #[Choice(choices: ['foo ' , 'bar ' ], message: 'myMessage ' )]
67
+ private $ b ;
68
+
69
+ #[Choice([1 , 2 ], groups: ['my_group ' ], payload: 'some attached data ' )]
70
+ private $ c ;
71
+
72
+ #[Choice(choices: ['one ' => 1 , 'two ' => 2 ])]
73
+ private $ stringIndexed ;
25
74
}
0 commit comments