dsa111111 2015-09-21 12:03
浏览 46
已采纳

PHPUnit - 模拟回调函数

Here is a function which uses Zend DB / 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);
    }
}

I use PHPUnit to do unit tests. Perhaps, I don't know how to make the function which crosses my previous method. I thought this could be functional :

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));
}

But this doesn't go further the :

function (Select $select) use ($sSidx, $sSord, $iOffset, $iLimit) {

Could somebody help me ? Thanks.

  • 写回答

1条回答 默认 最新

  • dongqi1245 2015-09-21 13:07
    关注

    You can fetch and use any arguments given to a mocked method with 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;
            }));
    

    Still, you might want to reconsider your current code as it is not necessary to write it nested like this. You could, for example, get an instance of Select yourself and use it with 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);
        }
    }
    

    In this case it is now possible to test if your method puts the Select together the way you want. You can simply create another Select object in your test the way you expect it to be:

        $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));
    

    If the Select object used by the mocked method selectWith looks any different from the one you created, PHPUnit will throw an error in your test.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的