Doctrine 的实体管理器崩溃并停止运行

发布于 2024-11-23 15:09:39 字数 755 浏览 1 评论 0原文

因此,当我在 ZF/Doctrine 应用程序上运行测试时,某些测试碰巧破坏了 Doctrine 实体管理器,并且所有顺序测试由于 EM 关闭而失败。

我在 test/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,然后将其设置到服务中,但没有成功。

有没有一种简单的方法可以使用一些教义方法来重新打开 EM?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时常饿 2024-11-30 15:09:39

不,据我所知,至少没有。解决这个问题的最简单方法是简单地(重新)引导您的应用程序在每个测试的设置阶段运行。因此,每个测试都会获得一个新的 $application 实例和一个全新的 $em 实例。这就是快速解决办法。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文