Jeff Puckett
Jeff Puckett

Reputation: 40881

How to override exported variables in phpunit?

I know I can set an environment variable inside my phpunit.xml

<php>
    <env name="MY_VARIABLE" value="foo" />
</php>

So this works great

phpunit
// foo

However, that value is ignored if the variable has been exported in the parent shell.

export MY_VARIABLE=bar
phpunit
// bar

I need this value exported for other child processes, but I really don't want to open another shell just to run tests. How can I configure phpunit to use the values defined for the test environment only?

Upvotes: 2

Views: 1320

Answers (1)

Jakub Zalas
Jakub Zalas

Reputation: 36201

By default environment variables defined in phpunit.xml are overridden by the shell environment. This behaviour is consistent with the way environment variables are usually used in command line tools. Setting an environment variable we usually expect it to be passed to the tool and to be used instead of the configured value.

The new force attribute was introduced in PHPUnit 6.3 to let us disable this behaviour for chosen variables:

<env name="FOO" value="bar" force="true" />

Related github issues:

Upvotes: 2

Related Questions