下面是一个使用Zend / Tablegateway的函数:
public function listAttestations($sSidx = null, $sSord = null, $iOffset = 0, $iLimit = 0)
{
try {
$resultSet = $this->tableGateway->select(
function (Select $select) use ($sSidx, $sSord, $iOffset, $iLimit) {
if ($sSidx != null && $sSord != null) {
$select->order($sSidx.' '.$sSord);
}
$select->join(
'f_travclient',
'syndic_client_id = f_travclient.travClient_id',
array('syndic' => 'nom')
);
$select->offset($iOffset);
$select->limit($iLimit);
}
);
return $resultSet;
} catch (\Exception $e) {
throw new \Exception($e);
}
}我使用PHPUnit进行单元测试。也许,我不知道如何使与我以前的方法交叉的函数。我想这可以起作用:
public function testListAttestations()
{
$resultSet = new ResultSet();
$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select'), array(), '', false);
$mockTableGateway->expects($this->once())
->method('select')
->with()
->will($this->returnValue($resultSet));
$attestTable = new FMaiAttestationTable($mockTableGateway, $this->adapter, $this->sql);
$this->assertSame($resultSet, $attestTable->listAttestations('maiAttestation_id', 'ASC', 0, 30));
}但这并没有更进一步:
function (Select $select) use ($sSidx, $sSord, $iOffset, $iLimit) {有人能帮我吗?谢谢。
发布于 2015-09-21 13:07:59
可以用returnCallback()获取和使用给模拟方法的任何参数。
$mockTableGateway->expects($this->once())
->method('select')
->with()
->will($this->returnCallback(function($function) use ($resultSet) {
// do something with the function, for example:
if(!is_callable($function)) {
return NULL;
}
call_user_func($function, new Select());
return $resultSet;
}));不过,您可能需要重新考虑当前代码,因为没有必要像这样编写嵌套代码。例如,您可以自己获取Select的一个实例,并将其与selectWith()一起使用。
public function listAttestations($sSidx = null, $sSord = null, $iOffset = 0, $iLimit = 0)
{
try {
$select = new Select();
if ($sSidx != null && $sSord != null) {
$select->order($sSidx.' '.$sSord);
}
$select->join(
'f_travclient',
'syndic_client_id = f_travclient.travClient_id',
array('syndic' => 'nom')
);
$select->offset($iOffset);
$select->limit($iLimit);
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
} catch (\Exception $e) {
throw new \Exception($e);
}
}在这种情况下,现在可以测试您的方法是否以您想要的方式将Select放在一起。您只需按照预期的方式在测试中创建另一个Select对象:
$resultSet = new ResultSet();
$sSidx = 'maiAttestation_id';
$sSord = 'ASC';
$iOffset = 0;
$iLimit = 30;
$select = new Select();
$select->order($sSidx.' '.$sSord);
$select->join(
'f_travclient',
'syndic_client_id = f_travclient.travClient_id',
array('syndic' => 'nom')
);
$select->offset($iOffset);
$select->limit($iLimit);
$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
array('selectWith'), array(), '', false);
$mockTableGateway->expects($this->once())
->method('selectWith')
->with($select)
->will($this->returnValue($resultSet));
$attestTable = new FMaiAttestationTable($mockTableGateway, $this->adapter, $this->sql);
$this->assertEquals($resultSet, $attestTable->listAttestations($sSidx, $sSord, $iOffset, $iLimit));如果模拟方法selectWith使用的selectWith对象与您创建的对象有任何不同,PHPUnit将在测试中抛出一个错误。
https://stackoverflow.com/questions/32694776
复制相似问题