函数惰性加载
{
$this->assertFalse(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn1'));
$this->assertFalse(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn2'));
$result = (new Fn())('Tests\\Support\\Fixtures\\Fn\\testgroup_fn1');
$this->assertSame('hello world', $result);
$result = (new Fn())('Tests\\Support\\Fixtures\\Fn\\testgroup_fn2');
$this->assertSame('hello world2', $result);
$this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn1'));
$this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn2'));
}
闭包调用已载入的分组函数
public function testGroupWithClosureWithFuncWasLoaded(): void
{
$this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn1'));
$this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn2'));
$result = (new Fn())(function () {
return testgroup_fn1();
});
$this->assertSame('hello world', $result);
return testgroup_fn2();
});
$this->assertSame('hello world2', $result);
}
字符串调用单个文件函数
public function testSingleFn(): void
{
$this->assertFalse(function_exists('Tests\\Support\\Fixtures\\Fn\\single_fn'));
$result = (new Fn())('Tests\\Support\\Fixtures\\Fn\\single_fn');
$this->assertSame('hello single fn', $result);
$this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\single_fn'));
}
public function testSingleFnWithClosure(): void
{
$this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\single_fn'));
$result = (new Fn())(function () {
return single_fn();
});
$this->assertSame('hello single fn', $result);
}
字符串调用 index 索引函数
public function testIndexWithClosure(): void
$this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\foo_bar'));
$result = (new Fn())(function () {
return foo_bar();
});
$this->assertSame('foo bar', $result);
$result = (new Fn())(function () {
return foo_bar(' haha');
});
$this->assertSame('foo bar haha', $result);
}
闭包调用多个函数
public function testIndexWithClosureWithMulti(): void
{
$this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\foo_bar'));
$result = (new Fn())(function () {
$result1 = foo_bar();
return $result1.' '.foo_bar();
});
$this->assertSame('foo bar foo bar', $result);