4

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

4
  • 2
    Which version of laravel are you using? Commented Jun 14, 2019 at 8:25
  • I'm at latest "laravel/framework": "5.8.*", Commented Jun 14, 2019 at 8:28
  • Try php artisan config:clear && php artisan test Commented Apr 8 at 15:05
  • See stackoverflow.com/a/79562540/5129122 Commented Apr 8 at 16:03

2 Answers 2

3

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"/>
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.