unit-testing - 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
解决方案
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.
推荐阅读
- spring-boot - 在 Spring Boot 实体类中实现可序列化
- python-3.x - sklearn 的线性回归给出了不正确的结果,但不知何故具有很高的准确性
- python - TXT 文件上的 Python 项目,如何读取字数和排序
- sql - 识别购买模式 SQL
- rxjs - 为什么 `of(undefined) 会发出,但 `of()` 不会?
- c - 使用 fscanf 填充二维数组时的语法
- python - 如何在python中使用requests和beautifulsoup从图像中获取src属性
- powerbi - 从 C# 应用 (Power BI Desktop) 填充现有 PBIX 文件中的数据
- php - 使用消息“object(PDO)#1 (0)”连接到 Mysql 数据库时出错
- node.js - 如何使用带有身份验证的 postman nodesj 记录 GET 请求?