如何使用phpunit测试传递给closure的数据
我有一个类,它生成一个数据对象并将其传递给给定的可调用变量如何使用phpunit测试传递给closure的数据,php,unit-testing,phpunit,Php,Unit Testing,Phpunit,我有一个类,它生成一个数据对象并将其传递给给定的可调用变量 <?php class Foo { public function bar(callable $closure) { $data = $this->generateData(); call_user_func_array($closure, compact($data)); } } // example usage $baz = new Foo() $baz-
<?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
});
还要确保您的闭包甚至被调用,可能是通过设置一个变量:
$baz = new Foo();
$called = false;
$baz->bar(function($data) use (&$called) {
$called = true;
$this->assertSame('expected', $data);
});
$this->assertTrue($called);
$baz = new Foo();
$called = false;
$baz->bar(function($data) use (&$called) {
$called = true;
$this->assertSame('expected', $data);
});
$this->assertTrue($called);