Search code examples
unit-testingdoctrinesymfony4

symfony4: how do I configure dataFixture to write in test db


I want to load the testdata for my unit tests into the test db via DataFixtures. The documentation says, that if I set the environment variable the test db should be used:

$ php bin/console doctrine:fixtures:load --env=test
Careful, database will be purged. Do you want to continue y/N ?y
  > purging database
  > loading App\DataFixtures\PropertyFixtures
  > loading App\DataFixtures\UserFixtures
  > loading App\DataFixtures\UserPropertyFixtures

However if I check the data end up in my default database.

Where do I configure my test db with symfony 4?

And where do I configure it, so that DataFixtures knows where to write?

For my functional tests I configured the db setting in the phpunit.xml


Solution

  • What we did to solve it was the following. I don't know if that is a good way, so alternative solutions are still appreciated.

    in ../config/packages there is the file doctrine.yaml

    I copied the file in the folder ./config/packages/test and edited it in the following way:

    parameters:
        # Adds a fallback DATABASE_URL if the env var is not set.
        # This allows you to run cache:warmup even if your
        # environment variables are not available yet.
        # You should not need to change this value.
        env(DATABASE_URL): ''
    
    doctrine:
        dbal:
            # configure these for your database server
            driver: 'pdo_mysql'
            server_version: '5.7'
            charset: utf8
            default_table_options:
                charset: utf8
                collate: utf8_unicode_ci
    
            url: mysql://%db_user_test%:%db_password_test%@%db_host_test%:%db_port_test%/%db_name_test%
        orm:
            auto_generate_proxy_classes: '%kernel.debug%'
            naming_strategy: doctrine.orm.naming_strategy.underscore
            auto_mapping: true
            mappings:
                App:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity'
                    prefix: 'App\Entity'
    

    So what I changed was the like with the url: mysql....

    In my parameters I had added the parameters db_user_test etc and used them to connect to the test db.

    It works, but still I am not sure if this is how it should be done.