18
18
use Symfony \Component \HttpKernel \Controller \ArgumentResolver \RequestPayloadValueResolver ;
19
19
use Symfony \Component \HttpKernel \ControllerMetadata \ArgumentMetadata ;
20
20
use Symfony \Component \HttpKernel \Exception \HttpException ;
21
- use Symfony \Component \Serializer \Encoder \JsonEncoder ;
22
21
use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
23
22
use Symfony \Component \Serializer \Serializer ;
24
23
use Symfony \Component \Serializer \SerializerInterface ;
@@ -39,7 +38,7 @@ public function testNotTypedArgument()
39
38
$ argument = new ArgumentMetadata ('notTyped ' , null , false , false , null , false , [
40
39
MapRequestPayload::class => new MapRequestPayload (),
41
40
]);
42
- $ request = Request::create ('/ ' , 'POST ' , server: ['HTTP_ACCEPT ' => 'application/json ' ]);
41
+ $ request = Request::create ('/ ' , 'POST ' , server: ['HTTP_CONTENT_TYPE ' => 'application/json ' ]);
43
42
44
43
$ this ->expectException (\LogicException::class);
45
44
$ this ->expectExceptionMessage ('Could not resolve the "$notTyped" controller argument: argument should be typed. ' );
@@ -49,18 +48,26 @@ public function testNotTypedArgument()
49
48
50
49
public function testValidationNotPassed ()
51
50
{
52
- $ serializer = new Serializer ([new DummyDenormalizer ()], ['json ' => new JsonEncoder ()]);
51
+ $ content = '{"quantity": 50} ' ;
52
+ $ payload = new RequestPayload (50 );
53
+ $ serializer = $ this ->createMock (DummySerializerDenormalizerInterface::class);
54
+ $ serializer ->expects ($ this ->once ())
55
+ ->method ('deserialize ' )
56
+ ->with ($ content )
57
+ ->willReturn ($ payload );
58
+
53
59
$ validator = $ this ->createMock (ValidatorInterface::class);
54
60
$ validator ->expects ($ this ->once ())
55
61
->method ('validate ' )
62
+ ->with ($ payload )
56
63
->willReturn (new ConstraintViolationList ([new ConstraintViolation ('Test ' , null , [], '' , null , '' )]));
57
64
58
65
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
59
66
60
- $ argument = new ArgumentMetadata ('invalid ' , \stdClass ::class, false , false , null , false , [
67
+ $ argument = new ArgumentMetadata ('invalid ' , RequestPayload ::class, false , false , null , false , [
61
68
MapRequestPayload::class => new MapRequestPayload (),
62
69
]);
63
- $ request = Request::create ('/ ' , 'POST ' , server: ['HTTP_ACCEPT ' => 'application/json ' ], content: ' ["abc"] ' );
70
+ $ request = Request::create ('/ ' , 'POST ' , server: ['HTTP_CONTENT_TYPE ' => 'application/json ' ], content: $ content );
64
71
65
72
try {
66
73
$ resolver ->resolve ($ request , $ argument );
@@ -171,24 +178,6 @@ interface DummySerializerDenormalizerInterface extends SerializerInterface, Deno
171
178
{
172
179
}
173
180
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
181
class RequestPayload
193
182
{
194
183
public function __construct (public readonly int $ quantity )
0 commit comments