I'm learning Symfony 5 at Symfony 5: The Fast Track and I'm at the step 17.4 Working with a Test Database.
I can read from the beginning of this section:
... the Symfony CLI automatically exposes the DATABASE_URL environment variable. When APP_ENV is test, like set when running PHPUnit, it changes the database name from main to main_test so that tests have their very own database.
Before being able to run the test, we need to “initialize” the test database (create the database and migrate it):
$ APP_ENV=test symfony console doctrine:database:create $ APP_ENV=test symfony console doctrine:migrations:migrate -n
My database name is guestbook, running under MySql & Apache, as specified in the .env file:
DATABASE_URL="mysql://root:@127.0.0.1:3306/guestbook?serverVersion=mariadb-10.4.19"
So, while I was expecting a database named guestbook_test to be created when I execute the command symfony console doctrine:database:create, it outputs the following error:
Could not create database `guestbook` for connection named default
An exception occurred while executing 'CREATE DATABASE `guestbook`':
SQLSTATE[HY000]: General error: 1007 Can't create database 'guestbook'; database exists
exit status 1
To make sure the environment is "test", I, first, tried these two ways:
- In the
.envfile:APP_ENV=test - In the
.env.localfile:APP_ENV=test
But, I got the same error.
When I displayed the content of environment variable APP_ENV from a controller, using return new Response("APP_ENV = '" . getenv('APP_ENV') . "'");, it displays a null value: APP_ENV=''.
So, I assigned the value 'test' to APP_ENV in Real environment variables (for me, that was Windows/Paramaters/environment variables...etc). This once, the controller displays APP_ENV='test', but I get always the same error, when I execute the Doctrine command: symfony console doctrine:database:create
I don't know what I'm missing !! Please, any help?
UPDATE ON: 07 June 2021 22:05
I, even tried assigning the value test to APP_ENV in php.ini; but, when I displayed this env var using: symfony console debug:container --env-var=APP_ENV, I got:
Symfony Container Environment Variables
=======================================
// Displaying detailed environment variable usage matching APP_ENV
None of the environment variables match this name.
It's, little bit, strange since I have this env var assigned every where (in .env file, .env.local, php.ini and in a real env vars) and this command says there is no env var called APP_ENV !