Skip to content

[Symfony CLI] Document about APP_ENV=test behavior #15228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2021

Conversation

Kocal
Copy link
Member

@Kocal Kocal commented Apr 12, 2021

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:

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:

# ...
DATABASE_URL=${DATABASE_APP_URL}

and my .env.test file:

# ...
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!

Running the Symfony CLI with `APP_ENV=test` has an interesting but undocumented behaviour, it updates `DATABASE_*` environment variables for a test environment.
@carsonbot carsonbot added this to the 4.4 milestone Apr 12, 2021
@Kocal
Copy link
Member Author

Kocal commented Apr 13, 2021

Funny, I updated the Symfony CLI from 4.21.6 to the latest version and there is a changelog entry about APP_ENV=test and databases:
image

This definitly needs to be in the documentation I think.

@javiereguiluz
Copy link
Member

Hugo, thanks for this contribution and for your perfectly detailed explanation.

Please note that while merging we did a minor syntax fix. Unlike Markdown, the RST format that we use for Symfony Docs requires "double backticks" for code:

// Instead of this:
Using `APP_ENV=test symfony` will [...]

// We must use this:
Using ``APP_ENV=test symfony`` will [...]

But everything else was perfect, so thank you!

@Kocal Kocal deleted the patch-1 branch April 13, 2021 10:45
@Kocal
Copy link
Member Author

Kocal commented Apr 13, 2021

Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants