2828use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
2929use Symfony \Component \Serializer \Serializer ;
3030use Symfony \Component \Validator \Constraints as Assert ;
31- use Symfony \Component \Validator \ConstraintViolation ;
3231use Symfony \Component \Validator \ConstraintViolationList ;
3332use Symfony \Component \Validator \Exception \ValidationFailedException ;
3433use Symfony \Component \Validator \Validator \ValidatorInterface ;
@@ -227,14 +226,11 @@ public function testWithoutValidatorAndCouldNotDenormalize()
227226 public function testValidationNotPassed ()
228227 {
229228 $ content = '{"price": 50, "title": ["not a string"]} ' ;
230- $ payload = new RequestPayload (50 );
231229 $ serializer = new Serializer ([new ObjectNormalizer ()], ['json ' => new JsonEncoder ()]);
232230
233231 $ validator = $ this ->createMock (ValidatorInterface::class);
234- $ validator ->expects ($ this ->once ())
235- ->method ('validate ' )
236- ->with ($ payload )
237- ->willReturn (new ConstraintViolationList ([new ConstraintViolation ('Test ' , null , [], '' , null , '' )]));
232+ $ validator ->expects ($ this ->never ())
233+ ->method ('validate ' );
238234
239235 $ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
240236
@@ -255,7 +251,36 @@ public function testValidationNotPassed()
255251 $ this ->assertSame (422 , $ e ->getStatusCode ());
256252 $ this ->assertInstanceOf (ValidationFailedException::class, $ validationFailedException );
257253 $ this ->assertSame (sprintf ('This value should be of type %s. ' , class_exists (InvalidTypeException::class) ? 'string ' : 'unknown ' ), $ validationFailedException ->getViolations ()[0 ]->getMessage ());
258- $ this ->assertSame ('Test ' , $ validationFailedException ->getViolations ()[1 ]->getMessage ());
254+ }
255+ }
256+
257+ public function testValidationNotPerformedWhenPartialDenormalizationReturnsViolation ()
258+ {
259+ $ content = '{"password": "abc"} ' ;
260+ $ serializer = new Serializer ([new ObjectNormalizer ()], ['json ' => new JsonEncoder ()]);
261+
262+ $ validator = $ this ->createMock (ValidatorInterface::class);
263+ $ validator ->expects ($ this ->never ())
264+ ->method ('validate ' );
265+
266+ $ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
267+
268+ $ argument = new ArgumentMetadata ('invalid ' , User::class, false , false , null , false , [
269+ MapRequestPayload::class => new MapRequestPayload (),
270+ ]);
271+ $ request = Request::create ('/ ' , 'POST ' , server: ['CONTENT_TYPE ' => 'application/json ' ], content: $ content );
272+
273+ $ kernel = $ this ->createMock (HttpKernelInterface::class);
274+ $ arguments = $ resolver ->resolve ($ request , $ argument );
275+ $ event = new ControllerArgumentsEvent ($ kernel , function () {}, $ arguments , $ request , HttpKernelInterface::MAIN_REQUEST );
276+
277+ try {
278+ $ resolver ->onKernelControllerArguments ($ event );
279+ $ this ->fail (sprintf ('Expected "%s" to be thrown. ' , HttpException::class));
280+ } catch (HttpException $ e ) {
281+ $ validationFailedException = $ e ->getPrevious ();
282+ $ this ->assertInstanceOf (ValidationFailedException::class, $ validationFailedException );
283+ $ this ->assertSame ('This value should be of type unknown. ' , $ validationFailedException ->getViolations ()[0 ]->getMessage ());
259284 }
260285 }
261286
@@ -688,3 +713,24 @@ public function __construct(public readonly float $page)
688713 {
689714 }
690715}
716+
717+ class User
718+ {
719+ public function __construct (
720+ #[Assert \NotBlank, Assert \Email]
721+ private string $ email ,
722+ #[Assert \NotBlank]
723+ private string $ password ,
724+ ) {
725+ }
726+
727+ public function getEmail (): string
728+ {
729+ return $ this ->email ;
730+ }
731+
732+ public function getPassword (): string
733+ {
734+ return $ this ->password ;
735+ }
736+ }
0 commit comments