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:

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

Most upvoted comments

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:

PHPUnit 4.8.36 by Sebastian Bergmann and contributors.

EEEEEEEEEEEEEEEEE

Time: 637 ms, Memory: 14.00MB

There were 17 errors:

1) AuditoriaTest::testSePuedeInstanciar
Exception: Serialization of 'Closure' is not allowed

2) AuditoriaTest::testSiSeAgregaUnDuplicadoSeActualiza
Exception: Serialization of 'Closure' is not allowed

...

FAILURES!
Tests: 17, Assertions: 0, Errors: 17.

Something like…

Error in the bootstrap file C:\...\bootstrap.php:
Exception: Serialization of 'Closure' is not allowed

C:\...\bootstrap.php:17
C:\...\conection.php:25

Hello World!

For anyone having the problem because using dataprovider like this :

public function provideData(): \Generator
{
   yield 'test' => [
       'closure' => function () {}
   ]
}

you must convert the above code like this rather :

public function provideData(): \Generator
{
   $closure = function () {};
   yield 'test' => [
       'closure' => convert_closure_to_serializable_closure ($closure)
   ]
}

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.