|
| 1 | +# Running tests |
| 2 | + |
| 3 | +Since the `APP_ENV` value hardcoded as an environment value in the php container, |
| 4 | +running `composer dump-env test` will not override this. Therefore, it is not natively possible |
| 5 | +to run tests in the `test` environment when using your development container(s). |
| 6 | + |
| 7 | +## I want to run tests on my server |
| 8 | + |
| 9 | +Simple: you only need to set up and deploy you project the same way as for the dev environment, |
| 10 | +but you will need to set APP_ENV to test. |
| 11 | + |
| 12 | +You can set this value in `compose.override.yaml` directly, or provide your environment values |
| 13 | +with a specific `env-file`: |
| 14 | + |
| 15 | +``` |
| 16 | +docker compose --env-file <your-env-file> up --wait |
| 17 | +``` |
| 18 | + |
| 19 | +## I want to run tests on my desktop |
| 20 | + |
| 21 | +On your desktop, your container is most likely running in the `dev` environment. |
| 22 | +And since `APP_ENV` is hardcoded as one of the container environment values, |
| 23 | +you won't be able to override it. |
| 24 | +So if you try to use `php bin/phpunit`, you will execute your tests in the |
| 25 | +`dev` environment. That's not ideal for unit tests, and clearly bad if you run |
| 26 | +tests against your database. |
| 27 | + |
| 28 | +The solution is to use a Makefile |
| 29 | +(https://symfony.com/doc/current/the-fast-track/en/17-tests.html#automating-your-workflow-with-a-makefile). |
| 30 | +You can start from this template: |
| 31 | + |
| 32 | +```makefile |
| 33 | +SHELL := /bin/bash |
| 34 | +CONTAINER_EXEC := docker exec -i -e APP_ENV=test <your_dev_container> php |
| 35 | + |
| 36 | +tests: |
| 37 | + $(CONTAINER_EXEC) bin/console doctrine:database:drop --force || true |
| 38 | + $(CONTAINER_EXEC) bin/console doctrine:database:create |
| 39 | + $(CONTAINER_EXEC) bin/console doctrine:migrations:migrate -n |
| 40 | + $(CONTAINER_EXEC) bin/console doctrine:fixtures:load -n |
| 41 | + $(CONTAINER_EXEC) bin/phpunit $(MAKECMDGOALS) |
| 42 | +.PHONY: tests |
| 43 | +``` |
| 44 | + |
| 45 | +That way, you can actually override the `APP_ENV` value to `test`. |
| 46 | + |
| 47 | +> [!NOTE] |
| 48 | +> |
| 49 | +> Be mindful about your databases names and/or the value of `APP_DATABASE`: |
| 50 | +> if you use the same for both dev and test environment, execution of fixtures |
| 51 | +> and tests will break your dev database. |
| 52 | +> Using `[email protected]_suffix: "_test"` is a possible solution |
| 53 | +> to have both databases in the database container. |
0 commit comments