@@ -251,6 +251,7 @@ public function testValidationNotPassed()
251
251
$ this ->fail (sprintf ('Expected "%s" to be thrown. ' , HttpException::class));
252
252
} catch (HttpException $ e ) {
253
253
$ validationFailedException = $ e ->getPrevious ();
254
+ $ this ->assertEquals (404 , $ e ->getStatusCode ());
254
255
$ this ->assertInstanceOf (ValidationFailedException::class, $ validationFailedException );
255
256
$ this ->assertSame ('This value should be of type unknown. ' , $ validationFailedException ->getViolations ()[0 ]->getMessage ());
256
257
$ this ->assertSame ('Test ' , $ validationFailedException ->getViolations ()[1 ]->getMessage ());
@@ -633,6 +634,41 @@ public function testQueryValidationErrorCustomStatusCode()
633
634
$ this ->assertSame ('Page is invalid ' , $ validationFailedException ->getViolations ()[0 ]->getMessage ());
634
635
}
635
636
}
637
+
638
+ public function testRequestPayloadValidationErrorCustomStatusCode ()
639
+ {
640
+ $ content = '{"price": 50, "title": ["not a string"]} ' ;
641
+ $ payload = new RequestPayload (50 );
642
+ $ serializer = new Serializer ([new ObjectNormalizer ()], ['json ' => new JsonEncoder ()]);
643
+
644
+ $ validator = $ this ->createMock (ValidatorInterface::class);
645
+ $ validator ->expects ($ this ->once ())
646
+ ->method ('validate ' )
647
+ ->with ($ payload )
648
+ ->willReturn (new ConstraintViolationList ([new ConstraintViolation ('Test ' , null , [], '' , null , '' )]));
649
+
650
+ $ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
651
+
652
+ $ argument = new ArgumentMetadata ('invalid ' , RequestPayload::class, false , false , null , false , [
653
+ MapRequestPayload::class => new MapRequestPayload (validationFailedStatusCode: 400 ),
654
+ ]);
655
+ $ request = Request::create ('/ ' , 'POST ' , server: ['CONTENT_TYPE ' => 'application/json ' ], content: $ content );
656
+
657
+ $ kernel = $ this ->createMock (HttpKernelInterface::class);
658
+ $ arguments = $ resolver ->resolve ($ request , $ argument );
659
+ $ event = new ControllerArgumentsEvent ($ kernel , function () {}, $ arguments , $ request , HttpKernelInterface::MAIN_REQUEST );
660
+
661
+ try {
662
+ $ resolver ->onKernelControllerArguments ($ event );
663
+ $ this ->fail (sprintf ('Expected "%s" to be thrown. ' , HttpException::class));
664
+ } catch (HttpException $ e ) {
665
+ $ validationFailedException = $ e ->getPrevious ();
666
+ $ this ->assertSame (400 , $ e ->getStatusCode ());
667
+ $ this ->assertInstanceOf (ValidationFailedException::class, $ validationFailedException );
668
+ $ this ->assertSame ('This value should be of type unknown. ' , $ validationFailedException ->getViolations ()[0 ]->getMessage ());
669
+ $ this ->assertSame ('Test ' , $ validationFailedException ->getViolations ()[1 ]->getMessage ());
670
+ }
671
+ }
636
672
}
637
673
638
674
class RequestPayload
0 commit comments