【发布时间】:2019-01-03 21:54:55
【问题描述】:
部分测试对象:
class AddOptionsProviderArgumentPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if(!$container->hasDefinition('gremo_highcharts')) {
return;
}
if(!$container->hasParameter('gremo_highcharts.options_provider')) {
return;
}
// ...
}
}
我想断言:
-
hasDefinition()使用参数 'gremo_highcharts' 调用将返回false - 方法
process()返回,即不会调用其他方法
一种解决方案是断言对hasParameter() 的后续调用:
public function testProcessWillReturnIfThereIsNoServiceDefinition()
{
$container = $this->getMockedContainerBuilder();
$pass = new AddOptionsProviderArgumentPass();
$container->expects($this->once())
->method('hasDefinition')
->with($this->equalTo('gremo_highcharts'))
->will($this->returnValue(false));
// Expects that hasParameter() is never invoked
$container->expects($this->never())
->method('hasParameter');
$pass->process($container);
}
但这似乎不是一个优雅的解决方案。
【问题讨论】:
标签: php unit-testing phpunit