28
28
use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
29
29
use Symfony \Component \Serializer \Serializer ;
30
30
use Symfony \Component \Validator \Constraints as Assert ;
31
- use Symfony \Component \Validator \ConstraintViolation ;
32
31
use Symfony \Component \Validator \ConstraintViolationList ;
33
32
use Symfony \Component \Validator \Exception \ValidationFailedException ;
34
33
use Symfony \Component \Validator \Validator \ValidatorInterface ;
@@ -227,14 +226,11 @@ public function testWithoutValidatorAndCouldNotDenormalize()
227
226
public function testValidationNotPassed ()
228
227
{
229
228
$ content = '{"price": 50, "title": ["not a string"]} ' ;
230
- $ payload = new RequestPayload (50 );
231
229
$ serializer = new Serializer ([new ObjectNormalizer ()], ['json ' => new JsonEncoder ()]);
232
230
233
231
$ 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 ' );
238
234
239
235
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
240
236
@@ -255,7 +251,36 @@ public function testValidationNotPassed()
255
251
$ this ->assertSame (422 , $ e ->getStatusCode ());
256
252
$ this ->assertInstanceOf (ValidationFailedException::class, $ validationFailedException );
257
253
$ 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 ());
259
284
}
260
285
}
261
286
@@ -688,3 +713,24 @@ public function __construct(public readonly float $page)
688
713
{
689
714
}
690
715
}
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