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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?