magento2: Functional api test unable to load install-config-mysql database credentials
Preconditions (*)
- Magento CLI 2.3.5
- WSL (Windows Sub-System For Linux)
Steps to reproduce (*)
- Follow the steps as per the documentation: https://devdocs.magento.com/guides/v2.4/get-started/web-api-functional-testing.html#howto and then try running the command:
../../../vendor/bin/phpunit --config phpunit_rest.xml
Expected result (*)
- It should load the expected
install-config-mysql.phpfile for database connection but it rather tries to connect with the default connection settings. For some reason the default connection is also throwing an error with this command. However, I could successfully run integration tests and also my default is fully functional when try to upgrade or clear any cache.

Actual result (*)

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.
- Severity: S0 - Affects critical data or functionality and leaves users without workaround.
- Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
- Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
- Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
- Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 24 (11 by maintainers)
OK, ive had a look at the API Functional test code and how it works and it exposes a fundamental issue with the way the API Functional tests work.
Lets consider the integration tests first. When they are run, they use the install-mysql-config.php and the test framework installs a bastardised runtime of magento into dev/tests/integration/tmp/sandbox---------- and creates a separate database as defined in install-mysql-config.php. The tests are all run in the context of this sandbox test environment so as to not affect any data in the main development database. This works fine and is well understood.
Now, the API Functional tests. Well, these seem to follow the same approach, as there is a install-mysql-config.php file detailing a separate database however the bootstrap functions in the api functional tests operate differently to the integration tests. The test framework seems to have been implemented by a mixture of people who don’t understand the difference between api functional and integration tests, and people that have hacked it to appear to work. Let me explain.
So, the difference between the two test types : whereas the integration tests run in their own sandbox, you cannot do this for the api functional tests so easily. The API Functional tests are essentially very simple REST/SOAP/GRAPHQL clients which makes a call to a Magento HTTP(s) endpoint. Had the test framework been configured to work in the same way as the integration tests with its own sandbox using details in install-mysql-config.php, then it would be ONLY the REST/SOAP/GRAPHQL clients that run in this sandbox. You would have a sandboxed database in existence for the test client, which then makes a call to a HTTP(s) endpoint somewhere else. but where??? There is only one HTTP(s) based Magento endpoint and that is the one configured in app/etc/env.php. We do not have a separate webserver setup that could serve the sandboxed version of the api-functional test installation.
I believe at this point the Magento team may well have just thrown their hands in the air and decided that the API functional tests should not run in a sandbox environment, and adjusted the test framework bootstrap code to operate on the main database. They did leave in a couple of nasty features though
<const name="TESTS_CLEANUP" value="disabled"/>setting is enabled in phpunit.xml then the system attempts to uninstall the system defined in app/etc/env.php<const name="TESTS_MAGENTO_INSTALLATION" value="enabled"/>setting is enabled in phpunit.xml then the system reinstalls into the main database defined in app/etc/env.phpSo, what do we do. The current API functional tests intentionally ignore the install-mysql-config.php data and use the app/etc/env.php details instead. The tests operate on the HTTP(s) endpoint configured in app/etc/env.php with the result being the tests are performed on the main development database and not a sandboxed version. People assume it uses a sandbox database but it doesn’t, it uses whichever database is configured behind the HTTP(s) endpoint. Their development database will be being polluted with data from the API Functional tests, if not cleaned up properly by the test.
It is fairly straight forward to adjust the test framework to use the install-mysql-config.php settings and generate a sandbox for the REST/SOAP/GRAPHQL client to run in, but we would also need to either adjust the main app/etc/env.php to temporarily use the sandbox details from install-mysql-config.php for the duration of the test, or create a separate web server config that points at the sandbox installation.
you could have two env.php files and use a symbolic link to switch between them. It could be triggered automatically by a test runner script. Lets see if Magento core comes back with any comments. I agree its not ideal.
Hi, @ihor-sviziev I wondered if you were aware of the issues with the API-Functional test framework as detailed in my post above. Can you copy anyone in, in the test team that may need to be aware of this. Its been in this broken/working only by chance state for a while now, and I am sure because of the bug, regularly wipes peoples development databases when they first try to run the intergration tests, and pollutes their development database with test data as the test framework uses the wrong database.
I raised https://github.com/magento/magento2/issues/27213 on it a while ago but got no traction.
A little bit more research that confirms the fundamental broken’ness of the API Functional tests.
If the following settings are set in phpunit.xml
Then running the API-Functional tests will run them against the Magento HTTP(s) endpoint as defined in the setting
<const name="TESTS_BASE_URL" value="http://localhost:8080/"/>. It will not attempt to uninstall or reinstall the database.if either of the TESTS_CLEANUP (ununstalls magento) or TESTS_MAGENTO_INSTALLATION (installs magento) are set to enabled, then because of this bug, then the database defined in app/etc/env.php is DROPPED and then the database defined in install-config-mysql.php is installed. This is probably not what you want and will most likely mess up your development node database.
So, two ways to run API Functional tests…
Run the API Functional Tests against Magento configured to use your development database.
Run the API Functional Tests against Magento configured to use a sandbox database.
I’ll take a look at this later today as I almost had a fix working when I last looked at this.