模拟调用zend db函数和reset()函数的服务

时间:2015-09-29 14:23:30

标签: php zend-framework2 phpunit zend-db

所以我创建了一个测试服务集:

class FMaiAffaireServiceTest extends TestCase
{
    /**
     * @var MyService
     */
    private $myService;
    private $typeaffaireTable;

    private $mockDriver;
    private $mockConnection;
    private $mockPlatform;
    private $mockStatement;
    private $adapter;
    private $sql;

    public function setUp()
    {
        $this->mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
        $this->mockConnection = $this->getMock('Zend\Db\Adapter\Driver\ConnectionInterface');
        $this->mockDriver->expects($this->any())->method('checkEnvironment')->will($this->returnValue(true));
        $this->mockDriver->expects($this->any())->method('getConnection')->will($this->returnValue($this->mockConnection));
        $this->mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface');
        $this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
        $this->mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($this->mockStatement));
        $this->adapter = new Adapter($this->mockDriver, $this->mockPlatform);
        $this->sql = new Sql($this->adapter);

        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array(), array(), '', false);

        $maiAffaireTable = $this->getMockBuilder('Maintenance\Model\BDD\FMaiAffaireTable')
                                ->setMethods(array())
                                ->setConstructorArgs(array($mockTableGateway, $this->adapter, $this->sql))
                                ->getMock();

        $stub = $this->returnValue(new ResultSet());
        $maiAffaireTable->expects($this->any())->method('listAffaires')->will($stub);


        $this->myService = new FMaiAffaireService(
            $maiAffaireTable
        );
    }

    public function testListAffaires()
    {
        $this->myService->listAffaires(1,10);
    }
}

我的服务看起来像这样,它是对我的Zend Db函数的调用:

class FMaiAffaireService
{
    private $maiAffaireTable;


    public function __construct(
        $maiAffaireTable,
    ) {
        $this->maiAffaireTable      = $maiAffaireTable;
    }

    public function listAffaires($iOffset, $iLimit) {
        $aResults = $this->maiAffaireTable->listAffaires($iOffset, $iLimit);
        return $aResults->toArray();
    }
}

以下是我的Zend DB功能的示例:

class FMaiAffaireTable
{
    protected $tableGateway;
    protected $adapter;
    protected $sql;

    public function __construct(
        TableGateway $tableGateway,
        Adapter $adapter,
        Sql $sql
    ) {
        $this->tableGateway = $tableGateway;
        $this->adapter      = $adapter;
        $this->sql          = $sql;
    }

    public function listAffaires($iOffset, $iLimit)
    {
        try {
            $resultSet = $this->tableGateway->select(
                function (Select $select) use (
                    $iOffset,
                    $iLimit
                ) {
                    $select->offset($iOffset);
                    $select->limit($iLimit);
                }
            );
            return $resultSet;
        } catch (\Exception $e) {
            throw new \Exception($e);
        }
    }
}

执行PHPUnit时出现了一个很大的问题:

  

1)目录 \ FMaiAffaireServiceTest :: testListAffaires reset()期望参数1为数组,给定为null

我不会在任何地方调用reset()!这就是问题......我认为这是一个PDO功能但是......我有点迷失了。

感谢。

1 个答案:

答案 0 :(得分:1)

问题出在这里

$stub = $this->returnValue(new ResultSet());
$maiAffaireTable->expects($this->any())->method('listAffaires')->will($stub);

非初始化的ResultSet将没有数据源,在其上运行toArray()(就像在服务中一样)将首先尝试重置数据源,该数据源将为null。

尝试

$resultSet = new ResultSet();
$resultSet->initialize(array());
$stub = $this->returnValue($resultSet);