0

I want to use Magento's integration test suite to test some of my customizations. My code calls an external web API, which has different URLs depending of the environment which is used.

My module's config.xml looks like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
 <default>
 <myproject>
 <api_token_manager>
 <base_url>env:CONFIG__DEFAULT__MYPROJECT__API_TOKEN_MANAGER__BASE_URL</base_url>
 <api_key>env:CONFIG__DEFAULT__MYPROJECT__API_TOKEN_MANAGER__API_KEY</api_key>
 <api_key_secret>env:CONFIG__DEFAULT__MYPROJECT__API_TOKEN_MANAGER__API_KEY_SECRET</api_key_secret>
 </api_token_manager>
 </myproject>
 </default>
</config>

When I run the integration tests on my local dev environment, a local mock server should be used. When I run the integration test on a external test environment, a test version of the API should be used.

So I need those configs to be customizable on different systems. And it is different between the real installation (which uses app/config/env.php) and the integration tests.

During the execution of the integration tests, Magento setup a separate database for the integration tests. I can adjust the installation params in the file dev/tests/integration/etc/install-config-mysql.php.

But I cannot specify the system config in this file.

I tried to use the dev/tests/integration/etc/config-global.php to do that. But the settings I put their are discarded during test execution by an event listener of the testing framework (see this bug report).

So my question is how can I achieve such a setup?

asked Oct 4, 2024 at 13:41

1 Answer 1

0

My current solution consists of an adjustment in the phpunit config, a prepended script and a script with the configs that should be used.

dev/tests/integration/phpunit.xml (copied from the phpunit.xml.dist):

- bootstrap="./framework/bootstrap.php"
+ bootstrap="./my_bootstrap.php"

dev/tests/integration/my_bootstrap.php:

<?php
$env = require file_exists(__DIR__ . '/env.local.php') ? __DIR__ . '/env.local.php' : __DIR__ . '/env.php';
if (isset($_ENV['TESTS_CLEANUP'])) {
 if (!in_array($_ENV['TESTS_CLEANUP'], ['enabled', 'disabled'])) {
 printf("Invalid value of TESTS_CLEANUP env variable. Must be enabled|disabled. Given \"%s\"\n", $_ENV['TESTS_CLEANUP']);
 exit(2);
 }
 define('TESTS_CLEANUP', $_ENV['TESTS_CLEANUP']);
}
foreach ($env as $key => $value) {
 if (false === getenv($key)) {
 putenv("$key=$value");
 $_ENV[$key] = $value;
 }
}
require __DIR__ . '/framework/bootstrap.php';

And than I have added dev/tests/integration/env.php:

<?php
// Environment variables used in integration tests.
// Copy to "env.local.php" to customize the values!
// System config paths has to be transformed like documented here:
// https://r-martins.github.io/m1docs/guides/v2.4/config-guide/prod/config-reference-var-name.html
return [
 'CONFIG__DEFAULT__MYPROJECT__API_TOKEN_MANAGER__BASE_URL' => 'https://api.example.com/',
 'CONFIG__DEFAULT__MYPROJECT__API_TOKEN_MANAGER__API_KEY' => 'foo',
 'CONFIG__DEFAULT__MYPROJECT__API_TOKEN_MANAGER__API_KEY_SECRET' => 'bar',
];

The env.php can then be added to the git repository. And if adjustment in the local checkout are required, you can copy the env.php to env.local.php and change the values of the variables:

<?php
return [
 'CONFIG__DEFAULT__MYPROJECT__API_TOKEN_MANAGER__BASE_URL' => 'https://my-mockserver.local/',
 'CONFIG__DEFAULT__MYPROJECT__API_TOKEN_MANAGER__API_KEY' => 'foo',
 'CONFIG__DEFAULT__MYPROJECT__API_TOKEN_MANAGER__API_KEY_SECRET' => 'bar',
];

But there might be a better solution. Because I cannot imagine that nobody else has this use case.

answered Oct 4, 2024 at 13:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.