0

不幸的是config(['key' => 'newValue']),在 Dusk 设置中不起作用(用于覆盖配置值),大概是因为它会更改运行测试的系统的配置,而不是打开以执行流程的无头浏览器的体验。

有时我看不出需要临时更改某个 Dusk 测试的 env 值。

例如,通常在“黄昏连接”时临时设置QUEUE_DRIVER=sync,但在一项特定测试中,我需要检查数据库中“作业”表中的值。

在升级到 Laravel >=5.8(以及更新版本的 DotEnv)之前,我能够使用之前在 Dusk 测试中调用的这个函数$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_DRIVER' => 'sync']);

但在最近的 Laravel 版本中,环境变量是不可变的(请参阅“只读 env Helper”)。

我怎样才能实现我的目标,Dusk.env.dusk.local用于大多数测试,但某些测试可能略有不同?

4

4 回答 4

1

终于在这个问题上苦苦挣扎了 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()我的 Dusk 测试类的函数中,我调用:

    $this->overrideDuskEnv([
        'SIGNUP_FORM_POST_PATH' => \App\Helpers\Routes::SIGNUP,
        'QUEUE_DRIVER' => \App\Helpers\ConfigConstants::DUSK_CONNECTION
    ]);

$this->browse(function (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 理解的棘手部分是运行测试的控制台进程的环境变量(以及配置数组)与头浏览器使用的环境变量(模拟真实用户的体验)不同。

顺便说一句,我一直对这样的方法充满希望但结果证明它们完全是浪费时间,因为DuskCommand 已经在调用overload以使 env 变量可变。

于 2019-10-20T14:28:48.107 回答
0

有关在黄昏测试期间覆盖 config([]) 的记录方法,请参见此处:

https://gist.github.com/wrabit/e01df16858505c395b7b0d271112a023

于 2020-11-04T12:47:48.387 回答
0

这有点容易,但如果你输入任何错误的条目,它就会爆裂。

public function store(Request $request) {
    foreach ($request->all() as $key => $value) {
        $_ENV[$key] = $value;
    }
    $x = '';
    unset($_ENV['_token']);
    foreach ($_ENV as $key => $value) {
        $x .= $key . "=" . $value . "\n";
    }
    base_path('.env');
    file_put_contents(base_path('.env'), $x);
    return redirect()->back();
}

使用

<form class="grid gap-2" action="{{ route('admin.enviroment.store') }}" method="post">
    <div>
        <x-label for="GOOGLE_TAG"  :value="__('GOOGLE_TAG')" />
        <x-input id='GOOGLE_TAG' name="GOOGLE_TAG" :value="__($_ENV['GOOGLE_TAG'])" class="w-full rounded-md dark:bg-gray-700" type="text" required  />
    </div>
    @csrf
    <x-button class="ml-auto dark:bg-blue-900/90">
        {{ __('Update GOOGLE TAG') }}
    </x-button>
</form>
于 2022-02-04T08:07:27.883 回答
0

您还可以为所有黄昏测试使用单独的环境。这里的 laravel 文档中也提到了它https://laravel.com/docs/8.x/dusk#environment-handling

于 2020-12-01T06:39:33.940 回答