1

我是 php 单元和 Zend 2 的新手。

我需要为以下功能编写单元测试:

public function getMyList($limitDay)
{
        // build time
        $limitTime = strtotime("-".$limitDay." day"); // UNIX timestamp
        $timeStr = date("Y-m-d h:i:s", $limitTime); // format time

        // run query
        $resultSet = $this->tableGateway->select(
                // use closure
                function (Select $select) use ($timeStr) {
                    $condition = function (Where $where) use($timeStr) {
                        $where->greaterThan('SEND_TIME', new Expression('TIMESTAMP \''.$timeStr.'\''));
                    };
                    $select->where($condition);
                }
        );

        return $resultSet;
}

作为单元测试的 ZF2 文档,我可以像这样创建一个单元测试函数:

public function testGetMyList()
{
        $resultSet = new ResultSet();
        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
                                           array('select'), array(), '', false);
        $mockTableGateway->expects($this->once())
                         ->method('select')
                         ->with(/*[argument]*/)
                         ->will($this->returnValue($resultSet));

        $albumTable = new AlbumTable($mockTableGateway);

        $this->assertSame($resultSet, $albumTable->getMyList(1));
}

但我不知道如何描述 Closure

                function (Select $select) use ($timeStr) {
                    $condition = function (Where $where) use($timeStr) {
                        $where->greaterThan('SEND_TIME', new Expression('TIMESTAMP \''.$timeStr.'\''));
                    };
                    $select->where($condition);
                }

为了

/*[argument]*/

没有用于描述它或为 Closure 创建模拟的教程。

在这种情况下请帮助我!谢谢

4

0 回答 0