Unit testing symfony4:如何配置dataFixture以写入测试数据库
我想通过DataFixtures将单元测试的testdata加载到测试数据库中。 文档中说,如果我设置了环境变量,那么应该使用test db:Unit testing symfony4:如何配置dataFixture以写入测试数据库,unit-testing,doctrine,symfony4,Unit Testing,Doctrine,Symfony4,我想通过DataFixtures将单元测试的testdata加载到测试数据库中。 文档中说,如果我设置了环境变量,那么应该使用test db: $ 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\Prope
$ 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配置测试数据库
我在哪里配置它,以便Datafixture知道在哪里写入
对于我的功能测试,我在phpunit.xml中配置了db设置,我们解决它的方法如下。我不知道这是不是一个好办法,所以我们仍然欢迎其他解决方案 在../config/packages中有文件doctor.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等,并使用它们连接到测试db
这是可行的,但我仍然不确定这是否是应该做到的