Why are my tests not running against MySQL?
I am working on a Symfony 2 project that runs tests with PHPUnit 4.6.2 against a test database. This test database is configured in configuration 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 save test_sqlite
as a value for doctrine.dbal.default_connection
in config_test.yml the tests are working fine. However, when I change this value to test_mysql
, I get the following error on each 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 that I can run tests successfully on a MySQL database?
I am guessing this is not a symptom of a missing MySQL driver, as I can for example. run symfony commands in command line, just fine with production db.
source to share
There is a typo in config_test.yml. The database name is specified as a parameter name
, but it should be dbname
.
When the parameter is supplied, name
it specifies the name of the connection in the YAML file. So the connection was named doctrine.dbal.myproject_test_connection
and could not be found when Symfony Dependency Injection searched 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
source to share