教义的实体经理崩溃并停留下来 [英] Doctrine's entity manager crashes and stays down

查看:83
本文介绍了教义的实体经理崩溃并停留下来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,当我在ZF / Doctrine应用程序上运行测试时,会发生一些测试,打破了Doctrine实体管理器,并且由于EM被关闭,所有的顺序测试失败。



在我的测试/ bootstrap.php中设置EM:

  $ application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH。'/configs/application.ini'
);
$ application-> bootstrap();
(...)
$ bootstrap = $ application-> getBootstrap();
$ em = $ bootstrap-> getResource('doctrinemanager');

然后我将其设置在测试setUp()函数内($ this - > _ service是服务测试):

  $ em = App :: getEntityManager(); 
$ this-> _em = clone $ em;
$ this-> _service-> setEm($ this-> _em);

然后,当我运行一个导致EM抛出异常并关闭的测试(这是正确的对于我的行为),它在所有的测试中保持关闭,当然由于EM被关闭,测试失败了。这不是我期待测试的行为,你可以猜到。



我尝试克隆EM,然后再将其设置在服务中,但没有起作用。 / p>

有没有一个简单的方法来重新打开EM可能使用一些Doctrine方法?

解决方案 $应用程序实例和一个新的,新的 $ em 。这是快速修复。



正确的解决方案可能是将您的测试与您的 Zend_Application 分离。允许您使用简单的实体管理器运行测试,可能使用模拟连接或连接到内存中的SQLite数据库。在测试设置阶段只创建这个实体管理器,所以每个测试都会获得一个新的实体管理器。这类似于上面的快速修复,除了现在您只专门创建一个实体管理器进行测试,而不是为每次测试引导整个应用程序。它更精简和简单。


So, when I run tests on my ZF/Doctrine application, some tests happen to break the Doctrine Entity Manager, and all the sequential tests fail due to EM being closed.

I set the EM up in my tests/bootstrap.php:

$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
(...)
$bootstrap = $application->getBootstrap();
$em = $bootstrap->getResource('doctrinemanager');

Then I set it inside the test setUp() function ($this->_service is the service being tested):

$em = App::getEntityManager();
$this->_em = clone $em;
$this->_service->setEm($this->_em);

And then when I run a test that causes EM to throw an exception and close (and that's the correct behaviour for me), it stays closed throughout all the tests which of course fail due to EM being closed. That's just not the behaviour I expect for tests, as you can guess.

I tried cloning the EM before setting it in the service, but it didn't work.

Is there an easy way to reopen the EM maybe using some Doctrine methods?

解决方案

No, not that I know of anyway. The simplest way around this would be to simply (re-)bootstrap you application to run in the setup phase of every test. So, every test gets a new $application instance and a fresh, new $em along with it. That's the quick fix.

The proper solution probably is to decouple your tests from your Zend_Application. Allow your tests to run with a simple entity manager, possibly using a mock connection or a connection to an in-memory SQLite database. Create only this entity manager in your test setup phase, so every test gets a new entity manager. This is similar to the quick fix above, except now you only specifically create an entity manager for testing instead of bootstrapping your entire application for every test. It's leaner and simpler.

这篇关于教义的实体经理崩溃并停留下来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆