How to force change of environment variable to a specific value inside phpunit test?

In my phpunit, I have this configuration:

<php>
    <env name="ENVIRONMENT" value="test"/>
</php>

I worked under the false assumption that he always set the environment variable to a value test. However, when the system already has a set of variables, it prefers an existing value.

$ export ENVIRONMENT=GNARF
$ phpunit -c test/phpunit.xml

Thus, inside the tests, the value env('ENVIRONMENT')will be "GNARF", although I expected "test".

Is there a way to force phpunit to view the parameter envnot as a default value, but as a specific value that it should use?

I would also like to avoid calling phpunit in a specific way to get the correct env variables.

So while this works:

ENVIRONMENT="test";./vendor/bin/phpunit -c tests/phpunit.xml

phpunit.xml, .

+4
3

( PHPUnit 6.5):

<phpunit bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="MyProject">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
    <php>
        <env name="API_KEY" value="fakeApiKey" force="true" />
    </php>
</phpunit>

, ( , ).

sebastianbergmann PHPUnit 6.3 4 2017 .

+2

PHPUnit_Util_Configuration::handlePHPConfiguration() , :

foreach ($configuration['env'] as $name => $value) {
    if (false === getenv($name)) {
        putenv("{$name}={$value}");
    }
    if (!isset($_ENV[$name])) {
        $_ENV[$name] = $value;
    }
}

, , . , .

0

env vars bootstrap. bootstrap phpunit.xml, , .

0

Source: https://habr.com/ru/post/1629350/


All Articles