将自定义php.ini传递给phpunit


passing custom php.ini to phpunit

如何将自定义php.ini传递给phpunit?

源文件使用

get_cfg_var 

代替

ini_get

所以不幸的是它没有使用ini_set, -d选项等设置的值

现在传递值的唯一方法是使用额外的php.ini。如何将其传递给phpunit?

血淋淋的细节:

我试着输入-d

phpunit --filter testgetdesc -d SIEF_VALIDATOR_DOC_ROOT="htdocs" 
--configuration tests/phpunit.xml tests/configHelperTest.php
public function testgetdesc() {
    echo get_cfg_var("SIEF_VALIDATOR_DOC_ROOT")."---test---";
}

它只是回显"——test——"

原因是它也使用了ini_set:

https://github.com/sebastianbergmann/phpunit/blob/master/PHPUnit/TextUI/Command.php

            case 'd': {
                $ini = explode('=', $option[1]);
                if (isset($ini[0])) {
                    if (isset($ini[1])) {
                        ini_set($ini[0], $ini[1]);
                    } else {
                        ini_set($ini[0], TRUE);
                    }
                }
            }

在phpunt。xml中也有

<php>
  <ini name="SIEF_VALIDATOR_DOC_ROOT" value="bar"/>
</php>

它不能工作[我也不希望它能]。

-d应该工作,因为php的get_cfg_var()函数读取这些:

$ php -d display.errors2=1 -r "echo get_cfg_var('display.errors2');"
1

要传递自定义ini设置(或者将带有-c <file>的ini文件传递给phpunit),请调用以下配置:

$ php -c <file> -d setting=value $(which phpunit) <your-params>...

参考资料/参见:

  • php --help,命令行选项,执行PHP文件,get_cfg_var()函数
  • PHPUnit文档:命令行选项,XML配置文件
  • Shell: $( ... )命令替换(Bash GNU参考),which:为什么不使用"which"?那用什么呢?

Github问题建议使用-c标志。

php -c custom-php.ini `which phpunit` ...