问题
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