Skip to content

Commit 69013b1

Browse files
authored
Create a dedicated testing database (#388)
1 parent 1238874 commit 69013b1

File tree

6 files changed

+27
-0
lines changed

6 files changed

+27
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
mysql --user=root --password="$MYSQL_ROOT_PASSWORD" <<-EOSQL
4+
CREATE DATABASE IF NOT EXISTS testing;
5+
GRANT ALL PRIVILEGES ON testing.* TO '$MYSQL_USER'@'%';
6+
EOSQL
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT 'CREATE DATABASE testing'
2+
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'testing')\gexec

src/Console/InstallCommand.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function handle()
3939

4040
$this->buildDockerCompose($services);
4141
$this->replaceEnvVariables($services);
42+
$this->configurePhpUnit();
4243

4344
if ($this->option('devcontainer')) {
4445
$this->installDevContainer();
@@ -148,6 +149,21 @@ protected function replaceEnvVariables(array $services)
148149
file_put_contents($this->laravel->basePath('.env'), $environment);
149150
}
150151

152+
/**
153+
* Configure PHPUnit to use the dedicated testing database.
154+
*
155+
* @return void
156+
*/
157+
protected function configurePhpUnit()
158+
{
159+
$phpunit = file_get_contents($this->laravel->basePath('phpunit.xml'));
160+
161+
$phpunit = preg_replace('/^.*DB_CONNECTION.*\n/m', '', $phpunit);
162+
$phpunit = str_replace('<!-- <env name="DB_DATABASE" value=":memory:"/> -->', '<env name="DB_DATABASE" value="testing"/>', $phpunit);
163+
164+
file_put_contents($this->laravel->basePath('phpunit.xml'), $phpunit);
165+
}
166+
151167
/**
152168
* Install the devcontainer.json configuration file.
153169
*

stubs/mariadb.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
1212
volumes:
1313
- 'sail-mariadb:/var/lib/mysql'
14+
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
1415
networks:
1516
- sail
1617
healthcheck:

stubs/mysql.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
MYSQL_ALLOW_EMPTY_PASSWORD: 1
1212
volumes:
1313
- 'sail-mysql:/var/lib/mysql'
14+
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
1415
networks:
1516
- sail
1617
healthcheck:

stubs/pgsql.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
1010
volumes:
1111
- 'sail-pgsql:/var/lib/postgresql/data'
12+
- './vendor/laravel/sail/database/pgsql/create-testing-database.sql:/docker-entrypoint-initdb.d/10-create-testing-database.sql'
1213
networks:
1314
- sail
1415
healthcheck:

0 commit comments

Comments
 (0)