WordPress.org

Make WordPress Core

Opened 6 weeks ago

Last modified 6 weeks ago

#50482 reopened defect (bug)

Uncaught Exception: Serialization of 'Closure' is not allowed when run PHPUnit in plugin development

Reported by: yshinoda Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: trunk
Component: Build/Test Tools Keywords:
Focuses: Cc:

Description

Following error message is presented when I run PHPUnit in plugin development with latest trunk of WordPress:

root@bdc07b68467e:/plugin# phpunit
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

..
Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in /root/.composer/vendor/phpunit/phpunit/src/Util/GlobalState.php:187
Stack trace:
#0 /root/.composer/vendor/phpunit/phpunit/src/Util/GlobalState.php(187): serialize(Array)
#1 /root/.composer/vendor/phpunit/phpunit/src/Util/GlobalState.php(156): PHPUnit_Util_GlobalState::exportVariable(Array)
#2 /root/.composer/vendor/phpunit/phpunit/src/Framework/TestCase.php(783): PHPUnit_Util_GlobalState::getGlobalsAsString()
#3 /root/.composer/vendor/phpunit/phpunit/src/Framework/TestSuite.php(733): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#4 /root/.composer/vendor/phpunit/phpunit/src/Framework/TestSuite.php(733): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#5 /root/.composer/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(517): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#6 /root/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php(186): PHPUnit_TextUI_TestRunner->doRun(Obj in /root/.composer/vendor/phpunit/phpunit/src/Util/GlobalState.php on line 187

Last succeed in CI
2020-06-17 17:17
https://travis-ci.org/github/yukihiko-shinoda/staticpress2019/builds/699221099

First failed in CI
2020-06-26 22:01
https://travis-ci.org/github/yukihiko-shinoda/staticpress2019/builds/702342495

I'm using @runInSeparateProcess annotation to test function including die().
When I comment out all tests using @runInSeparateProcess,
PHPUnit worked well.

I'm perfectly following standard steps for PHPUnit:
https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests/

Travis CI is standard environment to run PHPUnit for WordPress plugin.

If you want to build environment to reproduce,
This Docker Image may be your help:

https://github.com/yukihiko-shinoda/dockerfile-phpunit-wordpress-plugin

docker build --build-arg PHP_VERSION=7.3 --build-arg WORDPRESS_VERSION=trunk .

VSCode integration also available:

https://github.com/yukihiko-shinoda/docker-compose-phpunit-wordpress-plugin

Change History (5)

#1 @swissspidy
6 weeks ago

  • Component changed from General to Build/Test Tools
  • Keywords close added

In your tests that use @runInSeparateProcess, are you using anonymous functions (closures) anywhere?

Because PHP cannot serialize anonmyous functions. The @runInSeparateProcess feature requires serialization, for instance of the data from a data provider.

So this error message is expected behavior, and is not an issue with the WordPress test suite, but just how PHPUnit works.

See https://github.com/sebastianbergmann/phpunit/issues/2739

#2 @SergeyBiryukov
6 weeks ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hi there welcome to WordPress Trac! Thanks for the report.

Closing per comment:1, as this appears to be expected PHPUnit behavior, not specific to WordPress.

#3 @yshinoda
6 weeks ago

Hi,
I had not understood mechanism why error message is presented,
Thanks to you for explaining so I got understand it.

After remove anonymous function from test code, PHPUnit worked fine.

By the way, I report here again since I'm curious that only test with trunk of WordPress failed:
https://travis-ci.org/github/yukihiko-shinoda/staticpress2019/builds/702342495

#4 @yshinoda
6 weeks ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

I mistaked how to check PHPUnit result in last post,
I got same error even though I removed all of anonymous function that was included in my code.

This reproduces only when test with trunk version of WordPress.
https://travis-ci.org/github/yukihiko-shinoda/staticpress2019/builds/702971274

Using dependencies is perfectly same.
Fail:
https://travis-ci.org/github/yukihiko-shinoda/staticpress2019/jobs/702971277
Success:
https://travis-ci.org/github/yukihiko-shinoda/staticpress2019/jobs/702971276

#5 @SergeyBiryukov
6 weeks ago

  • Milestone set to Awaiting Review
Note: See TracTickets for help on using tickets.