PHPUnit Exception : Serialization of closure is not allowed

來源:互聯網
上載者:User
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,翻譯為全域回溯?

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.