如何在Laravel黄昏中覆盖环境变量
不幸的是如何在Laravel黄昏中覆盖环境变量,laravel,laravel-dusk,laravel-6,phpdotenv,Laravel,Laravel Dusk,Laravel 6,Phpdotenv,不幸的是config(['key'=>'newValue'])在黄昏设置中不起作用(用于覆盖配置值),可能是因为它会更改运行测试的系统的配置,而不是打开以执行流的无头浏览器的体验 有时我看不出有什么办法可以暂时改变某个黄昏测试的环境值 例如,当通常是“黄昏连接”时,临时设置QUEUE\u DRIVER=sync,但在一个特定测试中,我需要检查数据库中“jobs”表中的值 在升级到Laravel>=5.8(以及更新版本的DotEnv)之前,我能够在$this->browse(…)之前的黄昏测试中使
config(['key'=>'newValue'])
在黄昏设置中不起作用(用于覆盖配置值),可能是因为它会更改运行测试的系统的配置,而不是打开以执行流的无头浏览器的体验
有时我看不出有什么办法可以暂时改变某个黄昏测试的环境值
例如,当通常是“黄昏连接”时,临时设置QUEUE\u DRIVER=sync
,但在一个特定测试中,我需要检查数据库中“jobs”表中的值
在升级到Laravel>=5.8(以及更新版本的DotEnv)之前,我能够在$this->browse(…
)之前的黄昏测试中使用此函数:
/**
* Overrides any .env variables for Dusk tests. https://laracasts.com/discuss/channels/testing/how-to-change-env-variable-config-in-dusk-test
* The changes exist only for that one test because of tearDown.
* Remember that you need to be using `php artisan dusk` instead of `phpunit`.
* https://stackoverflow.com/questions/54407784/laravel-dusk-how-to-change-config-values-before-each-test-for-the-browser#comment103224655_54407784
*
* @param array $variables
*/
protected function overrideDuskEnv($variables = []) {
$path = self::DOT_ENV;
if (file_exists($path)) {
$contentToPrepend = '';
foreach ($variables as $key => $value) {// Convert all new parameters to expected format
$contentToPrepend .= $key . '="' . $value . '"' . PHP_EOL;
}
$originalFileContents = $this->envContents;
$comment = '# ==============================================' . PHP_EOL . '# VARIABLES ABOVE THIS LINE are from "' . __FUNCTION__ . '" function in DuskTestCase ( https://laracasts.com/discuss/channels/testing/how-to-change-env-variable-config-in-dusk-test )' . PHP_EOL;
file_put_contents($path, $contentToPrepend . $comment . $originalFileContents); //"If they are appended, it doesn't seem to take priority."
} else {
throw new \Exception('Could not find env file to override!');
}
}
我可以这样称呼它:$this->overrideDuskEnv(['QUEUE\u DRIVER'=>'sync']);
但在最近的Laravel版本中,环境变量是不可变的(请参阅)
如果Dash在大多数测试中使用
.env.dash.local
,但在某些测试中可能会略有不同,那么我如何实现我的目标呢?最后,在与这个问题斗争了10多个小时后,我有了一个解决方案。
/**
* @param array $variables
*/
protected function overrideDuskEnv($variables = []) {
$path = self::DOT_ENV;
if (file_exists($path)) {
$contentToAppend = '';
foreach ($variables as $key => $value) {// Convert all new parameters to expected format
$contentToAppend .= $key . '="' . $value . '"' . PHP_EOL;
}
$originalFileContents = $this->envContents;
$comment = '# ==============================================' . PHP_EOL . '# VARIABLES BELOW THIS LINE are from "' . __FUNCTION__ . '" function in DuskTestCase ( https://laracasts.com/discuss/channels/testing/how-to-change-env-variable-config-in-dusk-test )' . PHP_EOL;
$this->baseCommand->consoleOutput('Appending to ' . $path . ': ' . $contentToAppend);
file_put_contents($path, $originalFileContents . $comment . $contentToAppend); //It used to be the case that "If they are appended [rather than prepended], it doesn't seem to take priority", but after the DotEnv upgrade in Laravel 5.8, it seems prepending doesn't work and appending does.
} else {
throw new \Exception('Could not find env file to override!');
}
}
然后在我的黄昏测试类中的setUp()
函数中,我调用:
$this->overrideDuskEnv([
'SIGNUP_FORM_POST_PATH' => \App\Helpers\Routes::SIGNUP,
'QUEUE_DRIVER' => \App\Helpers\ConfigConstants::DUSK_CONNECTION
]);
然后在关闭$this->browse(函数(Browser$Browser)…
之后和断言之前的每个测试函数中,我调用:
config(['queue.default' => \App\Helpers\ConfigConstants::DUSK_CONNECTION]); //this does not affect the headless browser but IS probably necessary here so that assertQueued knows to pull from the queue that the headless browser was using.
使用Dusk需要了解的棘手部分是,运行测试的控制台进程的环境变量(以及配置数组)与无头浏览器使用的环境变量(模拟真实用户的体验)不同
顺便说一句,我对这样的方法很有希望,但结果证明它们完全是浪费时间,因为要使env变量可变。请参阅此处,以了解在黄昏测试期间重写配置([])的有文档记录的方法:
您还可以为所有黄昏测试使用单独的env。 这里的laravel文档中也提到了这一点