Unit testig on multisite I am trying to simulate a multi-site environment in an integration test with phpunit. I know how to init a new database connection with Database::addConnectionInfo but how can I cleanly switch the database used by the current environment?

Database::setActiveConnection will only work for SQL queries but I would like to use the entity_type.manager service and the only way I found is to replace the database service in the container and call KernelTest::setUp. Switching back an forth between the databases is still pretty buggy and complex.

Any ideas?
"Unit testing" and "multi-site" are in direct conflict with each other. Multi-site is definitely an integration/functional testing concern.

What you can do in a unit test to help guarantee that things are working correctly for a multi-site setup is to unit test only the logic that is computing different results for each site in the multi-site setup. Use a @dataProvider to test the input for different sites in a multi-site setup.

The result of that logic should vary by the url.site cache context. You can assert the presence of that cache context in the X-Drupal-Cache-Contexts response header in your integration/functional test.

IOW:
- unit test for testing various inputs, with a @dataProvider
- functional test that asserts the presence of url.site cache context

Together, they guarantee things work correctly.