テスト用のマイグレーションが実行できない - d-sazanami/laravel-jissenkaihatsu-v1 GitHub Wiki
現象
首題の通りで、ターミナルは以下の表示となる
# php artisan migrate:refresh --database=testing
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel_app_test and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:712
708▕ // If an exception occurs when attempting to run a query, we'll format the error
709▕ // message to include the bindings with SQL, which will make this exception a
710▕ // lot more helpful to the developer instead of just the database's errors.
711▕ catch (Exception $e) {
➜ 712▕ throw new QueryException(
713▕ $query, $this->prepareBindings($bindings), $e
714▕ );
715▕ }
716▕ }
+47 vendor frames
48 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
対応
データベースの用意
WSLターミナルでプロジェクトディレクトリへ移動し、mysql
コンテナに接続。rootアカウントでmysql CLIを実行し、以下コマンドでテスト用データベースを作成
create database laravel_app_test character set = utf8mb4 collate=utf8mb4_unicode_ci;
データベースを作成しただけでは、エラーは解消されなかった。 sailユーザーでmysqlに接続したところ、作成されたデータベースを見ることができなかった。権限が足りない模様。
sailユーザーにテストデータベースへの権限を付与
GRANT ALL ON laravel_app_test.* TO sail;
それでも解決しない
APP_KEYを生成
.env
を .env.testing
としてコピーし、テスト用APP_KEYを生成すると、テスト用マイグレーションが通った
APP_KEYを空に設定
APP_KEY=
APP_KEY生成
WSLターミナルで、laravel.test
コンテナにアクセスし、以下コマンドでテスト用のキーを生成
php artisan config:clear
php artisan key:generate --env=testing
テスト用マイグレーション
APP_KEY生成コマンドに引き続き、以下を実行
php artisan migrate:refresh --env=testing