@@ -124,7 +124,7 @@ public function testNullableValueArgument()
124
124
125
125
$ resolver ->onKernelControllerArguments ($ event );
126
126
127
- $ this ->assertEquals ([null ], $ event ->getArguments ());
127
+ $ this ->assertSame ([null ], $ event ->getArguments ());
128
128
}
129
129
130
130
public function testQueryNullableValueArgument ()
@@ -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 ->assertSame (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 ());
@@ -601,6 +602,73 @@ public static function provideValidationGroupsOnManyTypes(): iterable
601
602
new MapQueryString (validationGroups: new Assert \GroupSequence (['strict ' ])),
602
603
];
603
604
}
605
+
606
+ public function testQueryValidationErrorCustomStatusCode ()
607
+ {
608
+ $ serializer = new Serializer ([new ObjectNormalizer ()], []);
609
+
610
+ $ validator = $ this ->createMock (ValidatorInterface::class);
611
+
612
+ $ validator ->expects ($ this ->once ())
613
+ ->method ('validate ' )
614
+ ->willReturn (new ConstraintViolationList ([new ConstraintViolation ('Page is invalid ' , null , [], '' , null , '' )]));
615
+
616
+ $ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
617
+
618
+ $ argument = new ArgumentMetadata ('page ' , QueryPayload::class, false , false , null , false , [
619
+ MapQueryString::class => new MapQueryString (validationFailedStatusCode: 400 ),
620
+ ]);
621
+ $ request = Request::create ('/?page=123 ' );
622
+
623
+ $ kernel = $ this ->createMock (HttpKernelInterface::class);
624
+ $ arguments = $ resolver ->resolve ($ request , $ argument );
625
+ $ event = new ControllerArgumentsEvent ($ kernel , function () {}, $ arguments , $ request , HttpKernelInterface::MAIN_REQUEST );
626
+
627
+ try {
628
+ $ resolver ->onKernelControllerArguments ($ event );
629
+ $ this ->fail (sprintf ('Expected "%s" to be thrown. ' , HttpException::class));
630
+ } catch (HttpException $ e ) {
631
+ $ validationFailedException = $ e ->getPrevious ();
632
+ $ this ->assertSame (400 , $ e ->getStatusCode ());
633
+ $ this ->assertInstanceOf (ValidationFailedException::class, $ validationFailedException );
634
+ $ this ->assertSame ('Page is invalid ' , $ validationFailedException ->getViolations ()[0 ]->getMessage ());
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
+ }
604
672
}
605
673
606
674
class RequestPayload
@@ -612,3 +680,10 @@ public function __construct(public readonly float $price)
612
680
{
613
681
}
614
682
}
683
+
684
+ class QueryPayload
685
+ {
686
+ public function __construct (public readonly float $ page )
687
+ {
688
+ }
689
+ }
0 commit comments