FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 703 forks source link

Symfony handling exceptions on PHPUnit Application tests #2364

Closed erikas-tranauskas closed 2 years ago

erikas-tranauskas commented 2 years ago

I have an API endpoint which returns bad request response like this:

{
    "code": 400,
    "message": "Entity App\\Entity\\Seller can not be found"
}

It is generated by using fos_rest.request_body ParamConverter.

Now the problem is that if I run this endpoint on my PHPUnit test case - it always returns 500 Internal Server Error. While the response with code 400 always comes back using regular browser or Postman app.

Any ideas how to make that response as it is on PHPUnit side? It should be 400 so I could assert it.

Application test client is created from this class: https://github.com/symfony/symfony/blob/6.0/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php

Also if I set the client catchExceptions parameter to false, then it shows the real error which is:

Symfony\Component\HttpKernel\Exception\BadRequestHttpException: Entity App\Entity\Seller can not be found /var/www/pmp-api/vendor/friendsofsymfony/rest-bundle/Request/RequestBodyParamConverter.php:94

Is it possible to catch the 400 response somehow or I will have to use try catch inside my testcase?