为什么我的测试不使用MySQL运行



我正在开发一个Symfony 2项目,该项目使用PHPUnit 4.6.2在测试数据库中运行测试。此测试数据库是在配置文件中设置的。以下是关于条令配置的部分:

config.yml:

doctrine:
dbal:
default_connection:       default
connections:
default:
driver:   "%database_driver%"
host:     "%database_host%"
port:     "%database_port%"
dbname:   "%database_name%"
user:     "%database_user%"
password: "%database_password%"
charset:  UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true

config_dev.yml:

imports:
- { resource: config.yml }

config_test.yml:

imports:
- { resource: config_dev.yml }
doctrine:
dbal:
default_connection:     test_sqlite
connections:
test_sqlite:
driver:   pdo_sqlite
path:     %kernel.cache_dir%/test.db
test_mysql:
driver: pdo_mysql
host: 192.168.56.2
port: null
name: myproject_test
user: root
password: mysupersafetestpassword
charset: UTF8

只要我在config_test.yml中保持test_sqlite作为doctrine.dbal.default_connection的值,测试就会运行良好。然而,当我将此值更改为test_mysql时,我在每一个测试中都会收到以下错误消息:

无法将别名"doctrine.dbal.test_mysql_connection"替换为"database_connection"。C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:48C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\Compiler\Compiler.php:117C: \projects\myproject\vendor\symfony\symfony \src\symfony\Component\DependencyInjection\ContainerBuilder.php:614C: \projects\myproject\app/bootstrap.php.cache:2565C: \projects\myproject\app/bootstrap.php.cache:2234C: \projects\myproject\app\AppKernel.php:43C: \projects\myproject\vendor\symfony\symfony \src\symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:141C: \projects\myproject\vendor\symfony\symfony \src\symfony\Bundle\FrameworkBundle\Test\WebTestCase.php:33C: \projects\myproject\src\mycompany\myprojectBundle\Tests\Controller\SomeControllerTest.php:23

这个错误意味着什么?我需要更改什么才能在MySQL数据库上成功运行测试?

我想这不是MySQL驱动程序丢失的迹象,因为我可以在命令行上运行Symfony命令,对生产数据库来说很好。

config_test.yml中有一个拼写错误。数据库名称为参数name,但需要为dbname

当提供参数name时,它会在YAML文件中指定连接的名称。因此,该连接被命名为doctrine.dbal.myproject_test_connection,当Symfony的依赖项注入正在查找配置文件中指定的doctrine.dbal.mysql_test_connection时,无法找到该连接。这导致了上面显示的错误。

因此,解决方案是将config_test.yml中的配置更改为以下内容:

test_mysql:
driver: pdo_mysql
host: 192.168.56.2
port: null
dbname: myproject_test
user: root
password: mysupersafetestpassword
charset: UTF8

相关内容

  • 没有找到相关文章

最新更新