dmaicher / doctrine-test-bundle

Symfony bundle to isolate your app's doctrine database tests and improve the test performance
MIT License
1.08k stars 61 forks source link

Using with multiple connections #103

Closed garak closed 4 years ago

garak commented 4 years ago

I use this bundle and it always worked seamlessly with default configuration (e.g. with a single orm/dbal config). Now I'm trying to add a second connection to a project, that it's working in dev env. But all my tests are errored, with following:

Doctrine\DBAL\Exception\ConnectionException: An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory

This is a stack trace:

/myprj/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:103
/myprj/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:145
/myprj/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php:47
/myprj/vendor/dama/doctrine-test-bundle/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php:50
/myprj/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:360
/myprj/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:1185
/myprj/vendor/dama/doctrine-test-bundle/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactory.php:56
/myprj/var/cache/test/ContainerSskoby3/getDoctrine_Dbal_MssqlConnectionService.php:26
/myprj/var/cache/test/ContainerSskoby3/AppTestDebugProjectContainer.php:1084
/myprj/var/cache/test/ContainerSskoby3/getDoctrine_Orm_MssqlEntityManagerService.php:40
/myprj/var/cache/test/ContainerSskoby3/AppTestDebugProjectContainer.php:1084
/myprj/vendor/symfony/dependency-injection/Container.php:304
/myprj/vendor/symfony/doctrine-bridge/ManagerRegistry.php:48
/myprj/vendor/doctrine/common/lib/Doctrine/Common/Persistence/AbstractManagerRegistry.php:231
/myprj/vendor/symfony/doctrine-bridge/CacheWarmer/ProxyCacheWarmer.php:49
/myprj/vendor/symfony/http-kernel/CacheWarmer/CacheWarmerAggregate.php:52
/myprj/vendor/symfony/http-kernel/Kernel.php:686
/myprj/vendor/symfony/http-kernel/Kernel.php:135
/myprj/vendor/beelab/test-bundle/Test/WebTestCase.php:57
dmaicher commented 4 years ago

Hmm weird error :confused:

I'm actually using the bundle on a project with 2 connections without any issues.

Can you share your dbal config?

garak commented 4 years ago

Hey, thanks for the prompt response!

Here it is (with some amendments):

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                url: '%env(resolve:APP_DATABASE_URL)%'
                server_version: 5.7
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
            mssql:
                url: '%env(resolve:APP_DBMSSQL_URL)%'
                server_version: 14.0.1000

    orm:
        default_entity_manager: default
        auto_generate_proxy_classes: '%kernel.debug%'
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore
                auto_mapping: false
                mappings:
                   # something probably useless in this context...
            mssql:
                connection: mssql
dmaicher commented 4 years ago

No idea yet how that error can be related to the bundle.

From what I read SQLSTATE[HY000] [2002] No such file or directory occurs whenever the hostname/port is wrong and the connection cannot be established.

Are you sure in test env the connection url is correct?

Can you try disabling the bundle to see what happens then?

garak commented 4 years ago

If I disable the bundle, error disappears

dmaicher commented 4 years ago

And this scenario also works?

garak commented 4 years ago

Well, that mssql connection is intended for production use only, indeed I don't get any MsSQL server to connect in dev/test environments. The weird part is that error looks related to MySQL connection. I tried to comment out mssql config from doctrine and it works. As soon as I re-add it, error.

dmaicher commented 4 years ago

Ok I see. I have an idea: we would need to make it configurable per connection if we want the transactional behavior or not.

Currently as soon as this is true https://github.com/dmaicher/doctrine-test-bundle/blob/master/tests/Functional/config.yml#L17

we will enable it for all connections.

So additionally it could be:


dama_doctrine_test:
    enable_static_connection:
        default: true
        mssql: false
garak commented 4 years ago

For now, I'm using enable_static_connection: false and it works

dmaicher commented 4 years ago

@garak can you test this patch?

https://github.com/dmaicher/doctrine-test-bundle/compare/5.x...issue-103?expand=1

it should work with a config like


dama_doctrine_test:
    enable_static_connection:
        default: true
garak commented 4 years ago

Unfortunately I'm currently on a php 7.0 project. I'll try to reproduce the problem on a different project and see if patch works

garak commented 4 years ago

I'm sorry but I'm not able to reproduce this problem on a different project 😔