【问题标题】:PHPUnit - mock callback functionPHPUnit - 模拟回调函数
【发布时间】:2015-12-18 03:02:45
【问题描述】:

这是一个使用 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);
    }
}

我使用 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) {

有人可以帮我吗?谢谢。

【问题讨论】:

    标签: php zend-framework2 phpunit zend-db


    【解决方案1】:

    您可以通过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 使用的Select 对象看起来与您创建的对象有任何不同,PHPUnit 将在您的测试中抛出错误。

    【讨论】:

    • 谢谢!它现在进入我的方法:)
    猜你喜欢
    • 2020-05-08
    • 2017-01-13
    • 2016-01-30
    • 2015-10-03
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    相关资源
    最近更新 更多