If I make request through POSTMAN, record is created in my main database api.
I want to execute tests in database called api_test. When I run the test
Testing [critical] Error thrown while running command
"doctrine:database:drop –connection=test –force=1 –if-exists=1".
Message: "An exception occurred in the driver: SQLSTATE[HY000] [2002]
php_network_getaddresses: getaddrinfo failed: Name or service not
known"
This is absolute nonsense for me. My test url is same like my production one with only database name changed
.env
DATABASE_URL="mysql://root:toma123@database:3306/api?serverVersion=5.7&charset=utf8mb4"
DATABASE_TEST_URL="mysql://root:toma123@database:3306/api_test?serverVersion=5.7&charset=utf8mb4"
In my doctrine.yaml I have
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
when@test:
doctrine:
dbal:
connections:
test:
url: '%env(resolve:DATABASE_TEST_URL)%'
when@prod:
doctrine:
orm:
auto_generate_proxy_classes: false
query_cache_driver:
type: pool
pool: doctrine.system_cache_pool
result_cache_driver:
type: pool
pool: doctrine.result_cache_pool
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system
AGAIN! Can create a record through postman with my default connection.
>Solution :
The when@test section only takes effect when the environment matches the condition: test in this case.
Since you are not passing the -e argument, it’s executing in the default environment, so the test connection doesn’t exist.
Even if the environment matches, the connection is used by the orm mapping and the command you are using stills uses the default mapping (and connection).
Just rely on standard environment configuration: remove the when@test section, rename DATABASE_TEST_URL to DATABASE_URL and put it in the .env.test and execute the command bin/console d:d:d -e test --force.