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 704 forks source link

UnsupportedMediaTypeHttpException: Format 'html' not supported, handler must be implemented error only in UnitTests #2289

Closed mgluesenkamp closed 3 years ago

mgluesenkamp commented 3 years ago

Hi,

I have the following error while executing unit tests in the test environment. [error] Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException: "Format 'html' not supported, handler must be implemented" at /var/www/symfony/vendor/friendsofsymfony/rest-bundle/View/ViewHandler.php line 151

This is strange, because the route works in dev env without any problems. I've the same configuration for my environments (in config/packages/fos_rest.yaml):

fos_rest:
    zone:
      - { path: ^/api/* }
    param_fetcher_listener: true
    body_listener: true
    format_listener:
        enabled: true
        rules:
            - { path: '^/api/', priorities: ['json', 'xml'], fallback_format: 'html' }
    versioning: true
    view:
        view_response_listener: true

I have the same .env file for dev and test except the additional part for unit tests. Here it is:

KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
DATABASE_URL=mysql://database:password@database:3306/database?serverVersion=5.7
CORS_ALLOW_ORIGIN=*

I can also post my phpunit.xml.dist, but it is not changed at all. The environment there is set to test. It would be nice if someone has an advice to solve my problem. Thanks!

mgluesenkamp commented 3 years ago

I've now added the following in my fos_rest.yaml:

fos_rest:
    [...]
    view:
        view_response_listener: true
        formats:
            json: true
            html: true

Now I get the following error: [critical] Uncaught PHP Exception JMS\Serializer\Exception\UnsupportedFormatException: "The format "html" is not supported for serialization." at /var/www/symfony/vendor/jms/serializer/src/Serializer.php line 137

mgluesenkamp commented 3 years ago

I’ve experimented a bit more and realized it was about the fallback to html. I changed it to json and now it works. So, I closed the issue. Thanks anyways!