liip / LiipFunctionalTestBundle

Some helper classes for writing functional tests in Symfony

Home Page:http://liip.ch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fails to connect into mysql database when database not created

webmake opened this issue · comments

Hello,

This library fails to connect into mysql database when database not created with error An exception occurred in driver: SQLSTATE[HY000] [1049] Unknown database 'test'

Place where error occurs:
https://github.com/liip/LiipFunctionalTestBundle/blob/2.0.0-alpha10/src/Services/DatabaseTools/ORMDatabaseTool.php#L63 or commit to be exact fails because used dbal library performs connect https://github.com/doctrine/dbal/blob/b45ed5e13e4834f64fbc56bfdc0344430cecae63/lib/Doctrine/DBAL/Connection.php#L358 into database despite unset($params['dbname']); in your class ORMDatabaseTool in 61 line

Steps to reproduce

class MyClassTest extends \Liip\FunctionalTestBundle\Test\WebTestCase
{
    protected function setUp(): void
    {
        self::bootKernel();
        $this->loadFixtures([
            LoadEntities::class,
        ]);
        parent::setUp();
    }
}

and

"doctrine/dbal": "^2.8",
"liip/functional-test-bundle": "~2.0@alpha",

As a workaround, you can create the database before running tests:

(app|bin)/console doctrine:database:create --env=test --if-not-exists

Can you try to call self::bootKernel(); after $this->loadFixtures([]);?

I can't reproduce this bug on my local environment.

Do you use driver: pdo_mysql in your configuration?

I tried to your method to switch bootKernel and loadFixtures methods, still same problem. Yes
it is driver: pdo_mysql.

It cannot work at any case, because of such stack trace (please follow):
in test $this->loadFixtures([]);
calls return $dbTool->loadFixtures($classNames, $append); line in Liip\FunctionalTestBundle\Test\WebTestCase:258,
then on $this->createDatabaseIfNotExists(); Liip\FunctionalTestBundle\Services\DatabaseTools\ORMDatabaseTool:96
then $tmpConnection->connect(); Liip\FunctionalTestBundle\Services\DatabaseTools\ORMDatabaseTool:63 here important, you unset $params['dbname']
then $this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions); Doctrine\DBAL\Connection:389 But here $this->_params
then $conn = new PDOConnection( $this->constructPdoDsn($params), $username, $password, $driverOptions ); on Doctrine\DBAL\Driver\PDOMySql\Driver:44
and in parent::__construct($dsn, $user, $password, $options); onDoctrine\DBAL\Driver\PDOConnection:46 occurs PDOException, because dsn: mysql:host=127.0.0.1;port=3306;dbname=test;charset=utf8mb4; test database not created, because you create in

        $tmpConnection->connect(); //here FAILS

        if (!in_array($dbName, $tmpConnection->getSchemaManager()->listDatabases())) {
            $tmpConnection->getSchemaManager()->createDatabase($dbName); // here
        }

I changed the database name in the configuration used on Travis CI and the tests passed: #477

It looks like a specific configuration leads to the error you have, but I'm still wondering what are these parameters.

Could you please share your Doctrine configuration?

parameters:
    env(DATABASE_URL): ''
    env(DATABASE_SETTINGS_URL): ''

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci

                url: '%env(resolve:DATABASE_URL)%'
            settings:
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci

                url: '%env(resolve:DATABASE_SETTINGS_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: true
                mappings:
                    App:
                        is_bundle: false
                        type: yml
                        dir: '%kernel.project_dir%/config/doctrine/provider'
                        prefix: 'App\Entity\Provider'
                        alias: App

                    Money:
                        is_bundle: false
                        type: yml
                        dir: '%kernel.project_dir%/config/doctrine-money'
                        prefix: 'Money'
                        alias: Money

            settings:
                connection: settings
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    Settings:
                        is_bundle: false
                        type: yml
                        dir: '%kernel.project_dir%/config/doctrine/settings'
                        prefix: 'App\Entity\Settings'
                        alias: AppSettings

As I see, your tests are such, that loadfixtures are made in test case, not in setUp, maybe this is why problem not reproducible?

Thanks for sharing your configuration.

Yes it may be due to the fact that you load fixtures in setUp, the fact that several databases are configured or the fact that you use url. This is very interesting because the internal tests of this bundle use the simplest configurations, it's probably not enough to cover complex configurations like yours.

I debugged your WebTestCaseConfigMysqlTest.php, dsn mysql:host=127.0.0.1;charset=UTF8; doesn't contain database name in PDOConnection constructor when performs connect.. That's weird..

Okay, in my case doesn't work unset as I mentioned, because parseDatabaseUrl again set dbname from url: mysql://admin:admin@127.0.0.1:3306/test

Until the bundle supports multiple databases, we can use a workaround to use only one database during tests: https://stackoverflow.com/a/53726916/2257664