PHPUnit/Zend_Test:PDOException:您无法序列化或反序列化 PDO 实例

发布于 2024-10-08 06:54:51 字数 1131 浏览 0 评论 0 原文

我收到异常

PDOException:您无法序列化或反序列化 PDO 实例

PDOException:当我尝试使用 PHPUnit 进行单元测试时, 。我没有太多事情发生。我正在使用 Zend Framework 1.11。我想我可能已经引导我的应用程序将实体管理器存储在 Zend_Registry 中?

// application/Bootstrap.php -> _initDoctrine()
$em = EntityManager::create($doctrineOptions['connectionOptions'], $config);
Zend_Registry::set("em", $em);

对于我的单元测试,它看起来像

class Application_Models_UserTest extends Zend_Test_PHPUnit_ControllerTestCase
  public function testUnitTest() {
    $this->assertTrue(true);
  }
}

我的 phpunit.xml 看起来像 http://pastebin.com/BCv2Ci8R,我认为主要关注的区域是第 1 行,因此 bootstrap.php 看起来像 http://pastebin.com/hVZhJAG1

更新

有该行时,问题就开始了

$schemaTool->dropSchema($classes);
$schemaTool->updateSchema($classes);

我发现当我在 bootstrap.php http://pastebin 中 .com/hVZhJAG1

I am getting the exception

PDOException: You cannot serialize or unserialize PDO instances

when I am trying to use PHPUnit for Unit Tests. I have not much going on. I am using Zend Framework 1.11. I guess it maybe that I have Bootstrapped my application storing the entity manager in Zend_Registry?

// application/Bootstrap.php -> _initDoctrine()
$em = EntityManager::create($doctrineOptions['connectionOptions'], $config);
Zend_Registry::set("em", $em);

For my unit test, it looks like

class Application_Models_UserTest extends Zend_Test_PHPUnit_ControllerTestCase
  public function testUnitTest() {
    $this->assertTrue(true);
  }
}

My phpunit.xml looks like http://pastebin.com/BCv2Ci8R, I think the main area of concern is line 1, So bootstrap.php looks like http://pastebin.com/hVZhJAG1

UPDATE

I have found that the problem starts when I have the line

$schemaTool->dropSchema($classes);
$schemaTool->updateSchema($classes);

in bootstrap.php http://pastebin.com/hVZhJAG1

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

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

发布评论

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

评论(3

随波逐流 2024-10-15 06:54:51

PHPUnit 备份全局变量。如果 PDO 位于 $GLOBALS 中的某个位置或位于 $GLOBALS 中的对象内部,则会出现此问题。

PHPUnit backups globals. If PDO is somewhere in $GLOBALS or inside an object that is in $GLOBALS you get this problem.

微暖i 2024-10-15 06:54:51

我之前发现过这个问题,在网上搜索后,我从 http://www.phpunit.de/ 得到了一个解决方案票/376。只需在 PHPUnit/Frameword/TestCase.php 中的 protected $backupGlobals = TRUE; 处将 backupGlobals 设置为 false 即可。

但 PHPUnit 开发团队并不提倡这样做:大多数 PHPUnit 用户希望它能够像启用 $GLOBALS 备份功能时那样工作。这就是默认情况下启用它的原因。

如果您的测试练习代码将不可序列化的对象放入 $GLOBALS 中,您可以禁用该功能。从软件设计的角度来看,您一开始就不应该有 PDO 的全局实例。

所以,我使用这个得到了完美的解决方案:

$db = SmartPHP_Db::factory($dbConfig);
SmartPHP_Pool::set("db" , $db);
SmartPHP_Db_Table::setDefaultAdapter($db);

在单元测试代码之后:

unset($db);

I found this problem before, after searching the web I got one solution from http://www.phpunit.de/ticket/376. Just set backupGlobals to false at protected $backupGlobals = TRUE; in PHPUnit/Frameword/TestCase.php.

But PHPUnit developer team don't advocate that: the majority of users of PHPUnit expects it to work as it does when the backup of $GLOBALS feature is enabled. This is why it is enabled by default.

If your tests exercise code that puts unserializable objects into $GLOBALS you can disable the feature. From a software design perspective, you should not have a global instance of PDO to begin with.

So, I got perfect solution using this:

$db = SmartPHP_Db::factory($dbConfig);
SmartPHP_Pool::set("db" , $db);
SmartPHP_Db_Table::setDefaultAdapter($db);

after unit testing code:

unset($db);
烟柳画桥 2024-10-15 06:54:51

Zend_Registry 的使用来解决了这个问题

已经有一段时间了,但我想我通过从 bootstrap.php ="nofollow">http://pastebin.com/BS79xviM

Its been sometime, but I think I fixed the problem by removing usage of Zend_Registry from bootstrap.php

http://pastebin.com/BS79xviM

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