As far as I have read, in Laravel 4 you could define your database integration test environment by adding a testing/database.php or .env.testing.php file containing your configuiration. In Laravel 5 both ways does no longer work. To switch your environment you have two options:

  1. Put both configuration definitions (testing, dev/production) inside your config/database.php:
    'connections' => [
    
    'sqlite' => [
    'driver' => 'sqlite',
    'database' => storage_path('database.sqlite'),
    'prefix' => '',
    ],
    'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'database' => env('DB_DATABASE', 'schema'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', 'root'),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    ],
    'mysql_testing' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST_TEST', 'localhost'),
    'database' => env('DB_DATABASE_TEST', 'schema_test'),
    'username' => env('DB_USERNAME_TEST', 'root'),
    'password' => env('DB_PASSWORD_TEST', 'root'),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    ],
    

    and store the configuration defaults in your .env file by adding the configuration keys DB_HOST_TEST, DB_DATABASE_TEST and so on. Eventually you must modify your base TestCase::createApplication to use the mysql_testing connection:

    /**
    * Creates the application.
    *
    * @return \Illuminate\Foundation\Application
    */
    public function createApplication()
    {
    putenv('DB_CONNECTION', 'mysql_testing');
    
    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
    
    return $app;
    }
    
  2. I prefer the second solution: Copy the .env file to .env.testing, modify the settings and override the default Dotenv file .env by modifying your base TestCase::createApplication:
    public function createApplication()
    {
    $app = require __DIR__.'/../bootstrap/app.php';
    // ckl: use .env.testing in favor of .env; clear separation between configuration values and configuration definition
    $app->loadEnvironmentFrom('.env.testing');
    
    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
    
    return $app;
    
Categories: Laravel