为什么我的 env 变量被 phpunit 忽略了?

Why is my env variables being ignored for phpunit?

我有以下phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" printerClass="\Sempro\PHPUnitPrettyPrinter\PrettyPrinterForPhpUnit9">
  <coverage processUncoveredFiles="true">
    <include>
      <directory suffix=".php">./app</directory>
    </include>
    <exclude>
      <directory suffix=".php">./app/Flare/MapGenerator</directory>
    </exclude>
  </coverage>
  <testsuites>
    <testsuite name="Feature">
      <directory suffix="Test.php">./tests/Console</directory>
    </testsuite>
    <testsuite name="Feature">
      <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
    <testsuite name="Unit">
      <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
  </testsuites>
  <php>
      <ini name="memory_limit" value="-1" />
      <env name="APP_ENV" value="testing"/>
      <env name="BCRYPT_ROUNDS" value="4"/>
      <env name="CACHE_DRIVER" value="array"/>
      <env name="MAIL_DRIVER" value="array"/>
      <env name="MAIL_USERNAME" value="test@example.ca" />
      <env name="QUEUE_CONNECTION" value="sync"/>
      <env name="DB_CONNECTION" value="sqlite"/>
      <env name="DB_DATABASE" value=":memory:"/>
      <env name="SESSION_DRIVER" value="array"/>
      <env name="BROADCAST_DRIVER" value="log"/>
      <env name="TIME_ZONE" value="America/Edmonton"/>
  </php>
</phpunit>

当我这样做时 ./vendor/bin/phpunit 测试真的很慢而且我的数据库正在被覆盖。

据我所知,这是告诉测试使用 sqlite 数据库的方式,我已经设置了配置:

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => database_path('database.sqlite'),
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    ],

    // ...
]

如果我删除那个数据库文件,运行 测试 - 它不在乎。它没有像它应该的那样爆炸,它只是保持 运行 测试。

我错过了什么吗?我设置了正确的变量,PHP 单元应该会选择它。它只是随机停止工作并停止使用 sqlite 数据库并转而使用 mysql.

如果我弄乱了 phpunit 文件然后 运行 命令它会崩溃,所以我知道它正在读取文件。只是出于某种原因没有使用变量。

想法?

我知道这个问题。

如果你在开发中永远不会运行:php artisan config:cache那些缓存的值将被用来代替你的 phpunit 环境变量,造成各种破坏。

相反,您必须 php artisan config:clear 然后 运行 再次测试。