so I am trying to load my env into unit tests to avoid defining api keys in the phpunit.xml file in Laravel 5.8. Here is my issue:
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
if (file_exists(dirname(__DIR__) . '/.env.testing')) {
(new \Dotenv\Dotenv(dirname(__DIR__), '.env.testing'))->load();
}
$app->make(Kernel::class)->bootstrap();
return $app;
}
The above works and the env vars all get loaded correctly. However, this is the old way of doing things, all the newer posts recommend doing:
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->loadEnvironmentFrom('.env.testing');
$app->make(Kernel::class)->bootstrap();
return $app;
}
Does anyone have any clue as to why the "suggested" way of loading the env does not work? $app->environmentFilePath() shows the env is being loaded from the correct location.
For unit testing purpose, Laravel already transparently handles existing .env.testing
file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.