davedriesmans's avatar

.env.testing not loaded

I want to use two connections: mysql for local dev, sqlite for testing. I configured this in .env and .env.testing

The issue is that it always takes the connection of the .env file

Any ideas where i can look?

php artisan migrate --env=testing gives [PDOException] SQLSTATE[HY000] [2002] Connection refused

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
            <exclude>
                <file>./app/Http/routes.php</file>
            </exclude>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing" override="true"/>
        <env name="DB_CONNECTION" value="sqlite" override="true"/>
    </php>
    <listeners>
        <listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>
    </listeners>
</phpunit>

testcase

public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';
        $app->loadEnvironmentFrom('.env.testing');

        $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

        return $app;
    }

    public function setUp()
    {
        parent::setUp();
        $this->prepareForTests();
    }
    private function prepareForTests()
    {
        Artisan::call('migrate');
        Artisan::call('db:seed');
    }

    public function tearDown()
    {
        parent::tearDown();
    }
0 likes
5 replies
cre3z's avatar

Why are you using two .env files? I usually use two connections one for production and one for local. I declare both connections in my only .env file and change the default connection when I need a different one.

Also check your config/database.php file both connections need to specified in this file and in your .env file.

ejdelmonico's avatar

You can do this in phpunit.xml and achieve what you want:

<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
cre3z's avatar

You can still achieve this using only one .env file.

If you want to use two .env files then do as @ejdelmonico suggests, make sure the name is correct.

ejdelmonico's avatar

@davedriesmans@gmail.com No, it's not. Pay particular attention to DB_CONNECTION and DB_DATABASE. I use that in all my setups. I use MariaDB represented in the .env file and I use a SQLite in memory DB for testing. There is no other env file needed unless you want to another MariaDB, MySQL or MongoDB DB for testing...but its not necessary.

Please or to participate in this conversation.