DEV Community

David Carr
David Carr

Posted on • Originally published at dcblog.dev on

2 1

Laravel how to set app environment during tests

Laravel how to set app environment during tests

If you need to set an environment to be a specific one such as staging you can override the environment by changing the config value app_env

config(['app.env' => 'staging']);
Enter fullscreen mode Exit fullscreen mode

Then doing a dd on config(‘app.env’) will return that environment you’ve just set.

I haven’t found a way to set environments with app()->environment() so instead, I recommend using this in_array function and passing in the config(‘app.env’) and then specifically defining the environments.

in_array(config('app.env'), ['local', 'staging'])
Enter fullscreen mode Exit fullscreen mode

For example, say you have this in a command:

if (! in_array(config('app.env'), ['local', 'staging'])) {
    $this->error(Will only run on local and staging environments);
    return true;
}
Enter fullscreen mode Exit fullscreen mode

Test this runs when the environment is set to production

test(cannot run on production, function () {

    config(['app.env' => 'production']);

    $this->artisan('db:production-sync')
        ->expectsOutput(DB sync will only run on local and staging environments)
        ->assertExitCode(true);
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more