为什么我的测试没有 运行 和 MySQL?

Why do my tests not run with MySQL?

我正在开发一个 Symfony 2 项目,运行s 使用 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

只要我将 test_sqlite 作为 config_test.yml 中 doctrine.dbal.default_connection 的值,测试 运行 就可以了。但是,当我将此值更改为 test_mysql 时,我的每一个测试都会收到以下错误消息:

Unable to replace alias "doctrine.dbal.test_mysql_connection" with "database_connection".
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:48
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass.php:63
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\Compiler.php:117
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\ContainerBuilder.php:614
 C:\projects\myproject\app\bootstrap.php.cache:2565
 C:\projects\myproject\app\bootstrap.php.cache:2344
 C:\projects\myproject\app\AppKernel.php:43
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:141
 C:\projects\myproject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\WebTestCase.php:33
 C:\projects\myproject\src\mycompany\myprojectBundle\Tests\Controller\SomeControllerTest.php:23

这个错误是什么意思,我需要更改什么才能成功 运行 MySQL 数据库上的测试?

我想这不是 MySQL driver 丢失的迹象,例如运行 命令行上的 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