Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO connection is not closed when phpunit is running with symfony 2.X

We have about 180 unit tests implement webtestcase class and tests are running over controllers.

However when we run the unit tests, they open too many connection to db. Because of too many active tcp connection test are fail after 120th test. All the connection are active while tests are running.

In the tearDown function we call the close function of entity manager, but there is nothing, it doesn't any affect. There is some class keeping the connection object reference, i think.

Because in php manual mentioned about pdo connection closed when the object assigned to null. We also do that but no changes. P.S: Our unit tests are functional tests. Works over controller and integrated with db, there is no mock objects

Where is our mistake ? How can we fix the problem?

This my connection parameters in config_test.yml

imports:
    - { resource: config_dev.yml }

framework:
    test: ~
    session:
        storage_id: session.storage.mock_file

web_profiler:
    toolbar: false
    intercept_redirects: false

doctrine:
    dbal:
        driver: pdo_mysql
        port: 3306
        host: localhost
        dbname: mydb
        user: myuser
        password: mypass
        charset: UTF8
like image 237
GirginSoft Avatar asked Jun 03 '13 08:06

GirginSoft


2 Answers

Did you check your phpunit.xml.dist file?

I think you should look at this; http://www.slideshare.net/fabpot/unit-and-functional-testing-with-symfony2

Be sure your parameters are the same below

<phpunit
    backupGlobals               = "false"
    backupStaticAttributes      = "false"
    colors                      = "true"
    convertErrorsToExceptions   = "true"
    convertNoticesToExceptions  = "true"
    convertWarningsToExceptions = "true"
    processIsolation            = "true"
    stopOnFailure               = "false"
    syntaxCheck                 = "false" 
    bootstrap                   = "bootstrap.php.cache" >
like image 90
bulutcagatay Avatar answered Nov 02 '22 07:11

bulutcagatay


Enabling process isolation will have the side effect of making the test suite absurdly much more slow to execute.

A better approach is to just tell Doctrine explicitly to close its connections, either on test tearDown, tearDownAfterClass or any method annotated as such, like for example:

trait CloseConnectionAfterTestTrait {
    /** @after */
    public function avoidExhaustingDbConnections()
    {
        if(!empty($this->em)){
            $this->em->getConnection()->close();
        }
    }
}

In this example, it is up to the consumer to save whatever entity manager instance he has as $this->em. But if/since you use Doctrine you could probably generalize the code better by accessing the Doctrine service through static::$kernel->something.

like image 40
conny Avatar answered Nov 02 '22 07:11

conny