0

I am trying to set a database environment to test my Doctrine ORM Entity classes, without changing my real database.

I followed Symfony's documentation, but when I run php bin/console doctrine:fixtures:load I get the warning:

Careful, database "graph" will be purged. Do you want to continue? (yes/no) [no]:

This is despite the fact that I have set an environment variable in my .env.test file:

DATABASE_URL=mysql://testUser:testPassword@127.0.0.1/graph_test

Edit: here is the entire .env.test file:

# define your env variables for the test env here
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther

# Database test
DATABASE_URL=mysql://testUser:testPassword@127.0.0.1/graph_test

I tried to change my test/bootstrap.php file but I get the same warning.

Is there a way to tell doctrine to take the DATABASE_URL value from the .env.test file? Any help will be appreciated.

PS There is no need for more than one test .env file.

noam
  • 538
  • 5
  • 19
  • please provide your .env.test file (strip out credentials), maybe you should use `.env.local`? when you debugging locally? – Amacado Jul 11 '20 at 18:03
  • Maybe I will do that in the future, right now I conduct only local testing. – noam Jul 11 '20 at 18:08
  • Try renaming .env.test to .env.local to ensure it's used. As symfony documentation points out if you want to use a custom (in your case "test" env) additional steps are required. `.env.local` is used by default. https://symfony.com/doc/current/configuration.html#overriding-environment-values-via-env-local – Amacado Jul 11 '20 at 18:20
  • 2
    Try `php bin/console doctrine:fixtures:load --env=test` – geoB Jul 11 '20 at 18:22
  • You first should answer, so that there's something for me to accept. – noam Jul 11 '20 at 18:33

3 Answers3

4

If you use php bin/console doctrine:fixtures:load --env=test you will only affect your test database.

geoB
  • 4,578
  • 5
  • 37
  • 70
1

When you want to load a config make sure it's either defined in .env.local or if you're using a custom environment like test add the --env=test parameter to your commands

  php bin/console doctrine:fixtures:load --env=test
  php bin/console server:run --env=test
Amacado
  • 630
  • 5
  • 20
0

doctrine tests include running migrations in order to have the same data in the database for each test run.

from the page you posted...

Resetting the Database Automatically Before each Test¶

This will cause the message Careful, database ... will be purged. This is a warning, not an error.

Artistan
  • 1,982
  • 22
  • 33
  • But I don't want my real database and my test database to be the same. I want the real database to be left intact, but to reset the **test** database before each test. – noam Jul 11 '20 at 18:27