I ran into problems with "too many connections" for the PHPUnit tests for ZF3 and Doctrine, because I am running 200 tests for running PHPUnit. I already found some questions and answers about stack overflow, but not about that.
My setup: ZF2 / ZF3, Doctrine 2 and PHPUnit.
I have a base test class for all tests, and the setUp and tearDown function looks like this:
public function setUp()
{
$this->setApplicationConfig(Bootstrap::getConfig());
Bootstrap::loadAllFixtures();
if (!static::$em) {
echo "init em";
static::$em = Bootstrap::getEntityManager();
}
parent::setUp();
....
}
public function tearDown()
{
parent::tearDown();
static::$em->flush();
static::$em->clear();
static::$em->getConnection()->close();
$refl = new \ReflectionObject($this);
foreach ($refl->getProperties() as $prop) {
if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) {
$prop->setAccessible(true);
$prop->setValue($this, null);
}
}
gc_collect_cycles();
}
public static function (Bootstrap::)loadAllFixtures()
{
static::$em->getConnection()->executeUpdate("SET foreign_key_checks = 0;");
$loader = new Loader();
foreach (self::$config['data-fixture'] as $fixtureDir) {
$loader->loadFromDirectory($fixtureDir);
}
$purger = new ORMPurger(static::$em);
$executor = new ORMExecutor(static::$em, $purger);
$executor->execute($loader->getFixtures());
$executor = null;
$purger = null;
static::$em->getConnection()->executeUpdate("SET foreign_key_checks = 1;");
static::$em->flush();
static::$em->clear();
}
I track my local MySQL server with innotop and the number of connections is increasing.
Do you have any idea what I am missing?
Thank you, Alexander
Update 14.02.2017: I changed the functions to use static::$emand added a method Bootstrap::loadAllFixtures.
static::$em->close() to tearDown, "EntityManager ". echo "init em"; .
, ? AbstractHttpControllerTestCase