Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illuminate\Foundation\Testing\DatabaseTransactions に置き換えられるか検討する #734

Open
shibafu528 opened this issue Aug 6, 2021 · 3 comments · May be fixed by #735
Open
Assignees

Comments

@shibafu528
Copy link
Member

テストを実行した時に接続先を間違えると、手動テスト環境のDBを吹き飛ばしてしまうのがつらい。

@shibafu528 shibafu528 self-assigned this Aug 7, 2021
@shibafu528 shibafu528 linked a pull request Aug 7, 2021 that will close this issue
@shibafu528
Copy link
Member Author

テスト用のenvファイルを作ったらいいという話もあるけど rails s みたいに勝手にDB作ってくれたりしないから面倒くせえ! というのがある。
でもそっちで労力払ったほうがいいのかなあ、Laravel的にはどういうのを推奨しているんだろう。

@shibafu528
Copy link
Member Author

.env.testing あたりが読み込まれてくれるなら別にこうしなくても良さそうだけど、どうやったら読まれるのか分からなかった。そんな機能存在するんですか?

@Teruya-Himaki
Copy link
Contributor

.env.testing あたりが読み込まれてくれるなら別にこうしなくても良さそうだけど、どうやったら読まれるのか分からなかった。

いくつかの手動テスト環境を吹き飛ばしてから調べてみたのですが、Laravelには .env.環境名 を読みにいく(なければ .env を読む)機構がデフォルトで入っていて、これはシステムレベルで定義されている DB_DATABASE などの環境変数を上書きすることはしないようです。ですのでcompose.yaml内のenv_fileの記述を消してあげると普段は.envを見にいく一方

  • docker compose exec web php artisan migrate --env=testing
  • docker compose exec web composer test (これがenv=testingなのはphpunit.xmlで定義されている)

あたりで.env.testingを見に行ってくれて嬉しい気がします?

Reference

Before loading your application's environment variables, Laravel determines if an APP_ENV environment variable has been externally provided or if the --env CLI argument has been specified. If so, Laravel will attempt to load an .env.[APP_ENV] file if it exists. If it does not exist, the default .env file will be loaded.

Any variable in your .env file can be overridden by external environment variables such as server-level or system-level environment variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants