简体   繁体   English

Laravel - $app->loadEnvironmentFrom('.env.testing') 方法不起作用

[英]Laravel - $app->loadEnvironmentFrom('.env.testing') method does not work

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.所以我试图将我的 env 加载到单元测试中,以避免在 Laravel 5.8 的 phpunit.xml 文件中定义 api 键。 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?有没有人知道为什么“建议的”加载 env 的方式不起作用? $app->environmentFilePath() shows the env is being loaded from the correct location. $app->environmentFilePath() 显示正在从正确位置加载 env。

For unit testing purpose, Laravel already transparently handles existing .env.testing file.出于单元测试的目的,Laravel 已经透明地处理现有的.env.testing文件。

See https://laravel.com/docs/5.8/testing#environment请参阅https://laravel.com/docs/5.8/testing#environment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM