Doctrine's entity manager crashes and stays down

痴心易碎 提交于 2020-01-02 04:16:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6674456/doctrines-entity-manager-crashes-and-stays-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!