E
E
EVOSandru62021-08-16 16:45:21
symfony
EVOSandru6, 2021-08-16 16:45:21

What is the correct way to spill fixtures in a test environment inside a Docker container in symfony doctrine?

Hey guys.

Please tell me - how to pass the environment variable when starting the Docker container?
My task is to load the fixtures before running the test.
The offdoc contains the following construct for the Makefile for this task:

https://symfony.com/doc/current/the-fast-track/en/...

SHELL := /bin/bash
tests: export APP_ENV=test
tests:
    symfony console doctrine:database:drop --force || true
    symfony console doctrine:database:create
    symfony console doctrine:migrations:migrate -n
    symfony console doctrine:fixtures:load -n
    symfony php bin/phpunit [email protected]
.PHONY: tests


I try the following way:

SHELL := /bin/bash
tests: export APP_ENV=test
tests:
  docker-compose run --rm manager-php-cli bin/console doctrine:database:drop --force || true
  docker-compose run --rm manager-php-cli bin/console doctrine:database:create
  docker-compose run --rm manager-php-cli bin/console doctrine:migrations:migrate -n
  docker-compose run --rm manager-php-cli bin/console doctrine:fixtures:load -n
  docker-compose run --rm manager-php-cli php bin/phpunit [email protected]
.PHONY: tests


But when I run: make tests I catch an error, as if this environment variable was not picked up.

Could not create database "app" for connection named default
An exception occurred while executing 'CREATE DATABASE "app"':
SQLSTATE[42P04]: Duplicate database: 7 ERROR: database "app" already


exists app - the name of the real database, app_test - test
The script tries to recreate the currently existing working database.

In .env.test the connection is defined like this
DATABASE_URL=pgsql://app:[email protected]:5432/app

(when creating a database in a test, for some reason the name of the database changes to app_test)

In .env for the combat base, the connection is defined as follows:
DATABASE_URL= pgsql://app:[email protected]:5432/app

This test database is in a separate container.

manager-postgres:
    image: postgres:13.3-alpine
    volumes:
      - manager-postgres:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: app
    ports:
      - 54321:5432
  # тестовая база
  manager-postgres-test:
    image: postgres:13.3-alpine
    volumes:
      - manager-postgres-test:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: app_test
    ports:
      - 54322:5432


What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EVOSandru6, 2021-08-17
@EVOSandru6

I solved it by adding --env=test flag

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question