在闭包中使用static和phpunit的原因:不允许对'Closure'进行序列化 [英] Use of static and phpunit in closure causes: Serialization of 'Closure' is not allowed

查看:101
本文介绍了在闭包中使用static和phpunit的原因:不允许对'Closure'进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实例化并从关闭处启动PHPUnitTest,但我一直收到此消息:

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

mytest :: authenticate_test例外:不允许序列化关闭"

它可以在封闭区域外正常工作,并且路由由Aura Router管理.

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
    });
});

我该如何进行?

请帮助我.

谢谢.

推荐答案

似乎此答案拥有针对问题.

问题来自以下事实:PHPUnit在测试运行时将系统中的所有$ GLOBALS序列化为必需的备份.然后在测试完成后将其还原.但是,如果您在GLOBAL空间中有任何关闭,都会引起问题.

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.

尝试通过相应的PHPDoc禁用$GLOBALS的备份:

Try disabling backup of $GLOBALS via corresponding PHPDoc:

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

这篇关于在闭包中使用static和phpunit的原因:不允许对'Closure'进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆