开发者

Doctrine's entity manager crashes and stays down

开发者 https://www.devze.com 2023-03-19 17:00 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消