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
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" >
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With