Skip to content

Commit 47548e4

Browse files
committed
minor #15228 [Symfony CLI] Document about APP_ENV=test behavior (Kocal)
This PR was merged into the 4.4 branch. Discussion ---------- [Symfony CLI] Document about `APP_ENV=test` behavior Hi 👋 I'm miigrating some projects to the Symfony CLI and Docker and I made a mistake by creating two Docker containers for the main database and a test database (one database per container), but the test database was not used when running `symfony php bin/phpunit` (I have tests which need a database). This is my `.docker-compose.yaml`: ```yaml version: '3.6' services: database: restart: unless-stopped image: 'postgres:12-alpine' ports: [5432] environment: POSTGRES_USER: 'app' POSTGRES_PASSWORD: 'app' POSTGRES_DB: 'app' TZ: Etc/UTC PGTZ: Etc/UTC volumes: - type: bind source: .manala/.docker/db-data target: /var/lib/postgresql/data:z labels: com.symfony.server.service-prefix: 'DATABASE_APP' database_test: restart: unless-stopped image: 'postgres:12-alpine' ports: [5432] environment: POSTGRES_USER: 'test' POSTGRES_PASSWORD: 'test' POSTGRES_DB: 'test' TZ: Etc/UTC PGTZ: Etc/UTC volumes: - type: bind source: .manala/.docker/db-data-test target: /var/lib/postgresql/data:z labels: com.symfony.server.service-prefix: 'DATABASE_TEST' ``` my `.env` file: ```env # ... DATABASE_URL=${DATABASE_APP_URL} ``` and my `.env.test` file: ```env # ... DATABASE_URL=${DATABASE_TEST_URL} ``` I had to run PHPunit with `APP_ENV=test` to let Symfony CLI injects `DATABASE_TEST_URL` but it still didn't work because the database was `test_test`. It seems by luck I discovered an undocumented behaviour, running the Symfony CLI with `APP_ENV=test` updates `DATABASE_*` environment variables for a test environment, so we just need one database container and no `DATABASE_URL` tricks. `symfony var:export --multiline` outputs: ``` export DATABASE_DATABASE=app export DATABASE_DRIVER=postgres export DATABASE_HOST=127.0.0.1 export DATABASE_NAME=app export DATABASE_PASSWORD=app export DATABASE_PORT=49160 export DATABASE_SERVER=postgres://127.0.0.1:49160 export DATABASE_URL=postgres://app:app@127.0.0.1:49160/app?sslmode=disable&charset=utf8 export DATABASE_USER=app export DATABASE_USERNAME=app ``` `APP_ENV=test symfony var:export --multiline` outputs: ``` export DATABASE_DATABASE=app_test export DATABASE_DRIVER=postgres export DATABASE_HOST=127.0.0.1 export DATABASE_NAME=app_test export DATABASE_PASSWORD=app export DATABASE_PORT=49160 export DATABASE_SERVER=postgres://127.0.0.1:49160 export DATABASE_URL=postgres://app:app@127.0.0.1:49160/app_test?sslmode=disable&charset=utf8 export DATABASE_USER=app export DATABASE_USERNAME=app # ... ``` I think it can be intersting to document this behaviour, this way: - people won't do the same mistake than me - people won't be surprised by an undocumented behaviour WDYT? Thanks! Commits ------- 6b10589 [Symfony CLI] Document about `APP_ENV=test` behavior
2 parents 4586d28 + 6b10589 commit 47548e4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

setup/symfony_server.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,22 @@ prefixed with ``DB_``, but as the ``com.symfony.server.service-prefix`` is set
322322
to ``DATABASE``, the web server creates environment variables starting with
323323
``DATABASE_`` instead as expected by the default Symfony configuration.
324324

325+
You don't need to create two containers for a main database and a test database.
326+
Using `APP_ENV=test symfony` will automatically adjust `DATABASE_*` environment variables
327+
for a test environment.
328+
329+
.. code-block:: terminal
330+
331+
$ symfony var:export --multiline
332+
export DATABASE_DATABASE=app
333+
export DATABASE_NAME=app
334+
export DATABASE_URL=postgres://app:app@127.0.0.1:49160/app?sslmode=disable&charset=utf8
335+
336+
$ APP_ENV=test symfony var:export --multiline
337+
export DATABASE_DATABASE=app_test
338+
export DATABASE_NAME=app_test
339+
export DATABASE_URL=postgres://app:app@127.0.0.1:49160/app_test?sslmode=disable&charset=utf8
340+
325341
Here is the list of supported services with their ports and default Symfony
326342
prefixes:
327343

0 commit comments

Comments
 (0)