PHPUnit测试私有属性和方法功能示例
本文实例讲述了phpunit测试私有属性和方法功能。分享给大家供大家参考,具体如下:
一、测试类中的私有方法:
class sample { private $a = 0; private function run() { echo $a; } }
上面只是简单的写了一个类包含,一个私有变量和一个私有方法。对于protected和private方法,由于无法像是用public方法一样直接调用,所以在使用phpunit进行单测的时候,多有不便,特别是当一个类中,对外只提供少量接口,内部使用了大量private方法的情况。
对于protected方法,建议使用继承的方式进行测试,在此就不再赘述。而对于private方法的测试,建议使用php的反射机制来进行。话不多说,上代码:
class testsample() { $method = new reflectionmethod('sample', 'run'); $method->setaccessible(true); //将run方法从private变成类似于public的权限 $method->invoke(new sample()); //调用run方法 }
如果run方法是静态的,如:
private static function run() { echo 'run is a private static function'; }
那么invoke函数还可以这么写:
$method->invoke(null); //只有静态方法可以不必传类的实例化
如果run还需要传参,比如:
private function run($x, $y) { return $x + $y; }
那么,测试代码可以改为:
$method->invokeargs(new sample(), array(1, 2)); //array中依次写入要传的参数。执行结果返回3
【注意】:利用反射的方法测试私有方法虽好,但setaccessible函数是php5.3.2版本以后才支持的(>=5.3.2)
二、私有属性的get/set
说完了私有方法,再来看看私有属性,依旧拿sample类作为例子,想要获取或设置sample类中的私有属性$a的值可以用如下方法:
public function testprivateproperty() { $reflectedclass = new reflectionclass('sample'); $reflectedproperty = $reflectedclass->getproperty('a'); $reflectedproperty->setaccessible(true); $reflectedproperty->getvalue(); //获取$a的值 $reflectedproperty->setvalue(123); //给$a赋值:$a = 123; }
上述方法对静态属性依然有效。
到此,是不是瞬间感觉测试私有方法或属性变得很容易了。
附:phpunit 测试私有方法(英文原文)
this article is part of a series on testing untestable code:
- testing private methods
- testing code that uses singletons
- stubbing static methods
- stubbing hard-coded dependencies
no, not those privates. if you need help with those, this book might help.
one question i get over and over again when talking about unit testing is this:
"how do i test the private attributes and methods of my objects?"
lets assume we have a class foo:
<?php class foo { private $bar = 'baz'; public function dosomething() { return $this->bar = $this->dosomethingprivate(); } private function dosomethingprivate() { return 'blah'; } } ?>
before we explore how protected and private attributes and methods can be tested directly, lets have a look at how they can be tested indirectly.
the following test calls the testdosomething() method which in turn calls thedosomethingprivate() method:
<?php class footest extends phpunit_framework_testcase { /** * @covers foo::dosomething * @covers foo::dosomethingprivate */ public function testdosomething() { $foo = new foo; $this->assertequals('blah', $foo->dosomething()); } } ?>
the test above assumes that testdosomething() only works correctly whentestdosomethingprivate() works correctly. this means that we have indirectly testedtestdosomethingprivate(). the problem with this approach is that when the test fails we do not know directly where the root cause for the failure is. it could be in eithertestdosomething() or testdosomethingprivate(). this makes the test less valuable.
phpunit supports reading protected and private attributes through thephpunit_framework_assert::readattribute() method. convenience wrappers such asphpunit_framework_testcase::assertattributeequals() exist to express assertions onprotected and private attributes:
<?php class footest extends phpunit_framework_testcase { public function testprivateattribute() { $this->assertattributeequals( 'baz', /* expected value */ 'bar', /* attribute name */ new foo /* object */ ); } } ?>
php 5.3.2 introduces the reflectionmethod::setaccessible() method to allow the invocation of protected and private methods through the reflection api:
<?php class footest extends phpunit_framework_testcase { /** * @covers foo::dosomethingprivate */ public function testprivatemethod() { $method = new reflectionmethod( 'foo', 'dosomethingprivate' ); $method->setaccessible(true); $this->assertequals( 'blah', $method->invoke(new foo) ); } } ?>
in the test above we directly test testdosomethingprivate(). when it fails we immediately know where to look for the root cause.
i agree with dave thomas and andy hunt, who write in their book "pragmatic unit testing":
"in general, you don't want to break any encapsulation for the sake of testing (or as mom used to say, "don't expose your privates!"). most of the time, you should be able to test a class by exercising its public methods. if there is significant functionality that is hidden behind private or protected access, that might be a warning sign that there's another class in there struggling to get out."
so: just because the testing of protected and private attributes and methods is possible does not mean that this is a "good thing".
参考文献:
更多关于php相关内容感兴趣的读者可查看本站专题:《php错误与异常处理方法总结》、《php字符串(string)用法总结》、《php数组(array)操作技巧大全》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php面向对象程序设计入门教程》及《php优秀开发框架总结》
希望本文所述对大家php程序设计有所帮助。