我有一个类,它生成一个数据对象并将其传递给给定的可调用变量
<?php
class Foo {
public function bar(callable $closure)
{
$data = $this->generateData();
call_user_func_array($closure, compact($data));
}
}
// example usage
$baz = new Foo()
$baz->bar(function($data) {
var_dump($data); // I want to test $data type inside this closure
});
如何测试$data
dataType传递给匿名函数?
答案 0 :(得分:2)
$baz = new Foo();
$baz->bar(function($data) {
$this->assertSame('expected', $data);
});
还要确保甚至可以通过设置变量来调用闭包:
$baz = new Foo();
$called = false;
$baz->bar(function($data) use (&$called) {
$called = true;
$this->assertSame('expected', $data);
});
$this->assertTrue($called);