PHPUnit Exception:serialization of closure is not allowed

Source: Internet
Author: User
Not technically related to your issue. However, I had a really hard time trying to solve the "serialization of ' Closure ' isn't allowed" issue while using Phpuni T, and this question is the top Google result.

The problem comes from the fact, PHPUnit serializes all the $GLOBALS in the system to essential back them the Test is running. It then restores them after the test was done.

However, if you had any closures in your GLOBAL space, it's going to cause problems. There ' s ways to solve it.

You can disable the global backup procedure totally by using a annotation.

/**

* @backupGlobals Disabled

*/

Class MyTest extends Phpunit_framework_testcase

{

// ...

}

Or, if you know which variable are causing the problem (look for a lambda in Var_dump ($GLOBALS)), can just blacklist th E problem variable (s).

Class MyTest extends Phpunit_framework_testcase

{

Protected $backupGlobalsBlacklist = Array (' Application ');

// ...

}

Project in PHPUnit unit test times wrong: "Serialization of ' Closure ' is not allowed", here because PHPUnit all the $globals are serialized, and then after the execution of the release, So if you have closures in the global domain, you will get an error saying ' The serialization of closures is not allowed ', only because of the phpunit and mechanism, and the application level is irrelevant. The workaround is to add a declaration in front of the class PHPUnit unit test, disable backupglobals, and translate to global backtracking, as above.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.