0
大家好,我需要测试一段代码,调用另一个类的功能我现在不能编辑。PHPUnit测试函数的值通过引用和返回值
我只需要测试它,但问题是这个函数有一个值通过引用和返回值,所以我不知道如何模拟它。
这列类的功能:
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);
但同样的错误,我该如何嘲笑该功能?
感谢
是的,它的工作原理!非常感谢! –