Skip to content

Commit 2cd917b

Browse files
committed
First glance to documenting how to run tests
1 parent afd9810 commit 2cd917b

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ with [FrankenPHP](https://frankenphp.dev) and [Caddy](https://caddyserver.com/)
3535
3. [Support for extra services](docs/extra-services.md)
3636
4. [Deploying in production](docs/production.md)
3737
5. [Debugging with Xdebug](docs/xdebug.md)
38-
6. [TLS Certificates](docs/tls.md)
39-
7. [Using MySQL instead of PostgreSQL](docs/mysql.md)
40-
8. [Using Alpine Linux instead of Debian](docs/alpine.md)
41-
9. [Using a Makefile](docs/makefile.md)
42-
10. [Updating the template](docs/updating.md)
43-
11. [Troubleshooting](docs/troubleshooting.md)
38+
6. [Running tests](docs/testing.md)
39+
7. [TLS Certificates](docs/tls.md)
40+
8. [Using MySQL instead of PostgreSQL](docs/mysql.md)
41+
9. [Using Alpine Linux instead of Debian](docs/alpine.md)
42+
10. [Using a Makefile](docs/makefile.md)
43+
11. [Updating the template](docs/updating.md)
44+
12. [Troubleshooting](docs/troubleshooting.md)
4445

4546
## License
4647

docs/testing.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)