PHPUnit and Doctrine tests, too many connections

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

+6
2

tearDown , . .

protected function tearDown()
{
    parent::tearDown();

    $this->em->close();
    $this->em = null; 
}

Bootstrap:: loadAllFixtures? - db, ?

0

. PHPUnit, :

final public function getConnection()
{
    if ($this->conn === null) {
        if (self::$pdo == null) {

            //We get the EM from dependency injection container
            $container = $this->getContainer();
            self::$pdo = $container->get('Doctrine.EntityManager')->getConnection()->getWrappedConnection();
        }
        $this->conn = $this->createDefaultDBConnection(self::$pdo, 'spark_api_docker');
    }

    return $this->conn;
}

self:$pdo , "threads_connected", , show status like '%onn%'; , .

:


1)

public function tearDown()
{
    parent::tearDown();

    //You'll probably need to get hold of our entity manager another way
    $this->getContainer()->get('Doctrine.EntityManager')->getConnection()->close();
}

self::$pdo, self::$pdo null. , , .

, . , , , (.. PHPUnit ). , .


2) PHP

. , . phpunit.xml ':

<?xml version="1.0" encoding="UTF-8"?>

<phpunit
    ...
    processIsolation = "true"
    >
    ...
</phpunit>

PHPUnit, PDO , , . , .

0

Source: https://habr.com/ru/post/1669511/


All Articles