18
18
use PHPUnit \Framework \TestCase ;
19
19
use Prophecy \Argument ;
20
20
use Prophecy \PhpUnit \ProphecyTrait ;
21
+ use Symfony \Component \Serializer \NameConverter \AdvancedNameConverterInterface ;
21
22
use Symfony \Component \Serializer \NameConverter \NameConverterInterface ;
22
23
use Symfony \Component \Validator \Constraints \NotNull ;
23
24
use Symfony \Component \Validator \ConstraintViolation ;
@@ -44,17 +45,14 @@ public function testSupportNormalization(): void
44
45
}
45
46
46
47
/**
47
- * @dataProvider payloadFieldsProvider
48
+ * @dataProvider nameConverterAndPayloadFieldsProvider
48
49
*/
49
- public function testNormalize (?array $ fields , array $ result ): void
50
+ public function testNormalize (?object $ nameConverter , ? array $ fields , array $ expected ): void
50
51
{
51
52
$ urlGeneratorProphecy = $ this ->prophesize (UrlGeneratorInterface::class);
52
- $ nameConverterProphecy = $ this ->prophesize (NameConverterInterface::class);
53
-
54
53
$ urlGeneratorProphecy ->generate ('api_jsonld_context ' , ['shortName ' => 'ConstraintViolationList ' ])->willReturn ('/context/foo ' )->shouldBeCalled ();
55
- $ nameConverterProphecy ->normalize (Argument::type ('string ' ), null , Argument::type ('string ' ))->will (fn ($ args ): string => '_ ' .$ args [0 ]);
56
54
57
- $ normalizer = new ConstraintViolationListNormalizer ($ urlGeneratorProphecy ->reveal (), $ fields , $ nameConverterProphecy -> reveal () );
55
+ $ normalizer = new ConstraintViolationListNormalizer ($ urlGeneratorProphecy ->reveal (), $ fields , $ nameConverter );
58
56
59
57
// Note : we use NotNull constraint and not Constraint class because Constraint is abstract
60
58
$ constraint = new NotNull ();
@@ -64,7 +62,31 @@ public function testNormalize(?array $fields, array $result): void
64
62
new ConstraintViolation ('1 ' , '2 ' , [], '3 ' , '4 ' , '5 ' ),
65
63
]);
66
64
67
- $ expected = [
65
+ $ this ->assertSame ($ expected , $ normalizer ->normalize ($ list ));
66
+ }
67
+
68
+ public function nameConverterAndPayloadFieldsProvider (): iterable
69
+ {
70
+ $ basicExpectation = [
71
+ '@context ' => '/context/foo ' ,
72
+ '@type ' => 'ConstraintViolationList ' ,
73
+ 'hydra:title ' => 'An error occurred ' ,
74
+ 'hydra:description ' => "d: a \n4: 1 " ,
75
+ 'violations ' => [
76
+ [
77
+ 'propertyPath ' => 'd ' ,
78
+ 'message ' => 'a ' ,
79
+ 'code ' => 'f24bdbad0becef97a6887238aa58221c ' ,
80
+ ],
81
+ [
82
+ 'propertyPath ' => '4 ' ,
83
+ 'message ' => '1 ' ,
84
+ 'code ' => null ,
85
+ ],
86
+ ],
87
+ ];
88
+
89
+ $ nameConverterBasedExpectation = [
68
90
'@context ' => '/context/foo ' ,
69
91
'@type ' => 'ConstraintViolationList ' ,
70
92
'hydra:title ' => 'An error occurred ' ,
@@ -82,17 +104,35 @@ public function testNormalize(?array $fields, array $result): void
82
104
],
83
105
],
84
106
];
85
- if ([] !== $ result ) {
86
- $ expected ['violations ' ][0 ]['payload ' ] = $ result ;
87
- }
88
107
89
- $ this ->assertSame ($ expected , $ normalizer ->normalize ($ list ));
90
- }
108
+ $ advancedNameConverterProphecy = $ this ->prophesize (AdvancedNameConverterInterface::class);
109
+ $ advancedNameConverterProphecy ->normalize (Argument::type ('string ' ), null , Argument::type ('string ' ))->will (fn ($ args ): string => '_ ' .$ args [0 ]);
110
+ $ advancedNameConverter = $ advancedNameConverterProphecy ->reveal ();
91
111
92
- public function payloadFieldsProvider (): iterable
93
- {
94
- yield [['severity ' , 'anotherField1 ' ], ['severity ' => 'warning ' ]];
95
- yield [null , ['severity ' => 'warning ' , 'anotherField2 ' => 'aValue ' ]];
96
- yield [[], []];
112
+ $ nameConverterProphecy = $ this ->prophesize (NameConverterInterface::class);
113
+ $ nameConverterProphecy ->normalize (Argument::type ('string ' ))->will (fn ($ args ): string => '_ ' .$ args [0 ]);
114
+ $ nameConverter = $ nameConverterProphecy ->reveal ();
115
+
116
+ $ nullNameConverter = null ;
117
+
118
+ $ expected = $ nameConverterBasedExpectation ;
119
+ $ expected ['violations ' ][0 ]['payload ' ] = ['severity ' => 'warning ' ];
120
+ yield [$ advancedNameConverter , ['severity ' , 'anotherField1 ' ], $ expected ];
121
+ yield [$ nameConverter , ['severity ' , 'anotherField1 ' ], $ expected ];
122
+ $ expected = $ basicExpectation ;
123
+ $ expected ['violations ' ][0 ]['payload ' ] = ['severity ' => 'warning ' ];
124
+ yield [$ nullNameConverter , ['severity ' , 'anotherField1 ' ], $ expected ];
125
+
126
+ $ expected = $ nameConverterBasedExpectation ;
127
+ $ expected ['violations ' ][0 ]['payload ' ] = ['severity ' => 'warning ' , 'anotherField2 ' => 'aValue ' ];
128
+ yield [$ advancedNameConverter , null , $ expected ];
129
+ yield [$ nameConverter , null , $ expected ];
130
+ $ expected = $ basicExpectation ;
131
+ $ expected ['violations ' ][0 ]['payload ' ] = ['severity ' => 'warning ' , 'anotherField2 ' => 'aValue ' ];
132
+ yield [$ nullNameConverter , null , $ expected ];
133
+
134
+ yield [$ advancedNameConverter , [], $ nameConverterBasedExpectation ];
135
+ yield [$ nameConverter , [], $ nameConverterBasedExpectation ];
136
+ yield [$ nullNameConverter , [], $ basicExpectation ];
97
137
}
98
138
}
0 commit comments