27
27
use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
28
28
use Symfony \Component \Serializer \Serializer ;
29
29
use Symfony \Component \Validator \Constraints as Assert ;
30
- use Symfony \Component \Validator \ConstraintViolation ;
31
30
use Symfony \Component \Validator \ConstraintViolationList ;
32
31
use Symfony \Component \Validator \Exception \ValidationFailedException ;
33
32
use Symfony \Component \Validator \Validator \ValidatorInterface ;
@@ -226,14 +225,11 @@ public function testWithoutValidatorAndCouldNotDenormalize()
226
225
public function testValidationNotPassed ()
227
226
{
228
227
$ content = '{"price": 50, "title": ["not a string"]} ' ;
229
- $ payload = new RequestPayload (50 );
230
228
$ serializer = new Serializer ([new ObjectNormalizer ()], ['json ' => new JsonEncoder ()]);
231
229
232
230
$ validator = $ this ->createMock (ValidatorInterface::class);
233
- $ validator ->expects ($ this ->once ())
234
- ->method ('validate ' )
235
- ->with ($ payload )
236
- ->willReturn (new ConstraintViolationList ([new ConstraintViolation ('Test ' , null , [], '' , null , '' )]));
231
+ $ validator ->expects ($ this ->never ())
232
+ ->method ('validate ' );
237
233
238
234
$ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
239
235
@@ -253,7 +249,36 @@ public function testValidationNotPassed()
253
249
$ validationFailedException = $ e ->getPrevious ();
254
250
$ this ->assertInstanceOf (ValidationFailedException::class, $ validationFailedException );
255
251
$ this ->assertSame ('This value should be of type unknown. ' , $ validationFailedException ->getViolations ()[0 ]->getMessage ());
256
- $ this ->assertSame ('Test ' , $ validationFailedException ->getViolations ()[1 ]->getMessage ());
252
+ }
253
+ }
254
+
255
+ public function testValidationNotPerformedWhenPartialDenormalizationReturnsViolation ()
256
+ {
257
+ $ content = '{"password": "abc"} ' ;
258
+ $ serializer = new Serializer ([new ObjectNormalizer ()], ['json ' => new JsonEncoder ()]);
259
+
260
+ $ validator = $ this ->createMock (ValidatorInterface::class);
261
+ $ validator ->expects ($ this ->never ())
262
+ ->method ('validate ' );
263
+
264
+ $ resolver = new RequestPayloadValueResolver ($ serializer , $ validator );
265
+
266
+ $ argument = new ArgumentMetadata ('invalid ' , User::class, false , false , null , false , [
267
+ MapRequestPayload::class => new MapRequestPayload (),
268
+ ]);
269
+ $ request = Request::create ('/ ' , 'POST ' , server: ['CONTENT_TYPE ' => 'application/json ' ], content: $ content );
270
+
271
+ $ kernel = $ this ->createMock (HttpKernelInterface::class);
272
+ $ arguments = $ resolver ->resolve ($ request , $ argument );
273
+ $ event = new ControllerArgumentsEvent ($ kernel , function () {}, $ arguments , $ request , HttpKernelInterface::MAIN_REQUEST );
274
+
275
+ try {
276
+ $ resolver ->onKernelControllerArguments ($ event );
277
+ $ this ->fail (sprintf ('Expected "%s" to be thrown. ' , HttpException::class));
278
+ } catch (HttpException $ e ) {
279
+ $ validationFailedException = $ e ->getPrevious ();
280
+ $ this ->assertInstanceOf (ValidationFailedException::class, $ validationFailedException );
281
+ $ this ->assertSame ('This value should be of type unknown. ' , $ validationFailedException ->getViolations ()[0 ]->getMessage ());
257
282
}
258
283
}
259
284
@@ -612,3 +637,24 @@ public function __construct(public readonly float $price)
612
637
{
613
638
}
614
639
}
640
+
641
+ class User
642
+ {
643
+ public function __construct (
644
+ #[Assert \NotBlank, Assert \Email]
645
+ private string $ email ,
646
+ #[Assert \NotBlank]
647
+ private string $ password ,
648
+ ) {
649
+ }
650
+
651
+ public function getEmail (): string
652
+ {
653
+ return $ this ->email ;
654
+ }
655
+
656
+ public function getPassword (): string
657
+ {
658
+ return $ this ->password ;
659
+ }
660
+ }
0 commit comments