Yo @squibby Are you talking about this: https://laravel.com/docs/5.6/dusk#environment-handling
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.
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.
Please or to participate in this conversation.