Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.env.testing file ignored when running phpunit tests for a laravel app

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

like image 608
realtebo Avatar asked Oct 30 '25 10:10

realtebo


2 Answers

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"/>
like image 175
realtebo Avatar answered Nov 02 '25 00:11

realtebo


I faced this issue and I tried to update phpunit.xml to use

<env /> instead of <server />

and it works (Laravel 5.7)

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="APP_DEBUG" value="false"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="DB_CONNECTION" value="sqlite"/>
    <env name="DB_DATABASE" value=":memory:"/>
    <env name="MAIL_MAILER" value="array"/>
    <env name="QUEUE_CONNECTION" value="sync"/>
    <env name="SESSION_DRIVER" value="array"/>
</php>
like image 43
Mina William Avatar answered Nov 02 '25 01:11

Mina William



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!