|
23 | 23 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
24 | 24 | use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
25 | 25 | use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
| 26 | +use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
26 | 27 | use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
|
| 28 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
27 | 29 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
28 | 30 | use Symfony\Component\Serializer\Serializer;
|
| 31 | +use Symfony\Component\Serializer\SerializerInterface; |
29 | 32 | use Symfony\Component\Validator\Constraints as Assert;
|
30 | 33 | use Symfony\Component\Validator\ConstraintViolation;
|
31 | 34 | use Symfony\Component\Validator\ConstraintViolationList;
|
@@ -307,6 +310,39 @@ public function testRequestContentValidationPassed()
|
307 | 310 | $this->assertEquals([$payload], $event->getArguments());
|
308 | 311 | }
|
309 | 312 |
|
| 313 | + /** @dataProvider provideNoTypes */ |
| 314 | + public function testRequestContentWithUntypedErrors(?array $types) |
| 315 | + { |
| 316 | + $this->expectException(HttpException::class); |
| 317 | + $this->expectExceptionMessage('This value was of an unexpected type.'); |
| 318 | + $serializer = $this->createMock(SerializerDenormalizer::class); |
| 319 | + |
| 320 | + if (null === $types) { |
| 321 | + $exception = new NotNormalizableValueException('Error with no types'); |
| 322 | + } else { |
| 323 | + $exception = NotNormalizableValueException::createForUnexpectedDataType('Error with no types', '', []); |
| 324 | + } |
| 325 | + $serializer->method('deserialize')->willThrowException(new PartialDenormalizationException([], [$exception])); |
| 326 | + |
| 327 | + $resolver = new RequestPayloadValueResolver($serializer, $this->createMock(ValidatorInterface::class)); |
| 328 | + $request = Request::create('/', 'POST', server: ['CONTENT_TYPE' => 'application/json'], content: '{"price": 50}'); |
| 329 | + |
| 330 | + $arguments = $resolver->resolve($request, new ArgumentMetadata('valid', RequestPayload::class, false, false, null, false, [ |
| 331 | + MapRequestPayload::class => new MapRequestPayload(), |
| 332 | + ])); |
| 333 | + $event = new ControllerArgumentsEvent($this->createMock(HttpKernelInterface::class), function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST); |
| 334 | + |
| 335 | + $resolver->onKernelControllerArguments($event); |
| 336 | + } |
| 337 | + |
| 338 | + public static function provideNoTypes(): array |
| 339 | + { |
| 340 | + return [ |
| 341 | + [null], |
| 342 | + [[]] |
| 343 | + ]; |
| 344 | + } |
| 345 | + |
310 | 346 | public function testQueryStringValidationPassed()
|
311 | 347 | {
|
312 | 348 | $payload = new RequestPayload(50);
|
@@ -612,3 +648,7 @@ public function __construct(public readonly float $price)
|
612 | 648 | {
|
613 | 649 | }
|
614 | 650 | }
|
| 651 | + |
| 652 | +interface SerializerDenormalizer extends SerializerInterface, DenormalizerInterface |
| 653 | +{ |
| 654 | +} |
0 commit comments