Home > Back-end >  PHPUnit ignores DB_CONNECTION variable in phpunit.xml file in Laravel project
PHPUnit ignores DB_CONNECTION variable in phpunit.xml file in Laravel project

Time:02-03

Everything was fine until yesterday when PHPUnit for no reason stoped seeing DB_CONNECTION environment variable from phpunit.xml file and started taking it from .env file.

I tried to recreate phpunit.xml file, clear all possible caches - no result.

This is my phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Modules Tests">
            <directory suffix="Test.php">./Modules/**/Tests</directory>
        </testsuite>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
    </testsuites>
    <listeners>
        <listener />
    </listeners>
    <filter>
        <whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
            <directory suffix=".php">./Modules</directory>
            <exclude>
                <directory suffix="blade.php">./Modules</directory>
                <directory suffix=".php">./Modules/**/Routes</directory>
                <directory suffix=".php">./Modules/**/Resources</directory>
                <directory suffix=".php">./Modules/**/Tests</directory>
                <directory suffix=".php">./Modules/**/Config</directory>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="tests/coverage" showUncoveredFiles="true" />
    </logging>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value="./database/database.sqlite"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

This is my config/database.php file:

    'connections' => [
        'sqlite_testing' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ],

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

Of courses file database/database.sqlite exists.

And when I try to get driver name in my test:

public function test_some(): void
{
    dd(\DB::getDriverName());
}

I get: enter image description here

CodePudding user response:

matiaslauriti:

"Of course... NEVER cache the config locally... NEVER run php artisan config:cache, because you have a local config that will not LOAD any ENV..."

So php artisan cache:clear && php artisan config:clear && composer dump-autoload solved the problem

  • Related