Skip to content

Commit daff0e8

Browse files
author
zim32
committed
Add testRequestPayloadValidationErrorCustomStatusCode test
1 parent f1ec641 commit daff0e8

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ public function testValidationNotPassed()
251251
$this->fail(sprintf('Expected "%s" to be thrown.', HttpException::class));
252252
} catch (HttpException $e) {
253253
$validationFailedException = $e->getPrevious();
254+
$this->assertEquals(404, $e->getStatusCode());
254255
$this->assertInstanceOf(ValidationFailedException::class, $validationFailedException);
255256
$this->assertSame('This value should be of type unknown.', $validationFailedException->getViolations()[0]->getMessage());
256257
$this->assertSame('Test', $validationFailedException->getViolations()[1]->getMessage());
@@ -633,6 +634,41 @@ public function testQueryValidationErrorCustomStatusCode()
633634
$this->assertSame('Page is invalid', $validationFailedException->getViolations()[0]->getMessage());
634635
}
635636
}
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+
}
636672
}
637673

638674
class RequestPayload

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy