简体   繁体   English

如何使用PHPUnit,Mock,Stub测试不属于我的测试类的测试方法

[英]How test methods that are not of my test class with PHPUnit, mock, stub

I have this class 我有这个课

<?php 

class Password 
{
    protected function checkPassword()
    {
        $this->callExit();
    }

    protected function callExit()
    {
    exit;
    }    
}

and this is my test: 这是我的测试:

 public function testAuthorizeExitsWhenPasswordNotSet()
    {


        $badCode = $this->getMockBuilder(Password::class)
            ->setMethods(array('callExit'))
            ->getMock();

        $badCode->expects($this->once())
            ->method('callExit');

        $badCode->checkPassword();

    }

In the earlier class the callExit method is of Password class. 在早期的类中, callExit方法属于Password类。 My question is, Can I test methods that aren't of the Password class ? 我的问题是,我可以测试不是Password类的方法吗?

For example in checkPassword method: 例如在checkPassword方法中:

protected function checkPassword()
{
    $user = new User;

    $this->callExit();

    $user->fillOut();

}

I want to do a mock for fillOut method, How do I it? 我想为fillOut方法做一个模拟,我该怎么办?

Help me plis !! 帮帮我!

The way that your code is written, you cannot mock the fillOut method because you are instantiating the User object within the method that you want to test. 编写代码的方式无法模拟fillOut方法,因为您正在要测试的方法内实例化User对象。 There is no way to replace the object with a mock like this. 没有办法用这样的模拟代替对象。

In order to test this method, you should pass in a User object to the checkPassword method. 为了测试此方法,您应该将User对象传递给checkPassword方法。 Then you would be able to create a MockUser with the fillOut method mocked. 然后,您将能够MockUserfillOut方法来创建MockUser

So your method would look like this: 因此,您的方法将如下所示:

protected function checkPassword(User $user) {
    $this->callExit();
    $user->fillOut();
}

ALSO

In your code that you have posted, you are calling exit(). 在您发布的代码中,您正在调用exit()。 Please keep in mind that if this is executed it will halt PHPUnit also. 请记住,如果执行此命令,PHPUnit也将停止。

You are also trying to explicitly test a protected method, you really shouldn't do this. 您还尝试显式测试受保护的方法,实际上不应该这样做。 You should only test the public methods of your classes. 您应该只测试类的公共方法。 The protected and private methods should be executed when you are testing the public methods. 测试公共方法时,应执行受保护的方法和私有方法。 This lets you refactor the internals of your class and know that you haven't changed the functionality of your class. 这使您可以重构类的内部结构,并知道您尚未更改类的功能。

If you feel that you need to explicitly test a protected function, this is a sign that you should move that method into a separate class that gets provided to the object that you are testing. 如果您认为需要显式测试受保护的函数,则表明您应将该方法移到一个单独的类中,该类将提供给要测试的对象。

exit means exit php ... for your test and also for phpunit. exit表示退出php ...,供您进行测试,也可用于phpunit。

your may use exeptions to handle exit points. 您可能会使用异常处理出口点。 otherwise no mock will go on. 否则不会继续进行模拟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM