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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址