How to reference an environment variable in phpunit.xml file?

environment-variables, phpunit

Solution

Actually you should not have `phpunit.xml` in your repository. PHPUnit looks for two XML files on execution, that is `phpunit.xml` and `phpunit.xml.dist`. It will always prioritize `phpunit.xml` if it's found, otherwise it will fallback to `phpunit.xml.dist`. From the documentation:

If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file.

With that said you should have `phpunit.xml.dist` in your repository as a template/boilerplate setup for running the tests. Then each individual developer can create their own `phpunit.xml` from that if they wish to add their specific settings.

Also the file `phpunit.xml` should be ignored in your repository.

Problem

In my phpunit.xml file, I've got a chunk for logging code coverage reports, something like this: ``` <logging> <log type="coverage-html" target="./logs/coverage" ... </logging> ``` I don't like using a hard-coded string for the target path however. I would like each user to be able to specify their target via an environment variable so they can have the logs go wherever they like, without having to change a source-controlled file. I'm looking to do something like this: ``` <logging> <log type="coverage-html" target="$LOG_PATH" ... </logging> ``` I don't see any support for this sort of substitution in the PHPUnit docs. Does anyone have an idea for how to accomplish this?

Original source