Symfony 2 + Doctrine 2 + PHPUnit 3.5: Serialization of closure exception Symfony 2 + Doctrine 2 + PHPUnit 3.5: Serialization of closure exception symfony symfony

Symfony 2 + Doctrine 2 + PHPUnit 3.5: Serialization of closure exception


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');    // ...}


You can also try.

<phpunit backupGlobals="false">    <testsuites>        <testsuite name="Test">            <directory>.</directory>        </testsuite>    </testsuites></phpunit>