热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ZendFramework2和PHPUnit-模拟Zend\Db\Adapter\Adapter类

如何解决《ZendFramework2和PHPUnit-模拟Zend\Db\Adapter\Adapter类》经验,请问有没有懂的朋友?

几年前,我按照本教程开始学习Zend Framework .在那里,它显示使用Zend\Db\Adapter\Adapter类创建映射器以获取数据库连接,这就是我使用数据库的方式,因为没有问题.

我现在正在尝试学习如何在Zend应用程序上使用PHPUnit,并且在测试mapper中的函数时遇到了困难,因为我无法模拟Zend\Db\Adapter\Adapter类.

Zend网站上的本教程展示了模拟数据库连接,但它使用了Zend\Db\TableGateway\TableGateway该类.我在网上找到的所有其他教程使用此类过了,我就发现的唯一Zend\Db\Adapter\Adapter类是这样:

$date = new DateTime();
$mockStatement = $this->getMock('Zend\Db\Adapter\Driver\Pdo\Statement');
$mockStatement->expects($this->once())->method('execute')->with($this->equalTo(array(
    'timestamp' => $date->format(FormatterInterface::DEFAULT_DATETIME_FORMAT)
)));

$mockDbDriver = $this->getMockBuilder('Zend\Db\Adapter\Driver\Pdo\Pdo')
    ->disableOriginalConstructor()
    ->getMock();

$mockDbAdapter = $this->getMock('Zend\Db\Adapter\Adapter', array(), array($mockDbDriver));
$mockDbAdapter->expects($this->once())
    ->method('query')
    ->will($this->returnValue($mockStatement));

我已经尝试将它放入我的setUp方法但phpunit在测试类上运行会给我以下错误:

致命错误:在第128行的C:\ Program Files(x86)\ Zend\Apache2\htdocs\test_project\vendor\zendframework\zend-db\src\Sql\Sql.php中调用null上的成员函数createStatement()

所以我的问题是,你如何Zend\Db\Adapter\Adapter在PHPUnit中模拟类?

我已经看到了这个类似的问题,但是使用了一个Zend/Db/Adapter/AdapterInterface代替,我似乎无法将该代码转换成我的情况.Mapper和测试类代码如下.

ProductMapper.php:

public function __construct(Adapter $dbAdapter) {
    $this->dbAdapter = $dbAdapter;
    $this->sql = new Sql($dbAdapter);
}

public function fetchAllProducts() {
    $select = $this->sql->select('products');

    $statement = $this->sql->prepareStatementForSqlObject($select);
    $results = $statement->execute();

    $hydrator = new ClassMethods();
    $product = new ProductEntity();
    $resultset = new HydratingResultSet($hydrator, $product);
    $resultset->initialize($results);
    $resultset->buffer();

    return $resultset;
}

ProductMapperTest.php:

public function setUp() {
    $date = new DateTime();

    $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\Pdo\Statement');
    $mockStatement->expects($this->once())->method('execute')->with($this->equalTo(array(
            'timestamp' => $date->format(FormatterInterface::DEFAULT_DATETIME_FORMAT)
    )));

    $mockDbDriver = $this->getMockBuilder('Zend\Db\Adapter\Driver\Pdo\Pdo')->disableOriginalConstructor()->getMock();

    $this->mockDbAdapter = $this->getMock('Zend\Db\Adapter\Adapter', array(), array(
            $mockDbDriver
    ));
    $this->mockDbAdapter->expects($this->once())->method('query')->will($this->returnValue($mockStatement));
}

public function testFetchAllProducts() {
    $resultsSet = new ResultSet();

    $productMapper = new ProductMapper($this->mockDbAdapter);

    $this->assertSame($resultsSet, $productMapper->fetchAllProducts());
}

编辑#1:

继Wilt的回答之后,我改变了我的mapper以Sql在构造函数中使用该类并将我的Test类更改为:

public function setUp() {
    $mockSelect = $this->getMock('Zend\Db\Sql\Select');

    $mockDbAdapter = $this->getMockBuilder('Zend\Db\Adapter\AdapterInterface')->disableOriginalConstructor()->getMock();

    $this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\Pdo\Statement');

    $this->mockSql = $this->getMock('Zend\Db\Sql\Sql', array('select', 'prepareStatementForSqlObject'), array($mockDbAdapter));
    $this->mockSql->method('select')->will($this->returnValue($mockSelect));
    $this->mockSql->method('prepareStatementForSqlObject')->will($this->returnValue($this->mockStatement));
}

public function testFetchAllProducts() {
    $resultsSet = new ResultSet();

    $this->mockStatement->expects($this->once())->method('execute')->with()->will($this->returnValue($resultsSet));

    $productMapper = new ProductMapper($this->mockSql);

    $this->assertSame($resultsSet, $productMapper->fetchAllProducts());
}

但是,我现在收到以下错误:

ProductTest\Model\ProductMapperTest :: testFetchAllProducts断言两个变量引用同一个对象失败.

哪个来自这条线$this->assertSame($resultsSet, $productMapper->fetchAllProducts());.我是否错误地嘲笑了什么?


编辑#2:

正如Wilt所建议的那样,我改变了测试类来使用a StatementInterface来模拟语句,所以代码现在看起来像:

public function setUp() {

    $mockSelect = $this->getMock('Zend\Db\Sql\Select');

    $mockDbAdapter = $this->getMockBuilder('Zend\Db\Adapter\AdapterInterface')->disableOriginalConstructor()->getMock();

    $this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');

    $this->mockSql = $this->getMock('Zend\Db\Sql\Sql', array('select', 'prepareStatementForSqlObject'), array($mockDbAdapter));
    $this->mockSql->method('select')->will($this->returnValue($mockSelect));
    $this->mockSql->method('prepareStatementForSqlObject')->will($this->returnValue($this->mockStatement));

}

public function testFetchAllProducts() {
    $resultsSet = new ResultSet();

    $this->mockStatement->expects($this->once())->method('execute')->with()->will($this->returnValue($resultsSet));

    $productMapper = new ProductMapper($this->mockSql);

    $this->assertSame($resultsSet, $productMapper->fetchAllProducts());
}

但是测试用例仍然如上所述失败.我没有改变嘲弄execute方法的代码行,因为我相信它已经返回了$resultsSet,但是我可能错了!


推荐阅读
author-avatar
张琪健V
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有