diff --git a/database/mysql/create-testing-database.sh b/database/mysql/create-testing-database.sh new file mode 100755 index 00000000..c20ae5f7 --- /dev/null +++ b/database/mysql/create-testing-database.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +mysql --user=root --password="$MYSQL_ROOT_PASSWORD" <<-EOSQL + CREATE DATABASE IF NOT EXISTS testing; + GRANT ALL PRIVILEGES ON testing.* TO '$MYSQL_USER'@'%'; +EOSQL diff --git a/database/pgsql/create-testing-database.sql b/database/pgsql/create-testing-database.sql new file mode 100644 index 00000000..d84dc07b --- /dev/null +++ b/database/pgsql/create-testing-database.sql @@ -0,0 +1,2 @@ +SELECT 'CREATE DATABASE testing' +WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'testing')\gexec diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index 9c81f5d6..36797520 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -39,6 +39,7 @@ public function handle() $this->buildDockerCompose($services); $this->replaceEnvVariables($services); + $this->configurePhpUnit(); if ($this->option('devcontainer')) { $this->installDevContainer(); @@ -148,6 +149,21 @@ protected function replaceEnvVariables(array $services) file_put_contents($this->laravel->basePath('.env'), $environment); } + /** + * Configure PHPUnit to use the dedicated testing database. + * + * @return void + */ + protected function configurePhpUnit() + { + $phpunit = file_get_contents($this->laravel->basePath('phpunit.xml')); + + $phpunit = preg_replace('/^.*DB_CONNECTION.*\n/m', '', $phpunit); + $phpunit = str_replace('', '', $phpunit); + + file_put_contents($this->laravel->basePath('phpunit.xml'), $phpunit); + } + /** * Install the devcontainer.json configuration file. * diff --git a/stubs/mariadb.stub b/stubs/mariadb.stub index ec5aa3d0..6beb6bc9 100644 --- a/stubs/mariadb.stub +++ b/stubs/mariadb.stub @@ -11,6 +11,7 @@ MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' volumes: - 'sail-mariadb:/var/lib/mysql' + - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh' networks: - sail healthcheck: diff --git a/stubs/mysql.stub b/stubs/mysql.stub index 2e70d8c9..a775a2bc 100644 --- a/stubs/mysql.stub +++ b/stubs/mysql.stub @@ -11,6 +11,7 @@ MYSQL_ALLOW_EMPTY_PASSWORD: 1 volumes: - 'sail-mysql:/var/lib/mysql' + - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh' networks: - sail healthcheck: diff --git a/stubs/pgsql.stub b/stubs/pgsql.stub index 5aeced5f..7cff7438 100644 --- a/stubs/pgsql.stub +++ b/stubs/pgsql.stub @@ -9,6 +9,7 @@ POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}' volumes: - 'sail-pgsql:/var/lib/postgresql/data' + - './vendor/laravel/sail/database/pgsql/create-testing-database.sql:/docker-entrypoint-initdb.d/10-create-testing-database.sql' networks: - sail healthcheck: