【发布时间】:2017-06-13 09:39:51
【问题描述】:
大家好,我需要测试一段代码,该代码调用另一个类的函数,我现在无法编辑。
我只需要测试它,但问题是这个函数有一个通过引用传递的值和一个返回值,所以我不知道如何模拟它。
这是列类的功能:
public function functionWithValuePassedByReference(&$matches = null)
{
$regex = 'my regex';
return ($matches === null) ? preg_match($regex, $this->field) : preg_match($regex, $this->field, $matches);
}
这是调用的地方和我需要模拟的地方:
$matches = [];
if ($column->functionWithValuePassedByReference($matches)) {
if (strtolower($matches['parameters']) == 'distinct') {
//my code
}
}
所以我试过了
$this->columnMock = $this->createMock(Column::class);
$this->columnMock
->method('functionWithValuePassedByReference')
->willReturn(true);
如果我这样做会返回错误,即索引 parameters 显然不存在,所以我尝试了这个:
$this->columnMock = $this->createMock(Column::class);
$this->columnMock
->method('functionWithValuePassedByReference')
->with([])
->willReturn(true);
但同样的错误,我该如何模拟该函数?
谢谢
【问题讨论】:
标签: php unit-testing phpunit