From a0c605faaeb06cdd1426d48bfbe643954b4be582 Mon Sep 17 00:00:00 2001 From: Abijeet Date: Wed, 27 Jan 2021 03:08:28 +0530 Subject: [PATCH] Docker: Fix PHP tests This creates another mysql_testing database during db service setup Replace server with env tags in phpunit.xml in order to force override certain parameters when tests are run. See: https://github.com/sebastianbergmann/phpunit/issues/2353 for more information. Rename primary developer Docker database from bookstack-test to bookstack-dev. bookstack-test is used as the mysql_testing database --- app/Config/database.php | 3 ++- dev/docker/entrypoint.app.sh | 1 + dev/docker/init.db/01.sql | 5 +++++ docker-compose.yml | 5 +++-- phpunit.xml | 5 +++-- readme.md | 19 +++++++++++++++---- 6 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 dev/docker/init.db/01.sql diff --git a/app/Config/database.php b/app/Config/database.php index ed654ffb9..6e7cf529c 100644 --- a/app/Config/database.php +++ b/app/Config/database.php @@ -81,10 +81,11 @@ return [ 'mysql_testing' => [ 'driver' => 'mysql', 'url' => env('TEST_DATABASE_URL'), - 'host' => '127.0.0.1', + 'host' => $mysql_host, 'database' => 'bookstack-test', 'username' => env('MYSQL_USER', 'bookstack-test'), 'password' => env('MYSQL_PASSWORD', 'bookstack-test'), + 'port' => $mysql_port, 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', diff --git a/dev/docker/entrypoint.app.sh b/dev/docker/entrypoint.app.sh index e91d34a71..9709139ab 100755 --- a/dev/docker/entrypoint.app.sh +++ b/dev/docker/entrypoint.app.sh @@ -10,6 +10,7 @@ else composer install wait-for-it db:3306 -t 45 php artisan migrate --database=mysql + php artisan migrate --database=mysql_testing chown -R www-data:www-data storage exec apache2-foreground fi diff --git a/dev/docker/init.db/01.sql b/dev/docker/init.db/01.sql new file mode 100644 index 000000000..2536c3f80 --- /dev/null +++ b/dev/docker/init.db/01.sql @@ -0,0 +1,5 @@ +# create test database +CREATE DATABASE IF NOT EXISTS `bookstack-test`; + +# grant rights +GRANT ALL PRIVILEGES ON `bookstack-test`.* TO 'bookstack-test'@'%'; diff --git a/docker-compose.yml b/docker-compose.yml index 39f5bdc18..037c5f67e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,12 +10,13 @@ services: db: image: mysql:8 environment: - MYSQL_DATABASE: bookstack-test + MYSQL_DATABASE: bookstack-dev MYSQL_USER: bookstack-test MYSQL_PASSWORD: bookstack-test MYSQL_RANDOM_ROOT_PASSWORD: 'true' command: --default-authentication-plugin=mysql_native_password volumes: + - ./dev/docker/init.db:/docker-entrypoint-initdb.d - db:/var/lib/mysql app: build: @@ -25,7 +26,7 @@ services: DB_CONNECTION: mysql DB_HOST: db DB_PORT: 3306 - DB_DATABASE: bookstack-test + DB_DATABASE: bookstack-dev DB_USERNAME: bookstack-test DB_PASSWORD: bookstack-test MAIL_DRIVER: smtp diff --git a/phpunit.xml b/phpunit.xml index 033218288..3bcedfb42 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,7 +19,7 @@ - + @@ -29,7 +29,7 @@ - + @@ -56,5 +56,6 @@ + diff --git a/readme.md b/readme.md index 8cce8e661..d4cccf0d6 100644 --- a/readme.md +++ b/readme.md @@ -96,18 +96,29 @@ If all the conditions are met, you can proceed with the following steps: 1. **Copy `.env.example` to `.env`**, change `APP_KEY` to a random 32 char string and set `APP_ENV` to `local`. 2. Make sure **port 8080 is unused** *or else* change `DEV_PORT` to a free port on your host. 3. **Run `chgrp -R docker storage`**. The development container will chown the `storage` directory to the `www-data` user inside the container so BookStack can write to it. You need to change the group to your host's `docker` group here to not lose access to the `storage` directory. -4. **Run `echo -e "\n\nDOCKER_UID=$(id -u)" >> .env && echo "DOCKER_GID=$(id -g)" >> .env`** to add your UID/GID to the `.env` file. This is then used to set permissions inside the docker. This is necessary if you are working on Linux. -5. **Run `docker-compose up`** and wait until the image is built and all database migrations have been done. -6. You can now login with `admin@admin.com` and `password` as password on `localhost:8080` (or another port if specified). +4. **Run `docker-compose up`** and wait until the image is built and all database migrations have been done. +5. You can now login with `admin@admin.com` and `password` as password on `localhost:8080` (or another port if specified). If needed, You'll be able to run any artisan commands via docker-compose like so: - ```shell script +```shell script docker-compose run app php artisan list ``` The docker-compose setup runs an instance of [MailHog](https://github.com/mailhog/MailHog) and sets environment variables to redirect any BookStack-sent emails to MailHog. You can view this mail via the MailHog web interface on `localhost:8025`. You can change the port MailHog is accessible on by setting a `DEV_MAIL_PORT` environment variable. +#### Running tests + +After starting the general development Docker, seed the testing database: + ```shell script +# this is to be done only once +docker-compose run app php artisan db:seed --class=DummyContentSeeder --database=mysql_testing +``` + +Once the database has been seeded, you can run the tests by: + ```shell script +docker-compose run app php vendor/bin/phpunit +``` ## 🌎 Translations Translations for text within BookStack is managed through the [BookStack project on Crowdin](https://crowdin.com/project/bookstack). Some strings have colon-prefixed variables in such as `:userName`. Leave these values as they are as they will be replaced at run-time. Crowdin is the preferred way to provide translations, otherwise the raw translations files can be found within the `resources/lang` path.