phpunit: Serialization of 'Closure' is not allowed exception when run tests in process isolation mode
| Q | A |
|---|---|
| PHPUnit version | 5.7.21 |
| PHP version | 7.0.20 |
| Installation Method | Composer |
I discovered, that phpunit crashes with closure serialisation exception when run a test, that uses a data provider, which returns an object of \Closure class, as one of elements in a dataset.
PHP Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in /tmp/wtfunit/vendor/phpunit/phpunit/src/Framework/TestCase.php:814
The following test is green when running in process isolation mode turned off, but causes crash in process isolation mode turned on.
Code to reproduce error:
<?php
namespace Tests\Unit;
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* Test, that uses a data provider.
*
* @dataProvider foobarDataProvider
* @return void
*/
public function testFoobar($callback, $expected)
{
$actual = call_user_func($callback);
$this->assertEquals($expected, $actual);
}
/**
* Data provider, which returns an object of \Closure class, as one of
* elements in a dataset.
*
* @return array
*/
public function foobarDataProvider()
{
return [[
function () { return 'foo'; },
'foo',
], [
function () { return 'bar'; },
'bar',
]
];
}
}
Output:
vinter@vinter-pc /tmp/wtfunit $ ./vendor/bin/phpunit tests/Unit/FooTest.php --process-isolation
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
PHP Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in /tmp/wtfunit/vendor/phpunit/phpunit/src/Framework/TestCase.php:814
Stack trace:
#0 /tmp/wtfunit/vendor/phpunit/phpunit/src/Framework/TestCase.php(814): serialize(Array)
#1 /tmp/wtfunit/vendor/phpunit/phpunit/src/Framework/TestSuite.php(722): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#2 /tmp/wtfunit/vendor/phpunit/phpunit/src/Framework/TestSuite.php(722): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#3 /tmp/wtfunit/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(517): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#4 /tmp/wtfunit/vendor/phpunit/phpunit/src/TextUI/Command.php(186): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array, true)
#5 /tmp/wtfunit/vendor/phpunit/phpunit/src/TextUI/Command.php(116): PHPUnit_TextUI_Command->run(Array, true)
#6 /tmp/wtfunit/vendor/phpunit/phpunit/phpunit(52): PHPUnit_TextUI_Command::main()
#7 {main}
thrown in /tmp/wtfunit/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 814
May be related to:
- https://github.com/sebastianbergmann/phpunit/issues/282
- https://github.com/sebastianbergmann/phpunit/issues/223
- https://github.com/sebastianbergmann/phpunit/issues/1515
- (and more…)
Is this an expected behavior?
Are objects of a \Closure class allowed to be used as a dataset element in data providers? By now docs don’t cover this details.
Hope this issue will help to fix it! 😉
– Regards, Vinter Skogen
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 2
- Comments: 15 (4 by maintainers)
Commits related to this issue
- Remove anonymous function Since PHP cannot serialize anonymous functions. @see https://core.trac.wordpress.org/ticket/50482 @see https://github.com/sebastianbergmann/phpunit/issues/2739 — committed to yukihiko-shinoda/staticpress2019 by yukihiko-shinoda 6 years ago
- MDL-77935 contentbank: avoid phpunit crashes during isolated tests. Simplify the test for allowed contexts by removing problematic use of data provider annotation. See: https://github.com/sebastianb... — committed to paulholden/moodle by paulholden 4 years ago
- MDL-77935 contentbank: avoid phpunit crashes during isolated tests. Simplify the test for allowed contexts by removing problematic use of data provider annotation. See: https://github.com/sebastianb... — committed to paulholden/moodle by paulholden 4 years ago
- MDL-77935 contentbank: avoid phpunit crashes during isolated tests. Simplify the test for allowed contexts by removing problematic use of data provider annotation. See: https://github.com/sebastianb... — committed to paulholden/moodle by paulholden 4 years ago
- Squashed commit of the following: commit c36636264fe8a1a1e874c675f29d221b0809d6ae Author: Ilya Tregubov <ilya@moodle.com> Date: Thu Apr 27 19:28:42 2023 +0800 weekly release 4.0.8+ commit 6ba... — committed to Rosedean-Group/moodle by danchamp 4 years ago
- Squashed commit of the following: commit 4901ffe1ba570e14a46501050d7e4f36cdb9b78d Author: Ilya Tregubov <ilya@moodle.com> Date: Thu Apr 27 19:28:38 2023 +0800 weekly release 4.1.3+ commit 013... — committed to Rosedean-Group/moodle by danchamp 4 years ago
- Squashed commit of the following: commit 4901ffe1ba570e14a46501050d7e4f36cdb9b78d Author: Ilya Tregubov <ilya@moodle.com> Date: Thu Apr 27 19:28:38 2023 +0800 weekly release 4.1.3+ commit 013... — committed to Rosedean-Group/moodle by danchamp 4 years ago
- Squashed commit of the following: commit 4901ffe1ba570e14a46501050d7e4f36cdb9b78d Author: Ilya Tregubov <ilya@moodle.com> Date: Thu Apr 27 19:28:38 2023 +0800 weekly release 4.1.3+ commit 013... — committed to Rosedean-Group/moodle by danchamp 4 years ago
Agree, it’s an annoyance. But the crash on a dummy test is caused by internal logic of framework, so, nah, it is a bug in fact, in my opinion.
When introducing a bug of this kind in the bootstrap file, all tests fail with the same message. It would be nice to have some kind of back trace.
I’m still figuring out why phpunit fails on perform an external library’s method but my web browser doesn’t show any error of any kind (even after doing
error_reporting(E_ALL);ini_set('display_errors', 1);).Having a back trace would make this easier. I don’t know if this is possible or not, but if it is, then I would propose it as a feature request.
edit: Just in case it’s not clear, instead of:
Something like…
Hello World!
For anyone having the problem because using dataprovider like this :
you must convert the above code like this rather :
To implement
convert_closure_...function, you can use https://opis.io/closure/3.x/serialize.html#serialize-closures.@AeonFr You said the magic word! A backtrace surely would be handy, I will do a quick investigation and report back here. I have been adding new code location hints recently, let’s do some more.