Phpunit: Boolean environment variable values specified in XML get mangled

Created on 15 Oct 2016  路  5Comments  路  Source: sebastianbergmann/phpunit

An environment variable with the string value value="false" or value="true" ends up as value "" or "1" in the environment.

Example:

...
<php>
  <env name="foo" value="false"/>
  <env name="bar" value="true"/>
</php>
...

In the environment, retrieved via getenv:

  • foo will have the value `` (i.e. empty)
  • bar will have the value 1 (string)

I would expected that both values retain the string representation from the XML file, i.e. "false" and "true".

typbug

Most helpful comment

This fix was reverted due to BC concerns, but the change was never bumped up to a new major release. Environment variables are still getting mangled.

All 5 comments

Check out the code here. Specifically the call to putenv. putenv only takes a string, and the code in PHPUnit just concatenates the name and value directly.

If checking the truthiness or falsiness doesn't work for you, use $_ENV instead. Or run the value through filter var and validate it as a boolean.

if (filter_var(getenv('FOO'), FILTER_VALIDATE_BOOLEAN)) {
  // ...
}

PHPUnit just concatenates the name and value directly.

How can that be true and still a value like string "false" ends up being an empty string? I don't think this assertion is correct.

How can that be true and still a value like string "false" ends up being an empty string?

When it loads the configuration it converts the strings true and false to booleans. That behavior is, unfortunately, not really documented.

Exactly, my point.

Hence the title "Boolean environment variable values specified in XML get mangled", thanks for confirming!

This fix was reverted due to BC concerns, but the change was never bumped up to a new major release. Environment variables are still getting mangled.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TiMESPLiNTER picture TiMESPLiNTER  路  3Comments

sebastianbergmann picture sebastianbergmann  路  4Comments

keradus picture keradus  路  3Comments

AnmSaiful picture AnmSaiful  路  4Comments

klesun picture klesun  路  4Comments