symfony4:如何配置 dataFixture 写入测试数据库

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

我想通过 DataFixtures 将单元测试的测试数据加载到测试数据库中。 文档说,如果我设置环境变量,则应使用测试数据库:

$ 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

但是,如果我检查数据最终会出现在我的默认数据库中。

我在哪里用 symfony 4 配置我的测试数据库?

我在哪里配置它,以便 DataFixtures 知道在哪里写入?

对于我的功能测试,我在 phpunit.xml

中配置了数据库设置

我们为解决该问题所做的工作如下。我不知道这是否是一个好方法,所以仍然赞赏替代解决方案。

在 ../config/packages 中有文件 doctrine.yaml

我将文件复制到./config/packages/test 文件夹中,并按以下方式编辑:

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'

所以我改变的是url:mysql...

在我的参数中,我添加了参数 db_user_test 等,并使用它们连接到测试数据库。

有效,但我仍然不确定是否应该这样做。