-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialization of 'Closure' is not allowed #451
Comments
A few people seem to be having this issue including me. http://stackoverflow.com/questions/4366592/symfony-2-doctrine-2-phpunit-3-5-serialization-of-closure-exception |
Another case I'm getting it /* test.php */ class closureTest extends PHPUnit_Framework_TestCase { function testClosure() { $this->assertEquals(1, 1); call_user_func(function($a) { throw new \Exception("test"); }, 10); $this->assertEquals(2, 2); } } and then run it in process isolation phpunit --process-isolation test.php you would get There was 1 error: 1) phTest::testClosure PHPUnit_Framework_Exception: PHP Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed' in -:37 Stack trace: #0 -(37): serialize(Array) #1 -(123): __phpunit_run_isolated_test() #2 {main} thrown in - on line 37 FAILURES! Tests: 1, Assertions: 0, Errors: 1. |
I was having the same issue when using process isolation, to work around it I added __sleep to my testCase and cleaned up the closures in there. For me I had stored some mocks in properties of the testcase so it was my own fault really, I hope this helps someone. |
I am having the same issue as well, and can replicate the issue @tmilos mentioned earlier. I don't know if this ticket is dead, but it would be nice if we could resolve this. I'm more than willing to put in a pull request if that's preferable. |
Pull requests are always appreciated! |
I know it's been a bit, but I was off-project for a while. Further to the comment by @tmilos, I can replicate the problem if I force a syntax error, for example:
Note the lack of a provided argument at the end of
My question then, as this is my first time patching PHPUnit, is short of stripping the closure or modifying it to be otherwise benign - which would involve the use of Reflection no doubt - is there some PHPUnit-best-practice way to deal with this? Any thoughts or input at least? |
Also, I wrote a test under Regressions/GitHub. Is this acceptable? |
@kunjalpopat, in reference to your issue, The first thing I would check is for exceptions. When in process isolation, thrown exceptions may give you a "Serialization of Closure" message. Try wrapping your test code in a try/catch and see if that fixes the problem, if it does, then you may want to look at trimming down the coverage of the try/catch until you find the problem area. |
Problem lies in if ($key != 'GLOBALS' &&
!in_array($key, $superGlobalArrays) &&
!in_array($key, $blacklist) &&
!$GLOBALS[$key] instanceof Closure) { // <-- this is the problem
self::$globals['GLOBALS'][$key] = serialize($GLOBALS[$key]);
} I've made up some helper in public static function checkIfThereIsClosureInIt($arr) {
if ($arr instanceof Closure)
return true;
if (is_object($arr))
$arr = get_object_vars($arr);
if (is_array($arr))
foreach ($arr as $x)
if (PHPUnit_Util_GlobalState::checkIfThereIsClosureInIt($x))
return true;
return false;
} and changed a little foreach (array_keys($GLOBALS) as $key) {
if ($key != 'GLOBALS' &&
!in_array($key, $superGlobalArrays) &&
!in_array($key, $blacklist) &&
!PHPUnit_Util_GlobalState::checkIfThereIsClosureInIt($GLOBALS[$key])
// !$GLOBALS[$key] instanceof Closure
) {
self::$globals['GLOBALS'][$key] = serialize($GLOBALS[$key]);
}
} this version seems to be working (I don't catch that exception any more). |
The above code fix from stafr fixed the PHPunit issues I was having with the "Serialization of Closure" exceptions. Many thanks! |
I have this same error message whenever I instantiate a |
Unclear why PHPUnit is trying to serialize() data through its mocking system, but this simple workaround avoids it. https://phpunit.de/manual/4.8/en/fixtures.html#fixtures.global-state sebastianbergmann/phpunit#451 http://stackoverflow.com/questions/4366592/symfony-2-doctrine-2-phpunit-3-5-serialization-of-closure-exception
Just adding my experience. Basically whenever you put anonymous function or class as test class attribute, phpunit will throw |
I wrote the patch: mpyw/phpunit-patch-serializable-comparison |
Although
$GLOBAL
Closures are no longer serialised since 01aa347 & #352 a$GLOBAL
array which contain a Closure as a value will still cause the "Serialization of 'Closure' is not allowed" error.The text was updated successfully, but these errors were encountered: