1

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?

1

"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.

1
  • Thank you very much for taking the time to answer! So there is no stable way to execute code in two different drupal instances (e.g. to use the entity_type.manager to create nodes) right? I would like to write integration tests for a node replication via rest endpoints (relaxed module) and would have an easier game by jumping between instances instead of using curl with the rest endpoints for every task. – alexej_d Jan 1 '18 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.