3
votes

I am working on a Symfony 2 project that runs tests with PHPUnit 4.6.2 against a test database. This test database is set up in the config files. Here are the parts regarding the doctrine configuration:

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

As long as I keep test_sqlite as the value for doctrine.dbal.default_connection in config_test.yml, the tests run fine. However, when I change this value to test_mysql I get the following error message on every single one of my tests:

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

What does this error mean and what do I need to change so I can successfully run the tests on the MySQL database?

I guess it is not the sign of a missing MySQL driver as I can e.g. run Symfony commands on the command line just fine against the production DB.

1
FYI - you're not running unit tests if you're using your database. You're running functional / integration tests. You should mock your database calls if you are wanting to unit test.Seer
@Seer You are right, these are actually functional tests. However, they are all run by the same testing framework in a Symfony project. I have changed the wording of my post.Chris

1 Answers

1
votes

There is a typo in config_test.yml. The database name is given as parameter name but it needs to be dbname.

When supplying the parameter name, it specifies the connection's name in the YAML file. So the connection was named doctrine.dbal.myproject_test_connection and could not be found when Symfony's dependency injection was looking for doctrine.dbal.mysql_test_connection as specified in the config file. This resulted in the error shown above.

So the solution is to change the config in config_test.yml to the following:

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