squibby's avatar

How to change env variable / config in Dusk test?

I have a .env variable link to a Laravel config file which toggles certain section in my site. In Dusk testing I want to test that certain functionality is shown / hidden depending on this state.

How to set the config / or update dusk .env variable on each test?

I have tried Config::set() but it doesn't seem to work. Has anyone else done this before?

Thanks.

0 likes
13 replies
squibby's avatar

No, I already have a separate local dusk env file. I need to change the env variable for different tests. For example I have a section on the site which I only want to enable for certain deployments. I need to ensure that items are definitley not shown for those occasions.

goatshark's avatar

Got it. I've used config()->set('configfile.key.whatever', 'theValue') in the setUp() method of tests successfully, but that should do the same thing as Config::set().

squibby's avatar

@Sti3bas No solution as yet. I think it can't be changed from within the test. Perhaps will need to make some way to select different dusk .env file depending on the test....

The only way I can do now, is manually change the .env file for each group of tests, but its not ideal.

Bartestro's avatar

@squibby yes I faced the same problem and I have not found any solution for this.

Moreover I don't think it is even doable in current state at all.

Dusk uses working webserver (artisan serve, apache, nginx) as test source pages.

This should be started before running tests and has certain keys setup in .env.

So, as soon Dusk is run it will stick to only one config, till the end of the test.

Any attempts to modify config/env vars will endup in Dusk instance changes, and not webservers .env.

Therefore rendered pages will always be using your webservers initial .env vars....

squibby's avatar
squibby
OP
Best Answer
Level 8

I figured out a way to make this work. I had to directly alter the .env prior to any test being run.

I added the following method to my DuskTestCase.php

    /**
     * Overrides any .env files for dusk tests
     *
     * @param array $variables
     */
    protected function override($variables = [])
    {
        $path = '.env';

        if (file_exists($path)) {

            // The environment variables to prepend
            $prepend = '';

            // Convert all new parameters to expected format
            foreach($variables as $key => $value)
            {
                $prepend .= $key . '="' . $value . '"' . PHP_EOL;
            }

            // Grab original .env file contents
            $original = file_get_contents($path);

            // Write all to .env file for dusk test
            file_put_contents($path, $prepend . $original);
        } 

Then call in any dusk test method prior to using $this->browse like this:


public function test_something(){

        // Disable feature
        $this->override([ "ENABLE_SOME_FEATURE" => "false" ]);

        $this->browse(function (Browser $browser){
                   $browser->resize(1200, 2000)
                                ->loginAs(User::find(1))
                                // etc 
        });
    }

The new environment variables if prepended to the file override the current ones. If they are appended it doesn't seem to take priority. This all may have been fixed as a feature in newer versions of Dusk. I am running an older version.

1 like
ManyApp's avatar

I have the same problem: it is difficult to change the configuration during Dusk tests.

Therefore I took some hours to develop a module for that.

This is not perfect of course, but at least it works while providing a good flexibility: you can set any config variable from a Dusk test - and not only .env variables but the whole configuration, including arrays.

See https://github.com/manyapp/duskapiconf

Hope it helps!

squibby's avatar

@gotesla no i am only running upto 5.7 in my projects. But I will keep an eye on this. Thanks for the info.

Please or to participate in this conversation.