使用 PHPUnit 模拟私有方法
我有一个关于使用 PHPUnit 模拟类中的私有方法的问题。让我用一个例子来介绍一下:
class A {
public function b() {
// some code
$this->c();
// some more code
}
private function c(){
// some code
}
}
如何对私有方法的结果进行存根来测试公共函数的更多代码部分。
部分阅读此处解决
I have a question about using PHPUnit to mock a private method inside a class. Let me introduce with an example:
class A {
public function b() {
// some code
$this->c();
// some more code
}
private function c(){
// some code
}
}
How can I stub the result of the private method to test the some more code part of the public function.
Solved partially reading here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
通常你只是不测试或嘲笑私有&直接受保护的方法。
您要测试的是您的类的 public API。其他一切都是您的类的实现细节,如果您更改它,则不应“破坏”您的测试。
当您注意到“无法获得 100% 的代码覆盖率”时,这也会对您有所帮助,因为您的类中可能有无法通过调用公共 API 来执行的代码。
你通常不想这样做
但是如果你的类看起来像这样:
我可以看到需要模拟 c() 因为“随机”函数是全局状态并且你无法测试它。
“clean?/verbose?/overcomplicated-maybe?/i-like-it-usually”解决方案
现在不再需要模拟“c()”,因为它不包含任何全局变量,您可以很好地进行测试。
如果您不想或无法从私有函数中删除全局状态(坏事坏现实或者您对坏的定义可能不同),您可以针对模拟进行测试。
并针对此模拟运行测试,因为您取出/模拟的唯一函数是“c()”。
引用《实用单元测试》一书:
更多:
为什么你不这样做不想测试私有方法。
Usually you just don't test or mock the private & protected methods directy.
What you want to test is the public API of your class. Everything else is an implementation detail for your class and should not "break" your tests if you change it.
That also helps you when you notice that you "can't get 100% code coverage" because you might have code in your class that you can't execute by calling the public API.
You usually don't want to do this
But if your class looks like this:
i can see the need to want to mock out c() since the "random" function is global state and you can't test that.
The "clean?/verbose?/overcomplicated-maybe?/i-like-it-usually" Solution
now there is no more need to mock "c()" out since it does not contain any globals and you can test nicely.
If you don't want to do or can't remove the global state from your private function (bad thing bad reality or you definition of bad might be different) that you can test against the mock.
and run your tests against this mock since the only function you take out/mock is "c()".
To quote the "Pragmatic Unit Testing" book:
Some more:
Why you don't want test private methods.
您可以使用 反射 和
setAccessible()
在您的测试中允许您设置对象的内部状态,使其从私有方法返回您想要的内容。您需要使用 PHP 5.3.2。You can use reflection and
setAccessible()
in your tests to allow you to set the internal state of your object in such a way that it will return what you want from the private method. You'll need to be on PHP 5.3.2.您可以 测试私有方法
但您无法模拟(模拟)此方法的运行。
此外,反射不允许您将私有方法转换为受保护或公共方法。 setAccessible 只允许您调用原始方法。
或者,您可以使用 runkit 重命名私有方法并包含“新执行”。但是,这些功能是实验性的,不建议使用。
You can test private methods
but you can't simulate (mock) the running of this methods.
Furthermore, the reflection does not allow you to convert a private method to a protected or public method. setAccessible only allows you to invoke the original method.
Alternatively, you could use runkit for rename the private methods and include a "new implementation". However, these features are experimental and their use is not recommended.
您可以获得受保护方法的模拟,因此如果您可以将 C 转换为受保护,那么此代码将会有所帮助。
这肯定有用,对我有用。
然后,为了覆盖受保护的方法 C,您可以使用反射类。
You can get mock of protected method , so if you can convert C to protected then this code will help.
This will definitely work , It worked for me .
Then For covering protected method C you can use reflection classes.
假设您需要测试 $myClass->privateMethodX($arg1, $arg2),您可以通过反射来执行此操作:
Assuming that you need to test $myClass->privateMethodX($arg1, $arg2), you can do this with reflection:
一种选择是使
c()
protected
而不是private
,然后子类化并覆盖c()
。然后用你的子类进行测试。另一种选择是将c()
重构为一个不同的类,您可以将其注入到A
中(这称为依赖注入)。然后在单元测试中注入带有c()
模拟实现的测试实例。One option would be to make
c()
protected
instead ofprivate
and then subclass and overridec()
. Then test with your subclass. Another option would be to refactorc()
out into a different class that you can inject intoA
(this is called dependency injection). And then inject a testing instance with a mock implementation ofc()
in your unit test.以下是其他答案的变体,可用于在一行中进行此类调用:
Here's a variation of the other answers that can be used to make such calls one line:
我为我的案例想出了这个通用类:
通过这个类,您现在可以轻松地&透明地释放任何对象上的所有私有函数/字段。
如果性能很重要,可以通过缓存 Liberator 类中调用的属性/方法来提高性能。
I came up with this general purpose class for my case:
With this class you can now easily & transparently liberate all private functions/fields on any object.
If performance is important, it can be improved by caching the properties/methods called in the Liberator class.
另一种解决方案是将私有方法更改为受保护方法,然后进行模拟。
其中
myPublicMethod
调用myProtectedMethod
。不幸的是,我们不能使用私有方法来做到这一点,因为setMethods
无法找到私有方法,而它可以找到受保护的方法An alternative solution is to change your private method to a protected method and then mock.
where
myPublicMethod
callsmyProtectedMethod
. Unfortunately we can not do this with private methods sincesetMethods
can not find a private method where as it can find a protected method您可以使用 PHP 7 来使用匿名类。
在 PHP 的早期版本中,您可以创建一个扩展基类的测试类。
You can use anonymous classes using PHP 7.
In prior versions of PHP you can make a test class extending the base class.
当目标类既不是静态的也不是最终的时,我以接近@jgmjgm的方式解决此类问题,通过匿名和本地扩展原始类,因此不在不同的测试用例中多次测试相同的方法:
然后我可以测试
$extent->b()
的行为,而无需再次执行可能繁重的c()
方法。When the target class is neither static nor final, I solve this kind of problem in a way close to @jgmjgm, by anonymously and locally extending the original class and therefore not testing the same method multiple times in different test cases :
Then I can test the behaviour of
$extent->b()
without executing again the potentially heavyc()
method.