在phpunit中,是否有一种类似于onconecutivecalls的方法在"with"内部使用.方法? [英] in phpunit, is there a method similar to onconsecutivecalls for use inside the "with" method?

查看:86
本文介绍了在phpunit中,是否有一种类似于onconecutivecalls的方法在"with"内部使用.方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHPUnit来模拟pdo,但是我试图找到一种方法来准备多个数据库查询语句.

Using PHPUnit, I'm mocking the pdo, but I'm trying to find a way to prepare more than one database query statement.

$pdo = $this->getPdoMock();
$stmt = $this->getPdoStatementMock($pdo);

$pdo->expects($this->any())
    ->method('prepare')
    ->with($this->equalTo($title_query))
    ->will($this->returnValue($stmt));

$title_stmt = $pdo->prepare($title_query);
$desc_stmt = $pdo->prepare($desc_query);

我想为"with"方法传递类似于onConsecutiveCalls的内容,因此我可以准备多个语句,如上所示. 您将如何去做?

I want to pass something similar to onConsecutiveCalls for the "with" method, so I can prepare multiple statements, as seen above. How would you go about doing this?

推荐答案

通过用$this->at()而不是$this->any()编写单独的期望,可以匹配同一方法的连续调用:

You can match consecutive invocations of the same method by writing separate expectations with $this->at() instead of $this->any():

$pdo->expects($this->at(0))
    ->method('prepare')
    ->with($this->equalTo($title_query))
    ->will($this->returnValue($stmt));

$pdo->expects($this->at(1))
    ->method('prepare')
    ->with($this->equalTo($desc_query))
    ->will($this->returnValue($stmt));

$title_stmt = $pdo->prepare($title_query);
$desc_stmt = $pdo->prepare($desc_query);

这篇关于在phpunit中,是否有一种类似于onconecutivecalls的方法在"with"内部使用.方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆