Search code examples
phplaravelphpunitlaravel-8

Why is my env variables being ignored for phpunit?


  • Laravel 8
  • Phpunit 8
  • PHP 8

I have the following 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="[email protected]" />
      <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>

When I do ./vendor/bin/phpunit the tests are really slow and my database is being over written.

As far as I know this is how you tell the tests to use an sqlite database, I have the config already set up:

'connections' => [

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

    // ...
]

If I delete that database file, and run the tests - it doesn't care. It doesn't explode like it should, and it just keeping running the tests.

Am I missing something? I set the right variables and PHP unit should be picking this up. It just randomly stopped working and stopped using the sqlite database and went to using mysql.

If I mess up the phpunit file and then run the command it freaks out, so I know its reading the file. Just not using the variables for some reason.

thoughts?


Solution

  • I know the issue.

    If you are in dev never run: php artisan config:cache those cached values will be used instead of your phpunit environmental variables, causing all kinds of havoc.

    Instead you have to php artisan config:clear and then run your tests again.