【发布时间】:2020-06-05 00:50:05
【问题描述】:
对于特定需求,我必须在 symfony 4.4 项目中测试Kernel 类,除了registerContainerConfiguration() 下的方法外,一切都很好。它只包含一个将参数作为Anonymous function 的方法,我怎么能完全测试它?我找不到办法进去。
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function (ContainerBuilder $container) use ($loader) {
$container->loadFromExtension('framework', [
'router' => [
'resource' => 'kernel::loadRoutes',
'type' => 'service',
],
]);
if (!$container->hasDefinition('kernel')) {
$container->register('kernel', static::class)
->setSynthetic(true)
->setPublic(true)
;
}
$kernelDefinition = $container->getDefinition('kernel');
$kernelDefinition->addTag('routing.route_loader');
if ($this instanceof EventSubscriberInterface) {
$kernelDefinition->addTag('kernel.event_subscriber');
}
$this->configureContainer($container, $loader);
$container->addObjectResource($this);
});
}
我已经写的代码:
public function testRegisterContainerConfiguration(): void
{
$loader = $this->prophesize(LoaderInterface::class);
$loader->load()
->shouldBeCalledOnce() // here i'm stuck
;
$this->kernel->registerContainerConfiguration($loader->reveal());
}
PS:我试过this one,但它似乎只模拟匿名函数。
【问题讨论】: