如何在phpunit中覆盖导出的变量? [英] How to override exported variables in phpunit?

查看:74
本文介绍了如何在phpunit中覆盖导出的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以在phpunit中设置环境变量 .xml

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

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

所以这很好用

phpunit
// foo

但是,如果变量已在父外壳中导出,则将忽略该值.

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

export MY_VARIABLE=bar
phpunit
// bar

我需要为其他子进程导出该值,但是我真的不想只为运行测试而打开另一个shell.如何配置phpunit仅使用为测试环境定义的值?

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?

推荐答案

默认情况下,phpunit.xml中定义的环境变量被Shell环境覆盖.此行为与命令行工具中通常使用环境变量的方式一致.设置环境变量时,我们通常希望将其传递给工具并代替配置值使用.

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.

PHPUnit 6.3 中引入了新的force属性,以使我们无法为所选变量禁用此行为:

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" />

相关的github问题:

Related github issues:

  • https://github.com/sebastianbergmann/phpunit/pull/2723
  • https://github.com/sebastianbergmann/phpunit/issues/2353
  • https://github.com/sebastianbergmann/phpunit/issues/1181
  • https://github.com/sebastianbergmann/phpunit/issues/2353#issuecomment-290702962

这篇关于如何在phpunit中覆盖导出的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆