How to override env variables in Laravel Dusk

后端 未结 3 791
谎友^
谎友^ 2020-12-11 14:37

Unfortunately config([\'key\' => \'newValue\']) doesn\'t work in a Dusk setup (for overriding a config value), presumably because it would change the config

相关标签:
3条回答
  • 2020-12-11 14:52

    Finally after struggling with this problem for 10+ hours, I have a solution.

    /**
     * @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!');
        }
    }
    

    Then in my setUp() function in my Dusk test class, I call:

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

    Then in each test function after the closing of $this->browse(function (Browser $browser)... and before assertions, I call:

    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.
    

    The tricky part to understand with Dusk is that the environment variables (and therefore the config arrays) of the console process running the tests differ from those that get used by the headless browser (simulating what a real user would experience).

    By the way, I had been so hopeful about approaches like this one, but they turned out to be complete wastes of time because DuskCommand is already calling overload to make the env variables mutable.

    0 讨论(0)
  • 2020-12-11 15:00

    You can also use a seperate env for all the dusk tests. It is also mentioned in the laravel docs here https://laravel.com/docs/8.x/dusk#environment-handling

    0 讨论(0)
  • 2020-12-11 15:10

    See here for a documented approach to overriding config([]) during a dusk test:

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

    0 讨论(0)
提交回复
热议问题