PHPUnit:获取模拟方法调用的参数
我目前正在开发一个存储敏感数据的项目,因此必须能够根据请求删除它们。
我想测试我的实体(患者)是否使用空电话号码保存到数据库中。第一个想法是:将参数传递给 PatientDao::savePatient(PatientModel $ Patient)
,并查看其 phoneNumber
属性。
所以这里是 PatientDao
接口:
interface PatientDao {
function savePatient(PatientModel $patient);
}
以及我的测试文件中的代码:
$this->patientDao // This is my mock
->expects($this->once())
->method('savePatient'); // savePatient() must be called once
$this->controller->handleMessage(...);
$patient = ??; // How can I get the patient to make assertions with it ?
我该如何做到这一点,或者是否有其他方法来确保使用空电话号码保存患者?
I'm currently working on a project that stores sensitive data, and must therefore be able to erase them on request.
I'd like to test that my entity (patient) is saved to the database with a null phone number. First idea to do that: get the argument passed to PatientDao::savePatient(PatientModel $patient)
, and have a look at its phoneNumber
attribute.
So here is the PatientDao
interface:
interface PatientDao {
function savePatient(PatientModel $patient);
}
And the code in my test file:
$this->patientDao // This is my mock
->expects($this->once())
->method('savePatient'); // savePatient() must be called once
$this->controller->handleMessage(...);
$patient = ??; // How can I get the patient to make assertions with it ?
How can I do that, or is there any other way to make sure the patient is saved with a null phone number?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 returnCallback() 对参数进行断言。请记住通过 PHPUnit_Framework_Assert 静态调用断言函数,因为您不能在闭包内使用 self 。
You can make assertions on the argument using
returnCallback()
. Remember to call assert functions statically viaPHPUnit_Framework_Assert
because you cannot useself
inside a closure.这是我使用的技巧。我将此私有方法添加到我的测试类中:
然后在设置模拟时:
之后,
$arg
包含传递给模拟的参数值。Here's the trick I used. I added this private method to my test class:
Then when setting up the mock:
After that,
$arg
contains the value of the argument passed to the mock.使 Mock 对象方法返回第一个参数:
然后您可以断言它是 NULL。
Make the Mock objects method return the first argument:
You can then assert that it is
NULL
.一件事 - 在闭包内你仍然可以访问 $this,在这种情况下它会给你:
One thing - inside the closure you still can access $this, in that case it gives you: