LoginSignup
0
0

More than 5 years have passed since last update.

[Laravel 5.2] Laravelでテスト用のDBを指定する方法

Posted at

laravel入門したてでわからないことばかりな人の備忘録です。

環境変数を使って、デフォルトで使用するDBを指定

config/database.php
<?php

return [

    //

    'default' => env('DB_DEFAULT', 'mysql'),

    'connections' => [

        'sqlite_testing' => [
          'driver'   => 'sqlite',
          'database' => ':memory:',
          'prefix'   => '',
        ],

        'mysql' => [
          'driver'    => 'mysql',
          'host'      => env('DB_HOST', 'localhost'),
          'database'  => env('DB_DATABASE', 'forge'),
          'username'  => env('DB_USERNAME', 'forge'),
          'password'  => env('DB_PASSWORD', ''),
          'charset'   => 'utf8',
          'collation' => 'utf8_unicode_ci',
          'prefix'    => '',
            'strict'    => false,
          ],
        ],

    //
];

Testの際に使用するDBを指定

putenvでDB_DEFAULTを書き換えます

tests/unit/TestCase.php
<?php namespace UnitTests;

use Illuminate\Support\Facades\Artisan;

class TestCase extends \Illuminate\Foundation\Testing\TestCase
{

    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        putenv('DB_DEFAULT=sqlite_testing');

        $app = require __DIR__ . '/../../bootstrap/app.php';

参考

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0