Ad

.env.testing File Ignored When Running Phpunit Tests For A Laravel App

- 1 answer

NB: I consulted other similar questions, but all of them resolved clearing cache, setting APP_ENV in the xml, and/or creating the .env.testing.


Laravel version: "laravel/framework": "5.8.*",

I've the standard .env file with

APP_ENV=local
DB_CONNECTION=mysql

I've cloned the standard .env file and saved as .env.testing, then changed to

APP_ENV=testing
DB_CONNECTION=mongodb

From console, from inside the root of the project, I'm trying to run test using

php artisan config:cache && vendor/bin/phpunit

The problem is that Laravel/PHPUnit is still using the .env file, instead of the expected .env.testing. This is the proof

TypeError: Argument 1 passed to Jenssegers\Mongodb\Query\Builder::__construct() must be an instance of Jenssegers\Mongodb\Connection, instance of Illuminate\Database\MySqlConnection given,

This is the actual phpunit.xml config file

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

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>
</phpunit>

So, I kindly ask a suggestion for debugging and fixing

Ad

Answer

I found this issue: https://github.com/sebastianbergmann/phpunit/issues/2353

So I tried to change this portion of .xml file to force my configs.

It works. note all rows are now with appended force="true"

    <server name="APP_ENV" value="testing"  force="true"/>
    <server name="BCRYPT_ROUNDS" value="4" force="true"/>
    <server name="CACHE_DRIVER" value="array" force="true"/>
    <server name="MAIL_DRIVER" value="array" force="true"/>
    <server name="QUEUE_CONNECTION" value="sync" force="true"/>
    <server name="DB_CONNECTION" value="mongodb_testing" force="true"/>
    <server name="SESSION_DRIVER" value="array" force="true"/>
Ad
source: stackoverflow.com
Ad