How to specify a different .env file for phpunit in Laravel 5?

前端 未结 10 2208
礼貌的吻别
礼貌的吻别 2020-12-29 05:25

I have a .env file containing my database connection details, as is normal for Laravel 5. I want to override these for testing, which I can do in phpunit.

10条回答
  •  有刺的猬
    2020-12-29 05:58

    You could override the .env file being used in your TestCase file, where the framework is booted for testing.

    More specific:

    tests/TestCase.php

    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        /* @var \Illuminate\Foundation\Application $app */
        $app = require __DIR__ . '/../bootstrap/app.php';
    
        $app->loadEnvironmentFrom('.env.testing'); // specify the file to use for environment, must be run before boostrap
    
        $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
    
        return $app;
    }
    

    All the tests extending TestCase will use this configuration file.

    Please note that any setting defined in phpunit.xml will override this configuration.

    Update

    Starting Laravel5.4, the createApplication function is no longer found in tests\TestCase. It has been moved to tests\CreatesApplication trait.

提交回复
热议问题