3

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?

1
  • Good question, I use the Phinx migration system for my DB migrations when coding in php. It is configured via JSON (not xml) but you can set default and override with environment variables exactly as you propose in your question. Wish this existed for PHP unit. Don't like creating file local copies just to change simple things like base source code directory or test file directory--especially as recommended in at least one answer the urge to use fully qualified paths everywhere. Commented Jan 22, 2017 at 19:29

2 Answers 2

10
+100

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So the answer is there is no ability to do dynamic substitution via environment variables into your config XML file--you must modify each item you wish to change. Wish this functionality existed!
0

According to Herr Bergman:

protected $backupGlobals = FALSE;

In your test class will disable the (default) behaviour of backing up and restoring global variables. However, do note that putting this in the setUp method will have no effect. Hope that helps.

1 Comment

You misunderstand the question. I'm talking about shell environment variables, not PHP variables. I want to reference them in the phpunit.xml file, not the actual tests.

Your Answer

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.