Use of static and phpunit in closure causes: Serialization of 'Closure' is not allowed

半腔热情 提交于 2019-12-12 01:25:56

问题


I am trying to instantiate and start PHPUnitTest from a closure, but I keep getting this message:

mytest::authenticate_test Exception: Serialization of 'Closure' is not allowed

It works outside a closure without any problems and the route is managed by Aura Router.

class mytest extends TestCase {
    public function authenticate_test() {   
    // ...
    }   
}

$runner = 'PHPUnit_TextUI_TestRunner';
$suite = new PHPUnit_Framework_TestSuite('PHPUnit');
$suite->addTest(new mytest("authenticate_test"));

$map->attach('api.v1.', '/api/v1', function ($map)  use($runner, $suite) {
    $map->route('tests', '/tests', function ($request, $response)  
    use($runner, $suite) {      
        $runner::run($suite);  // <-- Error comes here
    });
});

How do I proceed on this?

Please help me on this.

Thanks in advance.


回答1:


It seems that this answer holds the solution for the problem.

The problem comes from the fact that PHPUnit serializes all the $GLOBALS in the system to essential back them up while the test is running. It then restores them after the test is done. However, if you have any closures in your GLOBAL space, it's going to cause problems.

Try disabling backup of $GLOBALS via corresponding PHPDoc:

/**
 * @backupGlobals disabled
 */
class mytest extends TestCase {
    public function authenticate_test() {   
      // ...
    }   
}


来源:https://stackoverflow.com/questions/40086065/use-of-static-and-phpunit-in-closure-causes-serialization-of-closure-is-not-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!