20
20
use Symfony \Component \HttpKernel \Exception \HttpException ;
21
21
use Symfony \Component \Serializer \Encoder \JsonEncoder ;
22
22
use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
23
+ use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
23
24
use Symfony \Component \Serializer \Serializer ;
24
25
use Symfony \Component \Serializer \SerializerInterface ;
25
26
use Symfony \Component \Validator \ConstraintViolation ;
@@ -39,7 +40,7 @@ public function testNotTypedArgument()
39
40
$ argument = new ArgumentMetadata ('notTyped ' , null , false , false , null , false , [
40
41
MapRequestPayload::class => new MapRequestPayload (),
41
42
]);
42
- $ request = Request::create ('/ ' , 'POST ' , server: ['HTTP_ACCEPT ' => 'application/json ' ]);
43
+ $ request = Request::create ('/ ' , 'POST ' , server: ['HTTP_CONTENT_TYPE ' => 'application/json ' ]);
43
44
44
45
$ this ->expectException (\LogicException::class);
45
46
$ this ->expectExceptionMessage ('Could not resolve the "$notTyped" controller argument: argument should be typed. ' );
@@ -49,18 +50,26 @@ public function testNotTypedArgument()
49
50
50
51
public function testValidationNotPassed ()
51
52
{
52
- $ serializer = new Serializer ([new DummyDenormalizer ()], ['json ' => new JsonEncoder ()]);
53
+ $ content = '{"price": 50} ' ;
54
+ $ payload = new RequestPayload (50 );
55
+ $ serializer = $ this ->createMock (DummySerializerDenormalizerInterface::class);
56
+ $ serializer ->expects ($ this ->once ())
57
+ ->method ('deserialize ' )
58
+ ->with ($ content )
59
+ ->willReturn ($ payload );
60
+
53
61
$ validator = $ this ->createMock (ValidatorInterface::class);
54
62
$ validator ->expects ($ this ->once ())
55
63
->method ('validate ' )
64
+ ->with ($ payload )
56
65
->willReturn (new ConstraintViolationList ([new ConstraintViolation ('Test ' , null , [], '' , null , '' )]));
57
66
58
67
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
59
68
60
- $ argument = new ArgumentMetadata ('invalid ' , \stdClass ::class, false , false , null , false , [
69
+ $ argument = new ArgumentMetadata ('invalid ' , RequestPayload ::class, false , false , null , false , [
61
70
MapRequestPayload::class => new MapRequestPayload (),
62
71
]);
63
- $ request = Request::create ('/ ' , 'POST ' , server: ['HTTP_ACCEPT ' => 'application/json ' ], content: ' ["abc"] ' );
72
+ $ request = Request::create ('/ ' , 'POST ' , server: ['HTTP_CONTENT_TYPE ' => 'application/json ' ], content: $ content );
64
73
65
74
try {
66
75
$ resolver ->resolve ($ request , $ argument );
@@ -93,7 +102,7 @@ public function testUnsupportedMedia()
93
102
94
103
public function testRequestContentValidationPassed ()
95
104
{
96
- $ content = '{"quantity ": 50} ' ;
105
+ $ content = '{"price ": 50} ' ;
97
106
$ payload = new RequestPayload (50 );
98
107
$ serializer = $ this ->createMock (DummySerializerDenormalizerInterface::class);
99
108
$ serializer ->expects ($ this ->once ())
@@ -108,7 +117,7 @@ public function testRequestContentValidationPassed()
108
117
109
118
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
110
119
111
- $ argument = new ArgumentMetadata ('valid ' , \stdClass ::class, false , false , null , false , [
120
+ $ argument = new ArgumentMetadata ('valid ' , RequestPayload ::class, false , false , null , false , [
112
121
MapRequestPayload::class => new MapRequestPayload (),
113
122
]);
114
123
$ request = Request::create ('/ ' , 'POST ' , content: $ content );
@@ -119,12 +128,9 @@ public function testRequestContentValidationPassed()
119
128
public function testQueryStringValidationPassed ()
120
129
{
121
130
$ payload = new RequestPayload (50 );
122
- $ query = ['quantity ' => 50 ];
123
- $ serializer = $ this ->createMock (Serializer::class);
124
- $ serializer ->expects ($ this ->once ())
125
- ->method ('denormalize ' )
126
- ->with ($ query )
127
- ->willReturn ($ payload );
131
+ $ query = ['price ' => '50 ' ];
132
+
133
+ $ serializer = new Serializer ([new ObjectNormalizer ()], ['json ' => new JsonEncoder ()]);
128
134
129
135
$ validator = $ this ->createMock (ValidatorInterface::class);
130
136
$ validator ->expects ($ this ->once ())
@@ -133,7 +139,7 @@ public function testQueryStringValidationPassed()
133
139
134
140
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
135
141
136
- $ argument = new ArgumentMetadata ('valid ' , \stdClass ::class, false , false , null , false , [
142
+ $ argument = new ArgumentMetadata ('valid ' , RequestPayload ::class, false , false , null , false , [
137
143
MapQueryString::class => new MapQueryString (),
138
144
]);
139
145
$ request = Request::create ('/ ' , 'GET ' , $ query );
@@ -143,13 +149,10 @@ public function testQueryStringValidationPassed()
143
149
144
150
public function testRequestInputValidationPassed ()
145
151
{
146
- $ input = ['quantity ' => 50 ];
152
+ $ input = ['price ' => ' 50 ' ];
147
153
$ payload = new RequestPayload (50 );
148
- $ serializer = $ this ->createMock (Serializer::class);
149
- $ serializer ->expects ($ this ->once ())
150
- ->method ('denormalize ' )
151
- ->with ($ input )
152
- ->willReturn ($ payload );
154
+
155
+ $ serializer = new Serializer ([new ObjectNormalizer ()], ['json ' => new JsonEncoder ()]);
153
156
154
157
$ validator = $ this ->createMock (ValidatorInterface::class);
155
158
$ validator ->expects ($ this ->once ())
@@ -158,7 +161,7 @@ public function testRequestInputValidationPassed()
158
161
159
162
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
160
163
161
- $ argument = new ArgumentMetadata ('valid ' , \stdClass ::class, false , false , null , false , [
164
+ $ argument = new ArgumentMetadata ('valid ' , RequestPayload ::class, false , false , null , false , [
162
165
MapRequestPayload::class => new MapRequestPayload (),
163
166
]);
164
167
$ request = Request::create ('/ ' , 'POST ' , $ input );
@@ -171,27 +174,9 @@ interface DummySerializerDenormalizerInterface extends SerializerInterface, Deno
171
174
{
172
175
}
173
176
174
- class DummyDenormalizer implements DenormalizerInterface
175
- {
176
- public function denormalize (mixed $ data , string $ type , string $ format = null , array $ context = []): mixed
177
- {
178
- return new \stdClass ();
179
- }
180
-
181
- public function getSupportedTypes (): array
182
- {
183
- return ['* ' => false ];
184
- }
185
-
186
- public function supportsDenormalization (mixed $ data , string $ type , string $ format = null , array $ context = []): bool
187
- {
188
- return true ;
189
- }
190
- }
191
-
192
177
class RequestPayload
193
178
{
194
- public function __construct (public readonly int $ quantity )
179
+ public function __construct (public readonly float $ price )
195
180
{
196
181
}
197
182
}
0 commit comments