2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Laravel】Dockerでテスト用のDBを作成する方法

Last updated at Posted at 2022-03-24

環境

Laravel v9.5.1 (PHP v8.1.3)

やり方

テスト用のDBを作成するためにdocker-compose.ymlに追記。

テスト用のDBを環境変数にしない理由は、Dockerを立ち上げたときにおそらく.envは読み込んでいるが.env.testingはテストを実行したときに読み込んでいるため、ここで環境変数にしても意味がないから。
環境変数にした場合、テストがうまく行かなかった。

docker-compose.yml
    mysql: &mysql
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: "%"
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
    # test用DB
    mysql_test:
        <<: *mysql
        volumes:
            - 'sail-mysql-test:/var/lib/mysql'
        ports: []
        environment:
            MYSQL_DATABASE: 'example_app'
            MYSQL_USER: 'root'
            MYSQL_PASSWORD: ''
            MYSQL_ROOT_PASSWORD: ''
            MYSQL_ROOT_HOST: "%"
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
    ...
    volumes:
        sail-mysql-test:
            driver: local

元々入っていた.env.exampleを丸々コピーしてテスト用のenvファイルを作成。
(※APP_KEYは新たに作成する必要あり php artisan key:generate --env=testing
DBに関するところだけ、docker-compose.ymlのテスト用DBの方の環境変数と合わせて追記する。

.env.testing
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:******/*****/*******************
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

####下記を編集
DB_CONNECTION=mysql
DB_HOST=mysql_test
DB_PORT=3306
DB_DATABASE=example_app
DB_USERNAME=root
DB_PASSWORD=
####

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Dockerを立ち上げ直すのを忘れずに!
これでテストを実行してもデータがテスト環境に保存されるはず。

2
0
0

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?