php - Lumen 8 不使用 .env.testing
问题描述
我正在使用 Lumen 8。我想使用里面的配置,.env.testing
但它总是读取里面的配置.env
测试/TestCase.php
<?php
use Dotenv\Dotenv;
abstract class TestCase extends Tests\Utilities\UnitTest\Testing\TestCase
{
public static function setUpBeforeClass(): void
{
Dotenv::createImmutable(dirname(__DIR__), '.env.testing')->load();
parent::setUpBeforeClass();
}
public function createApplication()
{
return require __DIR__ . '/../bootstrap/app.php';
}
}
.env.testing
APP_ENV=testing
APP_DEBUG=false
DB_CONNECTION=mysql
DB_HOST=db_testing
DB_PORT=3307
DB_DATABASE=db_testing
DB_USERNAME=db_username
DB_PASSWORD=db_password
.env
APP_ENV=local
APP_DEBUG=false
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3307
DB_DATABASE=db_local
DB_USERNAME=db_username
DB_PASSWORD=db_password
当我调试测试文件时,
dd(DB::connection()->getDatabaseName());
它返回db_local
而不是db_testing
我不想在phpunit.xml
缺少的内容中添加我的所有配置?我应该怎么办?
解决方案
您正在将环境文件加载到新的存储库实例中,但您的 lumen 应用程序不知道存储库实例存在。
接下来,当您的bootstrap/app.php
文件运行时,它将创建存储库实例,该存储库实例加载了.env
lumen 知道如何使用的普通文件。
最干净的解决方案可能是删除您的setUpBeforeClass()
方法并更新您的bootstrap/app.php
文件以支持加载不同的 .env 文件。
一个例子:
$env = env('APP_ENV');
$file = '.env.'.$env;
// If the specific environment file doesn't exist, null out the $file variable.
if (!file_exists(dirname(__DIR__).'/'.$file)) {
$file = null;
}
// Pass in the .env file to load. If no specific environment file
// should be loaded, the $file parameter should be null.
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
dirname(__DIR__),
$file
))->bootstrap();
如果您bootstrap/app.php
使用此代码更新文件,则可以在phpunit.xml
文件中指定一个环境变量以将APP_ENV
变量设置为testing
. 如果你这样做,上面的代码将加载.env.testing
文件。
注意:所有理论都基于阅读代码。未经测试。
推荐阅读
- go - SublimeText 中的 Gofmt 插件未找到 GOPATH 错误
- python - 指定大小的随机子集的 Numpy 列表
- c++ - 如何安全地将变量的值传递给 C++ 方法,该方法仅将 void* 作为参数?
- ios - 现在是否可以使用带有adjustsFontSizeToFitWidth 的Multiline UILabel?
- java - 如何在android中比较两个.db文件格式?
- python - 如何提高在另一个列表上搜索列表的时间?
- vue.js - 如何在数据变量中使用 vue font awesome 组件?
- javascript - 重定向到主路由时,如何呈现从当前路由中选择的数据?
- java - 无法弄清楚我在处理扫描仪、文件输入、nextLine() 等的作业中的错误
- python - 为什么我收到 Python Discord Bot 异常错误