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

时间:2015-04-17 08:00:31

标签: mysql symfony doctrine-orm phpunit

我正在开发一个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

只要我将test_sqlite作为doctrine.dbal.default_connection的值保存在config_test.yml中,测试运行正常。但是,当我将此值更改为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驱动程序缺失的标志,因为我可以在命令行上运行Symfony命令就可以很好地对生产数据库运行。

1 个答案:

答案 0 :(得分:1)

config_test.yml中有拼写错误。数据库名称作为参数name给出,但必须为dbname

提供参数name时,它在YAML文件中指定连接的名称。因此,当Symfony的依赖注入正在寻找配置文件中指定的doctrine.dbal.myproject_test_connection时,连接被命名为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