PHPUnit: Exception: Serialization of ‘Closure’ is not allowed

用phpstorm测试laravel程序的时候发现单独执行phpunit命令可以把所有测试用例都跑一遍,而单独指定某个测试文件,某个测试方法则报错。百思不得其解。今天终于下决心查一下。

基本上是因为你的全局变量中, 包含了闭包. PHPUnit 在执行过程中会对全局变量进行序列化备份, 而PHP 自身的机制限制了序列化对闭包(Closure)的操作. 因此只要禁用序列化全局变量好了.

执行的时候加上phpunit --no-globals-backup xxx参数

或者在phpunit.xml配置文件里加上backupGlobals="false"

引用:
https://xwsoul.com/posts/1137
https://github.com/sebastianbergmann/phpunit/issues/451
https://github.com/kenjis/ci-phpunit-test/issues/90
https://stackoverflow.com/questions/33477923/phpunit-tests-from-code-gives-exception-serialization-of-closure-is-not-allow

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.