Skip to content

Instantly share code, notes, and snippets.

@akatche
Last active October 7, 2022 13:08
Show Gist options
  • Save akatche/e8387e839387d31e22170696fcbd5d01 to your computer and use it in GitHub Desktop.
Save akatche/e8387e839387d31e22170696fcbd5d01 to your computer and use it in GitHub Desktop.
Run Laravel parallel tests using Laravel Sail
#
# Create this file in the following folder docker/mysql/docker-entrypoint-initdb.d
#
CREATE DATABASE IF NOT EXISTS `test_1` COLLATE 'utf8_general_ci' ;
GRANT ALL ON `test_1`.* TO 'root'@'%' ;
CREATE DATABASE IF NOT EXISTS `test_2` COLLATE 'utf8_general_ci' ;
GRANT ALL ON `test_2`.* TO 'root'@'%' ;
CREATE DATABASE IF NOT EXISTS `test_3` COLLATE 'utf8_general_ci' ;
GRANT ALL ON `test_3`.* TO 'root'@'%' ;
# Create as many databases as processes you can run
FLUSH PRIVILEGES ;
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
# - pgsql
- redis
# - selenium
# selenium:
# image: 'selenium/standalone-chrome'
# volumes:
# - '/dev/shm:/dev/shm'
# networks:
# - sail
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping"]
mysql_test:
image: "mysql:8.0"
environment:
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
MYSQL_DATABASE: "${DB_DATABASE}"
MYSQL_USER: "${DB_USERNAME}"
MYSQL_PASSWORD: "${DB_PASSWORD}"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
volumes:
- '${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d'
networks:
- sail
# pgsql:
# image: postgres:13
# ports:
# - '${FORWARD_DB_PORT:-5432}:5432'
# environment:
# PGPASSWORD: '${DB_PASSWORD:-secret}'
# POSTGRES_DB: '${DB_DATABASE}'
# POSTGRES_USER: '${DB_USERNAME}'
# POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
# volumes:
# - 'sailpostgresql:/var/lib/postgresql/data'
# networks:
# - sail
# healthcheck:
# test: ["CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}"]
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
# memcached:
# image: 'memcached:alpine'
# ports:
# - '11211:11211'
# networks:
# - sail
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
# sailpostgresql:
# driver: local
sailredis:
driver: local
DB_CONNECTION=mysql
DB_HOST=mysql_test
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
MYSQL_ENTRYPOINT_INITDB=./docker/mysql/docker-entrypoint-initdb.d
@akatche
Copy link
Author

akatche commented Mar 14, 2021

The following steps are needed in order to run Laravel parallel tests on dedicated test databases

  • Modify your docker-compose.yml with the new mysql_test entry (thanks to Michael Heap´s blog post)
  • Create a new folder under your root directory, for example "docker/mysql/docker-entrypoint-initdb.d", then add a new file called createdb.sql on it
  • Paste the content of my createdb.sql into yours, and adapt it as needed, for example, because I run my parallel tests with 3 processes I only created 3 databases
  • In your .env.testing modify your file in order to match mine
  • Run sail build --no-cache and the sail up to rebuild your container images, and that´s it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment