ZF + Doctrine2 phpUnit error: PDOExeption: You can

2019-05-07 05:11发布

问题:

I'm using DynamicGuys doctrine2 integration into zend framework(https://github.com/dynamicguy/zf1doctrine2). It works, but if i want to make tests with phpUnit i get this error: PDOExeption: You cannot serialize or unserialize PDO instances

I've searched a bit, and i found out that if i comment out line 44 in this file: https://github.com/dynamicguy/zf1doctrine2/blob/master/library/ZendX/Doctrine2/Application/Resource/Entitymanagerfactory.php phpUnit works, but of course the rest of the application wont work, as the entity manager wont be returned

Any ideas on where the error comes from?

回答1:

This has something to do with PHPUnit backuping globals and static attributes between each tests. If you have a PDO instance it will break up when trying to serialize. I ran into a similar issue and I could not find where the PDO instance was stored as a global parameter, but disabled the backupGlobals and backupStaticAttributes in the needed test did the trick for me.

/**
 * Search test.
 *
 * @backupGlobals disabled
 * @backupStaticAttributes disabled
 *
 * @author Steven Rosato
 */
class SearchControllerTest extends \Majisti\Test\TestCase
{
    ...
}

source: http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html