PHPUnit:获取模拟方法调用的参数

发布于 2024-12-10 21:55:40 字数 720 浏览 0 评论 0原文

我目前正在开发一个存储敏感数据的项目,因此必须能够根据请求删除它们。

我想测试我的实体(患者)是否使用空电话号码保存到数据库中。第一个想法是:将参数传递给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

意犹 2024-12-17 21:55:40

您可以使用 returnCallback() 对参数进行断言。请记住通过 PHPUnit_Framework_Assert 静态调用断言函数,因为您不能在闭包内使用 self 。

$this->patientDao
        ->expects($this->once()) 
        ->method('savePatient')
        ->will($this->returnCallback(function($patient) {
            PHPUnit_Framework_Assert::assertNull($patient->getPhoneNumber());
        }));

You can make assertions on the argument using returnCallback(). Remember to call assert functions statically via PHPUnit_Framework_Assert because you cannot use self inside a closure.

$this->patientDao
        ->expects($this->once()) 
        ->method('savePatient')
        ->will($this->returnCallback(function($patient) {
            PHPUnit_Framework_Assert::assertNull($patient->getPhoneNumber());
        }));
人疚 2024-12-17 21:55:40

这是我使用的技巧。我将此私有方法添加到我的测试类中:

private function captureArg( &$arg ) {
    return $this->callback( function( $argToMock ) use ( &$arg ) {
        $arg = $argToMock;
        return true;
    } );
}

然后在设置模拟时:

$mock->expects( $this->once() )
    ->method( 'someMethod' )
    ->with( $this->captureArg( $arg ) );

之后, $arg 包含传递给模拟的参数值。

Here's the trick I used. I added this private method to my test class:

private function captureArg( &$arg ) {
    return $this->callback( function( $argToMock ) use ( &$arg ) {
        $arg = $argToMock;
        return true;
    } );
}

Then when setting up the mock:

$mock->expects( $this->once() )
    ->method( 'someMethod' )
    ->with( $this->captureArg( $arg ) );

After that, $arg contains the value of the argument passed to the mock.

吹梦到西洲 2024-12-17 21:55:40

使 Mock 对象方法返回第一个参数:

$this->patientDao                    // This is my mock
            ->expects($this->once()) 
            ->method('savePatient') // savePatient() must be called once
            ->with($this->returnArgument(0));

然后您可以断言它是 NULL。

Make the Mock objects method return the first argument:

$this->patientDao                    // This is my mock
            ->expects($this->once()) 
            ->method('savePatient') // savePatient() must be called once
            ->with($this->returnArgument(0));

You can then assert that it is NULL.

蓝咒 2024-12-17 21:55:40

一件事 - 在闭包内你仍然可以访问 $this,在这种情况下它会给你:

$this->patientDao
    ->expects($this->once()) 
    ->method('savePatient')
    ->will($this->returnCallback(function($patient) {
        $this->assertNull($patient->getPhoneNumber());
    }));

One thing - inside the closure you still can access $this, in that case it gives you:

$this->patientDao
    ->expects($this->once()) 
    ->method('savePatient')
    ->will($this->returnCallback(function($patient) {
        $this->assertNull($patient->getPhoneNumber());
    }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文