Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mattiman's avatar

OK tested that and it does return "sqlite". So that works. Now to find out where the errors come from. Probably the seeding not working.

michaeldyrynda's avatar

Yeah, see if you can use SQLite as your default and try and run the db:seed and migrate commands on the command line, it may give you a better idea of what's going on.

1 like
bencavens's avatar

Hello Mattiman,

You can set the DB_CONNECTION which comes out of the box like so:

<testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/integration</directory>
        </testsuite>
    </testsuites>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="sqlite"/>
    </php>
mihatt's avatar

copying a part of my blog here http://www.webeks.net/laravel-testing-environment-file

After searching around a bit, we suppose to include variables inside phpunit.xml. There are some preincluded ones

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
</php>

However I wanted to use .env files. So there is always a way :)

All you need to do now is call applications loadEnvironmentFrom($file) with proper env file. Since I only needed it for testing I added .env.testing.example and .env.testing (that is excluded from git). I added a call to method in /tests/TestCase.php

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    //THIS IS IT :)
    $app->loadEnvironmentFrom('.env.testing');
    
    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

    return $app;
}
2 likes
patrikw1's avatar

I think prior to laravel 5.5 it's not necessary to call loadEnvironmentFrom method, since it should detect automatically that you are loading .env.testing, so you only include .env.testing.example in your VCS..

Important thing is that you should run php artisan cache:clear.. and more importantly run php artisan config:clear, then laravel should be able to benefit from phpunit.xml that has set APP_ENV to testing and should load .env.testing automatically like i said :) its working for me without having anything extra in CreatesApplication Trait or custom service provider that checks for file existance and uses loading from different .env file while bootstraping laravel application. Hope i helped you.

If i am not wrong it's happening in LoadEnvironmentVariables class:

    /**
     * Detect if a custom environment file matching the APP_ENV exists.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @return void
     */
    protected function checkForSpecificEnvironmentFile($app)
    {
        if ($app->runningInConsole() && ($input = new ArgvInput)->hasParameterOption('--env')) {
            $this->setEnvironmentFilePath(
                $app, $app->environmentFile().'.'.$input->getParameterOption('--env')
            );
        }

        if (! env('APP_ENV')) {
            return;
        }

        $this->setEnvironmentFilePath(
            $app, $app->environmentFile().'.'.env('APP_ENV')
        );
    }

    /**
     * Load a custom environment file.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  string  $file
     * @return void
     */
    protected function setEnvironmentFilePath($app, $file)
    {
        if (file_exists($app->environmentPath().'/'.$file)) {
            $app->loadEnvironmentFrom($file);
        }
    }
Previous

Please or to participate in this conversation.