From 2cd917b2aa9d2e8fba752ff51ff185c8afefe5e6 Mon Sep 17 00:00:00 2001 From: tcoch Date: Mon, 15 Sep 2025 11:04:39 +0200 Subject: [PATCH 1/5] First glance to documenting how to run tests --- README.md | 13 ++++++------ docs/testing.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 docs/testing.md diff --git a/README.md b/README.md index 0995172a2..16c914c04 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,13 @@ with [FrankenPHP](https://frankenphp.dev) and [Caddy](https://caddyserver.com/) 3. [Support for extra services](docs/extra-services.md) 4. [Deploying in production](docs/production.md) 5. [Debugging with Xdebug](docs/xdebug.md) -6. [TLS Certificates](docs/tls.md) -7. [Using MySQL instead of PostgreSQL](docs/mysql.md) -8. [Using Alpine Linux instead of Debian](docs/alpine.md) -9. [Using a Makefile](docs/makefile.md) -10. [Updating the template](docs/updating.md) -11. [Troubleshooting](docs/troubleshooting.md) +6. [Running tests](docs/testing.md) +7. [TLS Certificates](docs/tls.md) +8. [Using MySQL instead of PostgreSQL](docs/mysql.md) +9. [Using Alpine Linux instead of Debian](docs/alpine.md) +10. [Using a Makefile](docs/makefile.md) +11. [Updating the template](docs/updating.md) +12. [Troubleshooting](docs/troubleshooting.md) ## License diff --git a/docs/testing.md b/docs/testing.md new file mode 100644 index 000000000..97453a105 --- /dev/null +++ b/docs/testing.md @@ -0,0 +1,53 @@ +# Running tests + +Since the `APP_ENV` value hardcoded as an environment value in the php container, +running `composer dump-env test` will not override this. Therefore, it is not natively possible +to run tests in the `test` environment when using your development container(s). + +## I want to run tests on my server + +Simple: you only need to set up and deploy you project the same way as for the dev environment, +but you will need to set APP_ENV to test. + +You can set this value in `compose.override.yaml` directly, or provide your environment values +with a specific `env-file`: + +``` +docker compose --env-file up --wait +``` + +## I want to run tests on my desktop + +On your desktop, your container is most likely running in the `dev` environment. +And since `APP_ENV` is hardcoded as one of the container environment values, +you won't be able to override it. +So if you try to use `php bin/phpunit`, you will execute your tests in the +`dev` environment. That's not ideal for unit tests, and clearly bad if you run +tests against your database. + +The solution is to use a Makefile +(https://symfony.com/doc/current/the-fast-track/en/17-tests.html#automating-your-workflow-with-a-makefile). +You can start from this template: + +```makefile +SHELL := /bin/bash +CONTAINER_EXEC := docker exec -i -e APP_ENV=test php + +tests: + $(CONTAINER_EXEC) bin/console doctrine:database:drop --force || true + $(CONTAINER_EXEC) bin/console doctrine:database:create + $(CONTAINER_EXEC) bin/console doctrine:migrations:migrate -n + $(CONTAINER_EXEC) bin/console doctrine:fixtures:load -n + $(CONTAINER_EXEC) bin/phpunit $(MAKECMDGOALS) +.PHONY: tests +``` + +That way, you can actually override the `APP_ENV` value to `test`. + +> [!NOTE] +> +> Be mindful about your databases names and/or the value of `APP_DATABASE`: +> if you use the same for both dev and test environment, execution of fixtures +> and tests will break your dev database. +> Using `when@test.doctrine.dbal.dbname_suffix: "_test"` is a possible solution +> to have both databases in the database container. From 448e75f2985cff0bb2dbd58831c0e6436e4dec21 Mon Sep 17 00:00:00 2001 From: tcoch Date: Mon, 15 Sep 2025 11:09:23 +0200 Subject: [PATCH 2/5] Fixmarkdown linting issue --- docs/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/testing.md b/docs/testing.md index 97453a105..7001cd32c 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -12,7 +12,7 @@ but you will need to set APP_ENV to test. You can set this value in `compose.override.yaml` directly, or provide your environment values with a specific `env-file`: -``` +```bash docker compose --env-file up --wait ``` From c889ef5baa6ed91ca775ac4fc7aa73417238b761 Mon Sep 17 00:00:00 2001 From: tcoch Date: Mon, 15 Sep 2025 11:10:04 +0200 Subject: [PATCH 3/5] Use 'docker compose exec' instead of just 'docker exec' --- docs/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/testing.md b/docs/testing.md index 7001cd32c..b9eee4c7d 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -31,7 +31,7 @@ You can start from this template: ```makefile SHELL := /bin/bash -CONTAINER_EXEC := docker exec -i -e APP_ENV=test php +CONTAINER_EXEC := docker compose exec -i -e APP_ENV=test php tests: $(CONTAINER_EXEC) bin/console doctrine:database:drop --force || true From a001d76b72451adec9455ab2cabbb13d648b1032 Mon Sep 17 00:00:00 2001 From: tcoch Date: Mon, 15 Sep 2025 11:13:31 +0200 Subject: [PATCH 4/5] Fix md linting / Bare URL used --- docs/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/testing.md b/docs/testing.md index b9eee4c7d..efcd3138c 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -26,7 +26,7 @@ So if you try to use `php bin/phpunit`, you will execute your tests in the tests against your database. The solution is to use a Makefile -(https://symfony.com/doc/current/the-fast-track/en/17-tests.html#automating-your-workflow-with-a-makefile). +(check out [Automating your Workflow with a Makefile](https://symfony.com/doc/current/the-fast-track/en/17-tests.html#automating-your-workflow-with-a-makefile)). You can start from this template: ```makefile From ce6aff1755dcc59219584aaeaaf33065e830e7f7 Mon Sep 17 00:00:00 2001 From: tcoch Date: Mon, 15 Sep 2025 11:23:38 +0200 Subject: [PATCH 5/5] Fix md linting / Hard tabs --- docs/testing.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/testing.md b/docs/testing.md index efcd3138c..f8b577fde 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -34,11 +34,11 @@ SHELL := /bin/bash CONTAINER_EXEC := docker compose exec -i -e APP_ENV=test php tests: - $(CONTAINER_EXEC) bin/console doctrine:database:drop --force || true - $(CONTAINER_EXEC) bin/console doctrine:database:create - $(CONTAINER_EXEC) bin/console doctrine:migrations:migrate -n - $(CONTAINER_EXEC) bin/console doctrine:fixtures:load -n - $(CONTAINER_EXEC) bin/phpunit $(MAKECMDGOALS) + $(CONTAINER_EXEC) bin/console doctrine:database:drop --force || true + $(CONTAINER_EXEC) bin/console doctrine:database:create + $(CONTAINER_EXEC) bin/console doctrine:migrations:migrate -n + $(CONTAINER_EXEC) bin/console doctrine:fixtures:load -n + $(CONTAINER_EXEC) bin/phpunit $(MAKECMDGOALS) .PHONY: tests ```