Laravel phpunit tests – loadEnvironmentFrom not loading correct ENV

I’m having trouble loading the correct ENV file for Laravel 5.1 integration testing.

I found when I create a user/rollback DB, etc… It is acting on my app local database, and not my test database.

I am setting the env on ApplicationCreation with $app->loadEnvironmentFrom('.env.testing');, and it seems to be properly setting:

die('environment ' . $app->environment()); 

Outputs “testing”.

So I’m not sure why it’s running my test suite CRUD operations on my local DB, and not the testing environment.

TestCase.php:

class TestCase extends Illuminate\Foundation\Testing\TestCase
{
    protected $baseUrl = 'http://ymvp-api.localhost/';

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

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

        $app->loadEnvironmentFrom('.env.testing');

        die('environment ' . $app->environment()); //"testing"

        return $app;
    }
    ...

.env.testing:

APP_ENV=testing
APP_DEBUG=true
APP_KEY=my_key

DB_HOST=localhost
DB_DATABASE=testbench
DB_USERNAME=root
DB_PASSWORD=''

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

UserTest:

class UserTest extends TestCase
{

    public function setUp()
    {

        parent::setUp();

        Artisan::call('migrate:reset');

        Artisan::call('migrate');

        Artisan::call('db:seed');

    }

    ... test functions here
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s