Not technically related to your issue. However, I had a really hard time trying to solve the "Serialization of 'Closure' is not allowed" issue while using PHPUnit, and this question is the top Google result.
The problem comes from the fact that PHPUnit serializes all the $GLOBALS in the system to essential back them up while the test is running. It then restores them after the test is done.
However, if you have any closures in your GLOBAL space, it's going to cause problems. There's two ways to solve it.
You can disable the global backup procedure totally by using an annotation.
/**
* @backupGlobals disabled
*/
class MyTest extends PHPUnit_Framework_TestCase
{
// ...
}
Or, if you know which variable is causing the problem (look for a lambda in var_dump($GLOBALS)), you can just blacklist the problem variable(s).
class MyTest extends PHPUnit_Framework_TestCase
{
protected $backupGlobalsBlacklist = array('application');
// ...
}
項目在PHPUNIT單元測試時報錯:"Serialization of 'Closure' is not allowed",這裡因為PHPUNIT把所有的$GLOBALS都系列化了,然後等執行完再釋放,所以如果你的全域域裡有閉包的話就會報錯為 ‘對閉包的系列化是不允許的’,這隻是因為PHPUNIT和機製造成的,和application層面無關。解決方案就是像上面那樣,在PHPUNIT單元測試的類前面加一個聲明,禁用backupGlobals,翻譯為全域回溯?