Laravel php artisan serve dose not respect env flag

395 Views Asked by At

I'm working on a Laravel Project and try to integrate Laravel Scout. For testing with dusk I have created a .env.dusk.local environment file, that is used for testing. But when I try to run php artisan serve --env=dusk.local it just runs the default .env file. Also all other .env files don't work.

Im using Laravel 8.15.0. I also try clearing the config cache but nothing changed.

I'm currently out of ideas, so hopefully someone can help me.

1

There are 1 best solutions below

1
On

If you see laravel dusk documentation you can see that there is no need for you to so that your separate dusk env file replaces the original & once the test is done it restores it automatically.

When running tests, Dusk will back-up your .env file and rename your Dusk environment to .env. Once the tests have completed, your .env file will be restored.

enter image description here


If it still does not work

You can manually set the env thanks to vlucas/phpdotenv's methods 'load()',

<?php

namespace Tests\Browser;

use App\Models\User;
use Laravel\Dusk\Chrome;
use Tests\DuskTestCase;

class ExampleTest extends DuskTestCase
{
    private function setEnv(){
       $env_name = "dusk.local";
       if (isset($env_name)) {
            // Immutability refers to if Dotenv is allowed to overwrite existing environment variables. If you want Dotenv to overwrite existing environment variables, use createMutable instead of createImmutable(from vlucas/phpdotenv repo)
            $dotenv = \Dotenv\Dotenv::createImmutable(base_path(), '.env.'.$env_name);
    
            try {
                $dotenv->load();
            } catch (\Dotenv\Exception\InvalidPathException $e) {
                $e->getTraceAsString();
            }
        }
    }
    public function testBasicExample()
    {
        $this->setEnv();
    }
}