PHPUnit测试和Doctrine,连接太多

时间:2022-10-15 19:55:58

I'm facing problems with "too many connections" for PHPUnit tests for ZF3 and Doctrine, because I'm executing ~200 tests per PHPUnit execution. I've already found some questions and answers on stack overflow but non of these work.

对于ZF3和Doctrine的PHPUnit测试,我面临着“连接太多”的问题,因为我每次执行PHPUnit时都会执行~200次测试。我已经发现了一些关于堆栈溢出的问题和答案,但没有这些工作。

My setup: ZF2/ZF3, Doctrine 2 and PHPUnit.

我的设置:ZF2 / ZF3,Doctrine 2和PHPUnit。

I have a base test class for all tests and the setUp and tearDown function look like this:

我有一个基础测试类用于所有测试,setUp和tearDown函数如下所示:

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'm monitoring my local MySQL server with innotop and the number of connections is increasing.

我用innotop监视我的本地MySQL服务器,并且连接数量正在增加。

Do you have any ideas what I'm missing?

你有任何我缺少的想法吗?

Thank you, Alexander

谢谢你,亚历山大

Update 14.02.2017: I've changed functions to use static::$em and added Bootstrap::loadAllFixtures method.

更新14.02.2017:我已经更改了使用static :: $ em的函数并添加了Bootstrap :: loadAllFixtures方法。

If I add static::$em->close() to tearDown method, all following test fail with message like "EntityManager already closed". echo "init em"; is only call once and shown for the first test. Is there a possibility to check if my Application opens connections without closing them? My test cases are based on AbstractHttpControllerTestCase

如果我将static :: $ em-> close()添加到tearDown方法,则所有后续测试都会失败,并显示“EntityManager已关闭”等消息。 echo“init em”;只调用一次并显示第一次测试。是否有可能检查我的应用程序是否打开连接而不关闭它们?我的测试用例基于AbstractHttpControllerTestCase

1 个解决方案

#1


0  

Your tearDown method looks like it should do the trick. I just do this and have never experienced this issue

你的tearDown方法看起来应该可以解决问题。我这样做,从未遇到过这个问题

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

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

What does Bootstrap::loadAllFixtures method do? Is there any db connection in there that might be being overlooked?

Bootstrap :: loadAllFixtures方法有什么作用?那里有任何数据库连接可能被忽略了吗?

#1


0  

Your tearDown method looks like it should do the trick. I just do this and have never experienced this issue

你的tearDown方法看起来应该可以解决问题。我这样做,从未遇到过这个问题

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

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

What does Bootstrap::loadAllFixtures method do? Is there any db connection in there that might be being overlooked?

Bootstrap :: loadAllFixtures方法有什么作用?那里有任何数据库连接可能被忽略了吗?