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 applica开发者_运维问答tion 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
PHPUnit backups globals. If PDO is somewhere in $GLOBALS or inside an object that is in $GLOBALS you get this problem.
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);
Its been sometime, but I think I fixed the problem by removing usage of Zend_Registry
from bootstrap.php
http://pastebin.com/BS79xviM
精彩评论