I'm using Laravel and this is my ./phpunit.xml file

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="FeatureTests">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

        <testsuite name="UnitTests">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="sqlite_testing" />
    </php>
</phpunit>

我正在使用以下命令启动我的一个测试套件:

./vendor/phpunit/phpunit/phpunit --testsuite UnitTests

Inside my test method I have:

public function testAllMandatoryData()
{
   dump(env('APP_ENV'));
   ....
}

It's displaying "local" I was expecting "testing" as specified in phpunit.xml

         <env name="APP_ENV" value="testing"/>

Edit: additional details I have this laravel application running in a Docker container

在docker-compose.yml上,我设置了一些环境变量,如下所示:

environment:
  - APP_ENV=local
  - DB_HOST=192.168.0.22
  - DB_PORT=33306
  - DB_DATABASE=mydatabase
  - DB_USERNAME=homestead
  - DB_PASSWORD=homestead

What I've noticed is that directives in phpunit.xml like:

    <env name="APP_ENV" value="testing"/>

have no effect when the name is there in the docker-compose already.

相反,如果我添加了docker compose中未定义的部分.yml将在phpunit运行时正确设置,如:

    <env name="DB_CONNECTION" value="sqlite_test"/>

end Edit

What I'm missing?

Thanks

推荐答案

I try to answer myself with the best option I found.

如果你在docker compose设置环境变量.yml文件您将无法用phpunit覆盖它们.xml指令,例如:

  <env name="APP_ENV" value="testing"/>

Then you should opt for removing (like in this example) APP_ENV variable set from docker-compose.yml

And rely on .env Laravel file

APP_ENV=local

使用此设置,phpunit将能够将APP_ENV覆盖为"测试"

对于所有docker代理版本,我仍然不能100%确定是否需要这种安排.我的另一个主机与另一个Docker版本的主机表现不同.

Laravel相关问答推荐

为什么只删除了最后一个Like,而没有删除选中的?

Laravel 9 上的数组差异助手

使用 laravel 根据当月显示注册用户列表

Laravel - 出于某种原因,max 打破了我的查询

Laravel 5.4 - 如何为同一个自定义验证规则使用多个错误消息

如何在 laravel 5.2 中显示 500 内部服务器错误页面?

Carbon现在时间错了

克隆后设置 Laravel 项目

使用 Eloquent (Laravel) 在 Group By 之前排序

Heroku 上的 Laravel 队列工作者

如何在 Laravel 5 中验证当前、新密码和新密码确认?

如何将Carbon 转换为字符串,只取日期?

为什么 BroadCastEvent 在 Laravel 中排队?如何阻止它?

调整行数以形成:: Textarea Laravel 5

Laravel Mix 生成字体到另一个目录

Laravel 存储链接不适用于生产

如何在 Laravel 4 中组织不同版本的 REST API 控制器?

Laravel 验证存在于不存在的地方

laravel - 无法在控制器构造函数中获取会话

如果在 Laravel 5.1 中找不到路由,则显示 404 页面